Passes "snake in python test" after only 3 attempts

#1
by rombodawg - opened

After 3 diffrent attempts (the first 3 being failures) it made a perfect game of snake. And it was even able to itterate on the code an add aditional features. Originally this code only had a snake and no food, it added the food the second prompt.

To test this code save the file in a word document, name it "snake.py", open command prompt where that file is saved, and then run "python snake.py".

import pygame
import random
import sys

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH, HEIGHT = 800, 600
SPEED = 10
BLOCK_SIZE = 20
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up the snake and food
snake = [(200, 200), (220, 200), (240, 200)]
food = None
direction = 'right'

# Function to draw the snake and food
def draw_snake_and_food(snake, food):
    screen.fill(WHITE)
    for pos in snake:
        pygame.draw.rect(screen, RED, (pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE))
    if food:
        pygame.draw.rect(screen, (0, 255, 0), (food[0], food[1], BLOCK_SIZE, BLOCK_SIZE))

# Game loop
while True:
    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction!= 'down':
                direction = 'up'
            elif event.key == pygame.K_DOWN and direction!= 'up':
                direction = 'down'
            elif event.key == pygame.K_LEFT and direction!= 'right':
                direction = 'left'
            elif event.key == pygame.K_RIGHT and direction!= 'left':
                direction = 'right'

    # Move the snake
    head = snake[-1]
    if direction == 'up':
        new_head = (head[0], max(0, head[1] - BLOCK_SIZE))
    elif direction == 'down':
        new_head = (head[0], min(HEIGHT - BLOCK_SIZE, head[1] + BLOCK_SIZE))
    elif direction == 'left':
        new_head = (max(0, head[0] - BLOCK_SIZE), head[1])
    else:
        new_head = (min(WIDTH - BLOCK_SIZE, head[0] + BLOCK_SIZE), head[1])

    snake.append(new_head)

    # Check for collision with self or wall
    if new_head in snake[:-1]:
        print("Game Over")
        pygame.quit()
        sys.exit()
    elif not (0 <= new_head[0] < WIDTH and 0 <= new_head[1] < HEIGHT):
        print("Game Over")
        pygame.quit()
        sys.exit()

    # Check for food consumption
    if new_head == food:
        food = None
    else:
        snake.pop(0)

    # Generate new food if none exists
    if not food:
        food = (random.randint(0, WIDTH - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE,
                random.randint(0, HEIGHT - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE)

    # Draw everything
    draw_snake_and_food(snake, food)
    pygame.display.flip()

    # Cap the frame rate
    pygame.time.delay(1000 // SPEED)

Here is another one i had it do of pong instead of snake

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH, HEIGHT = 640, 480
BALL_RADIUS = 10
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 60
FPS = 60

# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up the title of the window
pygame.display.set_caption("Pong")

# Define some colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up the ball and paddles
ball_x, ball_y = random.randint(0, WIDTH - BALL_RADIUS) + BALL_RADIUS, random.randint(0, HEIGHT - BALL_RADIUS) + BALL_RADIUS
ball_vx, ball_vy = 5, -5
paddle1_y, paddle2_y = HEIGHT // 2, HEIGHT // 2
score1, score2 = 0, 0

# Font for displaying scores
font = pygame.font.Font(None, 36)

# Game loop
while True:
    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                paddle1_y -= 5
            elif event.key == pygame.K_s:
                paddle1_y += 5
            elif event.key == pygame.K_UP:
                paddle2_y -= 5
            elif event.key == pygame.K_DOWN:
                paddle2_y += 5

    # Move the paddles
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        paddle1_y -= 5
    if keys[pygame.K_s]:
        paddle1_y += 5
    if keys[pygame.K_UP]:
        paddle2_y -= 5
    if keys[pygame.K_DOWN]:
        paddle2_y += 5

    # Move the ball
    ball_x += ball_vx
    ball_y += ball_vy

    # Collision with walls
    if ball_y < BALL_RADIUS or ball_y > HEIGHT - BALL_RADIUS:
        ball_vy *= -1
    if ball_x < BALL_RADIUS or ball_x > WIDTH - BALL_RADIUS:
        ball_vx *= -1

    # Collision with paddles
    if ball_x < PADDLE_WIDTH + BALL_RADIUS and paddle1_y < ball_y < paddle1_y + PADDLE_HEIGHT:
        ball_vx *= -1
    if ball_x > WIDTH - PADDLE_WIDTH - BALL_RADIUS and paddle2_y < ball_y < paddle2_y + PADDLE_HEIGHT:
        ball_vx *= -1

    # Check for goals
    if ball_x < BALL_RADIUS:
        score2 += 1
        ball_x, ball_y = random.randint(0, WIDTH - BALL_RADIUS) + BALL_RADIUS, random.randint(0, HEIGHT - BALL_RADIUS) + BALL_RADIUS
        ball_vx, ball_vy = 5, -5
    elif ball_x > WIDTH - BALL_RADIUS:
        score1 += 1
        ball_x, ball_y = random.randint(0, WIDTH - BALL_RADIUS) + BALL_RADIUS, random.randint(0, HEIGHT - BALL_RADIUS) + BALL_RADIUS
        ball_vx, ball_vy = 5, -5

    # Draw everything
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, (0, paddle1_y, PADDLE_WIDTH, PADDLE_HEIGHT))
    pygame.draw.rect(screen, WHITE, (WIDTH - PADDLE_WIDTH, paddle2_y, PADDLE_WIDTH, PADDLE_HEIGHT))
    pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), BALL_RADIUS)

    # Display scores
    text_surface = font.render(f"{score1} - {score2}", True, WHITE)
    screen.blit(text_surface, (WIDTH // 4, 20))

    # Update the display
    pygame.display.flip()
    pygame.time.Clock().tick(FPS)

But what's the point, when standard llama3 can do that too?

Sign up or log in to comment