Home > front end >  Moving objects with keyboard input problem
Moving objects with keyboard input problem

Time:07-02

I am trying to make a pong game,using pygame,following a youtube tutorial and at one point to make the paddle move the guy does this:

def up_down():
global paddle_1_speed
if i.type==pygame.KEYDOWN:
    if i.key==pygame.K_w:
        paddle_1_speed-=6
    if i.key==pygame.K_s:
        paddle_1_speed =6
if i.type==pygame.KEYUP:
    if i.key==pygame.K_w:
        paddle_1_speed =6
    if i.key==pygame.K_s:
        paddle_1_speed-=6                             
return paddle_1_speed,paddle_2_speed

for i in pygame.event.get():
    paddle_1_speed,paddle_2_speed=up_down()

paddle_1.y =paddle_1_speed
paddle_2.y =paddle_2_speed

I can't understand why that's working and whats the difference with this:

def up_down() 
if i.type==pygame.KEYDOWN:
    if i.key==pygame.K_w:
        paddle_1_y-=6
    if i.key==pygame.K_s:
        paddle_1_y =6
if i.type==pygame.KEYUP:
    if i.key==pygame.K_w:
        paddle_1_y =6
    if i.key==pygame.K_s:
        paddle_1_y-=6  


for i in pygame.event.get():
    up_down()

The second code just moves the paddle 6 pixels while the key is being hold and when it's released it returns to where it was,which I understand why it's doing that,but I dont get the difference with the first code that moves the paddle just fine.I can post the whole code if needed,any help will be appreciated.

CodePudding user response:

The second one does not have the global keyword at the top. This means that the change of paddle_1_y is only in the local function scope and not in in the global scope, outside the function. You can fix this by adding global paddle_1_y just after the line starting with def.

By default, python functions do have their own scope. This means that code inside the function can use variables defined outside the scope, but any variables newly defined or changed by code inside the function, are not defined or changed outside the function. Because paddle_1_y is changed inside a function, this change is thus not reflected outside the function, meaning that the paddle isn't being moved.

However, this behavior can be changed using the global keyword. You can use it by adding it and a variable name just after the function's definition. It brings a variable in the global scope, meaning that the variable will also be changed or defined outside the function's scope. Because the first example uses global paddle_1_speed, the change in speed is also reflected outside the scope, meaning that the position of the paddle also changes.

For further reading, see this page.

CodePudding user response:

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action like jumping or spawning a bullet or a step-by-step movement.
Therefore the code in the event is only executed once. In the first case you change the variable paddle_1_speed once, but add the value stored in that variable to the position of the paddle (paddle_1.y) in each frame. This will give you continuous movement. In the second case the position of the paddle is changed once. This will give you a step-by-step movement.

Anyway the code can be simplified with pygame.key.get_pressed(). This function returns a sequence with the state of each key. If a key is held down, the state for the key is 1, otherwise 0. So you can calculate the resulting movement when 2 keys are pressed with keys[pygame.K_d] - keys[pygame.K_a] and keys[pygame.K_s] - keys[pygame.K_w]. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:

def up_down()
    keys = pygame.key.get_pressed()
    paddle_1.y  = (keys[pygame.K_s] - keys[pygame.K_w]) * 6
    paddle_2.y  = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 6

See also How can I make a sprite move when key is held down and How to get keyboard input in pygame?

  • Related