Skip to content

Commit 3bc040e

Browse files
committed
Add listings from Chapter 5
1 parent 474ac23 commit 3bc040e

File tree

8 files changed

+1197
-0
lines changed

8 files changed

+1197
-0
lines changed

listing5-1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# Escape - A Python Adventure
5+
# Personal work following the book “Mission Python” (Sean McManus)
6+
7+
planets = {"Mercury": "The smallest planet, nearest the Sun",
8+
"Venus": "Venus takes 243 days to rotate",
9+
"Earth": "The only planet known to have native life",
10+
"Mars": "The Red Planet is the second smallest planet",
11+
"Jupiter": "The largest planet, Jupiter is a gas giant",
12+
"Saturn": "The second largest planet is a gas giant",
13+
"Uranus": "An ice giant with a ring system",
14+
"Neptune": "An ice giant and farthest from the Sun",
15+
"Pluto": "Not considered a planet since 2006."
16+
}
17+
18+
while True:
19+
query = input("Which planet would you like information on? ")
20+
print(planets[query])

listing5-2.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# Escape - A Python Adventure
5+
# Personal work following the book “Mission Python” (Sean McManus)
6+
7+
planets = {"Mercury": "The smallest planet, nearest the Sun",
8+
"Venus": "Venus takes 243 days to rotate",
9+
"Earth": "The only planet known to have native life",
10+
"Mars": "The Red Planet is the second smallest planet",
11+
"Jupiter": "The largest planet, Jupiter is a gas giant",
12+
"Saturn": "The second largest planet is a gas giant",
13+
"Uranus": "An ice giant with a ring system",
14+
"Neptune": "An ice giant and farthest from the Sun",
15+
"Pluto": "Not considered a planet since 2006."
16+
}
17+
18+
while True:
19+
query = input("Which planet would you like information on? ")
20+
if query in planets.keys():
21+
print(planets[query])
22+
else:
23+
print("No data available! Sorry!")

listing5-3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# Escape - A Python Adventure
5+
# Personal work following the book “Mission Python” (Sean McManus)
6+
7+
planets = {"Mercury":
8+
["The smallest planet, nearest the Sun", False, 0],
9+
"Venus":
10+
["Venus takes 243 days to rotate", False, 0],
11+
"Earth":
12+
["The only planet known to have native life", False, 1],
13+
"Mars":
14+
["The Red Planet is the second smallest planet", False, 2],
15+
"Jupiter":
16+
["The largest planet, Jupiter is a gas giant", True, 67],
17+
"Saturn":
18+
["The second largest planet is a gas giant", True, 62],
19+
"Uranus":
20+
["An ice giant with a ring system", True, 27],
21+
"Neptune":
22+
["An ice giant and farthest from the Sun", True, 14],
23+
"Pluto":
24+
["Not considered a planet since 2006.", False, 5]
25+
}
26+
27+
while True:
28+
query = input("Which planet would you like information on? ")
29+
if query in planets.keys():
30+
print(planets[query])
31+
else:
32+
print("No data available! Sorry!")

listing5-4.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# Escape - A Python Adventure
5+
# Personal work following the book “Mission Python” (Sean McManus)
6+
7+
planets = {"Mercury":
8+
["The smallest planet, nearest the Sun", False, 0],
9+
"Venus":
10+
["Venus takes 243 days to rotate", False, 0],
11+
"Earth":
12+
["The only planet known to have native life", False, 1],
13+
"Mars":
14+
["The Red Planet is the second smallest planet", False, 2],
15+
"Jupiter":
16+
["The largest planet, Jupiter is a gas giant", True, 67],
17+
"Saturn":
18+
["The second largest planet is a gas giant", True, 62],
19+
"Uranus":
20+
["An ice giant with a ring system", True, 27],
21+
"Neptune":
22+
["An ice giant and farthest from the Sun", True, 14],
23+
"Pluto":
24+
["Not considered a planet since 2006.", False, 5]
25+
}
26+
27+
while True:
28+
query = input("Which planet would you like information on? ")
29+
if query in planets.keys():
30+
print(planets[query][0])
31+
print("Does it have rings? ", planets[query][1])
32+
print("Number of moons: ", planets[query][2])
33+
else:
34+
print("No data available! Sorry!")

