[pygame]deplacer perso quand touche reste enfoncée
Bonjour a tous!
Voila mon problème, depuis hier j'essaye de faire en sorte que dans mon jeu le personnage continue de bouger quand les touches gauche et droite restent enfoncées. Voici le code du jeu:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #!/usr/bin/python
import sys,pygame
pygame.init()
size=width,height=600,500
screen=pygame.display.set_mode(size)
pygame.display.set_caption("OpenMario")
white=255,255,255,255
def clavier():
for event in pygame.event.get():
while event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
return("up")
if event.key==pygame.K_DOWN:
return("down")
if event.key==pygame.K_LEFT:
return("left")
if event.key==pygame.K_RIGHT:
return("right")
if event.key==pygame.K_q:
sys.exit()
if event.type==pygame.QUIT:
sys.exit()
mario=pygame.image.load("/home/hugo/Desktop/ball.gif")
global mariorect
mariorect=mario.get_rect()
mariorect=mariorect.move([230,350])
def move(direction):
global mariorect
if direction=="up":
y=350
x=y
while x!=0:
mariorect=mariorect.move([0,-1])
screen.fill(white)
screen.blit(mario,mariorect)
pygame.display.flip()
x=x-1
x=y
while x!=0:
mariorect=mariorect.move([0,1])
screen.fill(white)
screen.blit(mario,mariorect)
pygame.display.flip()
x=x-1
if direction=="left":
newpos=[-20,0]
essai=mariorect.move(newpos)
if essai[0]>=0:
mariorect=essai
if direction=="right":
newpos=[20,0]
essai=mariorect.move(newpos)
if essai[0]<width:
mariorect=essai
while 1:
screen.fill(white)
move(clavier())
screen.blit(mario,mariorect)
pygame.display.update() |
Quelqu'un a une idée ? Merci d'avance.
Hugo