This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameplay.py
More file actions
215 lines (192 loc) · 10.5 KB
/
Gameplay.py
File metadata and controls
215 lines (192 loc) · 10.5 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
from PlayerShip import *
from HighScoreManager import *
from UserInputManager import *
from Obstacle import *
from Animations import *
from Globals import *
# This class creates the game play for the actual game.
class Gameplay():
def __init__(self, player_1_name, player_2_name, folder_name, difficulty_easy, music_on, sfx_on, cheat_code):
pygame.display.set_caption(application_name)
self.score_multiplier = 1
self.player1Name = player_1_name
self.player2Name = player_2_name
self.visual_screen = (background_rect.width, background_rect.height)
self.moving_background = Object((self.visual_screen[0] / 2, self.visual_screen[1] / 2),
background_image, 0.5, (0, 0))
self.moving_background_2 = Object((self.visual_screen[0] * 3 / 2, self.visual_screen[1] / 2),
background_image, 0.5, (0, 0))
player_position = (self.visual_screen[0] * 0.25, self.visual_screen[1] / 2)
self.playerShip = PlayerShip(player_position, folder_name)
self.difficulty_easy_select = difficulty_easy
self.music_on = True if music_on == sound_on_string else False
self.sfx_on = True if sfx_on == sound_on_string else False
self.explosion = 0
self.score = 0
self.user_manager = UserInputManager()
self.collision_ended = True
self.correct_sound = pygame.mixer.Sound(correct_press_sound)
self.incorrect_sound = pygame.mixer.Sound(incorrect_press_sound)
self.crash_sound = pygame.mixer.Sound(crash_sound)
self.speed = standard_velocity
self.rock_damage_multiplier = 1.0
self.set_cheats(cheat_code)
if difficulty_easy == 1:
self.speed = standard_easy_velocity
self.moving_background.velocity = self.moving_background_2.velocity = (standard_velocity, 0)
self.obstacles = Obstacle(WIDTH, rock_image, rock_damage * self.rock_damage_multiplier,
self.visual_screen)
self.obstacles.set_velocity((self.speed, 0))
self.background_music = pygame.mixer.music
self.timer = pygame.time.get_ticks()
def set_cheats(self, cheat_code):
if cheat_code == score_multiplier_code:
self.score_multiplier = score_cheat_multiplier
elif cheat_code == rock_multiplier_code:
self.rock_damage_multiplier = rock_damage_cheat_multiplier
elif cheat_code == initial_score_code:
self.score = initial_cheat_score
elif cheat_code == initial_fuel_code:
self.playerShip.fuel = initial_cheat_fuel
elif cheat_code == all_code:
self.score_multiplier = score_cheat_multiplier
self.rock_damage_multiplier = rock_damage_cheat_multiplier
self.score = initial_cheat_score
self.playerShip.fuel = initial_cheat_fuel
def run_game(self):
running = True
self.background_music = pygame.mixer.music
self.background_music.load(background_sound)
if self.music_on:
self.background_music.play(-1, 0.0)
self.user_manager.populate_random_panel_instructions(4, 0) # Zero is default mean
self.user_manager.set_player_instructions()
while running:
self.timer = pygame.time.get_ticks()
self.update()
if self.playerShip.health <= 0:
running = False
self.game_over()
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
self.playerShip.health = 0
if event.type == KEYDOWN:
score_value = self.user_manager.check_inputs(event.key)
if score_value == -1:
if self.sfx_on:
self.incorrect_sound.play()
self.playerShip.damage(bad_instruction_damage)
elif score_value:
self.instructions_completed(score_value)
if self.speed > max_velocity:
self.speed += acceleration
self.obstacles.set_velocity((self.speed, 0))
def game_over(self):
self.background_music.stop()
high_score_manager = HighScoreManager(high_score_file)
high_score_manager.add_high_score((self.player1Name, self.player2Name, self.score))
high_score_manager.draw(self.player1Name, self.player2Name, self.difficulty_easy_select,
self.playerShip.folder_name)
def instructions_completed(self, add_score):
if self.sfx_on:
self.correct_sound.play()
self.playerShip.fuel += fuel_amount
self.score += add_score * self.score_multiplier
self.user_manager.instructions = []
self.user_manager.populate_random_panel_instructions(4, self.score)
self.user_manager.set_player_instructions()
def update(self):
screen.fill(BLACK)
screen.blit(self.moving_background.image, self.moving_background.rect)
screen.blit(self.moving_background_2.image, self.moving_background_2.rect)
self.obstacles.draw(screen)
self.draw_instruction_panel()
screen.blit(self.playerShip.image, self.playerShip.rect)
if self.moving_background.rect.right <= 0:
self.moving_background.rect.left = self.moving_background_2.rect.right - back_overlap
if self.moving_background_2.rect.right <= 0:
self.moving_background_2.rect.left = self.moving_background.rect.right - back_overlap
self.obstacles.move(WIDTH)
self.moving_background_2.move()
self.moving_background.move()
pygame.draw.line(screen, WHITE, (WIDTH / 2, self.visual_screen[1] + 1), (WIDTH / 2, HEIGHT), 5)
self.draw_score_and_health()
if self.playerShip.fuel > 0:
keys = pygame.key.get_pressed()
self.playerShip.input(keys)
self.playerShip.move(self.speed, self.visual_screen[1])
damage_and_point = self.obstacles.check_collision(self.playerShip)
if damage_and_point[0]:
if self.collision_ended:
point = damage_and_point[1][0] - size_of_explosion + 28, damage_and_point[1][1] - size_of_explosion
# 28 is the only hard coded variable we have x_x, but we have no choice.
self.explosion = Animations((size_of_explosion, size_of_explosion), explosion_image,
point)
self.playerShip.damage(damage_and_point[0])
if self.sfx_on:
self.crash_sound.play()
self.collision_ended = False
if self.playerShip.fuel >= collision_fuel_punishment:
self.playerShip.fuel -= collision_fuel_punishment
else:
self.playerShip.fuel = 0
if self.explosion != 0:
self.explosion.update_animation(self.timer)
else:
self.collision_ended = True
pygame.display.update()
def draw_instruction_panel(self):
for display_instruction in self.user_manager.current_instructions:
display_instruction.draw(self.visual_screen[1])
offset2 = 80 + self.visual_screen[1]
control_label = pygame.font.Font(font_file, 20).render('Controls', True, YELLOW)
control_label2 = pygame.font.Font(font_file, 20).render('Controls', True, YELLOW)
control_label_rect = control_label.get_rect()
control_label_rect2 = control_label2.get_rect()
control_label_rect.centery = offset2
control_label_rect2.centery = offset2
control_label_rect.centerx = WIDTH * 1 / 4
control_label_rect2.centerx = WIDTH * 3 / 4
screen.blit(control_label, control_label_rect)
screen.blit(control_label2, control_label_rect2)
offset = 112 + self.visual_screen[1]
for (i, instruction) in enumerate(self.user_manager.instructions):
instruction_label = pygame.font.Font(font_file, 15).render('{0}'.format(instruction.get_message()),
True, (102, 178, 255))
instruction_label_rect = instruction_label.get_rect()
instruction_label_rect.centery = offset + i * 30
if i >= len(self.user_manager.instructions) / 2:
instruction_label_rect.centery = offset + (i - (len(self.user_manager.instructions) / 2)) * 30
if instruction.player_number == 0:
instruction_label_rect.centerx = WIDTH * 1 / 4
elif instruction.player_number == 1:
instruction_label_rect.centerx = WIDTH * 3 / 4
else:
assert False
screen.blit(instruction_label, instruction_label_rect)
def draw_score_and_health(self):
player_1_label = pygame.font.Font(font_file, 18).render('{0}'.format(self.player1Name),
True, GREEN)
player_1_label_rect = player_1_label.get_rect()
player_1_label_rect.centerx = WIDTH / 4
player_1_label_rect.top = self.visual_screen[1] + 5
player_2_label = pygame.font.Font(font_file, 18).render('{0}'.format(self.player2Name),
True, GREEN)
player_2_label_rect = player_2_label.get_rect()
player_2_label_rect.centerx = 3 * WIDTH / 4
player_2_label_rect.top = self.visual_screen[1] + 5
screen.blit(player_1_label, player_1_label_rect)
screen.blit(player_2_label, player_2_label_rect)
score_label = pygame.font.Font(font_file, 15).render('{0}'.format("SCORE: " + str(self.score)) + " "
"FUEL: "
+ str(self.playerShip.fuel), True, WHITE)
score_label_rect = score_label.get_rect()
score_label_rect.centerx = WIDTH / 4
score_label_rect.top = HEIGHT - 20
screen.blit(score_label, score_label_rect)
red_bar = pygame.image.load(healthbar_image)
green_bar = pygame.image.load(health_image)
health_value = self.playerShip.health
screen.blit(red_bar, (WIDTH * 5 / 8, HEIGHT - 20))
for thisHealth in range(int(health_value)):
screen.blit(green_bar, (thisHealth + WIDTH * 5 / 8, HEIGHT - 17))