I am currently making a pong game in python:
However, say the ball is coming at pink, and the yellow guy decides to spam his w
and s
keys. Then his paddle will start moving (fine), but then the pink one will stop (not fine).
Is it possible for python to listen to two event keys simultaneously?
Here is the code:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1439, 790))
YELLOW = (255, 255, 0)
PINK = (255, 0, 255)
BLUE = (120, 214, 208)
y1, y2 = (0, 0)
circx, circy = (1439//2, 790//2)
diffx, diffy = (15, 15)
pygame.key.set_repeat(1, 10)
while True:
if (0 <= circy <= 780) == False:
diffy*=-1
if circx <= 60 and y1-10 <= circy <= y1+75:
diffx*=-1
if circx >= 1439-60 and y2-10 <= circy <= y2+75:
diffx*=-1
if (0 <= circx <= 1439) == False:
circx, circy = (720, 395)
DISPLAYSURF.fill((0, 0, 0))
pygame.draw.rect(DISPLAYSURF, YELLOW, (50, y1, 10, 75))
pygame.draw.rect(DISPLAYSURF, PINK, (1439-60, y2, 10, 75))
pygame.draw.circle(DISPLAYSURF, BLUE, (circx, circy), 10)
circx+=diffx
circy+=diffy
try:
for event in pygame.event.get():
if event.type == QUIT or event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.key == pygame.K_w:
y1-=15
if event.key == pygame.K_s:
y1+=15
if event.key == pygame.K_UP:
y2-=15
if event.key == pygame.K_DOWN:
y2+=15
except AttributeError:
pass
pygame.display.flip()
How do I make it independently handle each key press if there are 2 simultaneous ones?