Search code examples
pythonpygamekeyboardpong

Moving objects in pygame using keyboard module


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.


Solution

  • 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()