def finish_game_window(): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: aux.quit_game() aux.game_finish_text(screen) aux.button(screen, 'QUIT', 630, 450, 100, 50, colors.RED, aux.quit_game) pygame.display.update() clock.tick(15)
def menu_window(): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: aux.quit_game() screen.fill(colors.WHITE) screen.blit(menu, (0, 0)) aux.button(screen, 'START', 550, 55, 240, 100, colors.START, game_loop) pygame.display.update() clock.tick(15)
def game_loop(): elapsed = time.perf_counter() player = Player() out = Exit() screen.fill(colors.WHITE) loop_counter = 0 ships = [] levels = {1: 100, 2: 50, 3: 25, 4: 13, 5: 7, 6: finish_game_window} while True: update(player, out, ships) if loop_counter == levels[level]: ships.append(Ship()) loop_counter = 0 else: ordered_array = mg.merge_sort(get_x_coordinates(ships, player)) collision(ships, ordered_array, player, elapsed) if math.sqrt((player.rect[0] - out.rect[0])**2 + (player.rect[1] - out.rect[1])**2) < 30: restart_game_window(True) for event in pygame.event.get(): if event.type == pygame.QUIT: aux.quit_game() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player.movement[0] = 1 if event.key == pygame.K_DOWN: player.movement[1] = 1 if event.key == pygame.K_LEFT: player.movement[2] = 1 if event.key == pygame.K_RIGHT: player.movement[3] = 1 elif event.type == pygame.KEYUP: if event.key == pygame.K_UP: player.movement[0] = 0 if event.key == pygame.K_DOWN: player.movement[1] = 0 if event.key == pygame.K_LEFT: player.movement[2] = 0 if event.key == pygame.K_RIGHT: player.movement[3] = 0 pygame.display.update() clock.tick(240) loop_counter += 1
def restart_game_window(win): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: aux.quit_game() if win and level == 5: finish_game_window() aux.button(screen, 'RESTART', 510, 450, 100, 50, colors.GREEN, game_loop) if win: aux.game_win_text(screen) aux.button(screen, 'NEXT', 630, 450, 100, 50, colors.BLUE, next_level) else: aux.game_lose_text(screen) aux.button(screen, 'QUIT', 750, 450, 100, 50, colors.RED, aux.quit_game) pygame.display.update() clock.tick(15)
def game_loop(): global level # defines max_load and required quota in each level levels = {1: [50, 60], 2: [48, 65], 3: [45, 70], 4: [43, 75], 5: [40, 80]} level_timer = time.perf_counter() show_timer = 80 elapsed = time.perf_counter() player = Player() out = Exit(levels[level]) shoals = [] wt = [] vl = [] while True: update(player, out, shoals) # time related variables aux.timer_text(screen, show_timer) show_timer -= (time.perf_counter() - level_timer) level_timer = time.perf_counter() if show_timer < 0: restart_game_window(False) if time.perf_counter() - elapsed > random.uniform(1.0, 2.0): shoals.append(Shoal()) elapsed = time.perf_counter() ordered_array = mg.merge_sort(get_x_coordinates(shoals, player)) collision(shoals, ordered_array, player) # calls knapsack and sets required variables if math.sqrt((player.rect[0] - out.rect[0])**2 + (player.rect[1] - out.rect[1])**2) < 30: out.load = 0 out.current_value = 0 for shoal in player.shoals: wt.append(shoal.weight) vl.append(shoal.value) for shoal in out.shoals: wt.append(shoal[0]) vl.append(shoal[1]) out.shoals = knapsack(out.max_load, wt, vl, len(wt)) player.shoals.clear() player.current_load = 0 player.total_value = 0 for shoal in out.shoals: out.load += shoal[0] out.current_value += shoal[1] if out.current_value >= out.quota: next_level() restart_game_window(True) wt.clear() vl.clear() # detects key presses for event in pygame.event.get(): if event.type == pygame.QUIT: aux.quit_game() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: if player.shoals: temp = player.shoals.pop() player.current_load -= temp.weight player.total_value -= temp.value if event.key == pygame.K_UP: player.movement[0] = 2 if event.key == pygame.K_DOWN: player.movement[1] = 2 if event.key == pygame.K_LEFT: player.movement[2] = 2 if event.key == pygame.K_RIGHT: player.movement[3] = 2 elif event.type == pygame.KEYUP: if event.key == pygame.K_UP: player.movement[0] = 0 if event.key == pygame.K_DOWN: player.movement[1] = 0 if event.key == pygame.K_LEFT: player.movement[2] = 0 if event.key == pygame.K_RIGHT: player.movement[3] = 0 pygame.display.update() clock.tick(240)