I'm making a very simple game using pygame to practice. Im trying to make my "player" jump when spacebar is pressed. Here is the loop code I'm trying:
x,y=10,300
movex,movey=0,0
if event.type==KEYDOWN:
if event.key==K_LEFT:
movex = -0.2
elif event.key==K_RIGHT:
movex=+0.2
elif event.key==K_DOWN:
movey=+0.2
elif event.key==K_SPACE:
movey=-0.4
movey=+0.4
if event.type==KEYUP:
if event.key==K_LEFT:
movex = 0
elif event.key==K_RIGHT:
movex=0
elif event.key==K_DOWN:
movey=0
elif event.key==K_SPACE:
movey=0
x+=movex
y+=movey
screen.blit(player,(x,y))
The controls work except for the jump part (when I press the spacebar). It just like slides the player down. Can anyone tell me why it doesn't work and how to fix it?
It doesn't work because on KEYDOWN event you are doing:
which in the end is equivalent to
movey=0
. To fix it you should do onlymovey=+0.4
and then on KEYUP it will revert to 0.