如何正确实现索尼克"Landing"?
How to right realize Sonic "Landing"?
我在Python差点创造了第一款游戏,所以我改正不足。我在函数 def update(self)
中添加了部分代码以改进 Sonic
着陆。程序显示错误。怎么了?
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
#[...]
def update(self):
hits_4 = pygame.sprite.collide_rect(player, platform4)
if self.vel.y > 0:
if hits_4:
if self.pos.y < hits_4[0].rect.bottom:
self.pos.y = hits_4[0].rect.top +1
self.vel.y = 0
def jump(self):
hits4 = pygame.sprite.collide_rect(self, platform4)
if hits4:
self.vel.y = -15 // 2
*Platform4
不是可迭代的精灵。
错误:File "C:\Users\дом\My project\Survival_with_Sonic.py", line 149, in update if self.pos.y < hits_4[0].rect.bottom: TypeError: 'bool' object is not subscriptable
pygame.sprite.collide_rect
returns True
或 False
:
if pygame.sprite.collide_rect(player, platform4):
if 0 < self.pos.y < platform4.rect.bottom:
self.pos.y = platform4.rect.top +1
self.vel.y = 0
我在Python差点创造了第一款游戏,所以我改正不足。我在函数 def update(self)
中添加了部分代码以改进 Sonic
着陆。程序显示错误。怎么了?
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
#[...]
def update(self):
hits_4 = pygame.sprite.collide_rect(player, platform4)
if self.vel.y > 0:
if hits_4:
if self.pos.y < hits_4[0].rect.bottom:
self.pos.y = hits_4[0].rect.top +1
self.vel.y = 0
def jump(self):
hits4 = pygame.sprite.collide_rect(self, platform4)
if hits4:
self.vel.y = -15 // 2
*Platform4
不是可迭代的精灵。
错误:File "C:\Users\дом\My project\Survival_with_Sonic.py", line 149, in update if self.pos.y < hits_4[0].rect.bottom: TypeError: 'bool' object is not subscriptable
pygame.sprite.collide_rect
returns True
或 False
:
if pygame.sprite.collide_rect(player, platform4):
if 0 < self.pos.y < platform4.rect.bottom:
self.pos.y = platform4.rect.top +1
self.vel.y = 0