1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
碰撞箱不會(huì)隨著小行星“移動(dòng)”。如果你想讓這種方法起作用,那么當(dāng)小行星的位置改變時(shí),你必須改變 hitbox 的 y 坐標(biāo)。例如:
for a in asteroids_on_screen:
if -141 < a.y < 500:
a.y += a.vel
a.hitbox = (a.hitbox[0], a.hitbox[1]+a.vel, a.hitbox[2], a.hitbox[3])
else:
asteroids_on_screen.pop(asteroids_on_screen.index(a))
但我建議在draw. 例如創(chuàng)建一個(gè)屬性 currenthitbox,它返回當(dāng)前的命中框:
class Enemy:
def __init__(self, y, width, height):
self.width = width
self.height = height
self.vel = 1.5
self.y = y
self.x = random.randrange(screen_width - 64 * 2)
self.index = random.choice(number)
self.hitboxes = [(68, 68, - 10, - 14),
(38, 47, + 20, - 5),
(18, 12, + 32, + 30),
(20, 32, + 16, + 5),
(4, 7, - 24, - 31)]
self.hitbox = self.hitboxes[self.index]
@property
def currenthitbox(self):
return (self.hitbox[0]+self.x, self.hitbox[1]+self.y, self.hitbox[2]+self.width, self.hitbox[3]+self.height)
def draw(self, win):
win.blit(asteroids[self.index], (self.x, self.y))
pygame.draw.rect(win, (255, 0, 0), self.currenthitbox, 2)
添加回答
舉報(bào)