@@ -1,157 +1,177 @@
|
|||||||
# Snake Game (Day 24)
|
# Snake Game (Day 24)
|
||||||
|
|
||||||
The classic Snake game, built with Python's `turtle` module. It is the day-24
|
The classic Snake game, built with Python's `turtle` module. It is the day-24
|
||||||
assignment of Udemy's *100 Days of Code: The Complete Python Pro Bootcamp*. Day 24
|
assignment of Udemy's *100 Days of Code: The Complete Python Pro Bootcamp*. Day 24
|
||||||
teaches the local file system and reading and writing files with `open` and the
|
teaches the local file system and reading and writing files with `open` and the
|
||||||
`with` keyword, and applies them to the snake game: the scoreboard shows a high
|
`with` keyword, and applies them to the snake game: the scoreboard shows a high
|
||||||
score next to the score, the game goes on after a collision instead of ending, and
|
score next to the score, the game goes on after a collision instead of ending, and
|
||||||
the high score is kept in the file `data.txt`, so it is still there the next time
|
the high score is kept in the file `data.txt`, so it is still there the next time
|
||||||
the game is started.
|
the game is started.
|
||||||
|
|
||||||
The game has no runtime dependencies. The code keeps the names of the lectures
|
The game has no runtime dependencies. The code keeps the names of the lectures
|
||||||
(`Snake`, `create_snake`, `move`, `reset`, `up`, `down`, `left`, `right`,
|
(`Snake`, `create_snake`, `move`, `reset`, `up`, `down`, `left`, `right`,
|
||||||
`segments`, `head`, `game_is_on`, `Food`, `Scoreboard`, `refresh`, `extend`,
|
`segments`, `head`, `game_is_on`, `Food`, `Scoreboard`, `refresh`, `extend`,
|
||||||
`increase_score`, `update_scoreboard`, `high_score`) so that you can compare it
|
`increase_score`, `update_scoreboard`, `high_score`) so that you can compare it
|
||||||
with your own solution.
|
with your own solution.
|
||||||
|
|
||||||
This repository continues
|
This repository continues
|
||||||
[021-snake-game](https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/021-snake-game)
|
[021-snake-game](https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/021-snake-game)
|
||||||
and starts from its finished game; `docs/project-plan.md` tells how the work is
|
and starts from its finished game; `docs/project-plan.md` tells how the work is
|
||||||
split into milestones.
|
split into milestones.
|
||||||
|
|
||||||
> **Status:** in progress. The repository foundation (this README, the
|
> **Status:** in progress. The finished game of day 21 is adopted and runs: the
|
||||||
> configuration, the constants, the Doxygen settings and the continuous
|
> snake moves, eats and grows, the score is shown and the game ends at the wall
|
||||||
> integration workflow) is in place; the game itself, continuous play, the high
|
> or at the tail. Continuous play and the high score (milestone 3), the high
|
||||||
> score and the high score file follow in milestones 2 to 4, and the wiki in
|
> score file (milestone 4) and the wiki (milestone 5) follow. The sections below
|
||||||
> milestone 5. The sections below describe the finished game.
|
> describe the game as it is now; they are updated in each milestone.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Python 3.13 or newer, with `tkinter` (the `turtle` module needs it).
|
- Python 3.13 or newer, with `tkinter` (the `turtle` module needs it).
|
||||||
- `git`, to clone the repository.
|
- `git`, to clone the repository.
|
||||||
- Optional: [Doxygen](https://www.doxygen.nl/) to build the source documentation.
|
- Optional: [Doxygen](https://www.doxygen.nl/) to build the source documentation.
|
||||||
- No runtime dependencies. The development tools (`pytest`, `ruff`, `mypy`) are
|
- No runtime dependencies. The development tools (`pytest`, `ruff`, `mypy`) are
|
||||||
installed into the virtual environment by the `dev` extra.
|
installed into the virtual environment by the `dev` extra.
|
||||||
|
|
||||||
## Set up
|
## Set up
|
||||||
|
|
||||||
Clone the repository, then create a local virtual environment `.venv` in its
|
Clone the repository, then create a local virtual environment `.venv` in its
|
||||||
root, upgrade `pip` and install the project with its development tools. The
|
root, upgrade `pip` and install the project with its development tools. The
|
||||||
`.venv` folder is ignored by git.
|
`.venv` folder is ignored by git.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/024-snake.git
|
git clone https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/024-snake.git
|
||||||
cd 024-snake
|
cd 024-snake
|
||||||
```
|
```
|
||||||
|
|
||||||
### Windows powershell
|
### Windows powershell
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
.\.venv\Scripts\Activate.ps1
|
.\.venv\Scripts\Activate.ps1
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install -e ".[dev]"
|
python -m pip install -e ".[dev]"
|
||||||
```
|
```
|
||||||
|
|
||||||
If PowerShell refuses to run the activation script, allow scripts for this
|
If PowerShell refuses to run the activation script, allow scripts for this
|
||||||
window only with `Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned`
|
window only with `Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned`
|
||||||
and activate again. Python from python.org includes `tkinter`.
|
and activate again. Python from python.org includes `tkinter`.
|
||||||
|
|
||||||
### Linux debian
|
### Linux debian
|
||||||
|
|
||||||
Debian 13 or newer ships Python 3.13. On older releases install Python 3.13
|
Debian 13 or newer ships Python 3.13. On older releases install Python 3.13
|
||||||
separately.
|
separately.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo apt install python3 python3-venv python3-tk
|
sudo apt install python3 python3-venv python3-tk
|
||||||
python3 -m venv .venv
|
python3 -m venv .venv
|
||||||
source .venv/bin/activate
|
source .venv/bin/activate
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install -e ".[dev]"
|
python -m pip install -e ".[dev]"
|
||||||
```
|
```
|
||||||
|
|
||||||
### MacOS
|
### MacOS
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
brew install python@3.13 python-tk@3.13
|
brew install python@3.13 python-tk@3.13
|
||||||
python3.13 -m venv .venv
|
python3.13 -m venv .venv
|
||||||
source .venv/bin/activate
|
source .venv/bin/activate
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install -e ".[dev]"
|
python -m pip install -e ".[dev]"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Run
|
## Run
|
||||||
|
|
||||||
With the virtual environment active:
|
With the virtual environment active:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m snake_game
|
python -m snake_game
|
||||||
```
|
```
|
||||||
|
|
||||||
The game itself is added in milestone 2 (see the status above); this section
|
It opens a black 600 by 600 window titled "My Snake Game". The snake of three
|
||||||
is completed with the controls, the two scores and the high score file in
|
squares starts in the middle and moves to the right by itself, 20 pixels every
|
||||||
milestones 2 to 5.
|
0.1 seconds. Its body is light green and its head is dark green with a mouth.
|
||||||
|
|
||||||
## Run the tests
|
| Key | Effect |
|
||||||
|
| --- | --- |
|
||||||
The tests need no display: they use fakes instead of real turtles.
|
| Up, Down, Left, Right | Turn the snake |
|
||||||
|
|
||||||
```bash
|
The snake never turns straight back onto itself: the arrow key opposite to the
|
||||||
pytest
|
way it is going is ignored.
|
||||||
```
|
|
||||||
|
A small blue circle, the food, appears at a random place. When the head comes
|
||||||
The same checks that continuous integration runs:
|
closer to it than 15 pixels the snake eats it: the food moves to a new random
|
||||||
|
place, the snake grows by one segment, and the score at the top of the window
|
||||||
```bash
|
goes up by 1.
|
||||||
python -m pytest
|
|
||||||
python -m ruff check src tests
|
The game is over when the head passes the wall (more than 280 pixels from the
|
||||||
python -m ruff format --check src tests
|
centre on any side) or touches the tail. The snake stops, `GAME OVER` appears in
|
||||||
python -m mypy
|
the middle of the window, and a click on the window closes it. You can also close
|
||||||
```
|
the window with its close button at any time. (Milestone 3 replaces this with
|
||||||
|
continuous play and a high score.)
|
||||||
## Continuous integration
|
|
||||||
|
## Run the tests
|
||||||
`.gitea/workflows/ci.yml` runs on Gitea Actions on every push and on every pull
|
|
||||||
request. It sets up Python 3.13, upgrades `pip`, installs `.[dev]`, then runs
|
The tests need no display: they use fakes instead of real turtles.
|
||||||
`pytest`, `ruff` (lint and format check) and `mypy`. It does not build the source
|
|
||||||
documentation: run `doxygen Doxyfile` yourself, as the next section shows. No
|
```bash
|
||||||
step opens a turtle window.
|
pytest
|
||||||
|
```
|
||||||
The workflow lives in `.gitea/workflows` and not in `.github/workflows`, so
|
|
||||||
GitHub does not run it and pushing to a GitHub mirror needs no workflow
|
The same checks that continuous integration runs:
|
||||||
permission.
|
|
||||||
|
```bash
|
||||||
## Build the source documentation
|
python -m pytest
|
||||||
|
python -m ruff check src tests
|
||||||
The source is documented with Doxygen comments and the `Doxyfile` in the root.
|
python -m ruff format --check src tests
|
||||||
Install Doxygen (`winget install DimitriVanHeesch.Doxygen` on Windows,
|
python -m mypy
|
||||||
`sudo apt install doxygen` on Debian, `brew install doxygen` on macOS), then:
|
```
|
||||||
|
|
||||||
```bash
|
## Continuous integration
|
||||||
doxygen Doxyfile
|
|
||||||
```
|
`.gitea/workflows/ci.yml` runs on Gitea Actions on every push and on every pull
|
||||||
|
request. It sets up Python 3.13, upgrades `pip`, installs `.[dev]`, then runs
|
||||||
The HTML is written to `build/doxygen/index.html`. A warning fails the build.
|
`pytest`, `ruff` (lint and format check) and `mypy`. It does not build the source
|
||||||
|
documentation: run `doxygen Doxyfile` yourself, as the next section shows. No
|
||||||
## Project layout
|
step opens a turtle window.
|
||||||
|
|
||||||
```text
|
The workflow lives in `.gitea/workflows` and not in `.github/workflows`, so
|
||||||
.
|
GitHub does not run it and pushing to a GitHub mirror needs no workflow
|
||||||
├── .gitea/workflows/ci.yml continuous integration (Gitea Actions)
|
permission.
|
||||||
├── docs/ business case, plan, milestones, reviews
|
|
||||||
├── src/snake_game/ the game
|
## Build the source documentation
|
||||||
│ ├── __init__.py
|
|
||||||
│ └── constants.py every constant of the game
|
The source is documented with Doxygen comments and the `Doxyfile` in the root.
|
||||||
├── tests/ pytest tests
|
Install Doxygen (`winget install DimitriVanHeesch.Doxygen` on Windows,
|
||||||
├── Doxyfile source documentation settings
|
`sudo apt install doxygen` on Debian, `brew install doxygen` on macOS), then:
|
||||||
├── LICENSE
|
|
||||||
├── pyproject.toml project configuration
|
```bash
|
||||||
└── README.md
|
doxygen Doxyfile
|
||||||
```
|
```
|
||||||
|
|
||||||
The remaining modules (`snake.py`, `food.py`, `scoreboard.py`, `main.py` and
|
The HTML is written to `build/doxygen/index.html`. A warning fails the build.
|
||||||
`__main__.py`) are added in milestone 2.
|
|
||||||
|
## Project layout
|
||||||
## License
|
|
||||||
|
```text
|
||||||
GNU Affero General Public License v3.0 only. See [LICENSE](LICENSE).
|
.
|
||||||
|
├── .gitea/workflows/ci.yml continuous integration (Gitea Actions)
|
||||||
|
├── docs/ business case, plan, milestones, reviews
|
||||||
|
├── src/snake_game/ the game
|
||||||
|
│ ├── __init__.py
|
||||||
|
│ ├── __main__.py starts the game: python -m snake_game
|
||||||
|
│ ├── constants.py every constant of the game
|
||||||
|
│ ├── food.py the Food class (inherits from Turtle)
|
||||||
|
│ ├── main.py screen set-up and the main flow
|
||||||
|
│ ├── scoreboard.py the Scoreboard class (inherits from Turtle)
|
||||||
|
│ └── snake.py the Snake class
|
||||||
|
├── tests/ pytest tests (fakes.py holds the fake turtle and screen)
|
||||||
|
├── Doxyfile source documentation settings
|
||||||
|
├── LICENSE
|
||||||
|
├── pyproject.toml project configuration
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
GNU Affero General Public License v3.0 only. See [LICENSE](LICENSE).
|
||||||
|
|||||||
Reference in New Issue
Block a user