0

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?

Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
  • The check for keys needs to be *inside* the `for event in pygame.event.get():` loop. If there's more than one event generated at a time, your current code only looks at the last one; I guess that in the case of keys corresponding to printable characters, that's the `TEXTINPUT` event rather than the `KEYDOWN`. – jasonharper Feb 09 '23 at 01:12

1 Answers1

0
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])
pygame.rect.Rect().clamp

ly_speed = 0
ry_speed = 0

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #basicly returns a pygame object that if specific key is pressed returns True
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        ry_speed -= 3
    elif keys[pygame.K_DOWN]:
        ry_speed += 3
    if keys[pygame.K_w]:
        ly_speed -= 3
    elif keys[pygame.K_s]:
        ly_speed += 3
        
    screen.fill((255, 255, 255))
    draw_border()
        
    left_paddle(screen, ly_speed)
    right_paddle(screen, ry_speed)
    pygame.display.update()
    clock.tick(60)

pygame.quit()



First of all event.type == pygame.KEYDOWN is not suitable for your code because you need a handler that checks your keyboard events every frame in this case pygame.key.get_pressed() much more efficient. I change some functionality in your code and works well now you need to add collision for your game.I high recommend when you wanna move an object use pygame.rect.Rect and pygame.Surface() objects together to move from one location to another.

for example:

# Set the rectangle size and position
rect_size = (100, 50)
rect_pos = (0, 0)

# Set the rectangle color
rect_color = (255, 0, 0)

# Create the rectangle
rect = pygame.Surface(rect_size)
rect.fill(rect_color)

rect_speed = 5

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

    # Move the rectangle to the right
    rect_pos = (rect_pos[0] + rect_speed, rect_pos[1])

    screen.fill((255, 255, 255))

    # Draw the rectangle
    screen.blit(rect, rect_pos)

    pygame.display.update()
CodeX
  • 55
  • 6