Ramikan-BR
commited on
Commit
•
e37f308
1
Parent(s):
d926a10
Update README.md
Browse files
README.md
CHANGED
@@ -12,6 +12,93 @@ tags:
|
|
12 |
- sft
|
13 |
---
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Uploaded model
|
16 |
|
17 |
- **Developed by:** Ramikan-BR
|
|
|
12 |
- sft
|
13 |
---
|
14 |
|
15 |
+
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
16 |
+
|
17 |
+
### Input:
|
18 |
+
# Write a code to create the snake game
|
19 |
+
|
20 |
+
### Output:
|
21 |
+
|
22 |
+
# Here is a simple implementation of the classic Snake game in Python using the Pygame library. This code will create a window with a snake that moves in a grid, eating apples and growing in length.
|
23 |
+
|
24 |
+
```python
|
25 |
+
import pygame
|
26 |
+
import random
|
27 |
+
import time
|
28 |
+
|
29 |
+
# Initialize Pygame
|
30 |
+
pygame.init()
|
31 |
+
|
32 |
+
# Set the dimensions of the game window
|
33 |
+
width, height = 600, 600
|
34 |
+
screen = pygame.display.set_mode((width, height))
|
35 |
+
pygame.display.set_caption('Snake Game')
|
36 |
+
|
37 |
+
# Define colors
|
38 |
+
white = (255, 255, 255)
|
39 |
+
black = (0, 0, 0)
|
40 |
+
red = (255, 0, 0)
|
41 |
+
green = (0, 255, 0)
|
42 |
+
|
43 |
+
# Define the snake
|
44 |
+
snake_pos = [[100, 50], [90, 50], [80, 50]]
|
45 |
+
snake_dir = [0, -1]
|
46 |
+
|
47 |
+
# Define the apple
|
48 |
+
apple_pos = [random.randint(50, width-50), random.randint(50, height-50)]
|
49 |
+
|
50 |
+
# Define the score
|
51 |
+
score = 0
|
52 |
+
|
53 |
+
# Define the clock
|
54 |
+
clock = pygame.time.Clock()
|
55 |
+
|
56 |
+
# Game loop
|
57 |
+
running = True
|
58 |
+
while running:
|
59 |
+
# Handle events
|
60 |
+
for event in pygame.event.get():
|
61 |
+
if event.type == pygame.QUIT:
|
62 |
+
running = False
|
63 |
+
|
64 |
+
# Move the snake
|
65 |
+
new_head = [snake_pos[0][0] + snake_dir[0], snake_pos[0][1] + snake_dir[1]]
|
66 |
+
|
67 |
+
# Check if the snake eats the apple
|
68 |
+
if new_head[0] == apple_pos[0] and new_head[1] == apple_pos[1]:
|
69 |
+
score += 1
|
70 |
+
apple_pos = [random.randint(50, width-50), random.randint(50, height-50)]
|
71 |
+
snake_pos.insert(0, new_head)
|
72 |
+
if len(snake_pos) > 10:
|
73 |
+
snake_pos.pop()
|
74 |
+
else:
|
75 |
+
snake_pos.insert(0, new_head)
|
76 |
+
snake_pos.pop()
|
77 |
+
|
78 |
+
# Check if the snake hits the wall
|
79 |
+
if new_head[0] < 0 or new_head[0] > width-100 or new_head[1] < 0 or new_head[1] > height-100:
|
80 |
+
running = False
|
81 |
+
|
82 |
+
# Draw the game
|
83 |
+
screen.fill(white)
|
84 |
+
for pos in snake_pos:
|
85 |
+
pygame.draw.rect(screen, black, pygame.Rect(pos[0], pos[1], 10, 10))
|
86 |
+
pygame.draw.rect(screen, red, pygame.Rect(apple_pos[0], apple_pos[1], 10, 10))
|
87 |
+
pygame.display.flip()
|
88 |
+
|
89 |
+
# Set the frame rate
|
90 |
+
clock.tick(10)
|
91 |
+
|
92 |
+
# Print the score
|
93 |
+
print(f'Score: {score}')
|
94 |
+
|
95 |
+
pygame.quit()
|
96 |
+
```
|
97 |
+
|
98 |
+
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.
|
99 |
+
|
100 |
+
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|>
|
101 |
+
|
102 |
# Uploaded model
|
103 |
|
104 |
- **Developed by:** Ramikan-BR
|