Skip to content

Commit c3a1156

Browse files
committed
Added arrow controls, made game only start when control keys are hit. Added requirements file.
1 parent 893e97f commit c3a1156

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ This project is based on an example from the book *"Coding Games in Python"* by
66

77
![Gameplay Screenshot](images/gameplay.png)
88
## Features
9-
10-
- **Player Control**: Move the fox using the `W`, `A`, `S`, and `D` keys.
119
- **Scoring**: Gain 10 points for every coin collected.
1210
- **Countdown Timer**: The game lasts for 10 seconds.
1311
- **Game Over**: A final score screen is displayed when time runs out.
1412
- **Unit Tests**: The project includes a test suite using `pytest` to verify game logic.
15-
13+
## Controls
14+
You can control the fox using either the WASD keys or the Arrow Keys.
15+
- Move Up: `W` or `Up Arrow`
16+
- Move Down: `S` or `Down Arrow`
17+
- Move Left: `A` or `Left Arrow`
18+
- Move Right: `D` or `Right Arrow`
1619
## Requirements
17-
1820
- Python 3.x
1921
- Pygame Zero
2022
- Pytest (for running the tests)
21-
2223
## Installation
23-
2424
1. **Clone or download the repository:**
2525
If you have Git, you can clone it. Otherwise, download the project files as a ZIP.
2626

@@ -58,7 +58,7 @@ pytest
5858

5959
## Project Structure
6060

61-
The project is structured to separate game code from tests, which is a standard practice for maintainable software.
61+
The project is structured to separate game code from tests.
6262

6363
```
6464
CoinCollector/

coincollector.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
WIDTH = 400
77
HEIGHT = 400
8+
89
score = 0
910
game_over = False
11+
game_started = False
1012
time_left = 10
1113

1214
fox = Actor("fox")
@@ -24,7 +26,7 @@ def draw():
2426

2527
if game_over:
2628
screen.fill("orange") #changed from pink to orange
27-
screen.draw.text("Final Score: " + str(score), topleft=(10, 10), fontsize = 60)
29+
screen.draw.text("Final Score: " + str(score), center=(WIDTH/2, HEIGHT/2), fontsize = 60, color="black")
2830

2931
def place_coin():
3032
coin.x = randint(20, (WIDTH - 20))
@@ -36,28 +38,31 @@ def time_up():
3638

3739
def update(dt):
3840
# dt = delta time
39-
global score, game_over, time_left
41+
global score, game_over, time_left, game_started
4042

4143
if not game_over:
42-
if keyboard.a:
44+
if keyboard.w or keyboard.a or keyboard.s or keyboard.d or keyboard.up or keyboard.down or keyboard.left or keyboard.right:
45+
game_started = True
46+
47+
if keyboard.a or keyboard.left:
4348
fox.x = fox.x - 5
44-
elif keyboard.d:
49+
elif keyboard.d or keyboard.right:
4550
fox.x = fox.x + 5
46-
elif keyboard.w:
51+
elif keyboard.w or keyboard.up:
4752
fox.y = fox.y - 5
48-
elif keyboard.s:
53+
elif keyboard.s or keyboard.down:
4954
fox.y = fox.y + 5
5055

5156
coin_collected = fox.colliderect(coin)
5257
if coin_collected:
5358
score = score + 10
5459
place_coin()
5560

56-
time_left = time_left - dt
57-
if time_left <= 0:
58-
game_over = True
59-
time_left = 0
60-
61+
if game_started and not game_over:
62+
time_left = time_left - dt
63+
if time_left <= 0:
64+
game_over = True
65+
time_left = 0
6166
place_coin()
6267

6368
pgzrun.go()

requirements.txt

58 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)