本文介绍了Pygame 没有移动我的矩形,我不知道为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道为什么但我的角色在显示中但不能移动它在一个精灵组中并且一直在更新
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
推荐答案
你必须不断地改变.rect"的位置更新"中的属性快速"的方法:
You have to continuously change the position of the ".rect" attribute in the "update" method by "speedy":
self.rect.y += self.speedy
确保pygame.sprite.Group.update
在每一帧中被调用并在评估速度后改变位置:
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
这篇关于Pygame 没有移动我的矩形,我不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!