147 lines
4.1 KiB
YAML
147 lines
4.1 KiB
YAML
NOTICE
|
|
Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language)
|
|
ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example".
|
|
|
|
## Code: snake.py Write code with triple quoto, based on the following list and context.
|
|
1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT.
|
|
2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets
|
|
3. Attention1: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE.
|
|
4. Attention2: YOU MUST FOLLOW "Data structures and interface definitions". DONT CHANGE ANY DESIGN.
|
|
5. Think before writing: What should be implemented and provided in this document?
|
|
6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE.
|
|
7. Do not use public member functions that do not exist in your design.
|
|
|
|
-----
|
|
# Context
|
|
## Implementation approach
|
|
For the snake game, we can use the Pygame library, which is an open-source and easy-to-use library for game development in Python. Pygame provides a simple and efficient way to handle graphics, sound, and user input, making it suitable for developing a snake game.
|
|
|
|
## Python package name
|
|
```
|
|
"snake_game"
|
|
```
|
|
## File list
|
|
````
|
|
[
|
|
"main.py",
|
|
"game.py",
|
|
"snake.py",
|
|
"food.py"
|
|
]
|
|
```
|
|
## Data structures and interface definitions
|
|
```
|
|
classDiagram
|
|
class Game{
|
|
-int score
|
|
-bool game_over
|
|
+start_game() : void
|
|
+end_game() : void
|
|
+update() : void
|
|
+draw() : void
|
|
+handle_events() : void
|
|
}
|
|
class Snake{
|
|
-list[Tuple[int, int]] body
|
|
-Tuple[int, int] direction
|
|
+move() : void
|
|
+change_direction(direction: Tuple[int, int]) : void
|
|
+is_collision() : bool
|
|
+grow() : void
|
|
+draw() : void
|
|
}
|
|
class Food{
|
|
-Tuple[int, int] position
|
|
+generate() : void
|
|
+draw() : void
|
|
}
|
|
class Main{
|
|
-Game game
|
|
+run() : void
|
|
}
|
|
Game "1" -- "1" Snake: contains
|
|
Game "1" -- "1" Food: has
|
|
Main "1" -- "1" Game: has
|
|
```
|
|
## Program call flow
|
|
```
|
|
sequenceDiagram
|
|
participant M as Main
|
|
participant G as Game
|
|
participant S as Snake
|
|
participant F as Food
|
|
|
|
M->G: run()
|
|
G->G: start_game()
|
|
G->G: handle_events()
|
|
G->G: update()
|
|
G->G: draw()
|
|
G->G: end_game()
|
|
|
|
G->S: move()
|
|
S->S: change_direction()
|
|
S->S: is_collision()
|
|
S->S: grow()
|
|
S->S: draw()
|
|
|
|
G->F: generate()
|
|
F->F: draw()
|
|
```
|
|
## Anything UNCLEAR
|
|
The design and implementation of the snake game are clear based on the given requirements.
|
|
|
|
## Required Python third-party packages
|
|
```
|
|
"""
|
|
pygame==2.0.1
|
|
"""
|
|
```
|
|
## Required Other language third-party packages
|
|
```
|
|
"""
|
|
No third-party packages required for other languages.
|
|
"""
|
|
```
|
|
|
|
## Logic Analysis
|
|
```
|
|
[
|
|
["main.py", "Main"],
|
|
["game.py", "Game"],
|
|
["snake.py", "Snake"],
|
|
["food.py", "Food"]
|
|
]
|
|
```
|
|
## Task list
|
|
```
|
|
[
|
|
"snake.py",
|
|
"food.py",
|
|
"game.py",
|
|
"main.py"
|
|
]
|
|
```
|
|
## Shared Knowledge
|
|
```
|
|
"""
|
|
'game.py' contains the main logic for the snake game, including starting the game, handling user input, updating the game state, and drawing the game state.
|
|
|
|
'snake.py' contains the logic for the snake, including moving the snake, changing its direction, checking for collisions, growing the snake, and drawing the snake.
|
|
|
|
'food.py' contains the logic for the food, including generating a new food position and drawing the food.
|
|
|
|
'main.py' initializes the game and runs the game loop.
|
|
"""
|
|
```
|
|
## Anything UNCLEAR
|
|
We need to clarify the main entry point of the application and ensure that all required third-party libraries are properly initialized.
|
|
|
|
-----
|
|
## Format example
|
|
-----
|
|
## Code: snake.py
|
|
```python
|
|
## snake.py
|
|
...
|
|
```
|
|
----- |