1

I'm creating 2-player PONG game with pygame.I have my Racquets on both sides. One of them move with W and S and another with UP and DOWN arrow. I'm using this code to move Racquets:

chx = 0.051
chy = 0.051

def ychangeneg():
    global y
    if y <= 4:
        return
    else:
        y -= chy
    return

def ychangepos():
    global y
    if y >= 327:
        return
    else:
        y += chy
    return


def y1changeneg():
    global y1
    if y1 <= 4:
        return
    else:
        y1 -= chy
    return

def y1changepos():
    global y1
    if y1 >= 327:
        return
    else:
        y1 += chy
    return

while True:
    for event in pygame.event.get():
        keyQ = pygame.key.get_pressed()
    
        if event.type == pygame.QUIT:
            system("cls")
            quit()
            

        keyboard.add_hotkey("w",lambda:ychangeneg())
        keyboard.add_hotkey("s",lambda:ychangepos())

        keyboard.add_hotkey("up",lambda:y1changeneg())
        keyboard.add_hotkey("down",lambda:y1changepos())

chy variable changes y of Racquet and moves it.But I have these problems:

  1. When I start holding a key,Racquet starts moving with delay and then becomes faster
  2. When I holding 2 key (W and UP arrow) at a same time, Racquets don't move

At first, I found some codes that using key= pygame.key.get_pressed() but when you hold a key with this code, It moves but not continuously.

  • Btw you can remove the `else` statements placed after `return`, because if the `if` statement above is evaluated as `True`, then the function is stopped before checking the `else` statement. Also you can change `lambda:ychangeneg()` into `ychangeneg` without parenthesis for the same result. – D_00 Sep 15 '21 at 13:27
  • 1
    See the [documentation](https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed): `pygame.key.get_pressed()` is the way to go. Also check out [this question](https://stackoverflow.com/q/59457872#65367962). – D_00 Sep 15 '21 at 13:32

2 Answers2

1

Do not mix the pygame.key module with the python keyboard module.

See How can I make a sprite move when key is held down and that changes the y-coordinate, depending on the keys pressed:

def move_y(y, keys, up, down):
    new_y = y + (keys[down] - keys[up]) * chy
    return max(4, min(327, new_y))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            system("cls")
            quit()

    keyQ = pygame.key.get_pressed()
    y = move_y(y, keyQ, pygame.K_w, pygame.K_s)            
    y1 = move_y(y1, keyQ, pygame.K_UP, pygame.K_DOWN)   

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 350))
clock = pygame.time.Clock()

paddle1 = pygame.Rect(10, 0, 10, 20)
paddle1.centery = window.get_rect().centery
paddle2 = pygame.Rect(380, 0, 10, 20)
paddle2.centery = window.get_rect().centery
chy = 10

def move_y(y, keys, up, down):
    new_y = y + (keys[down] - keys[up]) * chy
    return max(4, min(327, new_y))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False    

    keyQ = pygame.key.get_pressed()
    paddle1.y = move_y(paddle1.y, keyQ, pygame.K_w, pygame.K_s)            
    paddle2.y = move_y(paddle2.y, keyQ, pygame.K_UP, pygame.K_DOWN)     

    window.fill(0)
    pygame.draw.rect(window, (255, 255, 255), paddle1)
    pygame.draw.rect(window, (255, 255, 255), paddle2)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

I'd suggest not using the keyboard module. Instead look, which key is pressed by using pygame.key.get_pressed(). Check if the key is in there, and if so then change the coordinates.

somewhat like this:

pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.UP]: # this evaluates to true if the up key is pressed
    ychangeneg()
if pressed_keys[pygame.DOWN]:
    ychangepos()
# and so on...
Einliterflasche
  • 473
  • 6
  • 18