Created
September 12, 2023 22:56
-
-
Save twobob/144bbeff9f08ba6db2d8144fa3435e82 to your computer and use it in GitHub Desktop.
prompt = "A simple snake game written in python using pygame" phi engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Question: Write a simple feature-complete snake game written in python using pygame. | |
#""" | |
# 1. Overview and imports | |
# Import pygame for the game | |
import pygame | |
pygame.init() | |
# Import image manipulation module for drawing image | |
from img_manip import ImageManip | |
# Define a list of items with their size and position | |
items = [ | |
("pencil", 10, (-50, -50), (10, 30)), | |
("pen", 10, (-50, 40), (10, 60)), | |
("pencil", 5, (-50, -50), (5, 30)), | |
] | |
# Define a button with a rectangle and an event handler | |
button_rect = pygame.Rect(50, 50, 100, 50) | |
button_text = pygame.Rect(50, 100, 100, 20) | |
button_text_font = pygame.font.Font(None, 35) | |
button_text = button_text_font.render("Play", 1, (255, 0, 255)) | |
button_rect.fill((0, 0, 0, 0)) | |
button_rect.inset_abs(button_text, button_text_rect) | |
button = pygame.Rect(50, 100, 50, 20) | |
# 2. Drawing and animation | |
# Create a game screen and set its resolution | |
screen = pygame.display.set_mode((800, 600)) | |
# Define a function to draw an image onto the screen | |
def draw_image(image, rect): | |
images = pygame.image.load('img/' + image) | |
screen_rect = pygame.Rect(rect) | |
screen.blit(images, rect) | |
# Create an animation frame for each item | |
class AnimationFrame: | |
def __init__(self, image, rect): | |
self.rect = rect | |
self.image = pygame.image.load('img/' + image) | |
frames = [] | |
# Add a 'Play' button to the game | |
play_button = Button((600, 50), "Play", button_rect, button_text, button_rect.inflate(-2, -2), 0.1) | |
play_button.add_event(pygame.MOUSEBUTTONDOWN, play_button) | |
# Add items to the game | |
for item, size, position, text in items: | |
draw_image(item, button_rect) | |
image_manip = ImageManip(item, size) | |
animation_frame = AnimationFrame(image_manip.image, button_rect) | |
frames.append(animation_frame) | |
# Draw all the animation frames onto the screen | |
for f in frames: | |
screen.blit(f.image, f.rect) | |
# 3. Implementing the snake AI | |
# Define a function to calculate the food positions for the snake | |
def calculate_food(x, y): | |
return (x-1, y) | |
# Define a function to draw the snake onto the screen | |
def draw_snake(snake): | |
# Draw the snake's body | |
for segment in snake: | |
pygame.draw.line(screen, (0, 0, 255), segment[:2], segment[2:]) | |
# Draw the head of the snake | |
pygame.draw.rect(screen, (255, 0, 0), button_rect) | |
segment_positions = [(snake[0], y) for y in range(-9, 10)] | |
for i, segment_position in enumerate(segment_positions): | |
pygame.draw.line(screen, (255, 255, 0), segment_position[:-1], segment_position[1:]) | |
pygame.draw.line(screen, (0, 255, 0), segment_position[:-1], segment_position[-1]) | |
pygame.draw.line(screen, (0, 0, 0), segment_positions[0], (50, -20)) | |
# Define a function to handle the snake collision and update its position | |
def snake_collision(snake, food_positions): | |
# Check for collision with food | |
for f in food_positions: | |
if head.collidepoint(f): | |
return False, pygame.Vector2(f, f) | |
# Check for collision with the opposite side of the screen | |
if head.right and pygame.Line2D(head.x+1, head.y, arrow.width, arrow.height).collidepoint(tail.vector2): | |
return False, pygame.Vector2(head.x, head.y) | |
if head.left and pygame.Line2D(head.x-1, head.y, arrow.width, arrow.height).collidepoint(tail.vector2): | |
return False, pygame.Vector2(head.x, head.y) | |
if head.top and pygame.Line2D(head.x, head.y-1, arrow.width, arrow.height).collidepoint(tail.vector2): | |
return False, pygame.Vector2(head.x, head.y) | |
return True, pygame.Vector2(0, 0) | |
# Define a function to render the game environment | |
def draw_environment(snake, food_positions, mouse, key_mouse): | |
# Set the background color | |
screen.fill((255, 255, 255, 128)) | |
# Draw the snake | |
draw_snake(snake) | |
# Draw the food positions | |
for f in food_positions: | |
pygame.draw.circle(screen, (0, 255, 0), f, 2) | |
# Draw the arrow | |
pygame.draw.line(screen, (255, 0, 0), button_rect, arrow) | |
# Check if the snake has eaten the food | |
if head.collidepoint(tail): | |
return True | |
# Render the input keys | |
if key_mouse.pressed: | |
return True | |
else: | |
return False | |
# Initialize the game loop | |
running = True | |
score = 0 | |
while running: | |
# Handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
elif event.type == pygame.KEYDOWN: | |
head = pygame.mouse.get_pos() | |
if mouse: | |
mouse.set_pos(head) | |
key_mouse = pygame.key.get_pressed() | |
if event.key == pygame.K_ESCAPE: | |
running = False | |
# Convert screen coordinates to game coordinates | |
mouse_pos = pygame.mouse.get_pos() | |
head_pos = pygame.mouse.get_pos() | |
score += head_pos.x + head_pos.y | |
tail_pos = tail.vector2 | |
head_velocity = head - tail_pos | |
# Update the head position | |
head.set_pos(head_pos.x + head_velocity.x, head_pos.y + head_velocity.y) | |
# Update the tail position (without reversing the direction) | |
tail.set_pos(head_pos.x - tail_velocity.x, head_pos.y - tail_velocity.y) | |
# Check for a collision with the food | |
food_positions = calculate_food(mouse_pos.x, mouse_pos.y) | |
if snake_collision(snake, food_positions): | |
# Eat the food and update the score | |
snake = [item[:2] for item in snake] | |
score += food_positions[1].x | |
food_positions = calculate_food(100, 100) | |
snake.append(food_positions) | |
# Draw the environment | |
pygame.draw.circle(screen, (0, 0, 0, 128), tail, 1) | |
pygame.draw.line(screen, (255, 255, 255, 128), pygame.Rect(mouse, (5, 10)), pygame.Rect((mouse, 80), (130, 10))) | |
pygame.draw.line(screen, (0, 255, 0, 128), (120, 150), pygame.Rect(head, (10, 20))) | |
# Render the snake and the food | |
draw_snake(snake) | |
draw_food(food_positions[1]) | |
# Draw the player | |
text = text_font.render(f"Score: {score}", 1, (0, 0, 0)) | |
screen.blit(text, (50, 50)) | |
pygame.display.update() | |
# Set the game window closing button | |
game_window = pygame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment