Skip to content

Instantly share code, notes, and snippets.

@Zedmor
Created May 13, 2024 23:00
Show Gist options
  • Save Zedmor/8f030bcf0b1a00130d186f1a091069a3 to your computer and use it in GitHub Desktop.
Save Zedmor/8f030bcf0b1a00130d186f1a091069a3 to your computer and use it in GitHub Desktop.
GPT-4o breakout - first try
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Paddle dimensions
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 10
# Ball dimensions
BALL_SIZE = 10
# Brick dimensions
BRICK_WIDTH = 75
BRICK_HEIGHT = 20
# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Breakout Game")
# Paddle class
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([PADDLE_WIDTH, PADDLE_HEIGHT])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = (SCREEN_WIDTH - PADDLE_WIDTH) // 2
self.rect.y = SCREEN_HEIGHT - PADDLE_HEIGHT - 10
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0] - PADDLE_WIDTH // 2
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > SCREEN_WIDTH - PADDLE_WIDTH:
self.rect.x = SCREEN_WIDTH - PADDLE_WIDTH
# Ball class
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([BALL_SIZE, BALL_SIZE])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2
self.rect.y = SCREEN_HEIGHT // 2
self.speed = [random.choice([-4, 4]), -4]
def update(self):
self.rect.x += self.speed[0]
self.rect.y += self.speed[1]
if self.rect.x <= 0 or self.rect.x >= SCREEN_WIDTH - BALL_SIZE:
self.speed[0] = -self.speed[0]
if self.rect.y <= 0:
self.speed[1] = -self.speed[1]
# Brick class
class Brick(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([BRICK_WIDTH, BRICK_HEIGHT])
self.image.fill(random.choice([RED, GREEN, BLUE]))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Create sprite groups
all_sprites = pygame.sprite.Group()
bricks = pygame.sprite.Group()
# Create paddle
paddle = Paddle()
all_sprites.add(paddle)
# Create ball
ball = Ball()
all_sprites.add(ball)
# Create bricks
for row in range(5):
for col in range(10):
brick = Brick(col * (BRICK_WIDTH + 5) + 35, row * (BRICK_HEIGHT + 5) + 35)
all_sprites.add(brick)
bricks.add(brick)
# Main game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
# Ball and paddle collision
if pygame.sprite.collide_rect(ball, paddle):
ball.speed[1] = -ball.speed[1]
# Ball and brick collision
brick_collision_list = pygame.sprite.spritecollide(ball, bricks, True)
if brick_collision_list:
ball.speed[1] = -ball.speed[1]
# Check if ball is out of bounds
if ball.rect.y >= SCREEN_HEIGHT:
running = False
# Clear screen
screen.fill(BLACK)
# Draw all sprites
all_sprites.draw(screen)
# Update screen
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment