如何让敌人追上玩家?我还想知道如何在跳跃时跳到移动的物体上,而不仅仅是穿过它



移动对象。它目前在屏幕上左右移动,我希望用户能够跳上它

def moving_object():
global x_speed, y_speed
pygame.draw.rect(win, (200, 140, 150), rectangle)
rectangle.x += x_speed
if rectangle.right >= 500 or rectangle.left <= 0:
x_speed *= -1

为我的敌人上课。他们目前在屏幕上左右移动,但我希望他们跟随用户。他们也不能跳过

class enemy():
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.WalkCount = 0
self.vel = 3
self.path = [self.x, self.end]
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.WalkCount + 1 >= 33:
self.WalkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.WalkCount //3], (self.x, self.y))
self.WalkCount += 1
else:
win.blit(self.walkLeft[self.WalkCount //3], (self.x, self.y))
self.WalkCount += 1
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0, 255, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.WalkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.WalkCount = 0
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
pass

因此,这里有一种制作寻路算法的方法:

import pygame
import time

pygame.init()
screen = pygame.display.set_mode((700, 500))
clock = pygame.time.Clock()
to_coords = [550, 100]
from_coords = (150, 400)

def go_to_koeff(from_, to):
dx = to[0] - from_[0]
dy = to[1] - from_[1]
if dx != 0:
dx /= abs(dx)
if dy != 0:
dy /= abs(dy)
return dx, dy

follow_coords_x = from_coords[0]
follow_coords_y = from_coords[1]
velocity = 1

run = True
while run:
clock.tick(60)

screen.fill((0, 0, 0))
pygame.draw.circle(screen, (0, 255, 0), to_coords, 5)
pygame.draw.circle(screen, (255, 0, 0), from_coords, 5)
pygame.draw.circle(screen, (255, 255, 255), (follow_coords_x, follow_coords_y), 5)
koeff = go_to_koeff((follow_coords_x, follow_coords_y), to_coords)
follow_coords_x += velocity * koeff[0]
follow_coords_y += velocity * koeff[1]
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
to_coords[1] -= 3
if keys[pygame.K_DOWN]:
to_coords[1] += 3
if keys[pygame.K_LEFT]:
to_coords[0] -= 3
if keys[pygame.K_RIGHT]:
to_coords[0] += 3
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

pygame.display.update()
pygame.quit()

所以基本上整个算法是go_to_koeff()函数:该函数返回可以用来改变白点移动位置的方向系数,可以看到系数乘以速度的地方。这个算法并不完美,但它能找到位置,所以就是这样。(你可以用箭头键移动绿点,看看它是如何移动的(

脑海中浮现的另一个想法是跟踪玩家在列表中的位置,让敌人跟随列表中的所有coords,这可能会更糟,但感觉它实际上是在跟随你。

此外,我刚刚注意到变量名有一些无用的更改,但它不会对任何事情产生太大影响。

相关内容

最新更新