I am learning Pygame and trying to make pong as a first game to learn.
At "Left Paddle Control" the "w" and "s" to move up and down do not work, however, the UP and DOWN arrow keys for the right paddle do work. So I'm confused.
The code is as follows :
import pygame
WIDTH, HEIGHT = 480, 360
screen = pygame.display.set_mode((WIDTH, HEIGHT))
BLACK = (0, 0, 0)
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
def draw_border():
pygame.draw.rect(screen, (255, 0, 0), [0, 0, 5, HEIGHT])
pygame.draw.rect(screen, (255, 0, 0), [0, 0, WIDTH, 5])
pygame.draw.rect(screen, (255, 0, 0), [WIDTH - 5, 0, 5, HEIGHT])
pygame.draw.rect(screen, (255, 0, 0), [0, HEIGHT - 5, WIDTH, 5])
def left_paddle(screen, y):
pygame.draw.rect(screen, BLACK, [20, y + 20, 8, 60])
def right_paddle(screen, y):
pygame.draw.rect(screen, BLACK, [(WIDTH - 30), y + 20, 8, 60])
ly_speed = 0
ry_speed = 0
ly_coord = 10
ry_coord = 10
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
draw_border()
# ----------- Left Paddle Control ----------------
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
ly_speed = -3
elif event.key == pygame.K_s:
ly_speed = 3
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
ly_speed = 0
# ----------- Right Paddle Control ----------------
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ry_speed = -3
elif event.key == pygame.K_DOWN:
ry_speed = 3
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
ry_speed = 0
ly_coord += ly_speed
ry_coord += ry_speed
left_paddle(screen, ly_coord)
right_paddle(screen, ry_coord)
pygame.display.update()
clock.tick(60)
pygame.quit()
I've tried changing the "w" and "s" on the left paddle to UP and DOWN arrows and that works but when I change it back to "w" and "s" it stops. Can you help me out?