-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
506 lines (422 loc) · 16.8 KB
/
Copy pathmain.py
File metadata and controls
506 lines (422 loc) · 16.8 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""
Auth: Cédric De Dryver, Andréas Bombaert,
Last Date: 7/11/2021
"""
# IMPORT SECTION
import curses
import random
import time
from kivy import Config
from PythonProject.GUI.GUI import MenuApp, launch_menu
from PythonProject.GUI.GUIpacman import GameInit, GameApp
from PythonProject.Module_PacDuel.MappingGen import Map
from PythonProject.Module_PacDuel.MovingEntities import Pacman, Ghost
from PythonProject.Module_PacDuel.ScoreCount import ScoreCount
from PythonProject.Module_PacDuel.MenuGen import Menu
def init_win(stdscr):
"""
Ecrit par Cedric de Dryver le 09 novembre 2020
Déplacé dans une fonction par Andréas le 10 novembre 2020
Description: initialisation de la fenetre avec curses ainsi que l'initialisation
des paires de couleur
"""
curses.curs_set(0)
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(True)
curses.start_color()
curses.initscr()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_BLUE)
y, x = stdscr.getmaxyx()
resize = curses.is_term_resized(y, x)
# Resize si c'est trop petit
if resize is True:
stdscr.clear()
curses.resizeterm(30, 25)
stdscr.refresh()
def game_won(stdscr, score):
"""
Ecrit par Cedric de Dryver le 09 novembre 2020
Déplacé dans une fonction par Andréas le 10 novembre 2020
Description: Affichage de fin de partie
"""
stdscr.erase()
gg = "You won ! Congratz"
endmessage = "Press Q to quit the game"
stdscr.addstr(1, 1, gg, curses.color_pair(3))
stdscr.addstr(2, 1, "Score: ", curses.color_pair(3))
stdscr.addstr(2, 7, str(score.get_score), curses.color_pair(3))
stdscr.addstr(3, 1, endmessage, curses.color_pair(3))
stdscr.refresh()
key = stdscr.getch()
if key == ord('q'):
return True
def life_lost(stdscr, lives, score):
"""
Ecrit par Andreas Bombaert
Description: [version console] affiche un message quand le pacman meurt une fois
"""
stdscr.erase()
msg = ""
if lives > 1:
msg = f"You lost one life, you have {lives} lives left!"
elif lives == 1:
msg = f"You lost one life, you have {lives} life left!"
else:
game_lost(stdscr, score)
nxt = "Press any key to go to next life"
stdscr.addstr(1, 1, msg, curses.color_pair(3))
stdscr.addstr(3, 1, nxt, curses.color_pair(3))
stdscr.refresh()
key = stdscr.getch()
return True
def game_lost(stdscr, score):
stdscr.erase()
gg = "You Lost ! Better luck next time"
endmessage = "Press any key to quit the game"
stdscr.addstr(1, 1, gg, curses.color_pair(3))
stdscr.addstr(2, 1, "Score: ", curses.color_pair(3))
stdscr.addstr(2, 7, str(score.get_score), curses.color_pair(3))
stdscr.addstr(3, 1, endmessage, curses.color_pair(3))
stdscr.refresh()
stdscr.getch()
stdscr.erase()
stdscr.refresh()
return True
def clear_window(stdscr):
stdscr.erase()
stdscr.refresh()
def game_loop(stdscr, lives):
""" Main function
Auth: Cédric De Dryver, November 09 2020 - 17h27
Modified by Andréas Bombaert, November 10 2020 - 17h
All lines have a step-by-step description in French.
But this main function allows you to play the simplified pac man game.
the keys are Z: up, S: down, Q: left, D: right.
It's better with an AZERTY keyboard.
"""
# Initialisation de la library curse:
init_win(stdscr)
# Position du PacMan et sa position initiale.
pacman = Pacman(lives)
pacman.setpos(16, 11)
# Initialisation de la carte
game_map = Map("data/map1.txt", pacman.pos[0], pacman.pos[1])
game_map.gen_map()
map_ar = game_map.map_ar
pacman.set_eaten(map_ar)
# Position du/des fantomes et leur(s) position initiale
ghosts = []
ghost1 = Ghost(curses.color_pair(2))
ghost2 = Ghost(curses.color_pair(4))
ghost3 = Ghost(curses.color_pair(3))
ghost4 = Ghost(curses.color_pair(1))
ghosts.append(ghost1)
ghosts.append(ghost2)
ghosts.append(ghost3)
ghosts.append(ghost4)
map_ar = ghost1.set_init_pos(map_ar, pacman)
map_ar = ghost2.set_init_pos(map_ar, pacman)
map_ar = ghost3.set_init_pos(map_ar, pacman)
map_ar = ghost4.set_init_pos(map_ar, pacman)
# Initialisation du Terrain curses
game_map.cast_map(map_ar, stdscr)
# Initialisation du score.
score = ScoreCount()
count_coll = game_map.collectibles
positions = [pacman.pos]
for x in ghosts:
positions.append(x.pos)
while True:
"""
boucle du jeu,
pour quitter cette boucle, appuyez sur "p" ou ramassez tout les collectibles
touches de mouvement:
Z : Haut
S : Bas
Q : Gauche
D : Droite
"""
if pacman.lives <= 0:
if game_lost(stdscr, score):
break
stdscr.addstr(1, 22, "Score: ", curses.color_pair(3)) # Refreshing du score a chaque mouvement.
stdscr.addstr(1, 29, str(score.get_score), curses.color_pair(3))
stdscr.addstr(1, 33, "Lives : ", curses.color_pair(3))
stdscr.addstr(1, 40, str(pacman.lives), curses.color_pair(3))
# affichage de la carte au début
game_map.cast_map(map_ar, stdscr)
# Si le compteur de collectible atteint 0, alors la partie est fini et affiche un ecran de victoire (Appuyer
# sur Q pour quitter cet ecran).
if count_coll == 0:
if game_won(stdscr, score):
break
# On va lire les touches que l'user va rentrer.
key = stdscr.getch()
# Reference : Haut(y => -∞) Bas(y => +∞) Gauche(x => -∞) Droite(x => +∞)
# => : tend vers ...
# Il y a sous ce commentaire,les 4 touches qui permettent de se déplacer.
# A Chaques fois que le PacMan veut se déplacer, il verifie ce qu'il y a devant lui, et si il peut,
# il se déplace et récupere des points (si il y en a)
# Si l'user decide, il peut quitter le jeu en appuyant sur P (attention pas de verification).
if key == ord('z') or key == ord('q') or key == ord('s') or key == ord('d'):
prev_pacman = pacman.pos.copy()
key, map_ar, score, count_coll = pacman.moves(key, map_ar, score, count_coll) # pacman moves
direction = random.randint(1, 4)
for x in ghosts:
# direction 1: forward, 2: left, 3: backward, 4: right
prev_ghost = x.pos.copy()
map_ar = x.moves(map_ar, direction, 1, pacman) # every ghost moves
if x.pos == prev_pacman and pacman.pos == prev_ghost:
pacman.death()
life_lost(stdscr, pacman.lives, score)
stdscr.erase()
stdscr.refresh()
game_loop(stdscr, pacman.lives)
positions = [[pacman.pos]]
for x in ghosts:
positions.append(x.pos)
if pacman.on_ghost(ghosts):
pacman.death()
if pacman.lives == 0:
if game_lost(stdscr, score):
break
life_lost(stdscr, pacman.lives, score)
stdscr.erase()
stdscr.refresh()
game_loop(stdscr, pacman.lives)
if key == ord('p'):
break
return score.get_score
def starting_multi_game(stdscr, player):
msg = f"Player {player}, please enter a key to start the game"
h, w = stdscr.getmaxyx()
x = w // 2 - len(msg) // 2
y = h // 2
stdscr.addstr(y, x, msg, curses.color_pair(4))
stdscr.getch()
def menu_gamemode(stdscr, menu_obj):
"""
auth: C. De Dryver
Cette fonction est utilisé pour creer un menu gamemode,
et l'utilisateur peut choisir l'un des choix qui s'offre a lui
en naviguant d'une réponse a l'autre.
PRE: (curses) stdscr : Interface console sur laquelle on travaille
(Menu) menu_obj : Classe local qui nous permet de creer un menu
POST: Creer un choix entre le mode solo ou le mode duo. Solo / Duo
"""
gamemode_current_row = 6
menu_obj.menu_gamemode(stdscr, gamemode_current_row)
while True:
key = stdscr.getch()
if (key == curses.KEY_LEFT or key == ord('q')) and gamemode_current_row == 7:
gamemode_current_row -= 1
elif (key == curses.KEY_RIGHT or key == ord('d')) and gamemode_current_row == 6:
gamemode_current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
if gamemode_current_row == 6:
stdscr.clear()
game_loop(stdscr, 3)
elif gamemode_current_row == 7:
stdscr.clear()
player1 = "1"
player2 = "2"
starting_multi_game(stdscr, player1)
time1_start = time.time()
score1 = game_loop(stdscr, 1)
time1_end = time.time()
time1 = time1_end - time1_start
stdscr.clear()
starting_multi_game(stdscr, player2)
time2_start = time.time()
score2 = game_loop(stdscr, 1)
time2_end = time.time()
time2 = time2_end - time2_start
time_div = 0
if time1 > time2:
time_div = time1
else:
time_div = time2
score1 = int((score1 * 10) / time_div)
score2 = int((score2 * 10) / time_div)
if score1 > score2:
win_msg = f"The winner is player {player1} with {score1} points, press any key to pass"
else:
win_msg = f"The winner is player {player2} with {score1} points, press any key to pass"
stdscr.clear()
h, w = stdscr.getmaxyx()
x = w // 2 - len(win_msg) // 2
y = h // 2
stdscr.addstr(y, x, win_msg, curses.color_pair(1))
stdscr.getch()
stdscr.clear()
msg_score1 = f"Player {player1} : {score1} points"
msg_score2 = f"Player {player2} : {score2} points"
stdscr.addstr(y - 1, x, msg_score1, curses.color_pair(1))
stdscr.addstr(y + 1, x, msg_score2, curses.color_pair(1))
stdscr.addstr(y - 3, x, "press any key to leave", curses.color_pair(1))
stdscr.refresh()
stdscr.getch()
exit()
menu_obj.menu_gamemode(stdscr, gamemode_current_row)
stdscr.refresh()
def menu_exit(stdscr, menu_obj):
"""
auth: C. De Dryver
Cette fonction est utilisé pour creer un menu exit,
et l'utilisateur peut choisir l'un des choix qui s'offre a lui
en naviguant d'une réponse a l'autre.
PRE: (curses) stdscr : Interface console sur laquelle on travaille
(Menu) menu_obj : Classe local qui nous permet de creer un menu
POST: Creer un choix pour quitter l'application. Yes / No
"""
leave_current_row = 4
menu_obj.menu_leave(stdscr, leave_current_row)
while True:
key = stdscr.getch()
if (key == curses.KEY_UP or key == ord('z')) and leave_current_row == 5:
leave_current_row -= 1
elif (key == curses.KEY_DOWN or key == ord('s')) and leave_current_row == 4:
leave_current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
if leave_current_row == 4:
exit()
elif leave_current_row == 5:
stdscr.clear()
break
menu_obj.menu_leave(stdscr, leave_current_row)
stdscr.refresh()
def main_curses(stdscr):
# Initialisation de la library curse:
init_win(stdscr)
menu_obj = Menu(5)
current_row = menu_obj.current_row
menu = menu_obj.menu_tab
menu_obj.print_menu(stdscr, current_row)
while True:
key = stdscr.getch()
if (key == curses.KEY_UP or key == ord('z')) and current_row > 5:
current_row -= 1
elif (key == curses.KEY_DOWN or key == ord('s')) and current_row < len(menu) - 1 + 5:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
if current_row == 5:
menu_gamemode(stdscr, menu_obj)
elif current_row == 6:
pass
# DISPLAY SCOREBOARD
elif current_row == 7:
pass
# DISPLAY SETTINGS
elif current_row == 8:
menu_exit(stdscr, menu_obj)
menu_obj.print_menu(stdscr, current_row)
stdscr.refresh()
def game_loop_gui(lives):
""" Game loop gui
Auth: Cédric De Dryver, Jan 02 18h03
This Game loop is used to create the game with a GUI
Not working, kivy is too outdated and incompatible with other libraries
the keys are Z: up, S: down, Q: left, D: right.
It's better with an AZERTY keyboard.
"""
# Position du PacMan et sa position initiale.
pacman = Pacman(lives)
pacman.setpos(16, 11)
Config.set('graphics', 'width', '1420')
Config.set('graphics', 'height', '800')
game_map = GameApp("data/map1.txt", pacman.pos[0], pacman.pos[1], pacman).run()
# Initialisation de la carte
map_ar = game_map.map_ar
pacman.set_eaten(map_ar)
# Position du/des fantomes et leur(s) position initiale
ghosts = []
ghost1 = Ghost(0)
ghost2 = Ghost(1)
ghost3 = Ghost(2)
ghost4 = Ghost(3)
ghosts.append(ghost1)
ghosts.append(ghost2)
ghosts.append(ghost3)
ghosts.append(ghost4)
map_ar = ghost1.set_init_pos(map_ar, pacman)
map_ar = ghost2.set_init_pos(map_ar, pacman)
map_ar = ghost3.set_init_pos(map_ar, pacman)
map_ar = ghost4.set_init_pos(map_ar, pacman)
# Initialisation du score.
score = ScoreCount()
count_coll = game_map.collectibles
positions = [pacman.pos]
for x in ghosts:
positions.append(x.pos)
while True:
"""
boucle du jeu,
pour quitter cette boucle, appuyez sur "p" ou ramassez tout les collectibles
touches de mouvement:
Z : Haut
S : Bas
Q : Gauche
D : Droite
"""
# On va lire les touches que l'user va rentrer.
key = GameApp.key
print(key)
if pacman.lives <= 0:
game_map.game_lost(score)
game_map.refresh()
# Si le compteur de collectible atteint 0, alors la partie est fini et affiche un ecran de victoire (Appuyer
# sur Q pour quitter cet ecran).
if count_coll == 0:
pass
game_map.game_win(score)
# Reference : Haut(y => -∞) Bas(y => +∞) Gauche(x => -∞) Droite(x => +∞)
# => : tend vers ...
# Il y a sous ce commentaire,les 4 touches qui permettent de se déplacer.
# A Chaques fois que le PacMan veut se déplacer, il verifie ce qu'il y a devant lui, et si il peut,
# il se déplace et récupere des points (si il y en a)
# Si l'user decide, il peut quitter le jeu en appuyant sur P (attention pas de verification).
key, map_ar, score, count_coll, prev_pacman= game_map.key, game_map.map_ar, game_map.score, game_map.collectible, game_map.prev_pacman # pacman moves
direction = random.randint(1, 4)
for x in ghosts:
# direction 1: forward, 2: left, 3: backward, 4: right
prev_ghost = x.pos.copy()
map_ar = x.moves(map_ar, direction, 1, pacman) # every ghost moves
if x.pos == prev_pacman and pacman.pos == prev_ghost:
pacman.death()
game_map.life_lost(pacman.lives, score)
game_map.refresh()
game_loop(pacman.lives)
positions = [[pacman.pos]]
for x in ghosts:
positions.append(x.pos)
if pacman.on_ghost(ghosts):
pacman.death()
if pacman.lives == 0:
game_map.game_lost(score)
game_map.life_lost(pacman.lives, score)
game_map.refresh()
game_loop_gui( pacman.lives)
if key == ord('p'):
break
return score.get_score
def main(choice):
"""
little main, to choose what interface we want
:param choice: str : work with these str : "curses", "gui", "pacman"
:return: launch one interface.
"""
if choice == 'curses':
curses.wrapper(main_curses)
elif choice == "gui":
launch_menu()
elif choice == 'pacman':
game_loop_gui(3)
if __name__ == "__main__":
main('pacman')