Skip to content

Commit b147f3d

Browse files
authored
Merge pull request #143 from realpython/top-python-game-engines
Top-Python-Game-Engines code
2 parents 987b745 + 36bc7ea commit b147f3d

File tree

219 files changed

+6785
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+6785
-0
lines changed

top-python-game-engines/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.directory
2+
3+
# Ren'Py files
4+
*.save
5+
navigation.json
6+
persistent
7+
*.rpy?
8+
*.rpy??

top-python-game-engines/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Top Python Game Engines
2+
3+
This repository contains source code for sample games that you'll see in Real Python's Top Python Game Engines tutorial.
4+
5+
After cloning this repository, you can navigate into each folder to find the source code for the sample games shown in the tutorial. Instructions on how to run each game is shown below.
6+
7+
## Set up the Virtual Environment
8+
9+
It's recommended to use a virtual environment to run these samples. To do so, follow these instructions.
10+
11+
First, create the virtual environment:
12+
13+
```bash
14+
$ python3 -m venv venv
15+
```
16+
17+
Next, activate the virtual environment:
18+
19+
```bash
20+
$ source ./venv/bin/activate
21+
```
22+
23+
Finally, install the dependencies for these projects:
24+
25+
```bash
26+
(venv) $ python -m pip install -r requirements.txt
27+
```
28+
29+
## Pygame
30+
31+
To run the Pygame sample code, first activate the virtual environment:
32+
33+
```bash
34+
$ source ./venv/bin/activate
35+
```
36+
37+
Then navigate to the Pygame folder:
38+
39+
```bash
40+
(venv) $ cd pygame
41+
```
42+
43+
To run the basic game:
44+
45+
```bash
46+
(venv) $ python pygame_basic.py
47+
```
48+
49+
To run the full sample game:
50+
51+
```bash
52+
(venv) $ python pygame_game.py
53+
```
54+
55+
## Pygame Zero
56+
57+
To run the Pygame Zero sample code, first activate the virtual environment:
58+
59+
```bash
60+
$ source ./venv/bin/activate
61+
```
62+
63+
Then navigate to the Pygame Zero folder:
64+
65+
```bash
66+
(venv) $ cd pygame_zero
67+
```
68+
69+
To run the basic game:
70+
71+
```bash
72+
(venv) $ python pygame_zero_basic.py
73+
```
74+
75+
To run the full sample game:
76+
77+
```bash
78+
(venv) $ python pygame_zero_game.py
79+
```
80+
81+
Alternately, you can use `pgzrun` to run both games:
82+
83+
```bash
84+
(venv) $ pgzrun pygame_zero_game.py
85+
```
86+
87+
### Note
88+
Checkers like [flake8 are unaware](https://pygame-zero.readthedocs.io/en/latest/installation.html#for-flake8-pyflakes) of Pygame Zero's extra built-ins.
89+
That's why xou'll find `# noqa: F821` inline comments in `pygame_zero_basic.py` and `pygame_zero_game.py`.
90+
With the `# noqa` inline comments [flake8](https://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html#in-line-ignoring-errors) ignores the errors that these lines would cause.
91+
92+
## Arcade
93+
94+
To run the Arcade sample code, first activate the virtual environment:
95+
96+
```bash
97+
$ source ./venv/bin/activate
98+
```
99+
100+
Then navigate to the Arcade folder:
101+
102+
```bash
103+
(venv) $ cd arcade
104+
```
105+
106+
To run the basic game:
107+
108+
```bash
109+
(venv) $ python arcade_basic.py
110+
```
111+
112+
To run the full sample game:
113+
114+
```bash
115+
(venv) $ python arcade_game.py
116+
```
117+
118+
## adventurelib
119+
120+
To run the adventurelib sample code, first activate the virtual environment:
121+
122+
```bash
123+
$ source ./venv/bin/activate
124+
```
125+
126+
Then navigate to the adventurelib folder:
127+
128+
```bash
129+
(venv) $ cd adventurelib
130+
```
131+
132+
To run the basic game:
133+
134+
```bash
135+
(venv) $ python adventurelib_basic.py
136+
```
137+
138+
To run the full sample game:
139+
140+
```bash
141+
(venv) $ python adventurelib_game.py
142+
```
143+
## Ren'Py
144+
145+
Unlike the other samples, Ren'Py games are developed and run from the Ren'Py Software Development Kit.
146+
147+
Visit the [Ren'Py web page](https://www.renpy.org/) to download the proper SDK for your environment (Windows, macOS, and Linux versions are available).
148+
149+
Then, run the Ren'Py Launcher using the proper command for your environment. Check out the [Ren'Py Quickstart Guide](https://www.renpy.org/doc/html/quickstart.html#the-ren-py-launcher) for the most up-to-date instructions.
150+
151+
To access the basic and full sample games in the Ren'Py launcher, click _Preferences_, then _Projects Directory_. Change the Projects Directory to the `renpy` folder in the repository folder you downloaded. Click _Return_ to return to the main Ren'Py Launcher page.
152+
153+
To run the basic game, click on `basic_game` in the Projects list on the left, then click _Launch Project_.
154+
To run the full sample game, click on `giant_quest_game` in the Projects list on the left, then click _Launch Project_.
155+
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
"""
2+
Basic "Hello, World!" program in adventurelib
3+
4+
This program is designed to demonstrate the basic capabilities
5+
of adventurelib. It will:
6+
- Create a simple three-room world
7+
- Add a single inventory item
8+
- Require that inventory item to move to the final room
9+
"""
10+
11+
# Import the library contents
12+
import adventurelib as adv
13+
14+
# Define your rooms
15+
bedroom = adv.Room(
16+
"""
17+
You are in your bedroom. The bed is unmade, but otherwise
18+
it's clean. Your dresser is in the corner, and a desk is
19+
under the window.
20+
"""
21+
)
22+
23+
living_room = adv.Room(
24+
"""
25+
The living room stands bright and empty. The TV is off,
26+
and the sun shines brightly through the curtains.
27+
"""
28+
)
29+
30+
front_porch = adv.Room(
31+
"""
32+
The creaky boards of your front porch welcome you as an
33+
old friend. Your front door mat reads 'Welcome'.
34+
"""
35+
)
36+
37+
# Define the connections between the rooms
38+
bedroom.south = living_room
39+
living_room.east = front_porch
40+
41+
# Define a constraint to move from the bedroom to the living room
42+
# If the door between the living room and front porch door is locked,
43+
# you can't exit
44+
living_room.locked = {"east": True}
45+
46+
# None of the other rooms have any locked doors
47+
bedroom.locked = dict()
48+
front_porch.locked = dict()
49+
50+
# Set the starting room as the current room
51+
current_room = bedroom
52+
53+
54+
# Define functions to use items
55+
def unlock_living_room(current_room):
56+
57+
if current_room == living_room:
58+
print("You unlock the door.")
59+
current_room.locked["east"] = False
60+
else:
61+
print("There is nothing to unlock here.")
62+
63+
64+
# Create your items
65+
key = adv.Item("a front door key", "key")
66+
key.use_item = unlock_living_room
67+
68+
# Create empty Bags for room contents
69+
bedroom.contents = adv.Bag()
70+
living_room.contents = adv.Bag()
71+
front_porch.contents = adv.Bag()
72+
73+
# Put the key in the bedroom
74+
bedroom.contents.add(key)
75+
76+
# Set up your current empty inventory
77+
inventory = adv.Bag()
78+
79+
80+
# Define your movement commands
81+
@adv.when("go DIRECTION")
82+
@adv.when("north", direction="north")
83+
@adv.when("south", direction="south")
84+
@adv.when("east", direction="east")
85+
@adv.when("west", direction="west")
86+
@adv.when("n", direction="north")
87+
@adv.when("s", direction="south")
88+
@adv.when("e", direction="east")
89+
@adv.when("w", direction="west")
90+
def go(direction: str):
91+
"""Processes your moving direction
92+
93+
Arguments:
94+
direction {str} -- which direction does the player want to move
95+
"""
96+
97+
# What is your current room?
98+
global current_room
99+
100+
# Is there an exit in that direction?
101+
next_room = current_room.exit(direction)
102+
if next_room:
103+
# Is the door locked?
104+
if direction in current_room.locked and current_room.locked[direction]:
105+
print(f"You can't go {direction} --- the door is locked.")
106+
else:
107+
current_room = next_room
108+
print(f"You go {direction}.")
109+
look()
110+
111+
# No exit that way
112+
else:
113+
print(f"You can't go {direction}.")
114+
115+
116+
# How do you look at the room?
117+
@adv.when("look")
118+
def look():
119+
"""Looks at the current room"""
120+
global current_room
121+
122+
# Describe the room
123+
adv.say(f"{current_room}")
124+
125+
# List the contents
126+
for item in current_room.contents:
127+
print(f"There is {item} here.")
128+
129+
# List the exits
130+
print(f"The following exits are present: {current_room.exits()}")
131+
132+
133+
# How do you look at items?
134+
@adv.when("look at ITEM")
135+
@adv.when("inspect ITEM")
136+
def look_at(item: str):
137+
138+
# Check if the item is in your inventory or not
139+
obj = inventory.find(item)
140+
if not obj:
141+
print(f"You don't have {item}.")
142+
else:
143+
print(f"It's an {obj}.")
144+
145+
146+
# How do you pick up items?
147+
@adv.when("take ITEM")
148+
@adv.when("get ITEM")
149+
@adv.when("pickup ITEM")
150+
def get(item: str):
151+
"""Get the item if it exists
152+
153+
Arguments:
154+
item {str} -- The name of the item to get
155+
"""
156+
global current_room
157+
158+
obj = current_room.contents.take(item)
159+
if not obj:
160+
print(f"There is no {item} here.")
161+
else:
162+
print(f"You now have {item}.")
163+
inventory.add(obj)
164+
165+
166+
# How do you use an item?
167+
@adv.when("unlock door", item="key")
168+
@adv.when("use ITEM")
169+
def use(item: str):
170+
"""Use an item, consumes it if used
171+
172+
Arguments:
173+
item {str} -- Which item to use
174+
"""
175+
176+
global inventory
177+
178+
# First, do you have the item?
179+
obj = inventory.take(item)
180+
if not obj:
181+
print(f"You don't have {item}")
182+
183+
# Try to use the item
184+
else:
185+
obj.use_item(current_room)
186+
187+
188+
if __name__ == "__main__":
189+
# Look at the starting room
190+
look()
191+
192+
adv.start()

0 commit comments

Comments
 (0)