def options(): """Shows the options menu.""" global mouse_movement_active x_margin = 150 mouse_button_rect = pygame.Rect( window.get_width() - x_margin - button_width, (window.get_height() / 2) - 50, button_width, button_height) keyboard_button_rect = pygame.Rect(x_margin, (window.get_height() / 2) - 50, button_width, button_height) back_button = ml.get_button_surface(button_width, button_height, 'Back', button_font_size, WHITE) back_button_rect = pygame.Rect(x_margin, window.get_height() - 200, button_width, button_height) while True: # Event handling loop for event in pygame.event.get(): if event.type == QUIT: terminate() # Escape closes the highscores screen elif event.type == KEYDOWN: if event.key == K_ESCAPE: return # Player clicked keyboard button elif event.type == MOUSEBUTTONUP: # noinspection PyArgumentList if keyboard_button_rect.collidepoint(event.pos): mouse_movement_active = False elif mouse_button_rect.collidepoint(event.pos): mouse_movement_active = True elif back_button_rect.collidepoint(event.pos): return window.fill(BLACK) # Change button color to indicate which is active if mouse_movement_active: mouse_color = WHITE keyboard_color = GRAY else: keyboard_color = WHITE mouse_color = GRAY mouse_button = ml.get_button_surface(button_width, button_height, 'Mouse', button_font_size, mouse_color) keyboard_button = ml.get_button_surface(button_width, button_height, 'Keyboard', button_font_size, keyboard_color) window.blit(keyboard_button, keyboard_button_rect) window.blit(mouse_button, mouse_button_rect) window.blit(back_button, back_button_rect) draw_cursor() pygame.display.update() clock.tick(FPS)
def highscores(): """Show the highscores screen""" x_margin = 150 top_scores = ml.get_highscores()[:8] pygame.event.clear() window.fill((0, 0, 0)) score_font = pygame.font.Font(ml.font, 28) high_scores_title_font = pygame.font.Font(ml.font, 48) score_name_surfaces = [] score_score_surfaces = [] # Score display for score in top_scores: score_name_surfaces.append( score_font.render('%s:' % score[0], True, WHITE)) for score in top_scores: score_score_surfaces.append( score_font.render(str(score[1]), True, WHITE)) high_scores_title = high_scores_title_font.render('High Scores', True, WHITE) # Back button back_button = ml.get_button_surface(button_width, button_height, 'Back', button_font_size, WHITE) back_button_rect = pygame.Rect(x_margin, window.get_height() - 200, button_width, button_height) while True: # Event handling loop for event in pygame.event.get(): if event.type == QUIT: terminate() # Escape closes the highscores screen elif event.type == KEYDOWN: if event.key == K_ESCAPE: return # Player clicked Back button elif event.type == MOUSEBUTTONUP: # noinspection PyArgumentList if back_button_rect.collidepoint(event.pos): return window.fill(BLACK) # Draw the scores for i, surface in enumerate(score_name_surfaces): window.blit(surface, (x_margin, 125 + (button_height + 15) * i)) for i, surface in enumerate(score_score_surfaces): window.blit(score_score_surfaces[i], (window.get_width() - x_margin - surface.get_width(), 125 + (button_height + 15) * i)) window.blit(back_button, back_button_rect) window.blit(high_scores_title, ((window.get_width() / 2) - (high_scores_title.get_width() / 2), 30)) draw_cursor() pygame.display.update() clock.tick(FPS)
def upgrade_screen(): """Shows the upgrade screen.""" from collections import OrderedDict upgrade_list = list(ml.upgrades.keys()) button_gap = 50 button_x_list = [] x_margin = 150 y_start = 125 u_button_width = 240 u_button_height = 75 name_font_size = 22 score_margin = 10 score_font = pygame.font.Font(ml.font, 36) value_font = pygame.font.Font(ml.font, 28) upgrades_title_font = pygame.font.Font(ml.font, 48) upgrades_title = upgrades_title_font.render('Upgrades', True, WHITE) continue_button = ml.get_button_surface(button_width, button_height, 'Continue', button_font_size, WHITE) continue_button_rect = pygame.Rect( window.get_width() - button_width - x_margin, window.get_height() - 75 - continue_button.get_height(), button_width, button_height) # Creates list with x coordinates alternating between left and right side of window for i in range(len(upgrade_list)): if i % 2 == 0: button_x_list.append(x_margin) else: button_x_list.append(window.get_width() - x_margin - u_button_width) # Example output: [200, 200, 270, 270, 340, 340, 410, 410, 480, 480] button_y_list = [y_start] for i in range(len(upgrade_list) - 1): if i % 2 == 0: button_y_list.append(button_y_list[i]) else: button_y_list.append(button_y_list[i] + button_gap + u_button_height) # Build list of Rects from those x and y coordinates button_rect_list = [] for i, j in enumerate(upgrade_list): button_rect_list.append( pygame.Rect((button_x_list[i], button_y_list[i]), (u_button_width, u_button_height))) # Loop while True: # Clear the window window.fill(BLACK) # Show the current score in bottom-left corner score_surface = score_font.render('Score: %d' % (ml.score), True, WHITE) window.blit(score_surface, (score_margin, window.get_height() - score_margin - score_surface.get_height())) # Build a list of button surfaces with upgrade names and values button_surfaces = OrderedDict() offset = int(-1 * (u_button_height / 4)) for upgrade in upgrade_list: # Create button with upgrade name on it button_surfaces[upgrade] = ml.get_button_surface( u_button_width, u_button_height, upgrade, name_font_size, WHITE, offset) value = str(ml.get_upgrade_values(upgrade)) value_surface = value_font.render(value, True, WHITE) # Add the text for the current value of each upgrade button_surfaces[upgrade].blit( value_surface, ((button_surfaces[upgrade].get_width() / 2) - (value_surface.get_width() / 2), button_surfaces[upgrade].get_height() - value_surface.get_height() - (button_surfaces[upgrade].get_height() / 20))) # Handle mouse-over pos = pygame.mouse.get_pos() for i, rect in enumerate(button_rect_list): if rect.collidepoint(pos): # Draw upgrade amount next to upgrade value upgrade_amount_surface = value_font.render( ('+' if ml.get_upgrade_amount(upgrade_list[i]) >= 0 else '') + str(ml.get_upgrade_amount(upgrade_list[i])), True, GREEN) button_surfaces[upgrade_list[i]].blit( upgrade_amount_surface, (button_surfaces[upgrade_list[i]].get_width() - upgrade_amount_surface.get_width() - 10, button_surfaces[upgrade_list[i]].get_height() - upgrade_amount_surface.get_height() - (button_surfaces[upgrade_list[i]].get_height() / 20))) # Draw upgrade cost next to score cost_surface = score_font.render( '-%d' % ml.get_upgrade_cost(upgrade_list[i]), True, RED) window.blit(cost_surface, (score_surface.get_width() + score_margin + 10, window.get_height() - score_margin - cost_surface.get_height())) # Event handling for event in pygame.event.get(): if event.type == QUIT: terminate() # Escape or space closes the upgrades screen elif event.type == KEYDOWN: if event.key in (K_ESCAPE, K_SPACE): return # Check for click on each button elif event.type == MOUSEBUTTONUP: for i, rect in enumerate(button_rect_list): if rect.collidepoint(event.pos): ml.purchase_upgrade(upgrade_list[i]) break # noinspection PyArgumentList if continue_button_rect.collidepoint(event.pos): return # Draw title window.blit(upgrades_title, ((window.get_width() / 2) - (upgrades_title.get_width() / 2), 30)) # Draw all of the buttons for count, button in enumerate(button_surfaces): window.blit(button_surfaces[button], button_rect_list[count]) window.blit(continue_button, continue_button_rect) draw_cursor() pygame.display.update() clock.tick(FPS)
def game_over(): """Shows the game over screen. Saves player score.""" global player_move_up, player_move_left, player_move_down, player_move_right, \ mouse_movement_active, shift_pressed, game_running logging.debug('Game Over') pygame.time.wait(1000) x_margin = 150 # Reset player movement and event queue player_move_up = False player_move_down = False player_move_right = False player_move_left = False game_running = False shift_pressed = False pygame.event.clear() ml.update_boss_data(1, 0, '') # Clear window window.fill(BLACK) # Add player's score font = pygame.font.Font(ml.font, 30) score_font = pygame.font.Font(ml.font, 50) message_surface = font.render('Your final score:', True, WHITE) score_surface = score_font.render(str(ml.score), True, WHITE) message2_surface = font.render('Would you like to save your score?', True, WHITE) message3_surface = font.render('Score saved!', True, WHITE) no_button = ml.get_button_surface(button_width, button_height, 'No', button_font_size, WHITE) no_button_rect = pygame.Rect(x_margin, window.get_height() - 200, button_width, button_height) yes_button = ml.get_button_surface(button_width, button_height, 'Yes', button_font_size, WHITE) yes_button_rect = pygame.Rect(window.get_width() - button_width - x_margin, window.get_height() - 200, button_width, button_height) while True: # Clear the window window.fill(BLACK) # Event handling for event in pygame.event.get(): if event.type == QUIT: terminate() # Escape returns to main menu elif event.type == KEYDOWN: if event.key == K_ESCAPE: return elif event.type == MOUSEBUTTONUP: # noinspection PyArgumentList # No button returns to main menu if no_button_rect.collidepoint(event.pos): return # Yes button asks player for their name, then adds to high score list elif yes_button_rect.collidepoint(event.pos): player_name = ask_player_input('Please enter your name:') # player_name is '' if player cancels input if player_name: window.blit(message3_surface, ((window.get_width() / 2) - (message3_surface.get_width() / 2), window.get_height() / 2 + 100)) logging.info('Saving score: %s: %d' % (player_name, ml.score)) ml.add_highscore(player_name, ml.score) pygame.display.update() pygame.time.wait(1500) return # Blit text and buttons window.blit(message_surface, ((window.get_width() / 2) - (message_surface.get_width() / 2), (window.get_height() / 2) - 150)) window.blit(score_surface, ((window.get_width() / 2) - (score_surface.get_width() / 2), (window.get_height() / 2) - 85)) window.blit(message2_surface, ((window.get_width() / 2) - (message2_surface.get_width() / 2), (window.get_height() / 2))) window.blit(no_button, no_button_rect) window.blit(yes_button, yes_button_rect) draw_cursor() pygame.display.update() clock.tick(FPS)
def main_menu(): """Shows the main menu.""" global game_running button_x = (window.get_width() / 2) - (button_width / 2) top_button_y = (window.get_height() / 2) + (window.get_height() / 10) button_spacing = 15 # Menu buttons if not game_running: start_game_text = 'Start Game' title_y = 150 title_font = pygame.font.Font(ml.font, 90) title1 = title_font.render('ECE102', True, WHITE) title1_rect = pygame.Rect(ml.window_width / 2 - title1.get_width() / 2, title_y, title1.get_width(), title1.get_height()) title2 = title_font.render('Final Project', True, WHITE) title2_rect = pygame.Rect(ml.window_width / 2 - title2.get_width() / 2, 0, title2.get_width(), title2.get_height()) title2_rect.y = title_y + title1.get_height() + 25 else: start_game_text = 'Continue' start_game_button = ml.get_button_surface(button_width, button_height, start_game_text, button_font_size, WHITE) start_game_button_rect = pygame.Rect(button_x, top_button_y, button_width, button_height) high_scores_button = ml.get_button_surface(button_width, button_height, 'High Scores', button_font_size, WHITE) high_scores_button_rect = pygame.Rect( button_x, top_button_y + button_height + button_spacing, button_width, button_height) options_button = ml.get_button_surface(button_width, button_height, 'Options', button_font_size, WHITE) option_button_rect = pygame.Rect( button_x, top_button_y + 2 * button_height + 2 * button_spacing, button_width, button_height) quit_button = ml.get_button_surface(button_width, button_height, 'Quit Game', button_font_size, WHITE) quit_button_rect = pygame.Rect( button_x, top_button_y + 3 * button_height + 3 * button_spacing, button_width, button_height) # Draw menu, wait for, and deal with choice while True: for event in pygame.event.get(): if event.type == QUIT: terminate() elif event.type == KEYDOWN: if event.key == K_ESCAPE: if game_running: return else: terminate() elif event.key in (K_SPACE, ): return # Check if player clicked a button if event.type == MOUSEBUTTONUP: # Player clicks Start Game # noinspection PyArgumentList if start_game_button_rect.collidepoint(event.pos): return # Player clicks Quit Game elif quit_button_rect.collidepoint(event.pos): terminate() # Player clicks Options elif option_button_rect.collidepoint(event.pos): options() # Player clicks High Scores elif high_scores_button_rect.collidepoint(event.pos): highscores() # Clear the window window.fill(BLACK) # Draw buttons window.blit(start_game_button, start_game_button_rect) window.blit(high_scores_button, high_scores_button_rect) window.blit(options_button, option_button_rect) window.blit(quit_button, quit_button_rect) if not game_running: window.blit(title1, title1_rect) window.blit(title2, title2_rect) draw_cursor() pygame.display.update() clock.tick(FPS)