def __init__(self, game): self.game = game self.game.set_screen(self) self.game_object_group = GameObjectGroup() self.game.drawer.clear() self.game.drawer.add_image( "resources/graphics/minigame_1/background.png") debug = self.game.DEBUG self.mouse_position = None self.player = Player(self.game_object_group, self.game.py_screen, debug) self.ai = AI(self.game_object_group, self.game.py_screen, debug) self.first = True self.passed_time = 0 self.player_health_bar = HealthBar(self.game.py_screen, 10, 10, True) self.ai_health_bar = HealthBar(self.game.py_screen, 860, 10, False) self.winner_label = Label( self.game.py_screen, "", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") self.fight_paused_label = Label( self.game.py_screen, "Bar Fight", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") self.fight_paused = True self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = [ "Game Objective", "Use your fists to fight against", "your opponent and win", "by beating him to 0 hp!" ] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = [ "Controls", "A and D", "Move your fighter to the left and right", "J", "Punch", "K", "Block", "ESC", "Show pause screen" ] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.controls_label_text_4 = TextLabel(self.game.py_screen, self.controls_text[4]) self.controls_label_text_5 = TextLabel(self.game.py_screen, self.controls_text[5]) self.controls_label_text_6 = TextLabel(self.game.py_screen, self.controls_text[6]) self.controls_label_text_7 = TextLabel(self.game.py_screen, self.controls_text[7]) self.controls_label_text_8 = TextLabel(self.game.py_screen, self.controls_text[8]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) self.ai_event = AIEvent(self.ai, self.player) py.time.set_timer(self.ai_event.type, 600)
def __init__(self, game): self.game = game self.game.set_screen(self) self.background_image = py.image.load("resources/graphics/minigame_4/background3.jpg") self.player_image = py.image.load("resources/graphics/minigame_4/cowboyhorse.png").convert_alpha() self.background_image_rect = self.background_image.get_rect() self.player_x = 640 self.player_y = 360 self.player_speed = 10 self.player_rect = py.Rect([self.player_x, self.player_y], [100, 100]) self.starting_timer = None self.timer_label = HeaderLabel(self.game.py_screen, "", [255, 255, 255], 50) self.game_duration = 120 self.time = self.game_duration # Dit zijn eigen events die we aanroepen via de timer class en handelen in de on_events methode self.spawn_cactus_event = py.USEREVENT + 1 self.move_cactus_event = py.USEREVENT + 2 # Laad de cactus image in die we later aan elke cactus class meegeven self.cactus_images = [] self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus.png")) self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus3.png")) self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus4.png")) # Maak een array aan waar we elke cactus_rij instoppen self.cactus_rows = [] self.cactus_speed = 20 self.total_cactus_spawned = 0 finish_x = self.game.py_screen.get_width() finish_y = 0 finish_width = 100 finish_height = self.game.py_screen.get_height() self.finish_line_rect = py.Rect([finish_x, finish_y], [finish_width, finish_height]) self.spawn_finish_line = False self.mouse_position = None self.StartGame = False self.finished_time = -1 self.won = None # Hiermee roepen we de spawn cactus event aan elke 2000ms en de move event elke 20 ms # https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer py.time.set_timer(self.spawn_cactus_event, 2000) py.time.set_timer(self.move_cactus_event, 20) self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.finished_label = HeaderLabel(self.game.py_screen, "") self.objective_text = ["Game Objective", "Take your horse and race", "Try not to touch the cuctus", "You will lose if your finish time is 0!"] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = ["Controls", "A and D", "Move to the left and right", "W and S ", "Move up and down"] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.controls_label_text_4 = TextLabel(self.game.py_screen, self.controls_text[4]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) self.paused_label = Label(self.game.py_screen, "", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2)
class Fight(ScreenBase): def __init__(self, game): self.game = game self.game.set_screen(self) self.game_object_group = GameObjectGroup() self.game.drawer.clear() self.game.drawer.add_image( "resources/graphics/minigame_1/background.png") debug = self.game.DEBUG self.mouse_position = None self.player = Player(self.game_object_group, self.game.py_screen, debug) self.ai = AI(self.game_object_group, self.game.py_screen, debug) self.first = True self.passed_time = 0 self.player_health_bar = HealthBar(self.game.py_screen, 10, 10, True) self.ai_health_bar = HealthBar(self.game.py_screen, 860, 10, False) self.winner_label = Label( self.game.py_screen, "", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") self.fight_paused_label = Label( self.game.py_screen, "Bar Fight", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") self.fight_paused = True self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = [ "Game Objective", "Use your fists to fight against", "your opponent and win", "by beating him to 0 hp!" ] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = [ "Controls", "A and D", "Move your fighter to the left and right", "J", "Punch", "K", "Block", "ESC", "Show pause screen" ] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.controls_label_text_4 = TextLabel(self.game.py_screen, self.controls_text[4]) self.controls_label_text_5 = TextLabel(self.game.py_screen, self.controls_text[5]) self.controls_label_text_6 = TextLabel(self.game.py_screen, self.controls_text[6]) self.controls_label_text_7 = TextLabel(self.game.py_screen, self.controls_text[7]) self.controls_label_text_8 = TextLabel(self.game.py_screen, self.controls_text[8]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) self.ai_event = AIEvent(self.ai, self.player) py.time.set_timer(self.ai_event.type, 600) def on_events(self, events): for event in events: if event.type == py.KEYDOWN: if not self.player.punch_animation_running: if event.key == py.K_j: self.player.punching = True if event.key == py.K_ESCAPE: self.handle_escape() elif event.type == py.MOUSEBUTTONDOWN: if self.btn[0].is_clicked(self.mouse_position): self.fight_paused = False elif self.btn[1].is_clicked(self.mouse_position): from src.global_screens.pick_minigame import PickMinigame self.game.drawer.clear() PickMinigame(self.game) elif event.type == self.ai_event.type: self.ai_event.execute() elif event.type == py.USEREVENT + 2: self.player.state = PlayerState.IDLE self.ai.state = AIState.IDLE event.instance.execute(self) return def handle_escape(self): if self.winner_label.text != "": from src.global_screens.pick_minigame import PickMinigame self.game.drawer.clear() PickMinigame(self.game) else: self.player.state = PlayerState.IDLE self.ai.state = AIState.IDLE self.fight_paused = not self.fight_paused def on_render(self): self.player.needs_new_rendering() self.ai.needs_new_rendering() if not self.fight_paused: self.game.drawer.draw_canvas() self.player.on_render() self.ai.on_render() if self.player.is_hitting_punch(self.ai): self.ai.on_hit(self.player) if self.ai.is_hitting_punch(self.player): self.player.on_hit(self.ai) self.ai_health_bar.on_render(self.ai) self.player_health_bar.on_render(self.player) elif self.fight_paused: # Draw the main stuff on the screen self.game.drawer.draw_canvas() self.ai_health_bar.on_render(self.ai) self.player_health_bar.on_render(self.player) # Draw the paused animation of the player and ai self.player.paused() self.ai.paused() # Draw an dark surface on the screen self.game.py_screen.blit(self.dark_surface, [0, 0]) # Draw objective self.objective_label_header.render(465, 190) self.objective_label_text_1.render(465, 240) self.objective_label_text_2.render(465, 260) self.objective_label_text_3.render(465, 280) # Draw controlls self.controls_label_header.render(465, 320) self.controls_label_text_1.render(465, 370) self.controls_label_text_2.render(585, 370) self.controls_label_text_3.render(465, 390) self.controls_label_text_4.render(585, 390) self.controls_label_text_5.render(465, 410) self.controls_label_text_6.render(585, 410) self.controls_label_text_7.render(465, 430) self.controls_label_text_8.render(585, 430) self.btn[1].render(self.mouse_position, self.button_x, 640) if self.player.won_fight is not None: self.winner_label.render(430, 70) else: self.btn[0].render(self.mouse_position, self.button_x, 560) self.fight_paused_label.render(450, 70) py.display.update() def on_update(self): if self.player.won_fight is not None: self.fight_paused = True else: self.passed_time += self.game.clock.get_time() if self.passed_time > 500: # self.ai.on_update(self.player.state == PlayerState.BLOCKING) self.passed_time = 0 def handle_mouse_position(self, mouse_position): self.mouse_position = mouse_position def handle_mouse_input(self, event): return def handle_key_input(self, keys): if not self.fight_paused: self.player.blocking = False if keys[py.K_k] and not self.player.punch_animation_running: self.player.set_state(PlayerState.BLOCKING) elif keys[py.K_d]: self.player.set_state(PlayerState.WALKING) elif keys[py.K_a]: self.player.set_state(PlayerState.WALKING_REVERSE) else: self.player.set_state(PlayerState.IDLE) return
class Minigame_4(ScreenBase): def __init__(self, game): self.game = game self.game.set_screen(self) self.background_image = py.image.load("resources/graphics/minigame_4/background3.jpg") self.player_image = py.image.load("resources/graphics/minigame_4/cowboyhorse.png").convert_alpha() self.background_image_rect = self.background_image.get_rect() self.player_x = 640 self.player_y = 360 self.player_speed = 10 self.player_rect = py.Rect([self.player_x, self.player_y], [100, 100]) self.starting_timer = None self.timer_label = HeaderLabel(self.game.py_screen, "", [255, 255, 255], 50) self.game_duration = 120 self.time = self.game_duration # Dit zijn eigen events die we aanroepen via de timer class en handelen in de on_events methode self.spawn_cactus_event = py.USEREVENT + 1 self.move_cactus_event = py.USEREVENT + 2 # Laad de cactus image in die we later aan elke cactus class meegeven self.cactus_images = [] self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus.png")) self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus3.png")) self.cactus_images.append(py.image.load("resources/graphics/minigame_4/cactus4.png")) # Maak een array aan waar we elke cactus_rij instoppen self.cactus_rows = [] self.cactus_speed = 20 self.total_cactus_spawned = 0 finish_x = self.game.py_screen.get_width() finish_y = 0 finish_width = 100 finish_height = self.game.py_screen.get_height() self.finish_line_rect = py.Rect([finish_x, finish_y], [finish_width, finish_height]) self.spawn_finish_line = False self.mouse_position = None self.StartGame = False self.finished_time = -1 self.won = None # Hiermee roepen we de spawn cactus event aan elke 2000ms en de move event elke 20 ms # https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer py.time.set_timer(self.spawn_cactus_event, 2000) py.time.set_timer(self.move_cactus_event, 20) self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.finished_label = HeaderLabel(self.game.py_screen, "") self.objective_text = ["Game Objective", "Take your horse and race", "Try not to touch the cuctus", "You will lose if your finish time is 0!"] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = ["Controls", "A and D", "Move to the left and right", "W and S ", "Move up and down"] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.controls_label_text_4 = TextLabel(self.game.py_screen, self.controls_text[4]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) self.paused_label = Label(self.game.py_screen, "", [249, 239, 196], 100, font="resources/fonts/Carnevalee Freakshow.ttf") screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) def handle_key_input(self, keys): if keys[py.K_w] and keys[py.K_s]: pass elif keys[py.K_w]: self.player_rect.move_ip(0, -self.player_speed) elif keys[py.K_s]: self.player_rect.move_ip(0, self.player_speed) if keys[py.K_a] and keys[py.K_d]: pass elif keys[py.K_a]: self.player_rect.move_ip(-self.player_speed, 0) elif keys[py.K_d]: self.player_rect.move_ip(self.player_speed, 0) def handle_mouse_input(self, event): pass def handle_mouse_position(self, mouse_position): self.mouse_position = mouse_position def on_events(self, events): for event in events: # Als er een nieuwe cactuse spawn event wordt aangeroepen maak dan een nieuwe cactus row aan en zet die in de bestaande rijën if event.type == self.spawn_cactus_event and self.StartGame: if self.total_cactus_spawned == -1: pass elif self.total_cactus_spawned >= 30: self.total_cactus_spawned = -1 self.spawn_finish_line = True else: self.total_cactus_spawned += 1 new_cactus_row = CactusRow(self.game.py_screen, self.cactus_images) new_cactus_row.create_row() self.cactus_rows.append(new_cactus_row) elif event.type == self.move_cactus_event and self.StartGame: for cactus_row in self.cactus_rows: cactus_row.move_row_left() if self.spawn_finish_line: self.finish_line_rect.move_ip(-1, 0) if event.type == py.MOUSEBUTTONDOWN: if self.btn[0].is_clicked(self.mouse_position): self.StartGame = True self.starting_timer = time.time() elif self.btn[1].is_clicked(self.mouse_position): from src.global_screens.pick_minigame import PickMinigame self.game.drawer.clear() PickMinigame(self.game) def on_update(self): if self.StartGame: # TODO check of de player een cactus raakt. Zo ja geef een penalty en verwijder de cactus van het scherm. for cactus_row in self.cactus_rows: # Als de cactus row uit het scherm zit remove hem dan uit de current rows if cactus_row.rect.right < 0: self.cactus_rows.remove(cactus_row) for cactus in cactus_row.items: if self.player_rect.colliderect(cactus.rect): cactus_row.items.remove(cactus) self.time -= 10 if self.spawn_finish_line: if self.player_rect.colliderect(self.finish_line_rect): self.won = True self.StartGame = False if self.finished_time == -1: self.finished_time = self.time_left # Kijk of de player rect collied met de finish line rect # TODO rework timer if self.finished_time != -1: self.timer_label.text = "Finished with: " + str(self.finished_time) else: # timer if self.StartGame: self.check_time = time.time() self.time_left = self.time - int(self.check_time - self.starting_timer) self.time_left -= 1 if self.time_left <= 0: self.won = False self.StartGame = False #time_to_seconds = self.time // 50 self.timer_label.text = "Time left: " + str(self.time_left) + " sec." def on_render(self): self.game.drawer.draw_canvas() self.game.py_screen.blit(self.background_image, self.background_image_rect) if self.StartGame: # Render elke cactus_rows die we hebben for cactus_row in self.cactus_rows: cactus_row.render() if self.spawn_finish_line: py.draw.rect(self.game.py_screen, [127, 255, 212], self.finish_line_rect) # self.game.py_screen.blit(self.player_image, self.player_rect) self.game.py_screen.blit(self.player_image, self.player_rect) self.player_rect.clamp_ip(self.game.py_screen.get_rect()) self.timer_label.render(500, 100) else: # Draw an dark surface on the screen self.game.py_screen.blit(self.dark_surface, [0, 0]) if self.won is not None: if self.won: self.finished_label.text = "You won with the time: " + str(self.finished_time) else: self.finished_label.text = "You lost" self.finished_label.render(465, 100) self.paused_label.render(400, 70) # Draw objective self.objective_label_header.render(465, 190) self.objective_label_text_1.render(465, 240) self.objective_label_text_2.render(465, 260) self.objective_label_text_3.render(465, 280) # Draw controlls self.controls_label_header.render(465, 320) self.controls_label_text_1.render(465, 370) self.controls_label_text_2.render(585, 370) self.controls_label_text_3.render(465, 390) self.controls_label_text_4.render(585, 390) self.btn[1].render(self.mouse_position, self.button_x, 640) self.btn[0].render(self.mouse_position, self.button_x, 560) py.display.update()
def __init__(self, game): self.game = game self.game.set_screen(self) self.enemy_is_spawned = True self.enemy_is_moving = True self.player_is_collided = False self.coyote_is_spawned = True self.coyote_is_moving = True self.start_game = False self.end_game = False self.win_game = False self.groundcolor = (240, 230, 140) self.skycolor = (20, 20, 255) self.groundrect = py.Rect([0, 250], [1280, 720]) self.skyrect = py.Rect([0, 0], [1280, 250]) self.beginning_lives = 2 self.lives = self.beginning_lives self.player_start_pos = [640, 650] self.coyote_start_pos = [640, 250] self.player() self.enemy() self.coyote() self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = [ "Game Objective", "Reach the coyote, and capture him to win the game!", "Watch out for the obstacles, though. They take lives from you!", "Once you reach 0 lives, you're game over!" ] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = [ "Controls", "Move left and right with the A and D keys.", "Move faster and slower with the W and S keys.", "Capture the coyote with the space bar!" ] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) self.death_text = [ "Oh no, you died!", "No worries! press 'Start' to try again!", "Or press 'Back' to play another game!" ] self.death_label_header = HeaderLabel(self.game.py_screen, self.death_text[0]) self.death_label_text_1 = TextLabel(self.game.py_screen, self.death_text[1]) self.death_label_text_2 = TextLabel(self.game.py_screen, self.death_text[2]) self.win_text = [ "Congratulations!", "You caught the coyote troubling the townspeople.", "To play again, press 'Start'", "to go back to the menu, press 'back'" ] self.win_label_header = HeaderLabel(self.game.py_screen, self.win_text[0]) self.win_label_text_1 = TextLabel(self.game.py_screen, self.win_text[1]) self.win_label_text_2 = TextLabel(self.game.py_screen, self.win_text[2]) self.win_label_text_3 = TextLabel(self.game.py_screen, self.win_text[3]) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) self.spawn_event = py.USEREVENT + 1 self.time_between_spawn = 3000 py.time.set_timer(self.spawn_event, self.time_between_spawn) self.move_event = py.USEREVENT + 2 self.time_between_movement = 5 py.time.set_timer(self.move_event, self.time_between_spawn) self.collide_event = py.USEREVENT + 3 self.coyote_event = py.USEREVENT + 4 self.coyote_move_event = py.USEREVENT + 5 self.time_between_coyote_movement = 200 py.time.set_timer(self.coyote_move_event, self.time_between_coyote_movement) self.lives_label = Label(self.game.py_screen, "Current Lives: " + str(self.lives), [0, 0, 0], 50)
class Minigame_3(ScreenBase): def __init__(self, game): self.game = game self.game.set_screen(self) self.enemy_is_spawned = True self.enemy_is_moving = True self.player_is_collided = False self.coyote_is_spawned = True self.coyote_is_moving = True self.start_game = False self.end_game = False self.win_game = False self.groundcolor = (240, 230, 140) self.skycolor = (20, 20, 255) self.groundrect = py.Rect([0, 250], [1280, 720]) self.skyrect = py.Rect([0, 0], [1280, 250]) self.beginning_lives = 2 self.lives = self.beginning_lives self.player_start_pos = [640, 650] self.coyote_start_pos = [640, 250] self.player() self.enemy() self.coyote() self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = [ "Game Objective", "Reach the coyote, and capture him to win the game!", "Watch out for the obstacles, though. They take lives from you!", "Once you reach 0 lives, you're game over!" ] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = [ "Controls", "Move left and right with the A and D keys.", "Move faster and slower with the W and S keys.", "Capture the coyote with the space bar!" ] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.controls_label_text_2 = TextLabel(self.game.py_screen, self.controls_text[2]) self.controls_label_text_3 = TextLabel(self.game.py_screen, self.controls_text[3]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start")) self.btn.append(PauseScreenButton(game.py_screen, "Back")) self.death_text = [ "Oh no, you died!", "No worries! press 'Start' to try again!", "Or press 'Back' to play another game!" ] self.death_label_header = HeaderLabel(self.game.py_screen, self.death_text[0]) self.death_label_text_1 = TextLabel(self.game.py_screen, self.death_text[1]) self.death_label_text_2 = TextLabel(self.game.py_screen, self.death_text[2]) self.win_text = [ "Congratulations!", "You caught the coyote troubling the townspeople.", "To play again, press 'Start'", "to go back to the menu, press 'back'" ] self.win_label_header = HeaderLabel(self.game.py_screen, self.win_text[0]) self.win_label_text_1 = TextLabel(self.game.py_screen, self.win_text[1]) self.win_label_text_2 = TextLabel(self.game.py_screen, self.win_text[2]) self.win_label_text_3 = TextLabel(self.game.py_screen, self.win_text[3]) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) self.spawn_event = py.USEREVENT + 1 self.time_between_spawn = 3000 py.time.set_timer(self.spawn_event, self.time_between_spawn) self.move_event = py.USEREVENT + 2 self.time_between_movement = 5 py.time.set_timer(self.move_event, self.time_between_spawn) self.collide_event = py.USEREVENT + 3 self.coyote_event = py.USEREVENT + 4 self.coyote_move_event = py.USEREVENT + 5 self.time_between_coyote_movement = 200 py.time.set_timer(self.coyote_move_event, self.time_between_coyote_movement) self.lives_label = Label(self.game.py_screen, "Current Lives: " + str(self.lives), [0, 0, 0], 50) def start(self): self.end_game = False self.win_game = False self.start_game = True def reset(self): # Reset lives and the label self.lives = self.beginning_lives self.lives_label.text = "Current Lives: " + str(self.lives) # Reset position of player and the coyote self.player_rect.x = self.player_start_pos[0] self.player_rect.y = self.player_start_pos[1] self.coyote_location = self.coyote_start_pos self.coyote_rect.x = self.coyote_start_pos[0] self.coyote_rect.y = self.coyote_start_pos[1] # Start the minigame self.start() def player(self): player_width = 100 player_height = 100 self.player_image = py.image.load( "resources/graphics/minigame_3/cowboy.png") # Resize je foto omdat de photo kleiner is dan de player rect self.player_image = py.transform.scale(self.player_image, [player_width, player_height]) self.player_rect = py.Rect(self.player_start_pos, [player_width, player_height]) self.lives = self.beginning_lives def enemy(self): enemy_width = 100 enemy_height = 100 self.enemy_image = py.image.load( "resources/graphics/minigame_3/cactus.png") # Resize je foto omdat de photo kleiner is dan de enemy rect self.enemy_image = py.transform.scale(self.enemy_image, [enemy_width, enemy_height]) self.enemy_location = Enemy().get_location() self.enemy_rect = py.Rect(self.enemy_location, [enemy_width, enemy_height]) def coyote(self): coyote_width = 100 coyote_height = 100 self.coyote_image = py.image.load( "resources/graphics/minigame_3/coyote.png") self.coyote_image = py.transform.scale(self.coyote_image, [coyote_width, coyote_height]) self.coyote_location = self.coyote_start_pos self.coyote_rect = py.Rect(self.coyote_location, [coyote_width, coyote_height]) def on_events(self, events): for event in events: if self.start_game is True: if event.type == self.spawn_event: self.enemy_is_spawned = True self.selected_location = Enemy().get_location() self.enemy_rect.x = self.selected_location[0] self.enemy_rect.y = self.selected_location[1] self.player_is_collided = False if event.type == self.move_event: self.enemy_is_moving = True self.enemy_rect.move_ip(0, 2) py.time.set_timer(self.move_event, self.time_between_movement) if event.type == self.collide_event: collision = self.player_rect.colliderect(self.enemy_rect) if collision: if self.player_is_collided is False: if self.lives >= 1: self.lives -= 1 self.player_is_collided = True self.lives_label.text = "Current Lives: " + str( self.lives) # End the game when 0 lives are left if self.lives == 0: self.end_game = True self.start_game = False if event.type == self.coyote_event: self.coyote_is_spawned = True self.coyote_selected_location = self.coyote_location if event.type == self.coyote_move_event: self.coyote_is_moving = True self.coyote_rect.move_ip(0, 1) else: if event.type == py.MOUSEBUTTONDOWN: if self.btn[0].is_clicked(self.mouse_position): if self.end_game is False: self.start() else: self.reset() elif self.btn[1].is_clicked(self.mouse_position): from src.global_screens.pick_minigame import PickMinigame self.game.drawer.clear() PickMinigame(self.game) def on_update(self): # Elke frame roepen wij deze event aan en kijken we of er collision is py.event.post(py.event.Event(self.collide_event)) def on_render(self): if self.start_game: self.game.drawer.draw_canvas() py.draw.rect(self.game.py_screen, self.groundcolor, self.groundrect) py.draw.rect(self.game.py_screen, self.skycolor, self.skyrect) self.game.py_screen.blit(self.player_image, self.player_rect) if self.enemy_is_spawned is True: py.draw.rect(self.game.py_screen, [240, 230, 140], self.enemy_rect) self.game.py_screen.blit(self.enemy_image, self.enemy_rect) if self.enemy_is_moving is True: py.draw.rect(self.game.py_screen, [240, 230, 140], self.enemy_rect) self.game.py_screen.blit(self.enemy_image, self.enemy_rect) if self.coyote_is_spawned is True: py.draw.rect(self.game.py_screen, [240, 230, 140], self.coyote_rect) self.game.py_screen.blit(self.coyote_image, self.coyote_rect) self.lives_label.render(100, 100) else: if self.end_game is False: self.game.drawer.draw_canvas() # Draw an dark surface on the screen self.game.py_screen.blit(self.dark_surface, [0, 0]) # Draw objective self.objective_label_header.render(465, 190) self.objective_label_text_1.render(465, 240) self.objective_label_text_2.render(465, 260) self.objective_label_text_3.render(465, 280) # Draw controlls self.controls_label_header.render(465, 320) self.controls_label_text_1.render(465, 370) self.controls_label_text_2.render(465, 420) self.controls_label_text_3.render(465, 470) self.btn[1].render(self.mouse_position, self.button_x, 640) self.btn[0].render(self.mouse_position, self.button_x, 560) elif self.win_game is False: self.game.py_screen.blit(self.dark_surface, [0, 0]) self.death_label_header.render(465, 190) self.death_label_text_1.render(465, 240) self.death_label_text_2.render(465, 260) self.btn[0].render(self.mouse_position, self.button_x, 560) self.btn[1].render(self.mouse_position, self.button_x, 640) elif self.win_game is True: self.game.py_screen.blit(self.dark_surface, [0, 0]) self.win_label_header.render(465, 190) self.win_label_text_1.render(465, 240) self.win_label_text_2.render(465, 260) self.win_label_text_3.render(465, 280) self.btn[0].render(self.mouse_position, self.button_x, 560) self.btn[1].render(self.mouse_position, self.button_x, 640) py.display.update() def handle_mouse_input(self, mouse): return def handle_mouse_position(self, mouse_position): self.mouse_position = mouse_position def handle_key_input(self, keys): if keys[py.K_a]: self.player_rect.move_ip(-10, 0) elif keys[py.K_d]: self.player_rect.move_ip(10, 0) if keys[py.K_w]: self.enemy_rect.move_ip(0, 5) self.coyote_rect.move_ip(0, 1) elif keys[py.K_s]: self.enemy_rect.move_ip(0, -1) self.coyote_rect.move_ip(0, -1) if keys[py.K_SPACE]: coyote_collision = self.player_rect.colliderect(self.coyote_rect) if coyote_collision: self.lives_label.text = "You won!" self.win_game = True self.start_game = False self.end_game = True return
def __init__(self, game): self.game = game self.game.set_screen(self) self.game.drawer.clear() self.game.drawer.add_background_image("resources/graphics/minigame_2/background.jpg") self.points = 0 self.indianImage = py.image.load("resources/graphics/minigame_2/rsz_indian.png") self.cowboyImage = py.image.load("resources/graphics/minigame_2/rsz_mccree.png") self.lives = 1 self.DisplayNewIndian = True; self.passed_time = 0 self.location = Indian.get_location(self) self.rect = self.indianImage.get_rect() #py.Rect(self.location, [100,100]) self.rect.center = self.location self.mouse_position = None self.ShowIndian = True; self.color = [255, 0, 0] self.livesLostLabel = Label(self.game.py_screen, "Ouch!", [255,0,0], 40) self.hitLabel = Label(self.game.py_screen, "Hit!", [0,255,0], 40) self.ShowTimer = 30 self.ShowHitLabel = False self.ShowLivesLostLabel = False self.LivesLabel = Label(self.game.py_screen, "", [0,0,255], 50) self.PointsLabel = Label(self.game.py_screen, "", [0, 0, 255], 50) self.StartGame = False self.PointsLabel.text = "Points: " + str(self.points) self.LivesLabel.text = "Lives: " + str(self.lives) self.EndGame = False self.EndGameLabel = Label(self.game.py_screen, "", [0, 0, 0], 50) self.highscore = 0 self.NewHighscore = False self.ScoreLabel = Label(self.game.py_screen, "", [0,0,0], 50) self.HighscoreLabel = Label(self.game.py_screen, "", [0,0,0], 50) if path.isfile("highscores.txt"): self.highscore = self.get_highscore() else: self.set_highscore() self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = ["Game Objective", "Use your mouse to click and defeat the Indians!", "Hitting an indian grants you 1 point,", "while hitting a cowboy loses you a life."] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = ["Controls", "Move your mouse around and use Left Click" "to hit the targets!"] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start new game")) self.btn.append(PauseScreenButton(game.py_screen, "Back to menu")) self.btn.append(PauseScreenButton(game.py_screen, "Restart")) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2)
class Whack(ScreenBase): def __init__(self, game): self.game = game self.game.set_screen(self) self.game.drawer.clear() self.game.drawer.add_background_image("resources/graphics/minigame_2/background.jpg") self.points = 0 self.indianImage = py.image.load("resources/graphics/minigame_2/rsz_indian.png") self.cowboyImage = py.image.load("resources/graphics/minigame_2/rsz_mccree.png") self.lives = 1 self.DisplayNewIndian = True; self.passed_time = 0 self.location = Indian.get_location(self) self.rect = self.indianImage.get_rect() #py.Rect(self.location, [100,100]) self.rect.center = self.location self.mouse_position = None self.ShowIndian = True; self.color = [255, 0, 0] self.livesLostLabel = Label(self.game.py_screen, "Ouch!", [255,0,0], 40) self.hitLabel = Label(self.game.py_screen, "Hit!", [0,255,0], 40) self.ShowTimer = 30 self.ShowHitLabel = False self.ShowLivesLostLabel = False self.LivesLabel = Label(self.game.py_screen, "", [0,0,255], 50) self.PointsLabel = Label(self.game.py_screen, "", [0, 0, 255], 50) self.StartGame = False self.PointsLabel.text = "Points: " + str(self.points) self.LivesLabel.text = "Lives: " + str(self.lives) self.EndGame = False self.EndGameLabel = Label(self.game.py_screen, "", [0, 0, 0], 50) self.highscore = 0 self.NewHighscore = False self.ScoreLabel = Label(self.game.py_screen, "", [0,0,0], 50) self.HighscoreLabel = Label(self.game.py_screen, "", [0,0,0], 50) if path.isfile("highscores.txt"): self.highscore = self.get_highscore() else: self.set_highscore() self.dark_surface = py.Surface((1280, 720)) self.dark_surface.set_alpha(170) self.objective_text = ["Game Objective", "Use your mouse to click and defeat the Indians!", "Hitting an indian grants you 1 point,", "while hitting a cowboy loses you a life."] self.objective_label_header = HeaderLabel(self.game.py_screen, self.objective_text[0]) self.objective_label_text_1 = TextLabel(self.game.py_screen, self.objective_text[1]) self.objective_label_text_2 = TextLabel(self.game.py_screen, self.objective_text[2]) self.objective_label_text_3 = TextLabel(self.game.py_screen, self.objective_text[3]) self.controls_text = ["Controls", "Move your mouse around and use Left Click" "to hit the targets!"] self.controls_label_header = HeaderLabel(self.game.py_screen, self.controls_text[0]) self.controls_label_text_1 = TextLabel(self.game.py_screen, self.controls_text[1]) self.btn = [] self.btn.append(PauseScreenButton(game.py_screen, "Start new game")) self.btn.append(PauseScreenButton(game.py_screen, "Back to menu")) self.btn.append(PauseScreenButton(game.py_screen, "Restart")) screen_center_width = self.game.py_screen.get_width() / 2 self.button_x = screen_center_width - (self.btn[0].width / 2) def on_events(self, events): for event in events: if event.type == py.MOUSEBUTTONDOWN and event.button == 1: self.mouse_position = py.mouse.get_pos() if self.rect is not None: if self.rect.collidepoint(self.mouse_position): if self.ShowIndian: self.updatePlayerPoints() else: self.updatePlayerLives() if event.type == py.MOUSEBUTTONDOWN: self.game.logger.info(str(self.mouse_position) + " " + str(self.btn.__len__())) if self.btn[0].is_clicked(self.mouse_position): self.__init__(self.game) self.StartGame = True self.EndGame = False elif self.btn[1].is_clicked(self.mouse_position): from src.global_screens.pick_minigame import PickMinigame self.game.drawer.clear() PickMinigame(self.game) return def updatePlayerPoints(self): self.ShowHitLabel = True self.points += 1 self.PointsLabel.text = "Points: " + str(self.points) self.rect = None def updatePlayerLives(self): self.ShowLivesLostLabel = True if self.lives >0: self.lives -= 1 if self.lives == 0: self.EndGameLabel.text = "You are dead!" self.ScoreLabel.text = "Points: " + str(self.points) self.EndGame = True self.game.logger.info("points: " + str(self.points)) self.LivesLabel.text = "Lives: " + str(self.lives) self.rect = None def on_update(self): self.passed_time += self.game.clock.get_time() return def on_render(self): if self.StartGame: self.game.drawer.draw_canvas() if self.EndGame: self.EndGameLabel.render(520, 250) if self.points > self.get_highscore(): self.set_highscore() self.NewHighscore = True if self.NewHighscore: self.HighscoreLabel.text = "You've beaten the previous Highscore! " + "New Highscore: " + str(self.points) self.HighscoreLabel.render(200, 300) else: self.HighscoreLabel.text = "Current Highscore: " + str(self.highscore) self.ScoreLabel.render(520, 300) self.btn[1].render(self.mouse_position, self.button_x, 640) self.btn[0].render(self.mouse_position, self.button_x, 560) py.display.update() return if self.passed_time > self.get_speed_in_miliseconds(): self.passed_time = 0 self.location = Indian.get_location(self) self.change_color() self.setNewTarget(False) self.ShowHitLabel = False self.ShowLivesLostLabel = False elif self.rect is not None: self.setNewTarget(True) if self.ShowHitLabel: self.ShowTimer -= 1 self.hitLabel.render(self.location[0], self.location[1]) if self.ShowLivesLostLabel: self.ShowTimer -= 1 self.livesLostLabel.render(self.location[0], self.location[1]) if self.ShowTimer == 0: self.ShowHitLabel = False self.ShowLivesLostLabel = False self.ShowTimer = 30 self.LivesLabel.render(900, 100) self.PointsLabel.render(900, 200) else: self.game.drawer.draw_canvas() # Draw an dark surface on the screen self.game.py_screen.blit(self.dark_surface, [0, 0]) # Draw objective self.objective_label_header.render(465, 190) self.objective_label_text_1.render(465, 240) self.objective_label_text_2.render(465, 260) self.objective_label_text_3.render(465, 280) # Draw controlls self.controls_label_header.render(465, 320) self.controls_label_text_1.render(465, 370) self.btn[1].render(self.mouse_position, self.button_x, 640) self.btn[0].render(self.mouse_position, self.button_x, 560) py.display.update() def handle_mouse_input(self, mouse): return def handle_key_input(self, keys): if self.EndGame == True: if keys[py.K_ESCAPE]: return #todo if keys[py.K_r]: self.__init__(self.game) #bad practice! but it works return def handle_mouse_position(self, mouse_position): self.mouse_position = mouse_position return def setNewTarget(self, requiredBlit): if self.ShowIndian: if requiredBlit: self.game.py_screen.blit(self.indianImage, self.location) self.rect = self.indianImage.get_rect() else: if requiredBlit: self.game.py_screen.blit(self.cowboyImage, self.location) self.rect = self.cowboyImage.get_rect() self.rect.y = self.location[1] self.rect.x = self.location[0] return def display_Indian(self): if self.DisplayNewIndian is True: self.DisplayNewIndian = False elif self.DisplayNewIndian is False: self.DisplayNewIndian == True return def get_speed_in_miliseconds(self): if self.points > 3: return 1500 if self.points > 10: return 1000 if self.points > 25: return 500 return 3000 def change_color(self): random_number = randint(1, 2) if random_number == 1: self.ShowIndian = False; self.color = [0, 255, 0] else: self.ShowIndian = True; self.color = [255, 0 ,0] return def set_highscore(self): with open("highscores.txt", "w") as file: file.write(str(self.points)) def get_highscore(self): with open("highscores.txt", "r") as file: data = file.read() if data != "": return int(data) else: return 0