-
Notifications
You must be signed in to change notification settings - Fork 3
/
userinterface_class.py
337 lines (295 loc) · 16.3 KB
/
userinterface_class.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
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
"""
Created By: Phoenix Cushman, Stefano Candiani, Joel Kubinsky, Danush Singla
Date: 11/3/2023 - 12/06/2023
Project: Project 4, "Planettoids"
Group Game: "Blazing Glory"
File: userinterface_class
"""
import pygame
import time
# prints the level
class Level:
def __init__(self, screen_width, screen_height, screen):
'''Initializes the screen variables for the Level object.'''
self.screen_width = screen_width
self.screen_height = screen_height
self.screen = screen
return
# screen that displays the level
def level_menu(self, level_number):
'''Displays the level number screen over the course of 2 seconds and also listens for pygame.QUIT events.'''
# Level number
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 50)
for i in range(60):
button = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Thank you for playing!")
quit()
text_surface = font_object_title.render(f'Level {level_number}', True, (255*i/60, 176*i/60, 20*i/60))
text_surface_rect = text_surface.get_rect()
text_surface_rect.center = (self.screen_width // 2, self.screen_height // 2)
self.screen.fill((0,0,0))
self.screen.blit(text_surface, text_surface_rect)
pygame.display.flip()
time.sleep(2/60)
return
# Level complete screen
def level_increment(self, level_num):
'''Displays the level complete text over the screen.'''
# Game Over
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 100)
text_surface = font_object_title.render(f'Level {level_num} Complete!', True, (0, 255, 0))
text_surface_rect = text_surface.get_rect()
text_surface_rect.center = (self.screen_width // 2, self.screen_height // 5)
self.screen.blit(text_surface, text_surface_rect)
# Move on to next level
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 30) # menu text to start game
text_start = font_object_title.render('Press Enter to Play Next Level', True, (255, 255, 255))
text_start_rect = text_start.get_rect()
text_start_rect.center = (self.screen_width // 2, self.screen_height - 100)
self.screen.blit(text_start, text_start_rect)
# # Or quit by pressing escape
# font_object_title_end = pygame.font.Font('assets/AmazDooMLeft.ttf', 20) # menu text to start game
# text_end = font_object_title_end.render('Press Escape to Quit Game', True, (255, 255, 255))
# text_end_rect = text_start.get_rect()
# text_end_rect.center = (self.screen_width - 350, self.screen_height - 50)
# self.screen.blit(text_end, text_end_rect)
return
# if the player wins the game
class PlayerWon:
def __init__(self, screen_width, screen_height, screen):
'''Initializes the screen variables for the PlayerWon object.'''
self.screen_width = screen_width
self.screen_height = screen_height
self.screen = screen
return
# You win screen
def player_won_menu(self):
'''Displays the "YOU WIN!" text over the screen. This tells the player that they have beat the game.'''
# Game Over
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 100)
text_surface = font_object_title.render('YOU WIN!', True, (0, 255, 0))
text_surface_rect = text_surface.get_rect()
text_surface_rect.center = (self.screen_width // 2, self.screen_height // 5)
self.screen.blit(text_surface, text_surface_rect)
# Start Game Again
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 30) # menu text to start game
text_start = font_object_title.render('Press Enter to Play Again', True, (255, 255, 255))
text_start_rect = text_start.get_rect()
text_start_rect.center = (self.screen_width // 2, self.screen_height - 100)
self.screen.blit(text_start, text_start_rect)
# Or quit by pressing escape
font_object_title_end = pygame.font.Font('assets/AmazDooMLeft.ttf', 20) # menu text to start game
text_end = font_object_title_end.render('Press Escape to Quit Game', True, (255, 255, 255))
text_end_rect = text_start.get_rect()
text_end_rect.center = (self.screen_width - 368, self.screen_height - 50)
self.screen.blit(text_end, text_end_rect)
return
# if the game ends when the player hits an asteroid
class GameOver:
def __init__(self, screen_width, screen_height, screen):
'''Initializes the screen variables for the GameOver object.'''
self.screen_width = screen_width
self.screen_height = screen_height
self.screen = screen
return
# Game Over screen
def game_over_menu(self):
'''Displays the "game over" text over the screen.'''
# Game Over
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 100)
text_surface = font_object_title.render('GAME OVER', True, (255, 0, 0))
text_surface_rect = text_surface.get_rect()
text_surface_rect.center = (self.screen_width // 2, self.screen_height // 5)
self.screen.blit(text_surface, text_surface_rect)
# Start Game Again
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 30) # menu text to start game
text_start = font_object_title.render('Press Enter to Play Again', True, (255, 255, 255))
text_start_rect = text_start.get_rect()
text_start_rect.center = (self.screen_width // 2, self.screen_height - 100)
self.screen.blit(text_start, text_start_rect)
# Or quit by pressing escape
font_object_title_end = pygame.font.Font('assets/AmazDooMLeft.ttf', 20) # menu text to start game
text_end = font_object_title_end.render('Press Escape to Quit Game', True, (255, 255, 255))
text_end_rect = text_start.get_rect()
text_end_rect.center = (self.screen_width - 368, self.screen_height - 50)
self.screen.blit(text_end, text_end_rect)
return
# Initial menu that is printed first
class Menu:
def __init__(self, screen_width = 800, screen_height = 600):
'''Initializes the screen variables for the Menu object.'''
self.screen_width = screen_width
self.screen_height = screen_height
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
# image = Image.open('assets/down_arrow_off.png') # run one time if image background is not correct size
# new_image = image.resize((50, 50))
# new_image.save('assets/down_arrow_off.png')
# image = Image.open('assets/down_arrow_on.png') # run one time if image background is not correct size
# new_image = image.resize((50, 50))
# new_image.save('assets/down_arrow_on.png')
return
# Sets all of the text for the menu
def set_menu(self):
'''This function displays the game title menu with the included background and foreground text, as well as waiting for user input to proceed. (Enter starts the game)'''
bg = pygame.image.load("assets/800x600/menu 800x600.png")
# Title
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 100) # title
text_surface = font_object_title.render('PLANETTOIDS', True, (255, 255, 255))
text_surface_rect = text_surface.get_rect()
text_surface_rect.center = (self.screen_width // 2, self.screen_height // 5 - 50)
# Text to press enter
font_object_title = pygame.font.Font('assets/AmazDooMLeft.ttf', 30) # menu text to start game
text_start = font_object_title.render('Press Enter to Start Game', True, (255, 255, 255))
text_start_rect = text_start.get_rect()
text_start_rect.center = (self.screen_width // 2, self.screen_height - 100)
# Copyright text
font_object_title = pygame.font.Font('assets/arial.ttf', 10) # copyright text on menu
text_copyright = font_object_title.render('Copyright© 2023. Phoenix Cushman, Joel Kubinsky, Stefano Candiani, Danush Singla. All rights reserved.', True, (255, 255, 255))
text_copyright_rect = text_copyright.get_rect()
text_copyright_rect.center = (self.screen_width // 2, self.screen_height - 10)
# while loop that displays the menu continuously until enter is pressed to start the game
running = True # Main execution boolean
while running == True:
button = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Thank you for playing!")
quit()
if button[pygame.K_RETURN]: # stop running the menu when enter is pressed
running = False
continue
# run the menu
self.screen.blit(bg, (0, 0, self.screen_width, self.screen_height))
self.screen.blit(text_surface, text_surface_rect) # title
self.screen.blit(text_start, text_start_rect) # start game text
self.screen.blit(text_copyright, text_copyright_rect) # copyright text
pygame.display.flip()
return
# part where the keys light up
class Legend:
screen = None # screen properties
screen_width = None
screen_height= None
font = ''
def __init__(self, screen, screen_width, screen_height, font='AmazDooMLeftOutline.ttf'): # takes data from main
'''Initializes the screen variables for the Legend object and loads all the arrow icon pngs into variables.'''
self.screen_width = screen_width
self.screen_height = screen_height
self.font = font
self.screen = screen
# image size: 745 by 648, 220 x 263 for arrow and 450x350 for box
self.offup_image = pygame.image.load('assets/up_arrow_off.png') # sets up default up arrow key
# self.offdown_image = pygame.image.load('assets/down_arrow_off.png') # sets up down arrow key - removed
self.offleft_image = pygame.image.load('assets/left_arrow_off.png') # sets up default left arrow key
self.offright_image = pygame.image.load('assets/right_arrow_off.png') # sets up default right arrow key
self.offshift_image = pygame.image.load('assets/shift_key_off.png') # sets up default shift key
self.onup_image = pygame.image.load('assets/up_arrow_on.png') # sets up pressed up arrow key
# self.ondown_image = pygame.image.load('assets/down_arrow_on.png') # sets down pressed up arrow key - removed
self.onleft_image = pygame.image.load('assets/left_arrow_on.png') # sets up pressed left arrow key
self.onright_image = pygame.image.load('assets/right_arrow_on.png') # sets up pressed right arrow key
self.onshift_image = pygame.image.load('assets/shift_key_on.png') # sets up pressed shift key
return
def offUp(self, image): # if the up key is not being pressed then load that png
'''Displays an unpressed up arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/up_arrow_off.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 100, self.screen_height - 105))
return
# def offDown(self, image): # if the down key is not being pressed then load that png - removed
# '''Displays an unpressed down arrow icon.'''
# # creates the icon
# # image = pygame.image.load('assets/down_arrow_off.png')
# # image = pygame.transform.scale(image, (50,50))
# self.screen.blit(image, (self.screen_width - 97, self.screen_height - 70))
# return
def offLeft(self, image): # if the left key is not being pressed then load that png
'''Displays an unpressed left arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/left_arrow_off.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 130, self.screen_height - 66))
return
def offRight(self, image): # if the right key is not being pressed then load that png
'''Displays an unpressed right arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/right_arrow_off.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 57, self.screen_height - 70))
return
def offShift(self, image): # if the shift key is not being pressed then load that png
'''Displays an unpressed shift key icon.'''
# creates the icon
# image = pygame.image.load('assets/right_arrow_off.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 250, self.screen_height - 64))
return
def onUp(self, image): # if the up key is being pressed then load that png
'''Displays a pressed up arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/up_arrow_on.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 100, self.screen_height - 105))
return
# pygame.display.flip()
# def onDown(self, image): # if the down key is being pressed then load that png - removed
# '''Displays a pressed down arrow icon.'''
# # creates the icon
# # image = pygame.image.load('assets/down_arrow_on.png')
# # image = pygame.transform.scale(image, (50,50))
# self.screen.blit(image, (self.screen_width - 97, self.screen_height - 70))
# return
def onLeft(self, image): # if the left key is being pressed then load that png
'''Displays a pressed left arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/left_arrow_on.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 130, self.screen_height - 66))
return
def onRight(self, image): # if the right key is being pressed then load that png
'''Displays a pressed right arrow icon.'''
# creates the icon
# image = pygame.image.load('assets/right_arrow_on.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 57, self.screen_height - 70))
return
def onShift(self, image): # if the shift key is not being pressed then load that png
'''Displays a pressed shift key icon.'''
# creates the icon
# image = pygame.image.load('assets/right_arrow_off.png')
# image = pygame.transform.scale(image, (50,50))
self.screen.blit(image, (self.screen_width - 250, self.screen_height - 64))
return
def showLegend(self, screen): # shows the legend by calling the respective functions
'''Displays the off-state icons for all four buttons (Up, Left, Right, LShift).'''
self.offUp(self.offup_image)
# self.offDown(self.offdown_image)
self.offLeft(self.offleft_image)
self.offRight(self.offright_image)
self.offShift(self.offshift_image)
return
def keyLightUp(self, button): # if a key is pressed relating to something on the legend, then light it up
'''This function uses the keyboard input to render all the on-state icons for each button the player can press.'''
if button[pygame.K_UP]: # up key
self.onUp(self.onup_image)
else:
self.offUp(self.offup_image)
# if button[pygame.K_DOWN]: # down key - removed
# self.onDown(self.ondown_image)
# else:
# self.offDown(self.offdown_image)
if button[pygame.K_LEFT]: # left key
self.onLeft(self.onleft_image)
else:
self.offLeft(self.offleft_image)
if button[pygame.K_RIGHT]: # right key
self.onRight(self.onright_image)
else:
self.offRight(self.offright_image)
if button[pygame.K_LSHIFT]: # shift key
self.onShift(self.onshift_image)
else:
self.offShift(self.offshift_image)
return