Skip to content

Instantly share code, notes, and snippets.

@guiambros
Created March 16, 2023 01:59
Show Gist options
  • Save guiambros/dc8b9621269e9c36d2ddeb0a8ddc3304 to your computer and use it in GitHub Desktop.
Save guiambros/dc8b9621269e9c36d2ddeb0a8ddc3304 to your computer and use it in GitHub Desktop.
Pong game created by GPT-4
import pygame
import sys
import random
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
PADDLE_WIDTH, PADDLE_HEIGHT = 20, 100
BALL_SIZE = 20
PADDLE_SPEED = 10
BALL_SPEED = 5
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Screen setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
# Paddles and ball
player_rect = pygame.Rect(20, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ai_rect = pygame.Rect(WIDTH - 40, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball_rect = pygame.Rect(WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2, BALL_SIZE, BALL_SIZE)
# Ball speed
ball_speed_x = BALL_SPEED * random.choice((-1, 1))
ball_speed_y = BALL_SPEED * random.choice((-1, 1))
# Score
player_score = 0
ai_score = 0
# Font
font = pygame.font.Font(None, 36)
def draw_objects():
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, player_rect)
pygame.draw.rect(screen, WHITE, ai_rect)
pygame.draw.ellipse(screen, WHITE, ball_rect)
pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))
# Draw scores
score_text = font.render(f"{player_score} - {ai_score}", True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 10))
def ai_move():
if ball_rect.centery > ai_rect.centery + PADDLE_SPEED:
ai_rect.y += PADDLE_SPEED
elif ball_rect.centery < ai_rect.centery - PADDLE_SPEED:
ai_rect.y -= PADDLE_SPEED
# Keep AI paddle inside screen
ai_rect.y = max(0, min(ai_rect.y, HEIGHT - PADDLE_HEIGHT))
def move_ball():
global ball_speed_x, ball_speed_y, player_score, ai_score
ball_rect.x += ball_speed_x
ball_rect.y += ball_speed_y
if ball_rect.top <= 0 or ball_rect.bottom >= HEIGHT:
ball_speed_y = -ball_speed_y
if ball_rect.left <= 0:
ai_score += 1
ball_speed_x = BALL_SPEED
ball_speed_y = BALL_SPEED * random.choice((-1, 1))
ball_rect.center = (WIDTH // 2, HEIGHT // 2)
if ball_rect.right >= WIDTH:
player_score += 1
ball_speed_x = -BALL_SPEED
ball_speed_y = BALL_SPEED * random.choice((-1, 1))
ball_rect.center = (WIDTH // 2, HEIGHT // 2)
if ball_rect.colliderect(player_rect) or ball_rect.colliderect(ai_rect):
ball_speed_x = -ball_speed_x
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Player input
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player_rect.y -= PADDLE_SPEED
if keys[pygame.K_DOWN]:
player_rect.y += PADDLE_SPEED
# Keep player paddle inside screen
player_rect.y = max(0, min(player_rect.y, HEIGHT - PADDLE_HEIGHT))
# AI movement
ai_move()
# Move ball
move_ball()
# Draw objects and update the screen
draw_objects()
pygame.display.flip()
# Cap frame rate
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment