I can't make my sprite move. It just moves for one time for 3px. I want to make it move both left and right directions. The called class is motion()
and gives values to 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()
There are similar questions already answered here. Please reffer to: how to make a sprite bounce of the edges of the window in pygame
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.