Skip to content

Instantly share code, notes, and snippets.

@joshuapack
Last active May 16, 2024 03:53
Show Gist options
  • Save joshuapack/d71483502351819151f61e1c0634b713 to your computer and use it in GitHub Desktop.
Save joshuapack/d71483502351819151f61e1c0634b713 to your computer and use it in GitHub Desktop.
import pygame
import sys
def speed_up_ball():
global ball_speed_x, ball_speed_y
if ball_speed_x < 0:
if ball_speed_x > -20:
ball_speed_x -= 1
ball_speed_y -= 1
if ball_speed_x > 0:
if ball_speed_x < 20:
ball_speed_x += 1
ball_speed_y += 1
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH = 800
HEIGHT = 600
BALL_SIZE = 20
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
PADDLE_SPEED = 8
FPS = 60
BALL_SPEED = 5
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
# Set up the ball
ball_x = WIDTH / 2
ball_y = HEIGHT / 2
ball_speed_x = BALL_SPEED
ball_speed_y = BALL_SPEED
# Set up the paddles
paddle1_y = HEIGHT / 2 - PADDLE_HEIGHT / 2
paddle2_y = HEIGHT / 2 - PADDLE_HEIGHT / 2
# Set up the point system
score1 = 0
score2 = 0
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move the paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
if paddle1_y > -1:
paddle1_y -= PADDLE_SPEED
if keys[pygame.K_s]:
if paddle1_y < HEIGHT - PADDLE_HEIGHT:
paddle1_y += PADDLE_SPEED
if keys[pygame.K_UP]:
if paddle2_y > -1:
paddle2_y -= PADDLE_SPEED
if keys[pygame.K_DOWN]:
if paddle2_y < HEIGHT - PADDLE_HEIGHT:
paddle2_y += PADDLE_SPEED
# Move the ball
ball_x += ball_speed_x
ball_y += ball_speed_y
# Bounce the ball off the walls and paddles
if ball_y < 0 or ball_y > HEIGHT - BALL_SIZE:
ball_speed_y *= -1
elif (ball_y + BALL_SIZE > paddle1_y) and (ball_y < paddle1_y + PADDLE_HEIGHT) and (ball_x < PADDLE_WIDTH):
ball_speed_x *= -1
speed_up_ball()
elif (ball_y + BALL_SIZE > paddle2_y) and (ball_y < paddle2_y + PADDLE_HEIGHT) and (ball_x > WIDTH - PADDLE_WIDTH - 20):
ball_speed_x *= -1
speed_up_ball()
# Check if the ball has gone out of bounds
if ball_x < -5:
score2 += 1
ball_x = WIDTH / 2
ball_y = HEIGHT / 2
ball_speed_x = BALL_SPEED
ball_speed_y = BALL_SPEED
elif ball_x > WIDTH + 5:
score1 += 1
ball_x = WIDTH / 2
ball_y = HEIGHT / 2
ball_speed_x = BALL_SPEED
ball_speed_y = BALL_SPEED
# Draw everything
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), (ball_x, ball_y, BALL_SIZE, BALL_SIZE))
pygame.draw.rect(screen, (255, 255, 255), (0, paddle1_y, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.rect(screen, (255, 255, 255), (WIDTH - PADDLE_WIDTH, paddle2_y, PADDLE_WIDTH, PADDLE_HEIGHT))
# Display the score
font = pygame.font.Font(None, 36)
text1 = font.render(str(score1), True, (255, 255, 255))
screen.blit(text1, (WIDTH / 4, HEIGHT / 2 - 20))
text2 = font.render(str(score2), True, (255, 255, 255))
screen.blit(text2, (3 * WIDTH / 4, HEIGHT / 2 - 20))
# Update the display
pygame.display.flip()
pygame.time.Clock().tick(FPS)
@joshuapack
Copy link
Author

Install Python 3 and install pygame, like pip3 install pygame

then you can play pong. My kids love the game!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment