I almost created the first game in Python, so I correct shortcomings. I added part of code in function def update(self) to improve Sonic Landing. the program shows error. What’s wrong?
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 isn’t iterable sprite.
Error: 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
>Solution :
pygame.sprite.collide_rect returns True or 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