为什么我的敌人移动脚本在 pygame 中不起作用
Why is my enemy movement script not working in pygame
我已经为我的游戏中的敌人进行了大约一天的移动处理,但似乎无法正常工作
问题是它会追逐角色一点点直到它击中 x 或 y 零并且会永远停留在那里,有时它会在击中玩家时停留在原地
任何帮助将不胜感激
import pygame
import math
ALPHA = (0, 255, 0)
path = "data/chr/squid.png"
def findDist(coords1, coords2):
try:
return math.sqrt(pow(coords1[0] - coords2[0], 2) + pow(coords1[1] - coords2[1], 2))
except:
print("invalid dist input, ignoring")
class Squid(pygame.sprite.Sprite):
#player object
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.images = []
for i in range(1, 5):
img = pygame.image.load(path).convert()
img.convert_alpha() # optimise alpha
img.set_colorkey(ALPHA) # set alpha
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.x = X
self.rect.y = Y
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.rect.x += dx * self.speed
self.rect.y += dy * self.speed
由于pygame.Rect
应该表示屏幕上的一个区域,因此pygame.Rect
对象只能存储整数数据。
The coordinates for Rect objects are all integers. [...]
当对象的新位置分配给 Rect 对象时,坐标的小数部分会丢失。如果每一帧都这样做,位置误差会随着时间累积。
如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量和属性中,并同步 pygame.Rect
对象。 round
坐标并将其分配给矩形的位置(例如 .topleft
)。
x, y = # floating point coordinates
rect.topleft = round(x), round(y)
阅读有关 Squid
class 的 Classes. If you want to spawn multiple enemies, you must create multiple Instance Objects。例如:
enemies = []
for _ in range(5):
x = random.randrange(screen_width)
y = random.randrange(screen_height)
enemies.append(Squid(x, y))
Squid
class:
class Squid(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.image = pygame.image.load(path).convert()
self.image.convert_alpha() # optimise alpha
self.image.set_colorkey(ALPHA) # set alpha
self.x = x
self.y = y
self.rect = self.image.get_rect(topleft = (round(x), round(y)))
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = round(self.x)
self.rect.y = round(self.y)
我已经为我的游戏中的敌人进行了大约一天的移动处理,但似乎无法正常工作
问题是它会追逐角色一点点直到它击中 x 或 y 零并且会永远停留在那里,有时它会在击中玩家时停留在原地
任何帮助将不胜感激
import pygame
import math
ALPHA = (0, 255, 0)
path = "data/chr/squid.png"
def findDist(coords1, coords2):
try:
return math.sqrt(pow(coords1[0] - coords2[0], 2) + pow(coords1[1] - coords2[1], 2))
except:
print("invalid dist input, ignoring")
class Squid(pygame.sprite.Sprite):
#player object
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.images = []
for i in range(1, 5):
img = pygame.image.load(path).convert()
img.convert_alpha() # optimise alpha
img.set_colorkey(ALPHA) # set alpha
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.x = X
self.rect.y = Y
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.rect.x += dx * self.speed
self.rect.y += dy * self.speed
由于pygame.Rect
应该表示屏幕上的一个区域,因此pygame.Rect
对象只能存储整数数据。
The coordinates for Rect objects are all integers. [...]
当对象的新位置分配给 Rect 对象时,坐标的小数部分会丢失。如果每一帧都这样做,位置误差会随着时间累积。
如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量和属性中,并同步 pygame.Rect
对象。 round
坐标并将其分配给矩形的位置(例如 .topleft
)。
x, y = # floating point coordinates
rect.topleft = round(x), round(y)
阅读有关 Squid
class 的 Classes. If you want to spawn multiple enemies, you must create multiple Instance Objects。例如:
enemies = []
for _ in range(5):
x = random.randrange(screen_width)
y = random.randrange(screen_height)
enemies.append(Squid(x, y))
Squid
class:
class Squid(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.image = pygame.image.load(path).convert()
self.image.convert_alpha() # optimise alpha
self.image.set_colorkey(ALPHA) # set alpha
self.x = x
self.y = y
self.rect = self.image.get_rect(topleft = (round(x), round(y)))
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = round(self.x)
self.rect.y = round(self.y)