【问题标题】:Python pygame sprite movementPython pygame 精灵移动
【发布时间】:2014-08-07 11:49:20
【问题描述】:

我无法让我的精灵移动。它只移动一次 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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    这里已经回答了类似的问题。 请参考: how to make a sprite bounce of the edges of the window in pygame

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多