-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacman.py
305 lines (225 loc) · 7.64 KB
/
pacman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# coding: utf-8
import curses
import time
from objects.character import Pacman, Ghost
from objects.game_box import GameBox
from objects.color import Color
COLOR = Color(curses)
# The only things configurable are the map (walls + food) and object postions
DATA = {
'map_file': 'maps/map.txt',
'map_chars': {
'wall': '█',
'door': '-',
'space': ' ',
'food': '·'
},
'character_positions': {
'pacman': [18, 29],
'ghosts': [[12, 25], [12, 29], [12, 33], [11, 29]]
},
}
LIVES = 3
FOOD_SCORE = 10
class PacmanGame():
def __init__(self):
self.screen_obj = curses.initscr()
curses.curs_set(0)
curses.noecho()
self.init_map()
self.show_start_screen()
def init_map(self):
self.game_box = GameBox(
self.screen_obj,
DATA['map_file'],
DATA['map_chars'],
{
'wall': COLOR.blue,
'door': COLOR.blue,
'space': COLOR.black,
'food': COLOR.yellow
}
)
def init_characters(self):
positions = DATA['character_positions']
ghost_pos = positions['ghosts']
self.pacman = Pacman(
self.game_box,
COLOR.yellow,
positions['pacman']
)
self.ghosts = {
'Blinky': Ghost(
self.game_box,
COLOR.red,
ghost_pos[0]
),
'Pinky': Ghost(
self.game_box,
COLOR.pink,
ghost_pos[1]
),
'Inky': Ghost(
self.game_box,
COLOR.cyan,
ghost_pos[2]
),
'Clyde': Ghost(
self.game_box,
COLOR.orange,
ghost_pos[3]
)
}
def start_new_game(self):
self.lives = LIVES
self.score = 0
self.game_box.set_map_matrix()
self.game_box.draw_map()
self.game_box.update_score(self.score)
self.game_box.update_lives(self.lives)
self.food_count = self.game_box.food_count
self.food_count -= 1 # Initial position over food
self.init_characters()
self.run_game_loop()
def restart(self):
self.reset_positions()
self.standby()
self.run_game_loop()
def standby(self):
time.sleep(1)
self.pacman.respawn()
for ghost in self.ghosts.values():
ghost.respawn()
self.blink_characters()
def win(self):
self.show_win_screen()
def end(self):
self.show_game_over_screen()
def run_game_loop(self):
game_loop_running = True
key = curses.KEY_RIGHT
while game_loop_running:
next_key = self.game_box.map_box.getch()
key = key if next_key == -1 else next_key
self.prev_position = self.pacman.current_position
# Ghosts move
for ghost in self.ghosts.values():
ghost.move_in_random_direction()
# Check state
if not self.pacman.stopped:
if self.food_eaten():
self.food_count -= 1
self.increment_score('food')
if self.food_count <= 0:
game_loop_running = False
time.sleep(1)
self.win()
if self.ghost_touched():
game_loop_running = False
for ghost in self.ghosts.values():
ghost.vanish()
self.die()
self.game_box.map_box.refresh()
if self.lives >= 0:
self.restart()
else:
self.end()
# Pacman moves
if key == curses.KEY_UP:
self.pacman.move('UP')
if key == curses.KEY_DOWN:
self.pacman.move('DOWN')
if key == curses.KEY_LEFT:
self.pacman.move('LEFT')
if key == curses.KEY_RIGHT:
self.pacman.move('RIGHT')
def die(self):
self.lives -= 1
self.game_box.update_lives(self.lives)
self.pacman.die()
def increment_score(self, score_type):
score_values = {
'food': FOOD_SCORE
}
self.score += score_values[score_type]
self.game_box.update_score(self.score)
def reset_positions(self):
positions = DATA['character_positions']
ghost_pos = positions['ghosts']
self.pacman.set_position(positions['pacman'])
for idx, ghost in enumerate(self.ghosts.values()):
ghost.set_position(ghost_pos[idx])
def blink_characters(self):
show = False
for i in range(4):
time.sleep(0.5)
self.pacman.toggle(show)
for ghost in self.ghosts.values():
ghost.toggle(show)
self.game_box.map_box.refresh()
show = not show
def food_eaten(self):
y, x = self.pacman.current_position
return self.game_box.map_matrix[y][x] == DATA['map_chars']['food']
def ghost_touched(self):
for ghost in self.ghosts.values():
if ghost.current_position in (self.pacman.current_position,
self.prev_position):
return True
return False
def show_start_screen(self):
blink_line_index = 0
mesg_line = '| Press any key to START |'
blank_line = '| |'
with open('screens/start.txt') as screen:
line_index = 0
for line in screen:
if 'Press' in line:
blink_line_index = line_index
self.game_box.map_box.addstr(
line_index,
0,
line,
COLOR.blue if '█' in line else COLOR.yellow
)
line_index += 1
self.game_box.border_box.refresh()
start_screen_running = True
show_msg = False
while start_screen_running:
next_key = self.game_box.map_box.getch()
self.game_box.map_box.addstr(
blink_line_index,
0,
mesg_line if show_msg else blank_line,
COLOR.yellow
)
show_msg = not show_msg
if next_key != -1:
start_screen_running = False
self.start_new_game()
time.sleep(0.5)
def show_win_screen(self):
self.show_end_screen('screens/win.txt', COLOR.yellow)
def show_game_over_screen(self):
self.show_end_screen('screens/game_over.txt', COLOR.orange)
def show_end_screen(self, filename, color):
with open(filename) as screen:
line_index = 7
for line in screen:
if 'SCORE: 0000' in line:
line = line.replace('SCORE: 0000', 'SCORE: ' + str(self.score).zfill(4))
self.game_box.map_box.addstr(line_index, 0, line, color)
line_index += 1
self.game_box.border_box.refresh()
game_over_screen_running = True
while game_over_screen_running:
next_key = self.game_box.map_box.getch()
if next_key != -1:
game_over_screen_running = False
if next_key == 27:
curses.endwin()
exit()
else:
self.start_new_game()
PacmanGame()