listing5-5.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# Escape - A Python Adventure
5+
# Personal work following the book “Mission Python” (Sean McManus)
6+
7+
# pgzrun import (+ pgzrun.go() command) makes the code running
8+
# when calling it using the python/python3 command.
9+
import pgzrun
10+
import time, random, math
11+
12+
13+
# Window Size
14+
WIDTH = 800
15+
HEIGHT = 800
16+
17+
PLAYER_NAME = "Steph"
18+
FRIEND1_NAME = "Friend 1"
19+
FRIEND2_NAME = "Friend 2"
20+
21+
DEMO_OBJECTS = [images.floor, images.pillar, images.soil]
22+
23+
MAP_WIDTH = 5
24+
MAP_HEIGHT = 10
25+
MAP_SIZE = MAP_WIDTH * MAP_HEIGHT
26+
GAME_MAP = [["Room 0 - Where unused objects are kept", 0, 0, False, False]]
27+
28+
LANDER_SECTOR = random.randint(1, 24)
29+
LANDER_X = random.randint(2, 11)
30+
LANDER_Y = random.randint(2, 11)
31+
32+
current_room = 31
33+
outdoor_rooms = range(1, 26)
34+
35+
top_left_x = 100
36+
top_left_y = 150
37+
38+
39+
for planetsectors in range(1, 26):
40+
GAME_MAP.append(["The dusty planet surface", 13, 13, True, True])
41+
42+
GAME_MAP += [["The airlock", 13, 5, True, False], # room 26
43+
["The engineering lab", 13, 13, False, False],
44+
["Poodle Mission Control", 9, 13, False, True], # room 28
45+
["The viewing gallery", 9, 15, False, False],
46+
["The crew’s bathroom", 5, 5, False, False], # room 30
47+
["The airlock entry bay", 7, 11, True, True],
48+
["Left elbow room", 9, 7, True, False], # room 32
49+
["Right elbow room", 7, 13, True, True],
50+
["The science lab", 13, 13, False, True], # room 34
51+
["The greenhouse", 13, 13, True, False],
52+
[PLAYER_NAME + "’s sleeping quarters", 9, 11, False, False],
53+
["West corridor", 15, 5, True, True],
54+
["The briefing room", 7, 13, False, True], # room 38
55+
["The crew’s community room", 11, 13, True, False],
56+
["Main Mission Control", 14, 14, False, False], # room 40
57+
["The sick bay", 12, 7, True, False],
58+
["West corridor", 9, 7, True, False], # room 42
59+
["Utilities control room", 9, 9, False, True],
60+
["Systems engineering bay", 9, 11, False, False], # room 44
61+
["Security portal to Mission Control", 7, 7, True, False],
62+
[FRIEND1_NAME + "’s sleeping quarters", 9, 11, True, True],
63+
[FRIEND2_NAME + "’s sleeping quarters", 9, 11, True, True],
64+
["The pipeworks", 13, 11, True, False], # room 48
65+
["The chief scientist’s office", 9, 7, True, True],
66+
["The robot workshop", 9, 11, True, False]
67+
]
68+
69+
70+
assert len(GAME_MAP) - 1 == MAP_SIZE, "Map size and GAME_MAP don’t match."
71+
72+
73+
def get_floor_type():
74+
if current_room in outdoor_rooms:
75+
return 2 # soil
76+
else:
77+
return 0 # tiled floor
78+
79+
def generate_map():
80+
global room_map, room_width, room_height, room_name, hazard_map
81+
global top_left_x, top_left_y, wall_transparency_frame
82+
room_data = GAME_MAP[current_room]
83+
room_name = room_data[0]
84+
room_height = room_data[1]
85+
room_width = room_data[2]
86+
87+
floor_type = get_floor_type()
88+
if current_room in range(1, 21):
89+
bottom_edge = 2 # soil
90+
side_edge = 2
91+
if current_room in range(21, 26):
92+
bottom_edge = 1 # wall
93+
side_edge = 2
94+
if current_room > 25:
95+
bottom_edge = 1
96+
side_edge = 1
97+
98+
# Top line of room map
99+
room_map = [[side_edge] * room_width]
100+
101+
# Add middle lines (wall, floor to fill width, wall)
102+
for y in range(room_height - 2):
103+
room_map.append([side_edge]
104+
+ [floor_type] * (room_width - 2) + [side_edge])
105+
106+
# Add bottom line of room map
107+
room_map.append([bottom_edge] * room_width)
108+
109+
# Add doorways
110+
middle_row = int(room_height / 2)
111+
middle_column = int(room_width / 2)
112+
113+
if room_data[4]: # If exit at right of this room
114+
room_map[middle_row][room_width - 1] = floor_type
115+
room_map[middle_row + 1][room_width - 1] = floor_type
116+
room_map[middle_row - 1][room_width - 1] = floor_type
117+
118+
if current_room % MAP_WIDTH != 1: # If not on the left of the map
119+
room_to_left = GAME_MAP[current_room - 1]
120+
if room_to_left[4]:
121+
room_map[middle_row][0] = floor_type
122+
room_map[middle_row + 1][0] = floor_type
123+
room_map[middle_row - 1][0] = floor_type
124+
125+
if room_data[3]: # If exit at top of this room
126+
room_map[0][middle_column] = floor_type
127+
room_map[0][middle_column + 1] = floor_type
128+
room_map[0][middle_column - 1] = floor_type
129+
130+
if current_room <= MAP_SIZE - MAP_WIDTH: # If room not on bottom row
131+
room_below = GAME_MAP[current_room + MAP_WIDTH]
132+
if room_below[3]:
133+
room_map[room_height - 1][middle_column] = floor_type
134+
room_map[room_height - 1][middle_column + 1] = floor_type
135+
room_map[room_height - 1][middle_column - 1] = floor_type
136+
137+
138+
def draw():
139+
global room_height, room_width, room_map
140+
generate_map()
141+
screen.clear()
142+
143+
for y in range(room_height):
144+
for x in range(room_width):
145+
image_to_draw = DEMO_OBJECTS[room_map[y][x]]
146+
screen.blit(image_to_draw,
147+
(top_left_x + (x * 30),
148+
top_left_y + (y * 30) - image_to_draw.get_height()))
149+
150+
# Exploring the room
151+
def movement():
152+
global current_room
153+
old_room = current_room
154+
155+
if keyboard.left:
156+
current_room -= 1
157+
if keyboard.right:
158+
current_room += 1
159+
if keyboard.up:
160+
current_room -= MAP_WIDTH
161+
if keyboard.down:
162+
current_room += MAP_WIDTH
163+
164+
if current_room > 50:
165+
current_room = 50
166+
if current_room < 1:
167+
current_room = 1
168+
169+
if current_room != old_room:
170+
print("Entering room: " + str(current_room))
171+
172+
clock.schedule_interval(movement, 0.1)
173+
174+
pgzrun.go()

0 commit comments

Comments
 (0)