Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
1import pygame
2import random
3import time
4
5# Initialize Pygame
6pygame.init()
7
8# Set the dimensions of the game window
9width, height = 600, 600
10screen = pygame.display.set_mode((width, height))
11pygame.display.set_caption('Snake Game')
12
13# Define colors
14white = (255, 255, 255)
15black = (0, 0, 0)
16red = (255, 0, 0)
17green = (0, 255, 0)
18
19# Define the snake
20snake_pos = [[100, 50], [90, 50], [80, 50]]
21snake_dir = [0, -1]
22
23# Define the apple
24apple_pos = [random.randint(50, width-50), random.randint(50, height-50)]
25
26# Define the score
27score = 0
28
29# Define the clock
30clock = pygame.time.Clock()
31
32# Game loop
33running = True
34while running:
35 # Handle events
36 for event in pygame.event.get():
37 if event.type == pygame.QUIT:
38 running = False
39
40 # Move the snake
41 new_head = [snake_pos[0][0] + snake_dir[0], snake_pos[0][1] + snake_dir[1]]
42
43 # Check if the snake eats the apple
44 if new_head[0] == apple_pos[0] and new_head[1] == apple_pos[1]:
45 score += 1
46 apple_pos = [random.randint(50, width-50), random.randint(50, height-50)]
47 snake_pos.insert(0, new_head)
48 if len(snake_pos) > 10:
49 snake_pos.pop()
50 else:
51 snake_pos.insert(0, new_head)
52 snake_pos.pop()
53
54 # Check if the snake hits the wall
55 if new_head[0] < 0 or new_head[0] > width-100 or new_head[1] < 0 or new_head[1] > height-100:
56 running = False
57
58 # Draw the game
59 screen.fill(white)
60 for pos in snake_pos:
61 pygame.draw.rect(screen, black, pygame.Rect(pos[0], pos[1], 10, 10))
62 pygame.draw.rect(screen, red, pygame.Rect(apple_pos[0], apple_pos[1], 10, 10))
63 pygame.display.flip()
64
65 # Set the frame rate
66 clock.tick(10)
67
68 # Print the score
69 print(f'Score: {score}')
70
71pygame.quit()
This code will create a window with a snake that moves in a grid, eating apples and growing in length. The score will be printed to the console.
Note that this is a very basic implementation and can be improved in many ways, such as adding a start screen, better collision detection, and more features.<|endoftext|>
This mistral model was trained 2x faster with
Unsloth and Huggingface's TRL library.