class Title(object): def __init__(self, font, screen_width=800, screen_height=600): self.font = font title_image = pygame.image.load(os.path.join(settings.main_path, 'res', 'tyrra_title.png')) pygame.mixer.music.load(os.path.join(settings.main_path, 'res', 'tyrra_bit.ogg')) self.title_image = Box(rect=pygame.Rect(10, 10, screen_width-20, screen_height-20), border_color=Color.white, highlight_color=Color.white, active_color=Color.white, border=4, image=title_image) self.new_game = TextBox(rect=pygame.Rect(screen_width/2-40, screen_height/2+20, 120, 30), highlight_color=Color.gray, active_color=Color.blue, name='new', message='New Game', text_color=Color.white, text_outline=True, font=font, highlight_text=True, highlight_box=False) self.load_game = TextBox(rect=pygame.Rect(screen_width/2-40, screen_height/2+60, 120, 30), highlight_color=Color.gray, active_color=Color.blue, name='load', message='Load Game', text_color=Color.white, text_outline=True, font=font, highlight_text=True, highlight_box=False) self.options = TextBox(rect=pygame.Rect(screen_width/2-40, screen_height/2+100, 100, 30), highlight_color=Color.gray, active_color=Color.blue, name='options', message='Options', text_color=Color.white, text_outline=True, font=font, highlight_text=True, highlight_box=False) self.quit = TextBox(rect=pygame.Rect(screen_width/2-40, screen_height/2+140, 80, 30), highlight_color=Color.gray, active_color=Color.blue, name='quit', message='Quit', text_color=Color.white, text_outline=True, font=font, highlight_text=True, highlight_box=False) self.buttons = [self.new_game, self.load_game, self.options, self.quit] @staticmethod def play_title_music(): pygame.mixer.music.set_volume(0.1) pygame.mixer.music.play() @staticmethod def stop_title_music(): pygame.mixer.music.fadeout(500) def update(self, key, mouse): for button in self.buttons: if button.update(key=key, mouse=mouse): return button.name def draw(self, screen): self.title_image.draw(screen) self.new_game.draw(screen) self.load_game.draw(screen) self.options.draw(screen) self.quit.draw(screen)
def draw(self, screen): Box.draw(self, screen) text_color = self.text_color if self.highlight_text: if self.status == 'ACTIVE': text_color = self.border_color['ACTIVE'] elif self.status == 'HIGHLIGHT': text_color = self.border_color['HIGHLIGHT'] if len(self.message) != 0: if self.text_outline: outline_rect = pygame.Rect(self.text_rect.left+2, self.text_rect.top+2, self.text_rect.width, self.text_rect.height) if text_color == src.Color.black: screen.blit(self.font.render(self.message, True, src.Color.white), outline_rect) else: screen.blit(self.font.render(self.message, True, src.Color.black), outline_rect) screen.blit(self.font.render(self.message, True, text_color), self.text_rect)
def draw(self, screen): if self.type == node_type['POWER']: self.box_color = Color.green elif self.type == node_type['GUN']: self.box_color = Color.red elif self.type == node_type['ENGINE']: self.box_color = Color.blue elif self.type == node_type['SHIELD']: self.box_color = Color.yellow elif self.type == node_type['ARMOR']: self.box_color = Color.d_gray elif self.type == node_type['FLOOR']: self.box_color = Color.gray else: self.box_color = Color.black zoomed_size = self.cell_size * self.zoom self.rect.x = self.cell_x * zoomed_size + self.offset[0] self.rect.y = self.cell_y * zoomed_size + self.offset[1] self.rect.width = zoomed_size self.rect.height = zoomed_size Box.draw(self, screen)
class Window(object): def __init__(self, position, size, name=None, border_color=src.Color.white): self.name = name self.position = position self.canvas = pygame.Surface(size=size) self.border = Box(pygame.Rect(position[0], position[1], size[0], size[1]), box_color=None, border_color=border_color, highlight_color=border_color, active_color=border_color) self.components = [] self.sprites = [] def always(self): elements = self.components + self.sprites for element in elements: always = getattr(element, 'always', None) if callable(always): element.always() def update(self, key, mouse, offset=None): if offset is None: offset = self.position # offset = (0, 0) if self.border.check_mouse_inside(mouse, (0, 0)): # print 'mouse inside {0}'.format(self.name) for component in self.components: update = getattr(component, 'update', None) if callable(update): # print 'Update on {0}'.format(component) component.update(key=key, mouse=mouse, offset=offset) def draw(self, screen): self.canvas.fill(src.Color.black) for component in self.components: component.draw(self.canvas) for sprite in self.sprites: sprite.draw(self.canvas) screen.blit(self.canvas, self.position) self.border.draw(screen)
class ControlPanel(object): def __init__(self, main_window_width=800, main_window_height=600, main_white_space=50, side_window_width=350, side_window_height=650, side_white_space=50, font=None, small_font=None): self.big_font_size = 24 self.small_font_size = 16 self.main_window = None self.main_width = main_window_width self.main_height = main_window_height self.console_height = 120 self.side_window = None self.font = font self.small_font = small_font # keeps buttons from being pressed when they aren't supposed to self.window_lock = False # some events consants self.intro_event_file = os.path.join(settings.main_path, 'data', 'intro.eve') self.intro_event_id = 'INTRO_1' self.station = None self.window_dict = {'console': False, 'Messages': True, 'email': False, 'Ship': True, 'System': True, 'planet': False, 'Battle': False, 'Warp': True, 'Debug': True, 'Station': True} self.window_list = {} self.sidebar_list = {} for window in self.window_dict: self.window_list[window] = Window((main_white_space, main_white_space), (main_window_width, main_window_height-self.console_height), name=window) self.sidebar_list[window] = Window((main_white_space + main_window_width + side_white_space, side_white_space), (side_window_width, side_window_height), name=window, border_color=Color.d_gray) # console self.the_big_board = Box(pygame.Rect(0, 0, main_window_width, main_window_height-self.console_height), box_color=None, border_color=None, highlight_color=None, active_color=None) self.board_bottom = Box(pygame.Rect(main_white_space, main_white_space+main_window_height-self.console_height, main_window_width, self.console_height), box_color=Color.d_gray, border_color=Color.gray, highlight_color=Color.gray, active_color=Color.gray, border=3, name='Console-back') self.console = TextBoxList(pygame.Rect(main_white_space+10, main_white_space+main_window_height - self.console_height+10, main_window_width, self.console_height), name='Console', text_color=Color.white, text_outline=True, font=self.small_font, list_size=5, line_size=20) self.event = Event(panel=self, picture=self.the_big_board, text=self.console) self.window_list['console'].sprites.append(self.the_big_board) # self.window_list['console'].sprites.append(self.board_bottom) # self.window_list['console'].sprites.append(self.console) # main navigation buttons self.nav_button = {} y_offset = 0 # self.big_font_size+4)/2*len(window) for window, visible in self.window_dict.iteritems(): if visible: self.nav_button[window] = TextBox(pygame.Rect(20, 50+y_offset, 200, 45), Color.d_gray, border_color=None, highlight_color=Color.white, active_color=None, message=window, text_color=Color.white, text_outline=True, font=self.font) y_offset += 55 for button in self.nav_button: self.sidebar_list['console'].components.append(self.nav_button[button]) self.back_to_console = TextBox(pygame.Rect(10, 10, 50, 30), Color.d_gray, border_color=None, highlight_color=Color.blue, active_color=None, message='< <', text_color=Color.white, font=self.font) # email client construct # self.email = EmailClient() self.sidebar_list['Messages'].components.append(self.back_to_console) # ship construct self.ship = Ship(size_x=40, size_y=40) self.window_list['Ship'].components.append(self.ship.main_screen) self.sidebar_list['Ship'].components.append(self.ship) self.sidebar_list['Ship'].components.append(self.back_to_console) # system construct self.system = None self.warp_to_system(x=6541, y=43322) self.screen_title = None self.switch_window('console') # battle screen self.space_battle = None ''' self.space_battle = SpaceBattle(player_ship=self.ship, font=self.font, small_font=self.small_font, window_size=(main_window_width, main_window_height - self.console_height)) self.window_list['Battle'].components.append(self.space_battle) self.sidebar_list['Battle'].components.append(self.space_battle.side_panel) ''' # warp menu self.sidebar_list['Warp'].components.append(self.back_to_console) self.warp = Warp(self, font=self.font, small_font=self.small_font) self.sidebar_list['Warp'].components.append(self.warp) # self.window_list['Warp'].sprites.append(self.board_bottom) # self.window_list['Warp'].sprites.append(self.console) # debug self.debug_console = TextBoxList(pygame.Rect(10, main_window_height-300, main_window_width, 300), name='D_con', text_color=Color.white, text_outline=True, font=self.small_font, list_size=14, line_size=20) self.debug = Debug(self.debug_console, self, self.ship, self.font) self.window_list['Debug'].sprites.append(Box(pygame.Rect(5, main_window_height-310, main_window_width-10, 5), box_color=Color.white, name='LINE')) self.window_list['Debug'].sprites.append(self.debug_console) self.sidebar_list['Debug'].components.append(self.debug) self.sidebar_list['Debug'].components.append(self.back_to_console) def load_event(self, event_file, event_name): self.event.read_event_file(event_file) self.event.run_event(event_name) def new_game(self, captain=None): self.ship.load(os.path.join(settings.main_path, 'data', 'start.shp')) if captain is not None: del self.ship.crew[:] self.ship.add_crew(captain) self.load_event(event_file=self.intro_event_file, event_name=self.intro_event_id) def warp_to_system(self, x, y): del self.window_list['System'].components[:] del self.sidebar_list['System'].components[:] self.station = None self.system = System(panel=self, font=self.font, small_font=self.small_font, x=x, y=y, add_station=True) self.window_list['System'].components.append(self.system.system_map) self.sidebar_list['System'].components.append(self.system) # self.system_map_index = len(self.window_list['System'].components)-1 self.sidebar_list['System'].components.append(self.back_to_console) self.event.adhoc_event(picture=self.system.family_portrait(), text='Warped to system: {0}'.format(self.system.name), goto='console') def dock_with_station(self, station=None): if station: self.station = station self.event.adhoc_event(picture=self.station.image, text='You have docked with {0}'.format(self.station.name), goto='console') def start_space_battle(self, battle_params=None): del self.window_list["Battle"].components[:] del self.sidebar_list["Battle"].components[:] enemies = None if battle_params: if "enemies" in battle_params: enemies = battle_params["enemies"] self.space_battle = SpaceBattle(player_ship=self.ship, font=self.font, small_font=self.small_font, window_size=(self.main_width, self.main_height - self.console_height), enemies=enemies) self.window_list["Battle"].components.append(self.space_battle) self.sidebar_list["Battle"].components.append(self.space_battle.side_panel) self.switch_window(new_window="Battle") def switch_window(self, new_window): self.window_lock = True try: self.main_window = self.window_list[new_window] self.side_window = self.sidebar_list[new_window] sleep(0.1) except Exception as e: print e pass self.screen_title = self.main_window.name self.window_lock = False def always(self): self.main_window.always() def update(self, key, mouse): if not self.window_lock: self.main_window.update(key=key, mouse=mouse) self.side_window.update(key=key, mouse=mouse) if self.back_to_console.update(key=key, mouse=mouse, offset=self.side_window.position): self.switch_window('console') if self.screen_title == 'console': for button in self.nav_button: if self.nav_button[button].update(key=key, mouse=mouse, offset=self.side_window.position): if button == 'Station' and self.station is None: self.event.adhoc_event(text='You are currently not docked at a station.') else: self.switch_window(button) def draw(self, screen): self.main_window.draw(screen) # always draw console probably self.board_bottom.draw(screen) self.console.draw(screen) self.side_window.draw(screen)
class CreateCharacter(object): def __init__(self, big_font, font, small_font, screen_height=800, screen_width=600): self.big_font = big_font self.font = font self.small_font = small_font self.box = Box(pygame.Rect(int(screen_width * 0.1), int(screen_height * 0.1), int(screen_width * 0.8), int(screen_height * 0.8)), box_color=Color.black, border_color=Color.white, border=4, highlight_box=False, name='Character Creation Box') box_x = self.box.rect.x box_y = self.box.rect.y # build portrait roster self.portrait_roster = [] self.portrait_index = 0 for face in os.listdir(settings.face_path): if face.endswith('.png'): path = os.path.join(settings.face_path, face) self.portrait_roster.append({"PATH": path, "IMAGE": pygame.image.load(path)}) for face in os.listdir(settings.mod_path): if face.endswith('.png'): path = os.path.join(settings.mod_path, face) self.portrait_roster.append({"PATH": path, "IMAGE": pygame.image.load(path)}) self.portrait = None self.portrait_box = Box(pygame.Rect(int(box_x + self.box.rect.width / 10), int(box_y + self.box.rect.height / 7), 250, 250), box_color=Color.black, border_color=Color.white, highlight_box=False, name='Portrait Box') self.update_roster_portrait_box() self.left_portrait = TextBox(pygame.Rect(self.portrait_box.rect.x - 35, self.portrait_box.rect.y + self.portrait_box.rect.height/3, 25, 40), box_color=Color.d_gray, border_color=Color.white, highlight_color=Color.gray, active_color=Color.white, name='p_left', message='<', text_color=Color.white, text_outline=True, font=self.font) self.right_portrait = TextBox(pygame.Rect(self.portrait_box.rect.x + self.portrait_box.rect.width + 10, self.portrait_box.rect.y + self.portrait_box.rect.height/3, 25, 40), box_color=Color.d_gray, border_color=Color.white, highlight_color=Color.gray, active_color=Color.white, name='p_right', message='>', text_color=Color.white, text_outline=True, font=self.font) # character name self.name = InputBox(pygame.Rect(box_x + self.box.rect.width / 2.4, box_y + self.box.rect.height/7, 400, 35), box_color=Color.d_gray, border_color=Color.gray, highlight_color=Color.white, active_color=Color.blue, message='Flash', text_color=Color.white, font=self.font, text_limit=32) # character age self.age = InputBox(pygame.Rect(box_x + self.box.rect.width / 2.4, box_y + self.box.rect.height/2.7, 70, 35), box_color=Color.d_gray, border_color=Color.gray, highlight_color=Color.white, active_color=Color.blue, message='25', text_color=Color.white, font=self.font, text_limit=5, allowed_characters=range(48, 58)) # character race self.race = InputBox(pygame.Rect(box_x + self.box.rect.width / 2.4, box_y + self.box.rect.height/4, 400, 35), box_color=Color.d_gray, border_color=Color.gray, highlight_color=Color.white, active_color=Color.blue, message='Human', text_color=Color.white, font=self.font, text_limit=32) # character bio self.bio = InputBox(pygame.Rect(box_x + self.box.rect.width / 2.4, box_y + self.box.rect.height/2, 400, 50), box_color=Color.d_gray, border_color=Color.gray, highlight_color=Color.white, active_color=Color.blue, message='A little bit about myself', text_color=Color.white, font=self.font, text_limit=300) # profession stuff self.profession_list = ['Pilot', 'Guns', 'Engineer', 'Medic', 'Scientist'] self.profession = TextBox(pygame.Rect(self.portrait_box.rect.x + self.portrait_box.rect.width/2, self.portrait_box.rect.y + self.portrait_box.rect.width + 20, 100, 20), highlight_color=Color.white, active_color=Color.gray, border=0, name='Profession', message=self.profession_list[0], text_color=Color.gray, font=self.small_font, highlight_box=False, highlight_text=True) self.skills = {} self.update_profession_stats() # done self.done = TextBox(pygame.Rect(box_x + self.box.rect.width - 150, box_y + self.box.rect.height - 100, 100, 50), box_color=Color.red, border_color=Color.gray, highlight_color=Color.white, active_color=Color.gray, name='done', message='START', text_color=Color.white, text_outline=True, font=self.font) def return_character(self): pawn = Pawn(name=self.name.message, age=self.age.message, race=self.race.message, bio=self.bio.message, profile=self.portrait_roster[self.portrait_index]["PATH"], battle_skills=self.skills) pawn.ship_skills[self.profession.message] = 1 return pawn def update_roster_portrait_box(self): self.portrait = self.portrait_roster[self.portrait_index]["IMAGE"] self.portrait_box.image = self.portrait def update_profession_stats(self): if self.profession.message == 'Pilot': self.skills = {'Melee': 2, 'Ranged': 1, 'Defense': 1, 'Intelligence': 1, 'Speed': 1} elif self.profession.message == 'Guns': self.skills = {'Melee': 1, 'Ranged': 2, 'Defense': 1, 'Intelligence': 1, 'Speed': 1} elif self.profession.message == 'Engineer': self.skills = {'Melee': 1, 'Ranged': 1, 'Defense': 1, 'Intelligence': 1, 'Speed': 2} elif self.profession.message == 'Medic': self.skills = {'Melee': 1, 'Ranged': 1, 'Defense': 2, 'Intelligence': 1, 'Speed': 1} elif self.profession.message == 'Scientist': self.skills = {'Melee': 1, 'Ranged': 1, 'Defense': 1, 'Intelligence': 2, 'Speed': 1} def update(self, key, mouse, offset=(0, 0)): if self.portrait_index > 0 and self.left_portrait.update(key=key, mouse=mouse, offset=offset): self.portrait_index -= 1 self.update_roster_portrait_box() if self.portrait_index < len(self.portrait_roster)-1 \ and self.right_portrait.update(key=key, mouse=mouse, offset=offset): self.portrait_index += 1 self.update_roster_portrait_box() self.name.update(key=key, mouse=mouse, offset=offset) self.age.update(key=key, mouse=mouse, offset=offset) self.race.update(key=key, mouse=mouse, offset=offset) self.bio.update(key=key, mouse=mouse, offset=offset) # if self.profession.update(key=key, mouse=mouse, offset=offset): if self.profession.message == self.profession_list[-1]: self.profession.message = self.profession_list[0] else: self.profession.message = self.profession_list[self.profession_list.index(self.profession.message)+1] self.update_profession_stats() # if self.done.update(key=key, mouse=mouse, offset=offset): return self.return_character() return def draw(self, screen): self.box.draw(screen) # non-interactive title = 'Create Your Character' title_width = self.big_font.size(title)[0] Text.draw_text(screen, self.big_font, text=title, color=Color.blue, position=(self.box.rect.x+(self.box.rect.width - title_width)/2, self.box.rect.y + 5)) Text.draw_text(screen, self.small_font, text='Name:', color=Color.green, position=(self.name.rect.x, self.name.rect.y - 20)) Text.draw_text(screen, self.small_font, text='Race:', color=Color.green, position=(self.race.rect.x, self.race.rect.y - 20)) Text.draw_text(screen, self.small_font, text='Age:', color=Color.green, position=(self.age.rect.x, self.age.rect.y - 20)) Text.draw_text(screen, self.small_font, text='Biography:', color=Color.green, position=(self.bio.rect.x, self.bio.rect.y - 20)) # profession box Text.draw_text(screen, self.small_font, text='Profession:', color=Color.l_gray, position=(self.portrait_box.rect.x, self.profession.rect.y)) # stats i = 25 for key, value in self.skills.iteritems(): Text.draw_text(screen, self.small_font, text=key, color=Color.l_gray, position=(self.portrait_box.rect.x, self.profession.rect.y + i)) Text.draw_text(screen, self.small_font, text=value, color=Color.l_gray, position=(self.portrait_box.rect.x + self.portrait_box.rect.width/2, self.profession.rect.y + i)) i += 25 # buttons and things self.portrait_box.draw(screen) self.left_portrait.draw(screen) self.right_portrait.draw(screen) # boxes self.name.draw(screen) self.race.draw(screen) self.age.draw(screen) self.bio.draw(screen) # self.profession.draw(screen) # self.done.draw(screen)