-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoonpatrolgui.py
executable file
·211 lines (167 loc) · 10.4 KB
/
moonpatrolgui.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
'''@authors Simone Orsi (305461) and Martina Gualtieri (308783)'''
import g2d_pyg as g2d
import datetime, random
from assets import Background
from moonpatrolgame import MoonPatrolGame
import constants
class MoonPatrolGui:
def __init__(self, g: MoonPatrolGame):
self._game = g
self._arena = self._game.arena()
self._arena_width, self._arena_height = self._arena.size()
self._sprite, self._background_image = g2d.load_image(constants.FOREGROUND_IMAGE), g2d.load_image(constants.BACKGROUND_IMAGE)
self._background_music, self._game_over_music = g2d.load_audio(constants.BACKGROUND_MUSIC), g2d.load_audio(constants.GAME_OVER_MUSIC)
self._game_over_sound_played, self._background_sound_played = False, False
self._start_game, self._time, self._elapsed_time = False, True, False
self._start_time, self._end_time = 0, (0, 0, 0)
self._count_player = 0
self._rover = None
self._score, self._level = 0, 1
'''self._game.commands() return ['1', '2', 'Enter', 'Backspace', 'w', 'a', 's', 'd', 'e', 'i', 'j', 'k', 'l', 'o', 'Spacebar']'''
(self._key_1, self._key_2, self._key_start_game, self._key_go_back, self._key_up_1, self._key_left_1, self._key_stay_1, self._key_right_1, self._key_bullet_1,
self._key_up_2, self._key_left_2, self._key_stay_2, self._key_right_2, self._key_bullet_2, self._key_restart) = 'Digit1', 'Digit2', 'Enter', 'Backspace', 'KeyW', 'KeyA', 'KeyS', 'KeyD', 'KeyE', 'KeyI', 'KeyJ', 'KeyK', 'KeyL', 'KeyO', 'Spacebar'
def tick(self):
self.handle_keyboard()
if not self._start_game:
self.rules()
else:
if self._count_player == 1:
self._game.remove_second_rover()
if self._time:
self._start_time = datetime.datetime.now()
self._time = False
if self._score > 0 and self._score % constants.STEP_LEVEL == 0 and self._level <= constants.LEVEL_NUMBERS:
self._level += 1
self._game.set_speed_background()
self._arena.move_all()
g2d.clear_canvas()
actors = sorted(self._arena.actors(), key=lambda obj: obj.priority(), reverse=True)
for a in actors:
if self._game.actor_type(a) == constants.BACKGROUND:
image = self._background_image
else:
image = self._sprite
g2d.draw_image_clip(image, a.symbol(), a.position())
if self._game.actor_type(a) == constants.CANNON and random.randint(0, 40) == 30:
cannon_x, cannon_y, cannon_w, cannon_h = a.position()
if 0 <= cannon_x <= self._arena_width:
self._game.add_bullet((cannon_x - 4, cannon_y + 1), constants.BULLET_LEFT) #-4 and +1 cause the bullets to be generated from the "mouth" of the cannon
if self._game.actor_type(a) == constants.UFO and random.randint(0, 200) == 30:
ufo_x, ufo_y, ufo_w, ufo_h = a.position()
if 0 <= ufo_x <= self._arena_width:
self._game.add_bullet((ufo_x, ufo_y), constants.BULLET_DOWN)
if not self._game.finished():
self._score += 1
else:
if not self._elapsed_time:
self._end_time = self.get_time(datetime.datetime.now() - self._start_time)
self._elapsed_time = True
self.game_over()
self.score()
self.soundtrack()
def soundtrack(self):
if not self._game.finished():
if not self._background_sound_played:
g2d.pause_audio(self._game_over_music)
g2d.play_audio(self._background_music, True)
self._background_sound_played = True
else:
if not self._game_over_sound_played:
g2d.pause_audio(self._background_music)
g2d.play_audio(self._game_over_music, False)
self._game_over_sound_played = True
def handle_keyboard(self):
if not self._start_game:
if self._count_player == 0:
if g2d.key_pressed(self._key_1):
self._count_player = 1
elif g2d.key_pressed(self._key_2):
self._count_player = 2
elif self._count_player > 0:
if g2d.key_pressed(self._key_start_game):
self._start_game = True
if g2d.key_pressed(self._key_go_back):
self._count_player = 0
self.rules()
else:
if g2d.key_pressed(self._key_restart) and self._game.finished():
self.restart_game()
else:
for a in self._arena.actors():
if self._game.actor_type(a) == constants.ROVER:
if a.player() == constants.PLAYER_1:
up, left, stay, right, bullet = self._key_up_1, self._key_left_1, self._key_stay_1, self._key_right_1, self._key_bullet_1
invincible = constants.KEY_INVINCIBLE_ROVER_1
elif a.player() == constants.PLAYER_2:
up, left, stay, right, bullet = self._key_up_2, self._key_left_2, self._key_stay_2, self._key_right_2, self._key_bullet_2
invincible = constants.KEY_INVINCIBLE_ROVER_2
if g2d.key_pressed(up):
a.jump()
elif g2d.key_pressed(right):
a.go_right()
elif g2d.key_pressed(left):
a.go_left()
elif (g2d.key_released(right) or g2d.key_released(left) or
g2d.key_released(left) or g2d.key_pressed(stay) or g2d.key_pressed(stay)):
a.stay()
if g2d.key_pressed(invincible):
a.set_invincible() #Just for testing purposes, I swear I never cheated.. almost never
if g2d.key_pressed(bullet) and self._game.count_bullets() < constants.MAX_BULLETS and not a.damaged():
rover_x, rover_y, rover_w, rover_h = a.position()
if a.get_sprite_type() == constants.DEFAULT_SPRITE:
bullet_up_x, bullet_up_y = rover_x + 8, rover_y - 2 #+8 and -2 cause the bullets to be generated from the cannon of the rover
bullet_right_x, bullet_right_y = rover_x + 34, rover_y + 8 #+34 and +8 cause the bullets to be generated from the cannon of the rover
elif a.get_sprite_type() == constants.JUMPING_SPRITE:
bullet_up_x, bullet_up_y = rover_x, rover_y + 2 #+2 cause the bullets to be generated from the cannon of the rover while jumping
bullet_right_x, bullet_right_y = rover_x + 20, rover_y #+20 cause the bullets to be generated from the cannon of the rover while jumping
self._game.add_bullet((bullet_up_x, bullet_up_y), constants.BULLET_UP)
self._game.add_bullet((bullet_right_x, bullet_right_y), constants.BULLET_RIGHT)
def score(self):
g2d.set_color(constants.WHITE)
g2d.draw_image_clip(self._sprite, (160, 13, 89, 14), (10, 10, 60, 9)) #Score
g2d.draw_text(str(self._score), (75, 9), constants.MEDIUM_FONT_SIZE)
g2d.draw_image_clip(self._sprite, (161, 36, 89, 14), (self._arena_width - 100, 10, 60, 9)) #Level
g2d.draw_text(str(self._level), (self._arena_width - 35, 9), constants.MEDIUM_FONT_SIZE)
def restart_game(self) -> bool:
self._game.restart()
gui_play(self._game)
def game_over(self) -> bool:
g2d.set_color(constants.BLACK)
g2d.fill_rect((0, 95, self._arena_width, 65))
g2d.set_color(constants.WHITE)
g2d.draw_image_clip(self._sprite, (161, 59, 133, 14), (179, 105, 133, 14)) #Game over
self.draw_text_rules(self._game.rules(constants.GAME_OVER)[1:], 0, self._arena_width // 2, 133, constants.BIG_FONT_SIZE)
g2d.draw_text_centered("Elapsed time - " + ":".join(map(str, self._end_time)), (self._arena_width // 2, 150), constants.MEDIUM_FONT_SIZE)
def get_time(self, date):
days, seconds = date.days, date.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
return hours, minutes, seconds
def draw_text_rules(self, text, _type: int, width: int, height: int, font_size: int):
g2d.set_color(constants.WHITE)
if _type == 1:
width *= 3
for s in text:
g2d.draw_text_centered(s, (width, height), font_size)
height += 15
def rules(self):
g2d.clear_canvas()
g2d.set_color(constants.BLACK)
g2d.fill_rect((0, 0, self._arena_width, self._arena_height))
g2d.draw_image_clip(self._sprite, (10, 5, 136, 81), (190, 25, 116, 69)) #Moon patrol logo
width, height = self._arena_width // 2, 120
if self._count_player == 0:
self.draw_text_rules(self._game.rules(constants.SINGLE_PLAYER), 0, width // 2, height + 30, constants.MEDIUM_FONT_SIZE)
self.draw_text_rules(self._game.rules(constants.MULTIPLAYER), 1, width // 2, height + 30, constants.MEDIUM_FONT_SIZE)
else:
if self._count_player == 1:
self.draw_text_rules(self._game.rules(constants.ROVER_1), 0, width, height, constants.MEDIUM_FONT_SIZE)
elif self._count_player == 2:
self.draw_text_rules(self._game.rules(constants.ROVER_1), 0, width // 2, height, constants.MEDIUM_FONT_SIZE)
self.draw_text_rules(self._game.rules(constants.ROVER_2), 1, width // 2, height, constants.MEDIUM_FONT_SIZE)
self.draw_text_rules(self._game.rules(constants.START_GAME), 0, width, 200, constants.MEDIUM_FONT_SIZE)
def gui_play(game: MoonPatrolGame):
g2d.init_canvas(game.arena().size())
ui = MoonPatrolGui(game)
g2d.main_loop(ui.tick)