-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenePlay.py
More file actions
244 lines (197 loc) · 9.53 KB
/
ScenePlay.py
File metadata and controls
244 lines (197 loc) · 9.53 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
# Play scene - the main game play scene
from typing import List, Optional, Any
from pygame.locals import *
import pygame
import pygwidgets
import pyghelpers
from BaseScene import BaseScene
from Player import Player
from Baddies import BaddieMgr
from Goodies import GoodieMgr
from Constants import *
def showCustomYesNoDialog(theWindow: pygame.Surface, theText: str) -> bool:
oDialogBackground = pygwidgets.Image(theWindow, (40, 250), 'images/dialog.png')
oPromptDisplayText = pygwidgets.DisplayText(theWindow, (0, 290), theText,
width=WINDOW_WIDTH,
justified='center', fontSize=36)
oYesButton = pygwidgets.CustomButton(theWindow, (320, 370),
'images/gotoHighScoresNormal.png',
over='images/gotoHighScoresOver.png',
down='images/gotoHighScoresDown.png',
disabled='images/gotoHighScoresDisabled.png')
oNoButton = pygwidgets.CustomButton(theWindow, (62, 370),
'images/noThanksNormal.png',
over='images/noThanksOver.png',
down='images/noThanksDown.png',
disabled='images/noThanksDisabled.png')
choiceAsBoolean = pyghelpers.customYesNoDialog(theWindow,
oDialogBackground, oPromptDisplayText,
oYesButton, oNoButton)
return choiceAsBoolean
BOTTOM_RECT = (0, GAME_HEIGHT + 1, WINDOW_WIDTH,
WINDOW_HEIGHT - GAME_HEIGHT)
class GameState:
WAITING = 'waiting'
PLAYING = 'playing'
GAME_OVER = 'game over'
class GameUI:
def __init__(self, window: pygame.Surface):
self.window = window
self.controlsBackground = pygwidgets.Image(self.window, (0, GAME_HEIGHT), 'images/controlsBackground.jpg')
self.quitButton = pygwidgets.CustomButton(self.window, (30, GAME_HEIGHT + 90),
up='images/quitNormal.png',
down='images/quitDown.png',
over='images/quitOver.png',
disabled='images/quitDisabled.png')
self.highScoresButton = pygwidgets.CustomButton(self.window, (190, GAME_HEIGHT + 90),
up='images/gotoHighScoresNormal.png',
down='images/gotoHighScoresDown.png',
over='images/gotoHighScoresOver.png',
disabled='images/gotoHighScoresDisabled.png')
self.newGameButton = pygwidgets.CustomButton(self.window, (450, GAME_HEIGHT + 90),
up='images/startNewNormal.png',
down='images/startNewDown.png',
over='images/startNewOver.png',
disabled='images/startNewDisabled.png',
enterToActivate=True)
self.soundCheckBox = pygwidgets.TextCheckBox(self.window, (430, GAME_HEIGHT + 17),
'Background music', True, textColor=WHITE)
self.gameOverImage = pygwidgets.Image(self.window, (140, 180), 'images/gameOver.png')
self.titleText = pygwidgets.DisplayText(self.window, (70, GAME_HEIGHT + 17),
'Score: High Score:',
fontSize=24, textColor=WHITE)
self.scoreText = pygwidgets.DisplayText(self.window, (80, GAME_HEIGHT + 47), '0',
fontSize=36, textColor=WHITE, justified='right')
self.highScoreText = pygwidgets.DisplayText(self.window, (270, GAME_HEIGHT + 47), '',
fontSize=36, textColor=WHITE, justified='right')
def draw(self, game_state: str) -> None:
self.controlsBackground.draw()
self.titleText.draw()
self.scoreText.draw()
self.highScoreText.draw()
self.soundCheckBox.draw()
self.quitButton.draw()
self.highScoresButton.draw()
self.newGameButton.draw()
if game_state == GameState.GAME_OVER:
self.gameOverImage.draw()
def enable_buttons(self) -> None:
self.newGameButton.enable()
self.highScoresButton.enable()
self.soundCheckBox.enable()
self.quitButton.enable()
def disable_buttons(self) -> None:
self.newGameButton.disable()
self.highScoresButton.disable()
self.soundCheckBox.disable()
self.quitButton.disable()
class GameAudio:
def __init__(self):
pygame.mixer.music.load('sounds/background.mid')
self.dingSound = pygame.mixer.Sound('sounds/ding.wav')
self.gameOverSound = pygame.mixer.Sound('sounds/gameover.wav')
def play_background_music(self) -> None:
pygame.mixer.music.play(-1, 0.0)
def stop_background_music(self) -> None:
pygame.mixer.music.stop()
def play_ding(self) -> None:
self.dingSound.play()
def play_game_over(self) -> None:
self.gameOverSound.play()
class ScenePlay(BaseScene):
def __init__(self, window: pygame.Surface):
super().__init__(window)
self._ui = GameUI(window)
self._audio = GameAudio()
self._player = Player(window)
self._baddie_mgr = BaddieMgr(window)
self._goodie_mgr = GoodieMgr(window)
self._highest_high_score = 0
self._lowest_high_score = 0
self._background_music = True
self._score = 0
self._playing_state = GameState.WAITING
@property
def score(self) -> int:
return self._score
@score.setter
def score(self, value: int) -> None:
self._score = value
self._ui.scoreText.setValue(value)
@property
def playing_state(self) -> str:
return self._playing_state
@playing_state.setter
def playing_state(self, value: str) -> None:
self._playing_state = value
def getSceneKey(self) -> str:
return SCENE_PLAY
def enter(self, data: Optional[Any] = None) -> None:
self.get_high_scores()
def get_high_scores(self) -> None:
info_dict = self.request(SCENE_HIGH_SCORES, HIGH_SCORES_DATA)
self._highest_high_score = info_dict['highest']
self._ui.highScoreText.setValue(self._highest_high_score)
self._lowest_high_score = info_dict['lowest']
def reset(self) -> None:
self.score = 0
self.get_high_scores()
self._baddie_mgr.reset()
self._goodie_mgr.reset()
if self._background_music:
self._audio.play_background_music()
self._ui.disable_buttons()
pygame.mouse.set_visible(False)
def handleInputs(self, eventsList: List[pygame.event.Event], keyPressedList: List[int]) -> None:
if self._playing_state == GameState.PLAYING:
return
for event in eventsList:
if self._ui.newGameButton.handleEvent(event):
self.reset()
self._playing_state = GameState.PLAYING
if self._ui.highScoresButton.handleEvent(event):
self.goToScene(SCENE_HIGH_SCORES)
if self._ui.soundCheckBox.handleEvent(event):
self._background_music = self._ui.soundCheckBox.getValue()
if self._ui.quitButton.handleEvent(event):
self.quit()
def update(self) -> None:
if self._playing_state != GameState.PLAYING:
return
mouseX, mouseY = pygame.mouse.get_pos()
player_rect = self._player.update(mouseX, mouseY)
n_goodies_hit = self._goodie_mgr.update(player_rect)
if n_goodies_hit > 0:
self._audio.play_ding()
self.score += (n_goodies_hit * POINTS_FOR_GOODIE)
n_baddies_evaded = self._baddie_mgr.update()
self.score += (n_baddies_evaded * POINTS_FOR_BADDIE_EVADED)
if self._baddie_mgr.hasPlayerHitBaddie(player_rect):
self._handle_game_over()
def _handle_game_over(self) -> None:
pygame.mouse.set_visible(True)
self._audio.stop_background_music()
self._audio.play_game_over()
self._playing_state = GameState.GAME_OVER
self.draw()
if self.score > self._lowest_high_score:
self._handle_high_score()
self._ui.enable_buttons()
def _handle_high_score(self) -> None:
score_string = f'Your score: {self.score}\n'
if self.score > self._highest_high_score:
dialog_text = score_string + 'is a new high score, CONGRATULATIONS!'
else:
dialog_text = score_string + 'gets you on the high scores list.'
result = showCustomYesNoDialog(self.window, dialog_text)
if result:
self.goToScene(SCENE_HIGH_SCORES, self.score)
def draw(self) -> None:
self.window.fill(BLACK)
self._baddie_mgr.draw()
self._goodie_mgr.draw()
self._player.draw()
self._ui.draw(self._playing_state)
def leave(self) -> Optional[Any]:
self._audio.stop_background_music()
return None