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 __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 __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("Choose a character", 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.character_buttons = [] character_index = 0 for character in self.characters: character_button = UTCharacterButton( [387, 128 + (character_index * 64), 250, 52], character, fonts, character_index, self.portraits) self.character_buttons.append(character_button) character_index += 1
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 __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 __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 __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 __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 __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", fonts, 0) 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 = "'>' and '<' to rotate selected tile" self.instructions_text5 = "F5 to save map" self.instructions_text6 = " Challenge 1 " self.instructions_text7 = "-------------" self.instructions_text8 = "Create a new island on the map and save it." self.window_x_centre = self.window_rect[0] + self.window_rect[2] * 0.5 self.title_text_render = self.fonts[1].render(self.window_title_str, True, self.text_colour) self.instructions_text_render1 = self.fonts[0].render( self.instructions_text1, True, self.text_colour) self.instructions_text_render2 = self.fonts[0].render( self.instructions_text2, True, self.text_colour) self.instructions_text_render3 = self.fonts[0].render( self.instructions_text3, True, self.text_colour) self.instructions_text_render4 = self.fonts[0].render( self.instructions_text4, True, self.text_colour) self.instructions_text_render5 = self.fonts[0].render( self.instructions_text5, True, self.text_colour) self.instructions_text_render6 = self.fonts[0].render( self.instructions_text6, True, self.text_colour) self.instructions_text_render7 = self.fonts[0].render( self.instructions_text7, True, self.text_colour) self.instructions_text_render8 = self.fonts[0].render( self.instructions_text8, True, self.text_colour)
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)
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
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 __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 __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 __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 __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 __init__(self): pygame.init() os.environ['SDL_VIDEO_CENTERED'] = '1' screen_width, screen_height = 1000, 600 self.window_panel = pygame.display.set_mode((screen_width, screen_height)) self.w, self.h = 800, 500 pygame.display.set_caption("Robot Arm Simulator") self.cx, self.cy = self.w // 2, self.h // 2 self.fov = min(self.w, self.h) self.screen = pygame.Surface((self.w, self.h)) self.clock = pygame.time.Clock() self.cam = Cam((self.w, self.h), (0, -600, -600)) pygame.event.get() pygame.mouse.get_rel() self.cubes = [Cube("Square Base", (0, -22.5, 0), (110, 45, 110), (0, 0, 0)), Cube("Rotating Base", (0, 0, 0), (80, 35, 80), (0, -40, 0)), # -40 Cube("Upper Arm", (0, -40, 0), (25, 150, 15), (0, -107, 0)), # -40 then offset -107 Cube("Lower Arm", (-23, -187.5, 0), (190, 15, 15), (-59, 0, 0)), Cube("Wrist", (-162, 0, 0), (15, 60, 15), (0, -22.5, 0)), Cube("Gripper", (-15, -45, 0), (15, 15, 15), (0, 0, 0)), Cube("stackable_cube_1", (-282, -15, -82), (30, 30, 30)), Cube("stackable_cube_2", (-251, -15, 52), (30, 30, 30)), Cube("stackable_cube_3", (-112, -15, 82), (30, 30, 30)), Cube("target", (-200, 0, 0), (10, 1, 10))] self.fonts = [pygame.font.Font(pygame.font.match_font('gillsanscondensed'), 24), pygame.font.Font(pygame.font.match_font('gillsanscondensed'), 16)] self.frame_average_range = 10 self.last_three_frames = deque([1000.0] * self.frame_average_range) self.ui_buttons = [] self.play_button = UTTextButton((25, 525, 100, 50), "Play", self.fonts, 0) self.ui_buttons.append(self.play_button) self.reset_button = UTTextButton((150, 525, 100, 50), "Reset", self.fonts, 0) self.ui_buttons.append(self.reset_button) self.optimise_arm_info_button = UTTextButton((275, 525, 200, 50), "Optimise Arm Len.", self.fonts, 0) self.ui_buttons.append(self.optimise_arm_info_button) self.arm = ArmSimulator(self.cubes) self.gravity = 100.0 self.playing = False
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 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.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 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 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 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 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 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 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 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
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 SimulatorApp: def __init__(self): pygame.init() os.environ['SDL_VIDEO_CENTERED'] = '1' screen_width, screen_height = 1000, 600 self.window_panel = pygame.display.set_mode((screen_width, screen_height)) self.w, self.h = 800, 500 pygame.display.set_caption("Robot Arm Simulator") self.cx, self.cy = self.w // 2, self.h // 2 self.fov = min(self.w, self.h) self.screen = pygame.Surface((self.w, self.h)) self.clock = pygame.time.Clock() self.cam = Cam((self.w, self.h), (0, -600, -600)) pygame.event.get() pygame.mouse.get_rel() self.cubes = [Cube("Square Base", (0, -22.5, 0), (110, 45, 110), (0, 0, 0)), Cube("Rotating Base", (0, 0, 0), (80, 35, 80), (0, -40, 0)), # -40 Cube("Upper Arm", (0, -40, 0), (25, 150, 15), (0, -107, 0)), # -40 then offset -107 Cube("Lower Arm", (-23, -187.5, 0), (190, 15, 15), (-59, 0, 0)), Cube("Wrist", (-162, 0, 0), (15, 60, 15), (0, -22.5, 0)), Cube("Gripper", (-15, -45, 0), (15, 15, 15), (0, 0, 0)), Cube("stackable_cube_1", (-282, -15, -82), (30, 30, 30)), Cube("stackable_cube_2", (-251, -15, 52), (30, 30, 30)), Cube("stackable_cube_3", (-112, -15, 82), (30, 30, 30)), Cube("target", (-200, 0, 0), (10, 1, 10))] self.fonts = [pygame.font.Font(pygame.font.match_font('gillsanscondensed'), 24), pygame.font.Font(pygame.font.match_font('gillsanscondensed'), 16)] self.frame_average_range = 10 self.last_three_frames = deque([1000.0] * self.frame_average_range) self.ui_buttons = [] self.play_button = UTTextButton((25, 525, 100, 50), "Play", self.fonts, 0) self.ui_buttons.append(self.play_button) self.reset_button = UTTextButton((150, 525, 100, 50), "Reset", self.fonts, 0) self.ui_buttons.append(self.reset_button) self.optimise_arm_info_button = UTTextButton((275, 525, 200, 50), "Optimise Arm Len.", self.fonts, 0) self.ui_buttons.append(self.optimise_arm_info_button) self.arm = ArmSimulator(self.cubes) self.gravity = 100.0 self.playing = False @staticmethod def check_cube_collisions(cube_a, cube_b): # check the X axis if (abs(cube_a.get_position()[0] - cube_b.get_position()[0]) < cube_a.get_half_dimensions()[0] + cube_b.get_half_dimensions()[ 0]): # check the Y axis if (abs(cube_a.get_position()[1] - cube_b.get_position()[1]) < cube_a.get_half_dimensions()[1] + cube_b.get_half_dimensions()[1]): # check the Z axis if (abs(cube_a.get_position()[2] - cube_b.get_position()[2]) < cube_a.get_half_dimensions()[2] + cube_b.get_half_dimensions()[2]): return True return False @staticmethod def try_optimise_arm_info(arm, cubes): found_base = 0 found_l1 = 0 found_l2 = 0 found_l3 = 0 # Final Total Error: 26.62675167617352 # Base Offset: 36 # L1: 179 # L2: 162 # L3: 69 # Final Total Error: 27.91442848959052 # Base Offset: 40 # L1: 175 # L2: 163 # L3: 70 # Base Range tested so far # 25 -> 40 test_positions = [[82, 282, 30], [-52, 251, 30], [-82, 112, 30], [82, 282, 100], [-52, 251, 100], [-82, 112, 50]] min_total_error = 100000000.0 for base_offset in range(36, 37): arm.z_offset_for_base = base_offset for L1 in range(165, 181): for L2 in range(150, 171): for L3 in range(60, 71): total_error = 0 for position in test_positions: x, y, z = position adjusted_z = z - arm.z_offset_for_base angles = [0, 0, 0] if not game.kinematics.adj_length_solve(L1, L2, L3, x, y, adjusted_z, angles): print("Failed to find solution") angle4 = math.pi / 2 - (angles[2] + angles[1]) cubes[1].set_rotation(0, angles[0], 0) cubes[2].set_rotation(0, 0, angles[1]) cubes[3].set_rotation(0, 0, angles[2]) cubes[4].set_rotation(0, 0, angle4) for obj in cubes: obj.update() obj.apply_final_transform() found_x = -cubes[5].get_position()[2] found_y = -cubes[5].get_position()[0] found_z = -cubes[5].get_position()[1] - arm.gripper_half_depth total_error += abs(x - found_x) + abs(y - found_y) + abs(z - found_z) if total_error < min_total_error: min_total_error = total_error found_l1 = L1 found_l2 = L2 found_l3 = L3 found_base = base_offset print("Min Total Error: " + str(min_total_error)) print("Base Offset: " + str(found_base)) print("L1: " + str(found_l1)) print("L2: " + str(found_l2)) print("L3: " + str(found_l3)) print("Final Total Error: " + str(min_total_error)) print("Base Offset: " + str(found_base)) print("L1: " + str(found_l1)) print("L2: " + str(found_l2)) print("L3: " + str(found_l3)) def run(self): while True: dt = self.clock.tick() / 1000 self.arm.update(dt) if self.play_button.was_pressed(): if not self.playing: self.playing = True robot_arm_routine(self.arm) else: self.playing = False self.arm.reset() self.cubes[6].set_position([-282, -15, -82]) self.cubes[7].set_position([-251, -15, 52]) self.cubes[8].set_position([-112, -15, 82]) if self.reset_button.was_pressed(): self.playing = False self.arm.reset() self.cubes[6].set_position([-282, -15, -82]) self.cubes[7].set_position([-251, -15, 52]) self.cubes[8].set_position([-112, -15, 82]) if self.optimise_arm_info_button.was_pressed(): self.playing = False self.try_optimise_arm_info(self.arm, self.cubes) for i in range(6, 9): cube_a = self.cubes[i] collided_with_ground_or_cube = True if cube_a.parent is None: # don't collide if we are on the arm if (-cube_a.get_position()[1]) - cube_a.get_half_dimensions()[1] > 0.0: collided = False for j in range(6, 9): cube_b = self.cubes[j] if cube_a != cube_b: if self.check_cube_collisions(cube_a, cube_b): collided = True if not collided: collided_with_ground_or_cube = False if not collided_with_ground_or_cube: current_position = cube_a.get_position() current_position[1] += self.gravity * dt cube_a.set_position(current_position) # subject to gravity for obj in self.cubes: obj.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() self.cam.events(event) for button in self.ui_buttons: button.handle_input_event(event) self.window_panel.fill((128, 128, 128)) self.screen.fill((0, 0, 0)) face_list = [] face_colour = [] depth = [] for obj in self.cubes: obj.apply_final_transform() vert_list = [] screen_coords = [] for x, y, z, q in obj.final_vertices: x -= self.cam.pos[0] y -= self.cam.pos[1] z -= self.cam.pos[2] x, z = rotate2d((x, z), self.cam.rot[0]) y, z = rotate2d((y, z), self.cam.rot[1]) vert_list += [[x, y, z]] f = self.fov / z x, y = x * f, y * f screen_coords += [(self.cx + int(x), self.cy + int(y))] for f in range(len(obj.faces)): face = obj.faces[f] on_screen = False for i in face: x, y = screen_coords[i] if vert_list[i][2] > 0 and 0 < x < self.w and 0 < y < self.h: on_screen = True break if on_screen: coords = [screen_coords[i] for i in face] face_list += [coords] face_colour += [obj.colours[f]] depth += [sum(sum(vert_list[j][i] / len(face) for j in face) ** 2 for i in range(3))] # drawing of all faces order = sorted(range(len(face_list)), key=lambda k: depth[k], reverse=True) for i in order: pygame.draw.polygon(self.screen, face_colour[i], face_list[i]) # frames per second calculation if dt > 0.0: self.last_three_frames.popleft() self.last_three_frames.append(1.0 / dt) average_fps = sum(self.last_three_frames) / self.frame_average_range fps_string = "FPS: " + "{:.2f}".format(average_fps) fps_text_render = self.fonts[0].render(fps_string, True, pygame.Color(255, 255, 255)) self.screen.blit(fps_text_render, fps_text_render.get_rect(centerx=self.w - 60, centery=24)) self.window_panel.blit(self.screen, (0, 0)) for button in self.ui_buttons: button.update() button.draw(self.window_panel) cube1_pos_title_str = "Cube 1" cube1_pos_str = "Pos: [{:.0f}, {:.0f}, {:.0f}]".format(-self.cubes[6].get_position()[0], -self.cubes[6].get_position()[2], -self.cubes[6].get_position()[1]) cube1_dimensions_str = "Dimensions: [30,30,30]" cube1_pos_title_render = self.fonts[0].render(cube1_pos_title_str, True, pygame.Color(255, 255, 255)) cube1_pos_str_render = self.fonts[0].render(cube1_pos_str, True, pygame.Color(255, 255, 255)) cube1_dimensions_str_render = self.fonts[0].render(cube1_dimensions_str, True, pygame.Color(255, 255, 255)) self.window_panel.blit(cube1_pos_title_render, cube1_pos_title_render.get_rect(centerx=900, centery=32)) self.window_panel.blit(cube1_pos_str_render, cube1_pos_str_render.get_rect(centerx=900, centery=58)) self.window_panel.blit(cube1_dimensions_str_render, cube1_dimensions_str_render.get_rect(centerx=900, centery=84)) cube2_pos_title_str = "Cube 2" cube2_pos_str = "Pos: [{:.0f}, {:.0f}, {:.0f}]".format(-self.cubes[7].get_position()[0], -self.cubes[7].get_position()[2], -self.cubes[7].get_position()[1]) cube2_dimensions_str = "Dimensions: [30,30,30]" cube2_pos_title_render = self.fonts[0].render(cube2_pos_title_str, True, pygame.Color(255, 255, 255)) cube2_pos_str_render = self.fonts[0].render(cube2_pos_str, True, pygame.Color(255, 255, 255)) cube2_dimensions_str_render = self.fonts[0].render(cube2_dimensions_str, True, pygame.Color(255, 255, 255)) self.window_panel.blit(cube2_pos_title_render, cube2_pos_title_render.get_rect(centerx=900, centery=142)) self.window_panel.blit(cube2_pos_str_render, cube2_pos_str_render.get_rect(centerx=900, centery=168)) self.window_panel.blit(cube2_dimensions_str_render, cube2_dimensions_str_render.get_rect(centerx=900, centery=194)) cube3_pos_title_str = "Cube 3" cube3_pos_str = "Pos: [{:.0f}, {:.0f}, {:.0f}]".format(-self.cubes[8].get_position()[0], -self.cubes[8].get_position()[2], -self.cubes[8].get_position()[1]) cube3_dimensions_str = "Dimensions: [30,30,30]" cube3_pos_title_render = self.fonts[0].render(cube3_pos_title_str, True, pygame.Color(255, 255, 255)) cube3_pos_str_render = self.fonts[0].render(cube3_pos_str, True, pygame.Color(255, 255, 255)) cube3_dimensions_str_render = self.fonts[0].render(cube3_dimensions_str, True, pygame.Color(255, 255, 255)) self.window_panel.blit(cube3_pos_title_render, cube3_pos_title_render.get_rect(centerx=900, centery=252)) self.window_panel.blit(cube3_pos_str_render, cube3_pos_str_render.get_rect(centerx=900, centery=278)) self.window_panel.blit(cube3_dimensions_str_render, cube3_dimensions_str_render.get_rect(centerx=900, centery=304)) target_pos_title_str = "Stack Target" target_pos_str = "Pos: [{:.0f}, {:.0f}, {:.0f}]".format(-self.cubes[9].get_position()[0], -self.cubes[9].get_position()[2], -self.cubes[9].get_position()[1]) target_pos_title_render = self.fonts[0].render(target_pos_title_str, True, pygame.Color(255, 255, 255)) target_pos_str_render = self.fonts[0].render(target_pos_str, True, pygame.Color(255, 255, 255)) self.window_panel.blit(target_pos_title_render, target_pos_title_render.get_rect(centerx=900, centery=362)) self.window_panel.blit(target_pos_str_render, target_pos_str_render.get_rect(centerx=900, centery=388)) instructions_str = "Left click view panel and move mouse to rotate view" instructions_str_render = self.fonts[1].render(instructions_str, True, pygame.Color(255, 255, 255)) self.window_panel.blit(instructions_str_render, instructions_str_render.get_rect(centerx=800, centery=525)) pygame.display.flip() key = pygame.key.get_pressed() self.cam.update(dt, key)
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