-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
239 lines (204 loc) · 7.74 KB
/
view.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
"""
This module creates a class to generate a view for a user based on the model.
It acts as the View section of MVC architecture.
Note: This file cannot be tested as it controls display and visuals.
"""
import pygame
pygame.font.init()
pygame.mixer.init()
FONT = pygame.font.Font("Font/PressStart2P-Regular.ttf", 15)
class View:
"""
Creates the view of the game for the player.
Attributes:
_model: An instance of Model class
_background_sound: pygame.mixer.Sound() that
is the background music of the game
_end_sound: pygame.mixer.Sound() that is the
end screen sound effect played
_easy_button: An instance of the Button class
that represents the button on the menu screen
that is used to select easy mode
_medium_button: An instance of the Button class
that represents the button on the menu screen
that is used to select medium mode
_hard_button: An instance of the Button class
that represents the button on the menu screen
that is used to select hard mode
_rocket_move_sprite: A pygame.Surface() that represents
the rocket jumping sprite
"""
def __init__(self, model) -> None:
"""
Initializes attributes that will be used to
display the model
Args:
model: An instance of the Model class
"""
self._model = model
self._background_sound = pygame.mixer.Sound(
"sounds/background_sound.mp3"
)
self._background_sound.play(loops=-1)
self._end_sound = pygame.mixer.Sound("sounds/end_sound.wav")
self._easy_button = Button((25, 200), "EASY")
self._medium_button = Button((150, 200), "MEDIUM")
self._hard_button = Button((275, 200), "HARD")
self._rocket_move_sprite = pygame.image.load(
"sprites/rocket_move_0.png"
)
self._rocket_move_sprite = pygame.transform.scale(
self._rocket_move_sprite,
(
self._model.player.rect.width,
self._model.player.rect.width
* (
self._rocket_move_sprite.get_height()
/ self._rocket_move_sprite.get_width()
),
),
)
def draw_menu(self, display_surface):
"""
Draws a display of the start menu for the user to view
Args:
display_surface: A surface object representing the menu window
"""
display_surface.fill((0, 0, 0))
self._easy_button.display(display_surface)
self._medium_button.display(display_surface)
self._hard_button.display(display_surface)
def draw_game(self, display_surface):
"""
Draws game screen for the user to view
Args:
display_surface: A pygame.display representing the window
to view
"""
display_surface.fill((0, 0, 0))
for platform in self._model.platforms:
display_surface.blit(platform.surf, platform.rect)
if self._model.player.velocity.y < 0:
display_surface.blit(
self._rocket_move_sprite, self._model.player.rect
)
else:
display_surface.blit(
self._model.player.image, self._model.player.rect
)
def draw_timer(self, time, display_surface):
"""
Draws the timer for the user to view during gameplay
Args:
display_surface: A A pygame.display object representing the window
to view.
"""
timer_text = FONT.render(time, True, (255, 255, 255))
display_surface.blit(timer_text, timer_text.get_rect(center=(200, 50)))
def draw_score(self, display_surface):
"""
Draws the score onto the display
Args:
display_surface: A pygame.display representing the window
to view
"""
score_text = FONT.render(
f"SCORE: {self._model.score}", True, (255, 255, 255)
)
display_surface.blit(score_text, (10, 10))
def draw_game_over(self, display_surface):
"""
Draws the game over display for the user to view
Args:
display_surface: A A pygame.display object representing the window
to view.
"""
display_surface.fill((0, 0, 0))
game_over = FONT.render("GAME OVER", True, (255, 255, 255))
score_text = FONT.render(
f"SCORE: {self._model.score}", True, (255, 255, 255)
)
display_surface.blit(game_over, game_over.get_rect(center=(200, 150)))
display_surface.blit(score_text, score_text.get_rect(center=(200, 200)))
self._background_sound.stop()
self._end_sound.play()
@property
def easy_button(self):
"""
Allows the private attribute _easy_button to be output
Returns:
Private attribute _easy_button which is a Button object
"""
return self._easy_button
@property
def medium_button(self):
"""
Allows the private attribute _medium_button to be output
Returns:
Private attribute _medium_button which is a Button object
"""
return self._medium_button
@property
def hard_button(self):
"""
Allows the private attribute _hard_button to be output
Returns:
Private attribute _hard_button which is a Button object
"""
return self._hard_button
class Button:
"""
Creates buttons on the display that a user may click as a form of input
Attributes:
_top_left: A tuple representing the top left position of where to
place a rectangle
_text: A string representing text to place on the button
_text_color: A tuple representing RGB color of the text
_width: An int representing the width of the button
_height: An int representing the height of the button
_button_color: A tuple representing the RGB color of the button
_button_rect: A pygame rectangle object representing the button size and
shape
_text_surf: A surface object to write text on
_text_rect: A rectangle object representing the shape to put text in
"""
def __init__(self, top_left, text) -> None:
"""
Initializes the button to be displayed.
Args:
top_left: A tuple representing the (x,y) position of the button
placement
text: A string representing the text on the button
"""
self._top_left = top_left
self._text = text
self._text_color = (0, 0, 0)
self._width = 100
self._height = 50
self._button_color = (210, 210, 210)
self._button_surf = pygame.Surface((self._width, self._height))
self._button_surf.fill(self._button_color)
self._button_rect = self._button_surf.get_rect(topleft=self._top_left)
self._text_surf = FONT.render(self._text, True, self._text_color)
self._text_rect = self._text_surf.get_rect(
center=self._button_rect.center
)
def display(self, display_surface):
"""
Draws a buttons for the user to view
Args:
display_surface: A pygame.display representing the window
to view.
"""
display_surface.blit(self._button_surf, self._button_rect)
display_surface.blit(self._text_surf, self._text_rect)
@property
def button_rect(self):
"""
Allows private attribute _button_rect to be output
Args:
none
Return:
Private attribute _button_rect which is of pygame.Rect()
"""
return self._button_rect