class HighScores: def __init__(self, fonts, screen_data, characters): self.menu_results = [0, 0] self.portraits = self.load_portrait_table("images/portraits_small.png", 32, 48, False) self.background_image = pygame.image.load( "images/menu_background.png").convert() self.back_button = UTTextButton([437, 465, 150, 35], "Back", fonts, 1) self.title_text_render = fonts[3].render("High Scores", True, pygame.Color("#000000")) self.title_text_render_rect = self.title_text_render.get_rect( centerx=screen_data.screen_size[0] * 0.5, centery=64) self.characters = characters self.characters.sort(key=attrgetter('score'), reverse=True) self.character_score_entries = [] character_index = 0 for character in self.characters: character_button = UIHighScoreEntry( [362, 128 + (character_index * 64), 300, 52], character, fonts, character_index, self.portraits) self.character_score_entries.append(character_button) character_index += 1 def run(self, screen): self.menu_results = [0, 0] for event in pygame.event.get(): self.back_button.handle_input_event(event) # if event.type == QUIT: # running = False screen.blit(self.background_image, (0, 0)) # draw the background screen.blit(self.title_text_render, self.title_text_render_rect) self.back_button.update() if self.back_button.was_pressed(): self.menu_results = [1, 0] self.back_button.draw(screen) for high_score_entry in self.character_score_entries: high_score_entry.draw(screen) return self.menu_results @staticmethod def load_portrait_table(filename, width, height, use_transparency): if use_transparency: image = pygame.image.load(filename).convert_alpha() else: image = pygame.image.load(filename).convert() image_width, image_height = image.get_size() tile_table = [] for tile_x in range(0, int(image_width / width)): line = [] tile_table.append(line) for tile_y in range(0, int(image_height / height)): rect = (tile_x * width, tile_y * height, width, height) line.append(image.subsurface(rect)) return tile_table
class MainMenu: def __init__(self, fonts): self.show_menu = True self.show_editor = False self.is_start_game_selected = True self.is_run_editor_selected = False self.start_game = False self.background_image = pygame.image.load( "images/menu_background.png").convert() self.play_game_button = UTTextButton([437, 465, 150, 35], "Play Game", fonts, 1) self.edit_map_button = UTTextButton([437, 515, 150, 35], "Edit Map", fonts, 1) def run(self, screen, fonts, screen_data): is_main_menu_and_index = [0, 0] for event in pygame.event.get(): self.play_game_button.handle_input_event(event) self.edit_map_button.handle_input_event(event) self.play_game_button.update() self.edit_map_button.update() if self.play_game_button.was_pressed(): self.start_game = True self.show_menu = False if self.edit_map_button.was_pressed(): self.show_editor = True self.show_menu = False screen.blit(self.background_image, (0, 0)) # draw the background main_menu_title_string = "Time Runs" main_menu_title_text_render = fonts[2].render(main_menu_title_string, True, pygame.Color("#FFFFFF")) screen.blit( main_menu_title_text_render, main_menu_title_text_render.get_rect( centerx=screen_data.screen_size[0] * 0.5, centery=128)) self.play_game_button.draw(screen) self.edit_map_button.draw(screen) if self.show_editor: is_main_menu_and_index[0] = 2 elif self.start_game: is_main_menu_and_index[0] = 1 else: is_main_menu_and_index[0] = 0 return is_main_menu_and_index
class MainMenu: def __init__(self, fonts, camera): self.fonts = fonts self.background_image = pygame.Surface( camera.dimensions ) # pygame.image.load("images/menu_background.png").convert() self.background_image.fill(pygame.Color("#F0F0F0")) main_menu_title_string = "Vania" self.main_menu_title_text_render = self.fonts["julee_128"].render( main_menu_title_string, True, pygame.Color("#000000")) self.title_text_position = self.main_menu_title_text_render.get_rect( centerx=camera.dimensions[0] * 0.5, centery=camera.dimensions[1] * 0.2) button_menu_vertical_start = camera.screen_rect.centery + ( 0.2 * camera.dimensions[1]) button_menu_spacing = 64 play_game_button_rect = pygame.Rect((0, 0), (150, 35)) play_game_button_rect.centerx = camera.screen_rect.centerx play_game_button_rect.centery = button_menu_vertical_start self.play_game_button = UTTextButton(play_game_button_rect, "Play Game", fonts, "default_16") edit_map_button_rect = pygame.Rect((0, 0), (150, 35)) edit_map_button_rect.centerx = camera.screen_rect.centerx edit_map_button_rect.centery = button_menu_vertical_start + button_menu_spacing self.edit_map_button = UTTextButton(edit_map_button_rect, "Edit Map", fonts, "default_16") def run(self, screen): is_main_menu_and_index = [0, 0] for event in pygame.event.get(): self.play_game_button.handle_input_event(event) self.edit_map_button.handle_input_event(event) if event.type == pygame.QUIT: is_main_menu_and_index[0] = 3 if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: is_main_menu_and_index[0] = 3 self.play_game_button.update() self.edit_map_button.update() if self.play_game_button.was_pressed(): is_main_menu_and_index[0] = 1 if self.edit_map_button.was_pressed(): is_main_menu_and_index[0] = 2 screen.blit(self.background_image, (0, 0)) # draw the background screen.blit(self.main_menu_title_text_render, self.title_text_position) self.play_game_button.draw(screen) self.edit_map_button.draw(screen) return is_main_menu_and_index
class SellGoodUILine: def __init__(self, good_and_quantity, sell_goods_and_prices, position, fonts, player): self.fonts = fonts self.player = player self.position = position self.good_and_quantity = good_and_quantity self.good_text = good_and_quantity[0].name self.good_value = 0 for good in sell_goods_and_prices: if good[0].name == good_and_quantity[0].name: self.good_value = good[1] self.test_slider = UISlider([self.position[0] + 80, self.position[1], 150, 20], [0, self.good_and_quantity[1]], 0, self.fonts, 0) self.sell_string = "Sell: +" + str(self.good_value * self.test_slider.get_value()) self.sell_button = UTTextButton([self.position[0] + 240, self.position[1], 80, 20], self.sell_string, self.fonts, 0) self.text_colour = pygame.Color("#FFFFFF") self.should_redraw_all_lines = False self.good_text_render = self.fonts[0].render(self.good_text, True, self.text_colour) def redraw(self): self.should_redraw_all_lines = False self.test_slider.set_max(self.good_and_quantity[1]) self.sell_string = "Sell: +" + str(self.good_value * self.test_slider.get_value()) self.sell_button.set_text(self.sell_string) def handle_input_event(self, event): self.test_slider.handle_input_event(event) self.sell_button.handle_input_event(event) def update(self): self.test_slider.update() self.sell_button.update() self.sell_string = "Sell: +" + str(self.good_value * self.test_slider.get_value()) self.sell_button.set_text(self.sell_string) if self.sell_button.was_pressed(): self.player.gold = self.player.gold + (self.good_value * self.test_slider.get_value()) self.should_redraw_all_lines = True self.player.remove_goods([self.good_and_quantity[0], self.test_slider.get_value()]) def draw(self, screen): self.test_slider.draw(screen) self.sell_button.draw(screen) screen.blit(self.good_text_render, self.good_text_render.get_rect(centerx=self.position[0], centery=self.position[1] + 10))
class MainMenu: def __init__(self, fonts): self.background_image = pygame.image.load("images/menu_background.png").convert() self.play_game_button = UTTextButton([402, 415, 220, 35], "Character Select", fonts, 1) self.view_high_scores_button = UTTextButton([427, 465, 170, 35], "High Scores", fonts, 1) self.edit_map_button = UTTextButton([437, 515, 150, 35], "Edit Map", fonts, 1) def run(self, screen, fonts, screen_data): is_main_menu_and_index = [0, 0] for event in pygame.event.get(): self.play_game_button.handle_input_event(event) self.edit_map_button.handle_input_event(event) self.view_high_scores_button.handle_input_event(event) self.play_game_button.update() self.view_high_scores_button.update() self.edit_map_button.update() if self.play_game_button.was_pressed(): is_main_menu_and_index[0] = 1 if self.view_high_scores_button.was_pressed(): is_main_menu_and_index[0] = 3 if self.edit_map_button.was_pressed(): is_main_menu_and_index[0] = 2 screen.blit(self.background_image, (0, 0)) # draw the background main_menu_title_string = "Mild Peril" main_menu_title_text_render = fonts[2].render(main_menu_title_string, True, pygame.Color("#000000")) screen.blit(main_menu_title_text_render, main_menu_title_text_render.get_rect(centerx=screen_data.screen_size[0] * 0.5, centery=128)) self.play_game_button.draw(screen) self.view_high_scores_button.draw(screen) self.edit_map_button.draw(screen) return is_main_menu_and_index
class MapEditorInstructionsWindow: def __init__(self, window_rect, fonts): self.windowRect = window_rect self.fonts = fonts self.backGroundColour = pygame.Color(25, 25, 25) self.textColour = pygame.Color(255, 255, 255) self.windowTitleStr = "Instructions" self.shouldExit = False self.doneButton = UTTextButton([ self.windowRect[0] + (self.windowRect[2] / 2) + 45, self.windowRect[1] + self.windowRect[3] - 30, 70, 20 ], "Done", fonts, "default_16") self.instructionsText1 = "Arrow keys to scroll map" self.instructionsText2 = "Left mouse click to select tile" self.instructionsText3 = "Right mouse click to place tile" self.instructionsText4 = "'>' and '<' to rotate selected tile" self.instructionsText5 = "F5 to save map" self.instructionsText6 = " Challenge 1 " self.instructionsText7 = "-------------" self.instructionsText8 = "Create a new island on the map and save it." self.windowXCentre = self.windowRect[0] + self.windowRect[2] * 0.5 self.title_text_render = None self.instructionsTextRender1 = self.fonts["default_16"].render( self.instructionsText1, True, self.textColour) self.instructionsTextRender2 = self.fonts["default_16"].render( self.instructionsText2, True, self.textColour) self.instructionsTextRender3 = self.fonts["default_16"].render( self.instructionsText3, True, self.textColour) self.instructionsTextRender4 = self.fonts["default_16"].render( self.instructionsText4, True, self.textColour) self.instructionsTextRender5 = self.fonts["default_16"].render( self.instructionsText5, True, self.textColour) self.instructionsTextRender6 = self.fonts["default_16"].render( self.instructionsText6, True, self.textColour) self.instructionsTextRender7 = self.fonts["default_16"].render( self.instructionsText7, True, self.textColour) self.instructionsTextRender8 = self.fonts["default_16"].render( self.instructionsText8, True, self.textColour) def handle_input_event(self, event): self.doneButton.handle_input_event(event) def update(self): self.doneButton.update() if self.doneButton.was_pressed(): self.shouldExit = True def is_inside(self, screen_pos): is_inside = False if self.windowRect[0] <= screen_pos[ 0] <= self.windowRect[0] + self.windowRect[2]: if self.windowRect[1] <= screen_pos[ 1] <= self.windowRect[1] + self.windowRect[3]: is_inside = True return is_inside def draw(self, screen): pygame.draw.rect( screen, self.backGroundColour, pygame.Rect(self.windowRect[0], self.windowRect[1], self.windowRect[2], self.windowRect[3]), 0) self.title_text_render = self.fonts["bod_pstc_32"].render( self.windowTitleStr, True, self.textColour) screen.blit( self.title_text_render, self.title_text_render.get_rect(centerx=self.windowRect[0] + self.windowRect[2] * 0.5, centery=self.windowRect[1] + 24)) screen.blit( self.instructionsTextRender1, self.instructionsTextRender1.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 50)) screen.blit( self.instructionsTextRender2, self.instructionsTextRender2.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 64)) screen.blit( self.instructionsTextRender3, self.instructionsTextRender3.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 78)) screen.blit( self.instructionsTextRender4, self.instructionsTextRender4.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 92)) screen.blit( self.instructionsTextRender5, self.instructionsTextRender5.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 106)) screen.blit( self.instructionsTextRender6, self.instructionsTextRender6.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 134)) screen.blit( self.instructionsTextRender7, self.instructionsTextRender7.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 148)) screen.blit( self.instructionsTextRender8, self.instructionsTextRender8.get_rect(centerx=self.windowXCentre, centery=self.windowRect[1] + 176)) self.doneButton.draw(screen)
class MapEditorInstructionsWindow: def __init__(self, window_rect, fonts): self.window_rect = window_rect self.fonts = fonts self.background_colour = pygame.Color("#222222") self.text_colour = pygame.Color("#FFFFFF") self.window_title_str = "Instructions" self.should_exit = False self.done_button = UTTextButton([self.window_rect[0] + (self.window_rect[2] / 2) + 45, self.window_rect[1] + self.window_rect[3] - 30, 70, 20], "Done", fonts, 0) self.title_text_render = None self.instructions_text_1 = "Arrow keys to scroll map" self.instructions_text_2 = "Left mouse click to select tile from palette" self.instructions_text_3 = "Right mouse click to place tile" self.instructions_text_4 = "'>' and '<' to rotate selected tile" self.instructions_text_5 = "F5 or quit to save map" self.instructions_text_6 = " Challenge 1 " self.instructions_text_7 = "-------------" self.instructions_text_8 = "Add a rocket launcher guard to the map" self.window_x_centre = self.window_rect[0] + self.window_rect[2] * 0.5 self.instructions_text_render_1 = self.fonts[0].render(self.instructions_text_1, True, self.text_colour) self.instructions_text_render_2 = self.fonts[0].render(self.instructions_text_2, True, self.text_colour) self.instructions_text_render_3 = self.fonts[0].render(self.instructions_text_3, True, self.text_colour) self.instructions_text_render_4 = self.fonts[0].render(self.instructions_text_4, True, self.text_colour) self.instructions_text_render_5 = self.fonts[0].render(self.instructions_text_5, True, self.text_colour) self.instructions_text_render_6 = self.fonts[0].render(self.instructions_text_6, True, self.text_colour) self.instructions_text_render_7 = self.fonts[0].render(self.instructions_text_7, True, self.text_colour) self.instructions_text_render_8 = self.fonts[0].render(self.instructions_text_8, True, self.text_colour) def handle_input_event(self, event): self.done_button.handle_input_event(event) def update(self): self.done_button.update() if self.done_button.was_pressed(): self.should_exit = True def is_inside(self, screen_pos): is_inside = False if self.window_rect[0] <= screen_pos[0] <= self.window_rect[0] + self.window_rect[2]: if self.window_rect[1] <= screen_pos[1] <= self.window_rect[1] + self.window_rect[3]: is_inside = True return is_inside def draw(self, screen): # noinspection PyArgumentList pygame.draw.rect(screen, self.background_colour, pygame.Rect(self.window_rect[0], self.window_rect[1], self.window_rect[2], self.window_rect[3]), 0) self.title_text_render = self.fonts[1].render(self.window_title_str, True, self.text_colour) screen.blit(self.title_text_render, self.title_text_render.get_rect(centerx=self.window_rect[0] + self.window_rect[2] * 0.5, centery=self.window_rect[1] + 24)) screen.blit(self.instructions_text_render_1, self.instructions_text_render_1.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 50)) screen.blit(self.instructions_text_render_2, self.instructions_text_render_2.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 64)) screen.blit(self.instructions_text_render_3, self.instructions_text_render_3.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 78)) screen.blit(self.instructions_text_render_4, self.instructions_text_render_4.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 92)) screen.blit(self.instructions_text_render_5, self.instructions_text_render_5.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 106)) screen.blit(self.instructions_text_render_6, self.instructions_text_render_6.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 134)) screen.blit(self.instructions_text_render_7, self.instructions_text_render_7.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 148)) screen.blit(self.instructions_text_render_8, self.instructions_text_render_8.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 176)) self.done_button.draw(screen)
class MainMenu: def __init__(self, fonts): self.show_menu = True self.show_high_scores = False self.start_game = False self.background_image = pygame.image.load("images/menu_background.png") self.play_game_button = UTTextButton([325, 490, 150, 35], "Play Game", fonts, 1) self.high_scores_button = UTTextButton([325, 545, 150, 35], "High Scores", fonts, 1) self.is_running = True def run(self, screen, background, fonts, screen_data): is_main_menu_and_index = [0, 0] for event in pygame.event.get(): self.play_game_button.handle_input_event(event) self.high_scores_button.handle_input_event(event) if event.type == QUIT: self.is_running = False self.play_game_button.update() self.high_scores_button.update() if self.play_game_button.was_pressed(): self.start_game = True self.show_menu = False if self.high_scores_button.was_pressed(): self.show_high_scores = True self.show_menu = False screen.blit(background, (0, 0)) # clear the background to black screen.blit(self.background_image, (0, 0)) # draw the background main_menu_title_string = "Pac-Man" main_menu_title_text_render = fonts[0].render(main_menu_title_string, True, pygame.Color("#FFFF00")) screen.blit( main_menu_title_text_render, main_menu_title_text_render.get_rect( centerx=screen_data.screen_size[0] * 0.5, centery=screen_data.screen_size[1] * 0.10)) self.play_game_button.draw(screen) self.high_scores_button.draw(screen) if self.show_high_scores: is_main_menu_and_index[0] = 2 self.show_menu = True self.show_high_scores = False self.start_game = False elif self.start_game: is_main_menu_and_index[0] = 1 self.show_menu = True self.show_high_scores = False self.start_game = False elif not self.is_running: is_main_menu_and_index[0] = 3 else: is_main_menu_and_index[0] = 0 return is_main_menu_and_index
class MapEditorInstructionsWindow: def __init__(self, window_rect, fonts): self.window_rect = window_rect self.fonts = fonts self.background_colour = pygame.Color("#191919") self.text_colour = pygame.Color("#FFFFFF") self.window_title_str = "Instructions" self.should_exit = False self.done_button = UTTextButton([self.window_rect[0] + (self.window_rect[2] / 2) + 45, self.window_rect[1] + self.window_rect[3] - 30, 70, 20], "Done", self.fonts, "default_16") self.instructions_text1 = "Arrow keys to scroll map" self.instructions_text2 = "Left mouse click to select tile" self.instructions_text3 = "Right mouse click to place tile" self.instructions_text4 = "'0' and '1' to change the layer of tiles to edit" self.instructions_text5 = "'[' and ']' to switch to the next set of tiles" self.instructions_text6 = "F5 or quit with X to save the map" self.window_x_centre = self.window_rect[0] + self.window_rect[2] * 0.5 self.title_text_render = self.fonts["default_32"].render(self.window_title_str, True, self.text_colour) self.instructions_text_render1 = self.fonts["default_12"].render(self.instructions_text1, True, self.text_colour) self.instructions_text_render2 = self.fonts["default_12"].render(self.instructions_text2, True, self.text_colour) self.instructions_text_render3 = self.fonts["default_12"].render(self.instructions_text3, True, self.text_colour) self.instructions_text_render4 = self.fonts["default_12"].render(self.instructions_text4, True, self.text_colour) self.instructions_text_render5 = self.fonts["default_12"].render(self.instructions_text5, True, self.text_colour) self.instructions_text_render6 = self.fonts["default_12"].render(self.instructions_text6, True, self.text_colour) def handle_input_event(self, event): self.done_button.handle_input_event(event) def update(self): self.done_button.update() if self.done_button.was_pressed(): self.should_exit = True def is_inside(self, screen_pos): is_inside = False if self.window_rect[0] <= screen_pos[0] <= self.window_rect[0] + self.window_rect[2]: if self.window_rect[1] <= screen_pos[1] <= self.window_rect[1] + self.window_rect[3]: is_inside = True return is_inside def draw(self, screen): pygame.draw.rect(screen, self.background_colour, pygame.Rect(self.window_rect[0], self.window_rect[1], self.window_rect[2], self.window_rect[3]), 0) screen.blit(self.title_text_render, self.title_text_render.get_rect(centerx=self.window_rect[0] + self.window_rect[2] * 0.5, centery=self.window_rect[1] + 24)) screen.blit(self.instructions_text_render1, self.instructions_text_render1.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 50)) screen.blit(self.instructions_text_render2, self.instructions_text_render2.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 64)) screen.blit(self.instructions_text_render3, self.instructions_text_render3.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 78)) screen.blit(self.instructions_text_render4, self.instructions_text_render4.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 92)) screen.blit(self.instructions_text_render5, self.instructions_text_render5.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 106)) screen.blit(self.instructions_text_render6, self.instructions_text_render6.get_rect(centerx=self.window_x_centre, centery=self.window_rect[1] + 120)) self.done_button.draw(screen)
class BuyGoodsWindow: def __init__(self, window_rect, fonts, player, all_goods, port): self.port = port self.all_goods = all_goods self.window_rect = window_rect self.fonts = fonts self.back_ground_colour = pygame.Color(25, 25, 25) self.text_colour = pygame.Color(255, 255, 255) self.player = player self.should_exit = False self.window_title_str = "Buy Goods" self.button_text_render = self.fonts[1].render(self.window_title_str, True, self.text_colour) self.all_good_ui_lines = [] counter = 0 for good in self.port.buy_goods_and_prices: self.all_good_ui_lines.append( BuyGoodUILine(good, [ self.window_rect[0] + 40, self.window_rect[1] + 50 + (counter * 25) ], fonts, player)) counter += 1 self.done_button = UTTextButton([ self.window_rect[0] + self.window_rect[2] / 2 - 35, self.window_rect[1] + self.window_rect[3] - 30, 70, 20 ], "Done", fonts, 0) def handle_input_event(self, event): for ui_line in self.all_good_ui_lines: ui_line.handle_input_event(event) self.done_button.handle_input_event(event) def redraw_all_lines(self): for ui_line in self.all_good_ui_lines: ui_line.redraw() def update(self): for ui_line in self.all_good_ui_lines: ui_line.update() self.done_button.update() if self.done_button.was_pressed(): self.should_exit = True redraw_all_lines = False for ui_line in self.all_good_ui_lines: if ui_line.should_redraw_all_lines: redraw_all_lines = True if redraw_all_lines: self.redraw_all_lines() def is_inside(self, screen_pos): is_inside = False if self.window_rect[0] <= screen_pos[ 0] <= self.window_rect[0] + self.window_rect[2]: if self.window_rect[1] <= screen_pos[ 1] <= self.window_rect[1] + self.window_rect[3]: is_inside = True return is_inside def draw(self, screen): pygame.draw.rect( screen, self.back_ground_colour, pygame.Rect(self.window_rect[0], self.window_rect[1], self.window_rect[2], self.window_rect[3]), 0) screen.blit( self.button_text_render, self.button_text_render.get_rect(centerx=self.window_rect[0] + self.window_rect[2] * 0.5, centery=self.window_rect[1] + 24)) for ui_line in self.all_good_ui_lines: ui_line.draw(screen) self.done_button.draw(screen)
class BuyGoodUILine: def __init__(self, good, position, fonts, player): self.fonts = fonts self.player = player self.position = position self.good = good[0] self.good_text = self.good.name self.good_value = good[1] affordable_max = int(self.player.gold / self.good_value) cargo_space_max = self.player.space_in_hold slider_max = min(affordable_max, cargo_space_max) self.test_slider = UISlider( [self.position[0] + 80, self.position[1], 150, 20], [0, slider_max], 0, self.fonts, 0) self.buy_string = "Buy: " + str( self.good_value * self.test_slider.get_value() * -1) self.buy_button = UTTextButton( [self.position[0] + 240, self.position[1], 80, 20], self.buy_string, self.fonts, 0) self.text_colour = pygame.Color(255, 255, 255) self.should_redraw_all_lines = False self.good_text_render = self.fonts[0].render(self.good_text, True, self.text_colour) def redraw(self): self.should_redraw_all_lines = False affordable_max = int(self.player.gold / self.good_value) cargo_space_max = self.player.space_in_hold slider_max = min(affordable_max, cargo_space_max) self.test_slider.set_max(slider_max) self.buy_string = "Buy: " + str( self.good_value * self.test_slider.get_value() * -1) self.buy_button.set_text(self.buy_string) def handle_input_event(self, event): self.test_slider.handle_input_event(event) self.buy_button.handle_input_event(event) def update(self): self.test_slider.update() self.buy_button.update() self.buy_string = "Buy: " + str( self.good_value * self.test_slider.get_value() * -1) self.buy_button.set_text(self.buy_string) if (self.good_value * self.test_slider.get_value()) > self.player.gold: self.buy_button.disable() if self.buy_button.was_pressed(): self.player.gold = self.player.gold - ( self.good_value * self.test_slider.get_value()) self.should_redraw_all_lines = True self.player.add_goods([self.good, self.test_slider.get_value()]) def draw(self, screen): self.test_slider.draw(screen) self.buy_button.draw(screen) screen.blit( self.good_text_render, self.good_text_render.get_rect(centerx=self.position[0], centery=self.position[1] + 10))
class PortUIWindow: def __init__(self, window_rect, fonts, port, player): self.window_rect = window_rect self.fonts = fonts self.background_colour = pygame.Color("#1A1A1A") self.text_colour = pygame.Color("#FFFFFF") self.port = port self.player = player self.window_title_str = self.port.name self.should_exit = False self.should_open_buy_goods_window = False self.should_open_sell_goods_window = False self.repair_cost = (self.player.max_health - self.player.health) * -2 self.title_text_render = self.fonts[1].render(self.window_title_str, True, self.text_colour) self.export_text_render = self.fonts[0].render( "Exports: " + self.port.export, True, self.text_colour) self.buy_button = UTTextButton( [self.window_rect[0] + 40, self.window_rect[1] + 80, 70, 20], "Buy Goods", fonts, 0) self.sell_button = UTTextButton( [self.window_rect[0] + 40, self.window_rect[1] + 110, 70, 20], "Sell Goods", fonts, 0) self.repair_button = UTTextButton( [self.window_rect[0] + 40, self.window_rect[1] + 140, 70, 20], "Repair: " + str(self.repair_cost), fonts, 0) if abs(self.repair_cost) > self.player.gold: self.repair_button.disable() self.done_button = UTTextButton( [self.window_rect[0] + 40, self.window_rect[1] + 180, 70, 20], "Done", fonts, 0) def handle_input_event(self, event): self.buy_button.handle_input_event(event) self.sell_button.handle_input_event(event) self.repair_button.handle_input_event(event) self.done_button.handle_input_event(event) def update(self): self.buy_button.update() self.sell_button.update() self.repair_button.update() self.done_button.update() if self.done_button.was_pressed(): self.should_exit = True if self.buy_button.was_pressed(): self.should_open_buy_goods_window = True if self.sell_button.was_pressed(): self.should_open_sell_goods_window = True if self.repair_button.was_pressed(): self.player.gold = self.player.gold + self.repair_cost self.player.health = self.player.max_health self.repair_cost = (self.player.max_health - self.player.health) * -2 self.repair_button.set_text("Repair: " + str(self.repair_cost)) def is_inside(self, screen_pos): is_inside = False if self.window_rect[0] <= screen_pos[ 0] <= self.window_rect[0] + self.window_rect[2]: if self.window_rect[1] <= screen_pos[ 1] <= self.window_rect[1] + self.window_rect[3]: is_inside = True return is_inside def draw(self, screen): pygame.draw.rect( screen, self.background_colour, pygame.Rect(self.window_rect[0], self.window_rect[1], self.window_rect[2], self.window_rect[3]), 0) screen.blit( self.title_text_render, self.title_text_render.get_rect(centerx=self.window_rect[0] + self.window_rect[2] * 0.5, centery=self.window_rect[1] + 24)) screen.blit( self.export_text_render, self.export_text_render.get_rect(centerx=self.window_rect[0] + self.window_rect[2] * 0.5, centery=self.window_rect[1] + 50)) self.buy_button.draw(screen) self.sell_button.draw(screen) self.repair_button.draw(screen) self.done_button.draw(screen)
class MainMenu: def __init__(self, fonts): self.show_menu = True self.show_editor = False self.is_start_game_selected = True self.is_run_editor_selected = False self.start_game = False self.background_image = pygame.image.load("images/menu_background.png") main_menu_title_string = "Nautical Adventure" self.main_menu_title_text_render = fonts[2].render( main_menu_title_string, True, pygame.Color(255, 255, 255)) self.play_game_button = UTTextButton([437, 465, 150, 35], "Play Game", fonts, 1) self.edit_map_button = UTTextButton([437, 515, 150, 35], "Edit Map", fonts, 1) # self.buy_goods_window = BuyGoodsWindow([100, 100, 400, 350], fonts) self.running = True def run(self, screen, screen_data): is_main_menu_and_index = [0, 0] for event in pygame.event.get(): self.play_game_button.handle_input_event(event) self.edit_map_button.handle_input_event(event) # self.buy_goods_window.handle_input_event(event) if event.type == QUIT: self.running = False self.play_game_button.update() self.edit_map_button.update() # self.buy_goods_window.update() if self.play_game_button.was_pressed(): self.start_game = True self.show_menu = False if self.edit_map_button.was_pressed(): self.show_editor = True self.show_menu = False screen.blit(self.background_image, (0, 0)) # draw the background screen.blit( self.main_menu_title_text_render, # draw the menu title self.main_menu_title_text_render.get_rect( centerx=screen_data.screen_size[0] * 0.5, centery=screen_data.screen_size[1] * 0.05)) self.play_game_button.draw(screen) self.edit_map_button.draw(screen) # self.buy_goods_window.draw(screen) if self.show_editor: is_main_menu_and_index[0] = 2 elif self.start_game: is_main_menu_and_index[0] = 1 elif not self.running: is_main_menu_and_index[0] = 3 else: is_main_menu_and_index[0] = 0 return is_main_menu_and_index
class MainMenu: def __init__(self, fonts): self.showMenu = True self.showEditor = False self.isStartGameSelected = True self.isRunEditorSelected = False self.startGame = False self.backgroundImage = pygame.image.load( "images/menu_background.png").convert() self.playGameButton = UTTextButton([437, 465, 150, 35], "Play Game", fonts, "bod_pstc_32") self.editMapButton = UTTextButton([437, 515, 150, 35], "Edit Map", fonts, "bod_pstc_32") def run(self, screen, fonts, screen_data): is_main_menu_and_index = [0, 0] running = True for event in pygame.event.get(): self.playGameButton.handle_input_event(event) self.editMapButton.handle_input_event(event) if event.type == QUIT: running = False self.playGameButton.update() self.editMapButton.update() if self.playGameButton.was_pressed(): self.startGame = True self.showMenu = False if self.editMapButton.was_pressed(): self.showEditor = True self.showMenu = False screen.blit(self.backgroundImage, (0, 0)) # draw the background main_menu_title_string = "MAXIMUM GUNISHMENT" main_menu_title_text_render = fonts["bod_pstc_150"].render( main_menu_title_string, True, pygame.Color(255, 255, 255)) screen.blit( main_menu_title_text_render, main_menu_title_text_render.get_rect( centerx=screen_data.screenSize[0] * 0.5, centery=128)) self.playGameButton.draw(screen) self.editMapButton.draw(screen) if self.showEditor: is_main_menu_and_index[0] = 2 elif self.startGame: is_main_menu_and_index[0] = 1 else: is_main_menu_and_index[0] = 0 if not running: is_main_menu_and_index[0] = 3 return is_main_menu_and_index