如何让敌人追击玩家?我也想知道我跳的时候怎么跳到移动的物体上而不是直接穿过它

问题描述

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

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.y))
                self.WalkCount += 1

            pygame.draw.rect(win,(255,0),(self.hitBox[0],self.hitBox[1] - 20,50,10))
            pygame.draw.rect(win,(0,255,10))
            self.hitBox = (self.x + 17,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))
    pygame.draw.circle(screen,(0,255,0),to_coords,5)
    pygame.draw.circle(screen,(255,from_coords,5)

    pygame.draw.circle(screen,255),(follow_coords_x,follow_coords_y),5)
    koeff = go_to_koeff((follow_coords_x,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() 函数:该函数返回方向的系数,可用于改变白点移动的位置,可以看到系数乘以速度的位置。这个算法并不完美,但它找到了位置,所以就是这样。 (你可以用箭头键移动绿点来看看它是如何移动的)

我想到的另一个想法是在列表中跟踪玩家位置并使敌人跟随列表中的所有坐标,这可能更糟,但感觉它实际上是在跟随您。

另外,我刚刚注意到变量名有一些无用的更改,但它并没有太大影响。