code python snack
- User
code python snack
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code python snack
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple Python Snake game using Pygame:
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
block_size = 20
width = 400
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
clock = pygame.time.Clock()
def spawn_food(snake_pos):
while True:
x = random.randint(0, (width//block_size)-1)
y = random.randint(0, (height//block_size)-1)
if [x, y] not in snake_pos:
return [x, y]
def main():
snake_pos = [[10, 10], [9, 10], [8, 10]]
direction = [1, 0]
food_pos = spawn_food(snake_pos)
score = 0
while True:
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 != [0, 1]:
direction = [0, -1]
elif event.key == pygame.K_DOWN and direction != [0, -1]:
direction = [0, 1]
elif event.key == pygame.K_LEFT and direction != [1, 0]:
direction = [-1, 0]
elif event.key == pygame.K_RIGHT and direction != [-1, 0]:
direction = [1, 0]
# Move the snake
new_head = [snake_pos[0][0] + direction[0], snake_pos[0][1] + direction[1]]
snake_pos = [new_head] + snake_pos[:-1]
# Check food collision
if new_head == food_pos:
snake_pos.append(snake_pos[-1]) # Grow the snake
food_pos = spawn_food(snake_pos)
score += 1
# Check collisions
# Wall collision
if (new_head[0] < 0 or new_head[0] >= width//block_size or
new_head[1] < 0 or new_head[1] >= height//block_size):
game_over()
# Self collision
if new_head in snake_pos[1:]:
game_over()
# Draw everything
screen.fill(black)
for pos in snake_pos:
pygame.draw.rect(screen, white, (pos[0]*block_size, pos[1]*block_size, block_size, block_size))
pygame.draw.rect(screen, red, (food_pos[0]*block_size, food_pos[1]*block_size, block_size, block_size))
pygame.display.flip()
clock.tick(10)
def game_over():
screen.fill(black)
font = pygame.font.SysFont(None, 40)
text = font.render("Game Over! Press R to restart or Q to quit.", True, white)
screen.blit(text, (50, 200))
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.key == pygame.K_r:
main() # Restart the game
waiting = False
if __name__ == "__main__":
main()
To run this game:
Make sure you have Pygame installed:
pip install pygame
Save the code to a file (e.g., snake_game.py).
Run the game:
python snake_game.py
Game controls:
R to restart the game after losing.Q to quit the game.The game features:
The snake moves at a speed of 10 blocks per second. You can adjust the speed by changing the clock.tick(10) value (lower numbers make it slower).