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:
- When I start holding a key,Racquet starts moving with delay and then becomes faster
- When I holding 2 key (
W
andUP 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.