forked from assamidanov/python_programming_class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartist.py
More file actions
306 lines (271 loc) · 9.44 KB
/
artist.py
File metadata and controls
306 lines (271 loc) · 9.44 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
import pygame
from color import Color
import numpy as np
class Artist:
"""A class containing static definitions for draw functions
Allows for a factory of artistry, dedicated to drawing objects based on
their provided coordinates, color, shape, and size
"""
@staticmethod
def draw(
surface: pygame.Surface,
x: int,
y: int,
color: tuple,
size: int,
shape: str) -> None:
"""
Draws the object based on its parameters
This function uses a dictionary to store shape: parameter, functions.
The shape is one of 's', 't', or 'c' signifying square, triangle, or circle.
The parameters is a tuple of the parameters that are to be passed to the
respective pygame.draw function.
Parameters
----------
surface : pygame.Surface
The surface to draw the object onto
x : int
The x coordinate of the object
y : int
The y coordinate of the object
color : tuple
A tuple representing the (R, G, B) values of the object's color
size : int
An int representing the size of the object
For a square, it will be the length of a side
For a circle, it will be its radius
For a triangle, it will be the length of a side
shape : str
A string of characters 's', 't', or 'c' denoting whether the object is a
square, triangle, or circle.
"""
*shape_to_draw, function_to_use = {
# Rectangle draw takes coords and side lengths
's': ((x, y, size, size), pygame.draw.rect),
# Polygon draw takes 2 coords of the points of the triangle
't': ((
(x, y),
(x - size//2, y + size//2),
(x + size//2, y + size//2)
), pygame.draw.polygon),
# Circle draw takes a tuple of coords and a radius
'c': ((x, y), size/2, pygame.draw.circle),
}[shape[0]] # Choose which shape to draw and function to use depending on the
# first character of the passed string
# Run the given function with the given parameters
function_to_use(surface, color, *shape_to_draw)
@staticmethod
def draw_cannon(
surface: pygame.Surface,
x: int,
y: int,
angle: int,
pow: int,
color: tuple) -> None:
"""
Draws the cannon based on its parameters
This function uses the angle and power to determine the size and angle
of the cannon
Parameters
----------
surface : pygame.Surface
The surface to draw the cannon onto
x : int
The x coordinate of the object
y : int
The y coordinate of the object
angle : int
The cannon's angle
pow : int
The power of the cannon (depending on how long the user held for)
color : tuple
A tuple representing the (R, G, B) values of the object's color
"""
vec_1 = np.array(
[
int(5*np.cos(angle - np.pi/2)),
int(5*np.sin(angle - np.pi/2))
]
)
vec_2 = np.array(
[
int(pow*np.cos(angle)),
int(pow*np.sin(angle))
]
)
gun_pos = np.array([x, y])
# Determine the gun's shape (a polygon needs a list of points)
# Even though this will come out as a rectangle, we just get its list
# of points as a polygon and draw that
gun_shape = []
gun_shape.append(
(gun_pos + vec_1).tolist()
)
gun_shape.append(
(gun_pos + vec_1 + vec_2).tolist()
)
gun_shape.append(
(gun_pos + vec_2 - vec_1).tolist()
)
gun_shape.append(
(gun_pos - vec_1).tolist()
)
pygame.draw.polygon(surface, color, gun_shape)
@staticmethod
def draw_score(
surface: pygame.Surface,
font: pygame.font.Font,
targets_destroyed: int,
projectiles_used: int,
score: int,
chosen_type: str,
health: int,
primary_color: tuple,
secondary_color: tuple) -> None:
"""
Draws the score table based on its parameters
This function uses the font, scores, and colors to draw the score table
Parameters
----------
surface : pygame.Surface
The surface to draw the cannon onto
font : pygame.font
The font to use for the text
targets_destroyed : int
The number of targets destroyed by the player
projectiles_used : int
The number of projectiles used
score : int
The player's total score, determined by targets and projectiles
chosen_type : str
The user's currently chosen type
health : int
The user's health
primary_color : tuple
The color to use for the score
The color to use for the statistics
secondary_color : tuple
The color to use for the statistics
"""
score_surf = []
# The text for targets_destroyed
score_surf.append(
font.render(
"Destroyed: {}".format(targets_destroyed),
True,
secondary_color
)
)
# The text for projectiles_used
score_surf.append(
font.render(
"Balls used: {}".format(projectiles_used),
True,
secondary_color
)
)
# The text for the total score
score_surf.append(
font.render(
"Total: {}".format(score),
True,
primary_color
)
)
# The test for the chosen type
if chosen_type == 's':
chosen_type = "Square"
if chosen_type == 'c':
chosen_type = "Circle"
if chosen_type == 't':
chosen_type = "Triangle"
score_surf.append(
font.render(
f"Chosen: {chosen_type}",
True,
secondary_color
)
)
# The text for the user health
score_surf.append(
font.render(
f"Health: {health}",
True,
primary_color
)
)
# Place each text piece to the screen
for i in range(3):
surface.blit(
score_surf[i],
[10, 10 + 30*i]\
)
surface.blit(score_surf[-2], [surface.get_size()[1] - 50, 10])
surface.blit(score_surf[-1], [surface.get_size()[1] - 50, 40])
@staticmethod
def draw_death_screen(
surface: pygame.Surface,
font: pygame.font.Font,
game_over_text: str,
score: int,
color: tuple) -> None:
"""
Draws the death screen when the user dies
Parameters
----------
surface : pygame.Surface
The surface to draw the death screen over
font : pygame.font.Font
The font to use for the text
game_over_text : str
The text to display
score : int
The player's score before they died
color : tuple
The color to set the text and score
"""
# Initialize a new screen
death_screen = pygame.Surface(surface.get_size())
death_screen.fill(Color.BLACK)
# Game over text
game_over_surface = font.render(
game_over_text,
True,
color
)
# Final score text
score_surface = font.render(
f"Final Score: {score}",
True,
color
)
# Instructions to exit
to_exit = font.render(
"Press any key to exit",
True,
Color.WHITE
)
text_piece = [game_over_surface, score_surface, to_exit]
screen_center = [
death_screen.get_size()[0]//2,
death_screen.get_size()[1]//2 - 30
]
# Place each text piece to the screen
for i in range(2):
death_screen.blit(
text_piece[i],
(
screen_center[0] - text_piece[i].get_width() // 2,
screen_center[1] + 30 * i - text_piece[i].get_height() // 2
)
)
# Put text on death screen
death_screen.blit(
text_piece[-1],
(
screen_center[0] - text_piece[-1].get_width() // 2,
screen_center[1] + 30 * 3 - text_piece[i].get_height() // 2
)
)
# Put death screen on main screen
surface.blit(death_screen, (0, 0))