-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
217 lines (177 loc) · 7.67 KB
/
menu.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
import random
import sys
from constant import HEIGHT, WIDTH, BLACK, WHITE, RED, GREEN, RestartGameSingle
from save import getBestScores, saveGame
class Menu:
def __init__(self, pygame, screen):
self.pygame = pygame
self.screen = screen
self.font = self.pygame.font.SysFont('Time New Roman', 60)
def displayStartMenu(self):
self.screen.fill(BLACK)
# Title Text
title = self.font.render('SNAKE', False, GREEN)
titleRect = title.get_rect()
titleRect.center = (WIDTH//2, 2*(HEIGHT//7))
self.screen.blit(title, titleRect)
# Play single Text
play = self.font.render('s - SINGLE PLAYER', False, WHITE)
playRect = play.get_rect()
playRect.center = (WIDTH//2, 3*(HEIGHT//7) + 50)
self.screen.blit(play, playRect)
# Play dual Text
play = self.font.render('d - DUAL PLAYER', False, WHITE)
playRect = play.get_rect()
playRect.center = (WIDTH//2, 3*(HEIGHT//7) + 100)
self.screen.blit(play, playRect)
# Play bot Text
play = self.font.render('b - BOT GAME', False, WHITE)
playRect = play.get_rect()
playRect.center = (WIDTH//2, 3*(HEIGHT//7) + 150)
self.screen.blit(play, playRect)
# Load Text
load = self.font.render('l - LOAD', False, WHITE)
loadRect = load.get_rect()
loadRect.center = (WIDTH//2, 3*(HEIGHT//7) + 200)
self.screen.blit(load, loadRect)
# Ranking Text
rank = self.font.render('r - RANKING', False, WHITE)
rankRect = rank.get_rect()
rankRect.center = (WIDTH//2, 3*(HEIGHT//7) + 250)
self.screen.blit(rank, rankRect)
# Exit Text
quit = self.font.render('e - EXIT', False, WHITE)
quitRect = quit.get_rect()
quitRect.center = (WIDTH//2, 6*(HEIGHT//7))
self.screen.blit(quit, quitRect)
self.pygame.display.update()
def displayRanking(self):
self.screen.fill(BLACK)
bestScores = getBestScores()
distance = 0
# Title Text
title = self.font.render('HIGH SCORES:', False, RED)
titleRect = title.get_rect()
titleRect.center = (WIDTH//2, HEIGHT//8)
self.screen.blit(title, titleRect)
# All Rankings Text
for entry in bestScores.scores:
score = self.font.render(
str(entry.username) + str(entry.score), False, WHITE)
scoreRect = score.get_rect()
scoreRect.center = (WIDTH//2, 2*(HEIGHT//8) + distance)
self.screen.blit(score, scoreRect)
distance += 50
self.pygame.display.update()
def displayPauseMenu(self):
self.screen.fill(BLACK)
# Pause Text
pause = self.font.render('- Pause -', False, WHITE)
pauseRect = pause.get_rect()
pauseRect.center = (WIDTH//2, 2*(HEIGHT//7))
self.screen.blit(pause, pauseRect)
# Resume Text
resume = self.font.render('c - RESUME', False, WHITE)
resumeRect = resume.get_rect()
resumeRect.center = (WIDTH//2, 3*(HEIGHT//7) + 50)
self.screen.blit(resume, resumeRect)
# Restart Text
restart = self.font.render('r - RESTART', False, WHITE)
restartRect = restart.get_rect()
restartRect.center = (WIDTH//2, 3*(HEIGHT//7) + 100)
self.screen.blit(restart, restartRect)
# Save Text
save = self.font.render('s - SAVE', False, WHITE)
saveRect = save.get_rect()
saveRect.center = (WIDTH//2, 3*(HEIGHT//7) + 150)
self.screen.blit(save, saveRect)
# Exit Text
quit = self.font.render('e - EXIT', False, WHITE)
quitRect = quit.get_rect()
quitRect.center = (WIDTH//2, 6*(HEIGHT//7))
self.screen.blit(quit, quitRect)
self.pygame.display.update()
def displayPauseMenuDualMode(self):
self.screen.fill(BLACK)
# Pause Text
pause = self.font.render('- Pause -', False, WHITE)
pauseRect = pause.get_rect()
pauseRect.center = (WIDTH//2, 2*(HEIGHT//7))
self.screen.blit(pause, pauseRect)
# Resume Text
resume = self.font.render('c - RESUME', False, WHITE)
resumeRect = resume.get_rect()
resumeRect.center = (WIDTH//2, 3*(HEIGHT//7) + 50)
self.screen.blit(resume, resumeRect)
# Restart Text
restart = self.font.render('r - RESTART', False, WHITE)
restartRect = restart.get_rect()
restartRect.center = (WIDTH//2, 3*(HEIGHT//7) + 100)
self.screen.blit(restart, restartRect)
# Exit Text
quit = self.font.render('e - EXIT', False, WHITE)
quitRect = quit.get_rect()
quitRect.center = (WIDTH//2, 6*(HEIGHT//7))
self.screen.blit(quit, quitRect)
self.pygame.display.update()
def displayGameOver(self, score):
self.screen.fill(BLACK)
# GameOver Text
gameOver = self.font.render('GAME OVER', False, RED)
gameOverRect = gameOver.get_rect()
gameOverRect.center = (WIDTH//2, (HEIGHT//2) - 25)
self.screen.blit(gameOver, gameOverRect)
# Final Score Text
currentScore = self.font.render(
'Your Score: ' + str(score), False, RED)
scoreRect = currentScore.get_rect()
scoreRect.center = (WIDTH//2, (HEIGHT//2) + 25)
self.screen.blit(currentScore, scoreRect)
self.pygame.display.update()
def displayGameOverDualMode(self, playerO, playerT):
self.screen.fill(BLACK)
# GameOver Text
gameOver = self.font.render('GAME OVER', False, RED)
if (playerT == False and playerO == True):
gameOver = self.font.render('Player 1 WIN', False, RED)
elif (playerT == True and playerO == False):
gameOver = self.font.render('Player 2 WIN', False, RED)
elif (playerT == False and playerO == False):
gameOver = self.font.render('Both Players are dead', False, RED)
gameOverRect = gameOver.get_rect()
gameOverRect.center = (WIDTH//2, (HEIGHT//2) - 25)
self.screen.blit(gameOver, gameOverRect)
self.pygame.display.update()
def pauseMenuSinglePlayer(self, pygame, player, apple, menu):
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
return False # False = Not restart game
# kills the snake but does not show death screen for resume and save
if event.key == pygame.K_r:
return True # True = Restart game
if event.key == pygame.K_s:
saveGame(player.size, player.state,
apple.x, apple.y)
player.state[0]['x'] = -5 # Kill player
return False
if event.key == pygame.K_e:
pygame.display.quit()
pygame.quit()
sys.exit()
menu.displayPauseMenu()
def pauseMenuDualPlayer(self, pygame, menu):
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
return False # False = Not restart game
# does not show death screen for resume and save
if event.key == pygame.K_r:
return True # True = Restart game
if event.key == pygame.K_e:
pygame.display.quit()
pygame.quit()
sys.exit()
menu.displayPauseMenuDualMode()