Making Player Jump in Pygame

2019-09-16 11:14发布

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?

1条回答
Evening l夕情丶
2楼-- · 2019-09-16 12:03

It doesn't work because on KEYDOWN event you are doing:

    movey=-0.4
    movey=+0.4

which in the end is equivalent to movey=0. To fix it you should do only movey=+0.4 and then on KEYUP it will revert to 0.

查看更多
登录 后发表回答