繁体   English   中英

python pygame精灵运动

[英]Python pygame sprite movement

我无法动弹。 它只移动了3px。 我想使其左右移动。 调用的类是motion() ,并将值velocity(velx)

#declarations
x=150
y=500

text= font.render("Press UP arrow To Start", True, white)

class states(pygame.sprite.Sprite):
mainsprite=pygame.sprite.Group()
def __init__(self, goku1, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.image=goku1
    states.mainsprite.add(self)
    self.rect=self.image.get_rect()
    self.rect.x=x
    self.rect.y=y
    self.velx=0

def motion(self):

    self.rect.x+=self.velx

while True:

#opening screen and text blitting
if count == 0: # to display only at start of the game
    obj=states(goku1, x, y)
    DISPLAYSURF.blit(text, (820/2, 640/2))
    count=count+1
keys=pygame.key.get_pressed()
if keys[K_UP]:
    obj=states(goku1, x, y)
    DISPLAYSURF.blit(bg, (0, 0))

#class objects declaration
if keys[K_RIGHT]:
    obj.velx=3

elif keys[K_LEFT]:
    obj.velx=-3


obj.motion()
obj=states(goku1, x, y)


#blit real image    
states.mainsprite.draw(DISPLAYSURF)


for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
pygame.display.update()

这里已经回答了类似的问题。 请参考: 如何在pygame中使窗口边缘的精灵反弹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM