[英]Pygame isn't moving my rectangle and I can't figure out why?
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):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface( (30 , 30))
self.image.fill (red)
self.rect = self.image.get_rect ()
self.rect.y = height /2
self.rect.x = width-50
self.speedy = 0
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
if self.rect.bottom >= height:
self.rect.top = 0
if self.rect.top <= 0:
self.rect.bottom = height
You have to continuously change the position of the ".rect" attribute in the "update" method by "speedy":您必须通过“speedy”不断更改“update”方法中“.rect”属性的位置:
self.rect.y += self.speedy
Ensure thatpygame.sprite.Group.update
is invoked in every frame and change the position after evaluating the speed:确保在每一帧中调用pygame.sprite.Group.update
并在评估速度后更改位置:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.