o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
write code for python game snake with field size 64*64
- User
write code for python game snake with field size 64*64
- Assistant
write code for python game snake with field size 64*64
Certainly! Below is a Python code for the classic Snake game using the pygame library with a field size of 64x64 grid cells. Each cell in the grid will be represented by a square block. We'll define the size of each block (cell) to fit within a reasonable window size.
First, ensure you have pygame installed:
pip install pygame
Now, here's the complete code:
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Set up display
CELL_SIZE = 10 # Size of each grid cell in pixels
GRID_WIDTH = 64
GRID_HEIGHT = 64
WINDOW_WIDTH = CELL_SIZE * GRID_WIDTH # 640 pixels
WINDOW_HEIGHT = CELL_SIZE * GRID_HEIGHT # 640 pixels
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Snake Game')
# Define colors
BLACK = pygame.Color(0, 0, 0) # Background color
WHITE = pygame.Color(255, 255, 255) # Unused, but can be for text
GREEN = pygame.Color(0, 255, 0) # Snake color
RED = pygame.Color(255, 0, 0) # Food color
# Frames per second controller
fps_controller = pygame.time.Clock()
# Initialize snake and food positions
def init_snake():
snake_pos = [WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2]
# Starting with three blocks of the snake's body
snake_body = [
[snake_pos[0], snake_pos[1]],
[snake_pos[0] - CELL_SIZE, snake_pos[1]],
[snake_pos[0] - (2 * CELL_SIZE), snake_pos[1]]
]
return snake_pos, snake_body
def spawn_food():
food_x = random.randint(0, GRID_WIDTH - 1) * CELL_SIZE
food_y = random.randint(0, GRID_HEIGHT - 1) * CELL_SIZE
return [food_x, food_y]
def game_over():
pygame.quit()
sys.exit()
def main():
# Initialize game variables
snake_pos, snake_body = init_snake()
direction = 'RIGHT'
change_to = direction
food_pos = spawn_food()
food_spawn = True
score = 0
while True:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
# Check for direction keys
if event.key == pygame.K_UP or event.key == ord('w'):
if direction != 'DOWN':
change_to = 'UP'
elif event.key == pygame.K_DOWN or event.key == ord('s'):
if direction != 'UP':
change_to = 'DOWN'
elif event.key == pygame.K_LEFT or event.key == ord('a'):
if direction != 'RIGHT':
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT or event.key == ord('d'):
if direction != 'LEFT':
change_to = 'RIGHT'
elif event.key == pygame.K_ESCAPE:
game_over()
# Validate direction change
if change_to == 'UP':
direction = 'UP'
if change_to == 'DOWN':
direction = 'DOWN'
if change_to == 'LEFT':
direction = 'LEFT'
if change_to == 'RIGHT':
direction = 'RIGHT'
# Update the snake's position
if direction == 'UP':
snake_pos[1] -= CELL_SIZE
if direction == 'DOWN':
snake_pos[1] += CELL_SIZE
if direction == 'LEFT':
snake_pos[0] -= CELL_SIZE
if direction == 'RIGHT':
snake_pos[0] += CELL_SIZE
# Snake body growing mechanism
snake_body.insert(0, list(snake_pos)) # Insert new position at the head
if snake_pos == food_pos:
score += 1
food_spawn = False # Food consumed
else:
snake_body.pop() # Remove the last block of the body
if not food_spawn:
food_pos = spawn_food()
food_spawn = True
# Draw everything
window.fill(BLACK)
for block in snake_body:
pygame.draw.rect(window, GREEN, pygame.Rect(block[0], block[1], CELL_SIZE, CELL_SIZE))
pygame.draw.rect(window, RED, pygame.Rect(food_pos[0], food_pos[1], CELL_SIZE, CELL_SIZE))
# Check for game over conditions
# Collision with walls
if (snake_pos[0] < 0 or snake_pos[0] >= WINDOW_WIDTH or
snake_pos[1] < 0 or snake_pos[1] >= WINDOW_HEIGHT):
game_over()
# Collision with self
if snake_pos in snake_body[1:]:
game_over()
# Update the display
pygame.display.update()
# Set game speed
fps_controller.tick(15)
if __name__ == "__main__":
main()
How to Play:
Up, Down, Left, Right) or WASD keys to control the snake's direction.Game Notes:
fps_controller.tick(15) to a different number.Additional Features to Consider Adding:
Reminder: Ensure you have the pygame library installed before running the script. You can install it using:
pip install pygame
Enjoy the game! Let me know if you need any further assistance or modifications.
Not yet included in a published release.