Pygame isn't moving my rectangle and I can't figure out why?

后端 未结 1 1059
一整个雨季
一整个雨季 2021-01-23 08:47

I dont know why but my character is in the display but it cant move it is in a sprite group and it is updated all the time

class player(pygame.sprite.Sprite):
            


        
相关标签:
1条回答
  • 2021-01-23 09:20

    You have to continuously change the position of the ".rect" attribute in the "update" method by "speedy":

    self.rect.y += self.speedy
    

    Ensure that pygame.sprite.Group.update is invoked in every frame and change the position after evaluating the speed:

    class player(pygame.sprite.Sprite):
        # [...]
    
        def update(self):
            self.speedy = 0
            keystate = pygame.key.get_pressed()
            if keystate[pygame.K_UP]:
                self.speedy = 8
            if keystate[pygame.K_DOWN]:
                self.speedy = -8
    
            self.rect.y += self.speedy
    
            if self.rect.bottom >= height:
                self.rect.top = 0
            if self.rect.top <= 0:
                self.rect.bottom = height
    
    0 讨论(0)
提交回复
热议问题