def test_console_printing(console, fg, bg): libtcodpy.console_set_background_flag(console, libtcodpy.BKGND_SET) assert (libtcodpy.console_get_background_flag(console) == libtcodpy.BKGND_SET) libtcodpy.console_set_alignment(console, libtcodpy.LEFT) assert (libtcodpy.console_get_alignment(console) == libtcodpy.LEFT) libtcodpy.console_print(console, 0, 0, 'print') libtcodpy.console_print_ex(console, 0, 0, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print ex') assert (libtcodpy.console_print_rect( console, 0, 0, 8, 8, 'print rect') > 0 ) assert (libtcodpy.console_print_rect_ex( console, 0, 0, 8, 8, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print rect ex') > 0 ) assert (libtcodpy.console_get_height_rect( console, 0, 0, 8, 8, 'get height') > 0 ) libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, fg, bg)
def do_main_menu(key, mouse): title_panel_h = 28 title_panel = tcod.console_new(g.screen_width, title_panel_h) tcod.console_set_alignment(title_panel, tcod.LEFT) tcod.console_set_default_foreground(title_panel, tcod.blue) for x in range(0, len(g.TITLE_GRAPHIC_TOP)): tcod.console_print_ex(title_panel, 1, x + 1, tcod.BKGND_SET, tcod.LEFT, g.TITLE_GRAPHIC_TOP[x][0]) for x in range(0, len(g.TITLE_GRAPHIC_BOTTOM)): tcod.console_print_ex(title_panel, 1, x + 4 + len(g.TITLE_GRAPHIC_TOP), tcod.BKGND_SET, tcod.LEFT, g.TITLE_GRAPHIC_BOTTOM[x][0]) selection_panel = tcod.console_new(g.screen_width, g.screen_height - title_panel_h) tcod.console_set_alignment(selection_panel, tcod.LEFT) tcod.console_set_default_foreground(selection_panel, tcod.sea) title_colors = [ tcod.azure, tcod.cyan, tcod.dark_purple, tcod.dark_violet, tcod.fuchsia, tcod.light_gray, tcod.purple, tcod.sea, tcod.turquoise, ] g.lexicon_counter = 0 questions_options = { '': [ ('start', 'Start'), ('exit', 'Exit game') ] } waiting_for_response = True while waiting_for_response: for y in range(1, 1 + len(g.TITLE_GRAPHIC_TOP)): color_index = random.randint(0, len(title_colors) - 1) for x in range(0, g.screen_width): tcod.console_set_char_foreground(title_panel, x, y, title_colors[color_index]) tcod.console_blit(title_panel, 0, 0, g.screen_width, title_panel_h, 0, 0, 0) tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) selection = handle_questions(selection_panel, key, mouse, questions_options, 24) if len(selection) > 0: if selection[0] == 'start': g.context = Context.TEAM_CREATION waiting_for_response = False elif selection[0] == 'exit': g.context = Context.EXIT waiting_for_response = False tcod.console_clear(title_panel) tcod.console_blit(title_panel, 0, 0, g.screen_width, title_panel_h, 0, 0, 0) tcod.console_clear(selection_panel) tcod.console_blit(selection_panel, 0, 0, g.screen_width, title_panel_h, 0, 0, 0) tcod.console_flush()
def __init__(self, root_console_width, root_console_height, frame_manager): self.entity_manager = frame_manager.parent_menu.entity_manager # load xp for bg console_bg_xp = gzip.open('assets\\ui\\ui_frame_libraries_bg.xp') self.bg_data = xp_loader.load_xp_string(console_bg_xp.read()) Frame.__init__(self, root_console_width, root_console_height, self.bg_data['width'], self.bg_data['height'], frame_manager) library_start_xy = xp_loader.get_position_key_xy( self.bg_data['layer_data'][1], xp_loader.poskey_color_red) self.library_start_xy = Vec2d(library_start_xy[0], library_start_xy[1]) self.library_line_extent = xp_loader.get_position_key_xy( self.bg_data['layer_data'][1], xp_loader.poskey_color_green) #TODO put these in config somewhere self.line_char = chr(196) self.line_bg = libtcod.Color(2, 22, 12) self.line_fg = libtcod.Color(6, 130, 60) self.libname_fg = libtcod.Color(102, 255, 178) libtcod.console_set_default_background(self.console, self.line_bg) libtcod.console_set_default_foreground(self.console, self.libname_fg) libtcod.console_set_alignment(self.console, libtcod.LEFT) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0])
def __init__(self, root_console_width, root_console_height, frame_manager): self.entity_manager = frame_manager.parent_menu.entity_manager # load xp for bg console_bg_xp = gzip.open('assets\\ui\\ui_frame_libraries_bg.xp') self.bg_data = xp_loader.load_xp_string(console_bg_xp.read()) Frame.__init__(self, root_console_width, root_console_height, self.bg_data['width'], self.bg_data['height'], frame_manager) library_start_xy = xp_loader.get_position_key_xy(self.bg_data['layer_data'][1], xp_loader.poskey_color_red) self.library_start_xy = Vec2d(library_start_xy[0], library_start_xy[1]) self.library_line_extent = xp_loader.get_position_key_xy(self.bg_data['layer_data'][1], xp_loader.poskey_color_green) #TODO put these in config somewhere self.line_char = chr(196) self.line_bg = libtcod.Color(2, 22, 12) self.line_fg = libtcod.Color(6, 130, 60) self.libname_fg = libtcod.Color(102, 255, 178) libtcod.console_set_default_background(self.console,self.line_bg) libtcod.console_set_default_foreground(self.console,self.libname_fg) libtcod.console_set_alignment(self.console, libtcod.LEFT) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0])
def main_menu(): img = libtcod.image_load(b'img/backgrounds/menu_background.png') while not libtcod.console_is_window_closed(): # Show the background image at twice the regular console resolution libtcod.image_blit_2x(img, 0, 0, 0) # Show the game's title libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_set_alignment(0, libtcod.CENTER) libtcod.console_print(0, int(round(SCREEN_WIDTH / 2)), int(round(SCREEN_HEIGHT / 2 - 4)), 'THE LEGEND OF THARSA') libtcod.console_print(0, int(round(SCREEN_WIDTH / 2)), int(round(SCREEN_HEIGHT - 2)), 'By Athemis') # Show the options and wait for the player's choice choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24) if choice == 0: # New game new_game() play_game() elif choice == 1: # Load last game try: load_game() except: msgbox('\n No saved game to load. \n', 24) continue play_game() elif choice == 2: # Quit break
def main_menu(): if (libtcod.random_get_int(0, 0, 1) == 0): img = libtcod.image_load('menu_background.png') else: img = libtcod.image_load('menu_background2.png') while not libtcod.console_is_window_closed(): #show the background image, at twice the regular console resolution libtcod.image_blit_2x(img, 0, 0, 0) #show the game's title, and some credits! libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_set_alignment(0, libtcod.CENTER) libtcod.console_print(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 4, 'Party Rogue WIP') libtcod.console_print(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 2, 'By Dragynrain') libtcod.console_set_alignment(0, libtcod.LEFT) #show options and wait for the player's choice choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24) if choice == 0: # new game new_game() play_game() if choice == 1: # load last game try: load_game() except: msgbox('\n No saved game to load.\n', 24) continue play_game() elif choice == 2: # quit break
def draw(self): libtcod.console_set_alignment(0, libtcod.CENTER) libtcod.console_print(0, self.text_base[0], self.text_base[1], self.title_text) libtcod.console_print(0, self.text_base[0], self.text_base[1] + 2, "a " + self.option_one_text) libtcod.console_print(0, self.text_base[0], self.text_base[1] + 4, "b " + self.option_two_text) libtcod.console_flush() libtcod.console_clear(0)
def draw(self): libtcod.console_clear(self.console) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0]) libtcod.console_set_alignment(self.console, libtcod.LEFT) libtcod.console_print( self.console, self.remaining_actions_display_position[0], self.remaining_actions_display_position[1], str(self.max_actions - self.current_action_count)) libtcod.console_print(self.console, self.max_actions_display_position[0], self.max_actions_display_position[1], str(self.max_actions)) for x in range(self.queued_actions_bar_width + 1): if x <= self.highlighted_tile_count and self.highlighted_tile_count > 0: libtcod.console_put_char( self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(178)) else: libtcod.console_put_char( self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(176)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, self.y_blit_offset)
def draw(self): libtcod.console_clear(self.console) libtcod.console_hline(self.console, 0, 0, self.width) libtcod.console_set_alignment(self.console, libtcod.LEFT) libtcod.console_print(self.console, 0, 0, "COMMANDS") libtcod.console_hline(self.console, 0, self.height - 1, self.width) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, self.start_x, 0)
def menu(header, options, width): global key global mouse if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options!') # Calculate total height for the header (after auto-wrap) # and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height # Create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) # Print the header, with auto-wrap libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_set_alignment(window, libtcod.LEFT) libtcod.console_set_default_background(window, libtcod.BKGND_NONE) libtcod.console_print_rect(window, 0, 0, width, height, header) # Print all the options y = header_height letter_index = ord('a') for option_text in options: text = '({}) {}'.format(chr(letter_index), option_text) libtcod.console_print(window, 0, y, text) y += 1 letter_index += 1 # Blit the contents of "window" to the root console x = int(round(SCREEN_WIDTH / 2 - width / 2)) y = int(round(SCREEN_HEIGHT / 2 - height / 2)) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) # Present the root console to the player and wait for a key-press libtcod.console_flush() libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS, key, mouse, False) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) # Convert the ASCII code to an index; if it corresponds to an # option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def console(session_console): """Return a root console. Be sure to use this fixture if the GUI needs to be initialized for a test. """ console = session_console libtcodpy.console_flush() libtcodpy.console_set_default_foreground(console, libtcodpy.white) libtcodpy.console_set_default_background(console, libtcodpy.black) libtcodpy.console_set_background_flag(console, libtcodpy.BKGND_SET) libtcodpy.console_set_alignment(console, libtcodpy.LEFT) libtcodpy.console_clear(console) return console
def start_new_round(self, winner_of_last_round=None): """starts a new round of pong says who won, shows a countdown, moves the ball to the center, resets the players at the end, wipes the conway sim and resumes play """ # lets do stuff in an off-screen console, so we can use transparency # so the player can see the map con = tcod.console_new(self.width, self.height) time_elapsed = 0.0 # in seconds x = self.width / 2 tcod.console_set_alignment(con, tcod.CENTER) while self.alive and not tcod.console_is_window_closed() and \ time_elapsed < 3.0: y = self.height / 2 if winner_of_last_round: tcod.console_set_default_foreground(con, winner_of_last_round.color) player_num = self.players.index(winner_of_last_round) + 1 string = "Player %d scores!" % player_num height = tcod.console_get_height_rect(con, x, y, self.width, self.height, string) tcod.console_print_rect(con, x, y, self.width, height, string) y += height tcod.console_set_default_foreground(con, tcod.white) string = "New round starting in %d seconds..." % int(3 - time_elapsed) height = tcod.console_get_height_rect(con, x, y, self.width, self.height, string) tcod.console_print_rect(con, x, y, self.width, height, string) self.handle_input() self.update_conway() self.render_all() tcod.console_blit(con, 0, 0, 0, 0, 0, 0, 0, 1.0, 0.75) tcod.console_flush() time_elapsed += tcod.sys_get_last_frame_length() # delete tcod console we created tcod.console_delete(con) #reset the ball self.ball = Ball(1, 1) #reset the player positions for player in self.players: player.top = self.height / 2 # wipe the conway simulation (by creating a new one) self.init_map(self.conway.size, self.conway.color)
def draw(self): libtcod.console_clear(self.console) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0]) libtcod.console_set_alignment(self.console, libtcod.LEFT) libtcod.console_print(self.console, self.remaining_actions_display_position[0], self.remaining_actions_display_position[1], str(self.max_actions - self.current_action_count)) libtcod.console_print(self.console, self.max_actions_display_position[0], self.max_actions_display_position[1], str(self.max_actions)) for x in range(self.queued_actions_bar_width + 1): if x <= self.highlighted_tile_count and self.highlighted_tile_count > 0: libtcod.console_put_char(self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(178)) else: libtcod.console_put_char(self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(176)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, self.y_blit_offset)
def __init__(self, win_name='BlueBox', boot_msg=True, graphics_mode=False, img=None, width=40, height=24, fps=24, foreground=color_on, background=color_off): # declare initial graphics colors and resolution (40 or 80 column modes) self.win_name = win_name self.boot_msg = boot_msg self.graphics_mode = graphics_mode self.img = img self.width = width self.height = height self.fps = fps self.foreground = foreground self.background = background # initialize libtcod console and store to self.con if width >= 80: libtcod.console_set_custom_font('bluebox80.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) else: libtcod.console_set_custom_font('bluebox.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(self.width, self.height, self.win_name, False) libtcod.sys_set_fps(self.fps) self.con = libtcod.console_new(self.width, self.height) # set console colors and alignment libtcod.console_set_default_foreground(self.con, self.foreground) libtcod.console_set_default_background(self.con, self.background) libtcod.console_set_alignment(self.con, libtcod.LEFT) # create the cursor self.cursor = Cursor() # create an array containing the screen contents self.screen = [[' ' for y in range(self.height)] for x in range(self.width)] # if graphics layer is enabled, initialize it. if graphics_mode: self.img = libtcod.image_new(self.width * 2, self.height * 2) libtcod.image_clear(self.img, self.background) # Display the default boot message, disable with boot_msg=False if boot_msg: self.text_out('BUTTECH CAI-1 (C) 1987') self.text_out('PRODUCED UNDER CONTRACT FOR THE BUTTE') self.text_out('COUNTY BOARD OF EDUCATION') self.text_out('')
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): #render a bar (HP, experience, etc). first calculate the width of the bar bar_width = int(float(value) / maximum * total_width) #render the background first libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False) #now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False) #finally, some centered text with the values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_set_alignment(panel, libtcod.CENTER) libtcod.console_print(panel, x + total_width / 2, y, name + ': ' + str(value) + '/' + str(maximum)) libtcod.console_set_alignment(panel, libtcod.LEFT)
def draw(self): libtcod.console_clear(self.console) libtcod.console_set_default_background(self.console, libtcod.Color(255,0,255)) libtcod.console_rect(self.console, 0, 0, self.width, self.height, libtcod.BKGND_SET) libtcod.console_set_default_background(self.console, libtcod.black) libtcod.console_set_alignment(self.console, libtcod.LEFT) current_height = 0 for line in self.console_command_history: libtcod.console_print(self.console, 0, current_height, line) current_height += 1 if self.input_enabled: libtcod.console_print(self.console, 0, current_height, self.prompt_string + self.input_command + self.blinking_cursor) libtcod.console_vline(self.console, self.width - 1, 0, self.height) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, self.root_console_height - self.height)
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): # Render a bar (HP, experience, etc). First calculate the width of the bar bar_width = int(float(value) / maximum * total_width) # Render the background first libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SET) # Now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SET) # Add centered text with values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_set_alignment(panel, libtcod.CENTER) bar_text = '{}: {}/{}'.format(name, str(value), str(maximum)) libtcod.console_print(panel, int(x + total_width / 2), y, bar_text)
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): # Render a bar (HP, experience, etc). First calculate the width of the bar bar_width = int(float(value) / maximum * total_width) # Render the background first libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SET) # Now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SET) # Add centered text with values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_set_alignment(panel, libtcod.CENTER) bar_text = '{}: {}/{}'. format(name, str(value), str(maximum)) libtcod.console_print(panel, int(x + total_width / 2), y, bar_text)
def do_post_race(key, mouse): finished_race = g.season.races[g.season.current_race] full_panel = tcod.console_new(g.screen_width, g.screen_height) tcod.console_set_alignment(full_panel, tcod.CENTER) tcod.console_set_default_foreground(full_panel, tcod.sea) tcod.console_clear(full_panel) title = finished_race.circuit.name + ' Results' tcod.console_print_frame(full_panel, 1, 1, g.screen_width - 2, g.screen_height - 2, False, tcod.BKGND_DEFAULT, title) LINE_LENGTH = 50 header = 'Team' + (' ' * (LINE_LENGTH - 10)) + 'Points' underline = '=' * LINE_LENGTH tcod.console_print_ex(full_panel, 30, 20, tcod.BKGND_SET, tcod.LEFT, header) tcod.console_print_ex(full_panel, 30, 21, tcod.BKGND_SET, tcod.LEFT, underline) for place, team in finished_race.places.items(): # Record point data points = g.POINTS[place] g.season.standings[team] += points # Print info place_name = str(place) + '. ' + team.name point_string = str(points) space_count = LINE_LENGTH - (len(place_name) + len(point_string)) line = place_name + (' ' * space_count) + point_string tcod.console_print_ex(full_panel, 30, 21 + place, tcod.BKGND_SET, tcod.LEFT, line) tcod.console_blit(full_panel, 0, 0, g.screen_width, g.screen_height, 0, 0, 0) tcod.console_flush() # Wait for `tcod.Enter` to continue confirm = False while not confirm: tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) action = handle_keys(key) confirm = action.get('confirm') if confirm: g.season.current_race += 1 g.context = Context.SEASON_OVERVIEW
def whole_number_menu(var, title, min_var=None): """A menu to change the value of a whole number x""" w, h = SCREEN_WIDTH, SCREEN_HEIGHT window = tcod.console_new(w, h) key = tcod.console_check_for_keypress() while not key.vk in (tcod.KEY_ENTER, tcod.KEY_ESCAPE): tcod.console_clear(window) x = SCREEN_WIDTH / 2 y = SCREEN_HEIGHT / 2 tcod.console_set_default_foreground(window, tcod.yellow) tcod.console_set_alignment(window, tcod.CENTER) tcod.console_print(window, x, y, title) tcod.console_set_default_foreground(window, tcod.white) tcod.console_print(window, x, y + 2, str(var)) tcod.console_set_default_foreground(window, tcod.grey) tcod.console_print(window, x, y + 4, 'Use arrows or number keys') tcod.console_print(window, x, y + 5, 'Press Enter or Escape to return') tcod.console_blit(window, 0, 0, w, h, 0, 0, 0, 1.0, 1.0) tcod.console_flush() key = tcod.console_wait_for_keypress(True) if key.pressed: if key.vk == tcod.KEY_LEFT: var = max(var - 1, 0) elif key.vk == tcod.KEY_BACKSPACE: string_var = str(var)[:-1] if string_var == '': var = 0 else: var = int(string_var) elif key.vk in number_keys: str_number = str(number_keys.index(key.vk)) var = int(str(var) + str_number) elif key.vk == tcod.KEY_RIGHT: var += 1 if min_var is None or var >= min_var: return var else: return whole_number_menu(var, title, min_var)
def draw(self, main_map): #Settings libtcod.console_set_default_background(self.console, libtcod.black) libtcod.console_set_alignment(self.console, libtcod.CENTER) if main_map.selected_unit: start = (self.width - 4)/2 #Draw all units in the unit image for x in range(self.width - 4): for y in range(self.width - 4): libtcod.image_put_pixel(self.unit_image, x, y, main_map.map_list[main_map.selected_unit.x + x - start][main_map.selected_unit.y + y - start].color) for u in main_map.units: if u.x == (main_map.selected_unit.x + x - start) and u.y == (main_map.selected_unit.y + y - start): libtcod.console_set_char_foreground(self.console, x + 2, y + 4, u.color) libtcod.console_set_char(self.console, x + 2, y + 4, u.char) libtcod.console_print_frame(self.console, 0, 0, self.width, self.height, False, libtcod.BKGND_NONE, main_map.selected_unit.name) libtcod.console_rect(self.console, 0,0, 20, 1, False) libtcod.image_blit_rect(self.unit_image, self.console, 2, 4, self.width - 4, self.width - 4, libtcod.BKGND_SET) libtcod.console_set_alignment(self.console, libtcod.LEFT) #Unit stats statx = self.width + 1 libtcod.console_print(self.console, 2, statx, 'Speed') libtcod.console_print(self.console, 2, statx + 1, 'Attack') libtcod.console_print(self.console, 2, statx + 2, 'Armor') libtcod.console_set_alignment(self.console, libtcod.RIGHT) libtcod.console_print(self.console, self.width - 2, statx, str(main_map.selected_unit.speed)) libtcod.console_print(self.console, self.width - 2, statx + 1, str(main_map.selected_unit.attack)) libtcod.console_print(self.console, self.width - 2, statx + 2, str(main_map.selected_unit.armor)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 1, 60 - self.height/2, 1, 0.75)
def do_team_creation(key, mouse): tcod.console_flush() full_panel = tcod.console_new(g.screen_width * 2, g.screen_height) tcod.console_set_alignment(full_panel, tcod.LEFT) tcod.console_set_default_foreground(full_panel, tcod.sea) questions_options = { 'What is the name of your team?': [], 'What color is your team?': [ (tcod.red, '%c%c%c%cRed%c'%(tcod.COLCTRL_FORE_RGB, tcod.red.r + 256, tcod.red.g + 256, tcod.red.b + 256, tcod.COLCTRL_STOP)), (tcod.orange, '%c%c%c%cOrange%c'%(tcod.COLCTRL_FORE_RGB, tcod.orange.r + 256, tcod.orange.g + 256, tcod.orange.b + 256, tcod.COLCTRL_STOP)), (tcod.yellow, '%c%c%c%cYellow%c'%(tcod.COLCTRL_FORE_RGB, tcod.yellow.r + 256, tcod.yellow.g + 256, tcod.yellow.b + 256, tcod.COLCTRL_STOP)), (tcod.green, '%c%c%c%cGreen%c'%(tcod.COLCTRL_FORE_RGB, tcod.green.r + 256, tcod.green.g + 256, tcod.green.b + 256, tcod.COLCTRL_STOP)), (tcod.sea, '%c%c%c%cSea%c'%(tcod.COLCTRL_FORE_RGB, tcod.sea.r + 256, tcod.sea.g + 256, tcod.sea.b + 256, tcod.COLCTRL_STOP)), (tcod.turquoise, '%c%c%c%cTurquoise%c'%(tcod.COLCTRL_FORE_RGB, tcod.turquoise.r + 256, tcod.turquoise.g + 256, tcod.turquoise.b + 256, tcod.COLCTRL_STOP)), (tcod.light_cyan, '%c%c%c%cLight cyan%c'%(tcod.COLCTRL_FORE_RGB, tcod.light_cyan.r + 256, tcod.light_cyan.g + 256, tcod.light_cyan.b + 256, tcod.COLCTRL_STOP)), (tcod.azure, '%c%c%c%cAzure%c'%(tcod.COLCTRL_FORE_RGB, tcod.azure.r + 256, tcod.azure.g + 256, tcod.azure.b + 256, tcod.COLCTRL_STOP)), (tcod.blue, '%c%c%c%cBlue%c'%(tcod.COLCTRL_FORE_RGB, tcod.blue.r + 256, tcod.blue.g + 256, tcod.blue.b + 256, tcod.COLCTRL_STOP)), (tcod.purple, '%c%c%c%cPurple%c'%(tcod.COLCTRL_FORE_RGB, tcod.purple.r + 256, tcod.purple.g + 256, tcod.purple.b + 256, tcod.COLCTRL_STOP)), (tcod.light_purple, '%c%c%c%cLight purple%c'%(tcod.COLCTRL_FORE_RGB, tcod.light_purple.r + 256, tcod.light_purple.g + 256, tcod.light_purple.b + 256, tcod.COLCTRL_STOP)), (tcod.pink, '%c%c%c%cPink%c'%(tcod.COLCTRL_FORE_RGB, tcod.pink.r + 256, tcod.pink.g + 256, tcod.pink.b + 256, tcod.COLCTRL_STOP)), (tcod.sepia, '%c%c%c%cSepia%c'%(tcod.COLCTRL_FORE_RGB, tcod.sepia.r + 256, tcod.sepia.g + 256, tcod.sepia.b + 256, tcod.COLCTRL_STOP)), (tcod.gray, '%c%c%c%cGray%c'%(tcod.COLCTRL_FORE_RGB, tcod.gray.r + 256, tcod.gray.g + 256, tcod.gray.b + 256, tcod.COLCTRL_STOP)), (tcod.white, '%c%c%c%cWhite%c'%(tcod.COLCTRL_FORE_RGB, tcod.white.r + 256, tcod.white.g + 256, tcod.white.b + 256, tcod.COLCTRL_STOP)) ] } responses = handle_questions(full_panel, key, mouse, questions_options) player_team = Team(responses[0], responses[1], Vehicle(vehicle_bodies.v_bod_1), True) teams = t.pick_season_teams(player_team) # Build season season_circuits = [] for circuit in circuits.ALL: season_circuits.append(circuit) g.season = Season(2094, season_circuits, teams) # Move on g.context = Context.SEASON_OVERVIEW
def render(mouse): libtcod.console_clear(0) libtcod.console_clear(unit_panel.console) main_map.draw(0, cam) #unit_panel rendering #unit rendering libtcod.console_set_alignment(0, libtcod.CENTER) libtcod.console_set_default_background(0, libtcod.black) for u in main_map.units: u.draw(0,cam) #Draw name function if (u.x == mouse.cx + cam.x and u.y == mouse.cy + cam.y) or u == main_map.selected_unit: libtcod.console_print(0, u.x - cam.x, u.y - cam.y -1, u.name) #Draw the destination if moving x,y = libtcod.path_get_destination(u.path) if not libtcod.path_is_empty(u.path): libtcod.console_set_char(0, x - cam.x, y - cam.y, libtcod.CHAR_ARROW_S) unit_panel.draw(main_map)
def render_bottom_panel(): # fill it with black tcod.console_set_default_background(bottom_panel, tcod.black) tcod.console_clear(bottom_panel) tcod.console_set_default_foreground(bottom_panel, tcod.white) # draw separating line for x in range(BOTTOM_PANEL_WIDTH): tcod.console_set_char(bottom_panel, x, 0, 205) # draw distance traveled tcod.console_print(bottom_panel, 1, 1, 'DISTANCE:' + str(terrain.map.scroll_amount + game.player.x)) # render player stats tcod.console_print(bottom_panel, 1, 4, 'Strength: ' + str(game.player.strength)) tcod.console_print(bottom_panel, 1, 5, 'Defense: ' + str(game.player.defense)) tcod.console_print(bottom_panel, 1, 7, '[z]' + str(game.player.inventory[0].name)) tcod.console_print(bottom_panel, 1, 9, '[x]' + str(game.player.inventory[1].name)) tcod.console_print(bottom_panel, 1, 11, '[c]' + str(game.player.inventory[2].name)) tcod.console_set_default_foreground(bottom_panel, tcod.cyan) tcod.console_print(bottom_panel, 4, 8, str(game.player.inventory[0].mana_use) + ' mana') tcod.console_print(bottom_panel, 4, 10, str(game.player.inventory[1].mana_use) + ' mana') tcod.console_print(bottom_panel, 4, 12, str(game.player.inventory[2].mana_use) + ' mana') tcod.console_set_alignment(bottom_panel, tcod.LEFT) # render game messages render_messages() # render object and tile names under mouse tcod.console_set_default_foreground(bottom_panel, tcod.light_gray) tcod.console_print(bottom_panel, 1, 0, game.get_names_under_mouse()) tcod.console_blit( bottom_panel, 0, 0, BOTTOM_PANEL_WIDTH, BOTTOM_PANEL_HEIGHT, 0, 0, SCREEN_HEIGHT - BOTTOM_PANEL_HEIGHT)
def draw(self): libtcod.console_clear(self.console) libtcod.console_set_default_background(self.console, libtcod.Color(255, 0, 255)) libtcod.console_rect(self.console, 0, 0, self.width, self.height, libtcod.BKGND_SET) libtcod.console_set_default_background(self.console, libtcod.black) libtcod.console_set_alignment(self.console, libtcod.LEFT) current_height = 0 for line in self.console_command_history: libtcod.console_print(self.console, 0, current_height, line) current_height += 1 if self.input_enabled: libtcod.console_print( self.console, 0, current_height, self.prompt_string + self.input_command + self.blinking_cursor) libtcod.console_vline(self.console, self.width - 1, 0, self.height) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, self.root_console_height - self.height)
def do_race(key, mouse): bottom_viewport_height = 7 main_viewport_height = g.screen_height - bottom_viewport_height main_viewport_width = g.MAIN_VIEWPORT_WIDTH side_viewport_width = g.screen_width - main_viewport_width main_viewport = tcod.console_new(main_viewport_width, main_viewport_height) bottom_viewport_y = g.screen_height - bottom_viewport_height bottom_viewport = tcod.console_new(main_viewport_width, bottom_viewport_height) tcod.console_set_alignment(bottom_viewport, tcod.LEFT) side_viewport_x = g.screen_width - side_viewport_width side_viewport = tcod.console_new(side_viewport_width, g.screen_height) intro_w = int(g.screen_width * .35) intro_h = int(g.screen_height * .20) intro_x = int(g.screen_width * 0.5 - intro_w * 0.5) intro_y = int(g.screen_height * 0.5 - intro_h * 0.5) intro_window = tcod.console_new(intro_w, intro_h) tcod.console_set_alignment(intro_window, tcod.CENTER) tcod.console_set_default_foreground(intro_window, tcod.sea) lexicon = lex.genres_lexicons[g.lexicon_counter][0] title_and_song = build_song(lexicon) race = Race(g.season.teams, g.season.circuits[g.season.current_race], title_and_song[1], title_and_song[0]) # Reset stuff for x in range(0, len(race.teams)): race.teams[x].reset() teams = race.teams player_team_index = 0 lane_count = len(race.teams) track_width = ((race.lane_size + 1) * lane_count) + 1 BASE_OFFSET_TO_CENTER = int((g.MAIN_VIEWPORT_WIDTH - track_width) / 2) for x in range(0, len(teams)): if (teams[x].isPlayer): player_team_index = x teams[x].vehicle.x = BASE_OFFSET_TO_CENTER + (x * (race.lane_size + 1)) + 2 exit_game = False lyrics = race.lyrics vehicles_collided = set([]) active_lyrics_character = 0 keypress_timer = 99999 race_finished = False race_started = False verse = 0 song_completed = False barricade_locations = [] # holds tuples of x, y barricade locations intro_lines = get_race_intro(title_and_song[0], lexicon, race.circuit) current_intro_line = 0 first_frame = True time_elapsed_last_frame = 0 race_start_time = tcod.sys_elapsed_seconds() while not race_finished and not exit_game: tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) keypress_timer += tcod.sys_get_last_frame_length() total_time_elapsed = tcod.sys_elapsed_seconds() if race_started: if not first_frame: time_elapsed_last_frame = tcod.sys_get_last_frame_length() for team in teams: team.ai_run_counters() # Apply collision physics if needed if team.vehicle in vehicles_collided: handle_post_collision(team) else: if team.vehicle.distance_traveled >= len(race.circuit.track_shape) and not team.finished_current_race: finish_race(race, team, total_time_elapsed - race_start_time) # Control player vehicle if team.isPlayer and not team.finished_current_race: action = handle_keys(key) pressed_key_char = action.get('key_char') steer = action.get('steer') exit = action.get('exit') if not song_completed: powerpct = g.get_powerpct_from_keyspeed(keypress_timer) else: powerpct = 1 team.vehicle.apply_power(powerpct) if pressed_key_char and not song_completed: correct = check_key_char_input(pressed_key_char, race.lyrics[verse], active_lyrics_character) if correct: keypress_timer = 0.0 active_lyrics_character += 1 if (active_lyrics_character >= len(lyrics[verse])): active_lyrics_character = 0 verse += 1 if verse >= len(lyrics): song_completed = True else: # TODO: mis-steer pass if steer and team.vehicle.speed > 0: # Can only steer if moving teams[player_team_index].vehicle.x += steer if exit: exit_game = True # If team is not player elif not team.finished_current_race: direction = team.ai_determine_direction() if direction == td.LEFT: team.vehicle.x += -1 elif direction == td.RIGHT: team.vehicle.x += 1 team.ai_apply_power() # If team has reached the finish line else: team.vehicle.apply_power(0) # Don't have time to do proper checks to wait for all teams to # finsh race. For now, just wait until the player team's vehicle # has coasted to a stop, and then take everyone's place from that # moment. if team.isPlayer and team.vehicle.speed == 0: race_finished = True # Apply acceleration, determine speed speed_to_add = time_elapsed_last_frame * team.vehicle.acceleration team.vehicle.speed += speed_to_add if team.vehicle.speed > team.vehicle.current_max_speed_from_power: team.vehicle.speed -= 0.1 if team.vehicle.speed > team.vehicle.max_speed: team.vehicle.speed = team.vehicle.max_speed elif team.vehicle.speed < 0: team.vehicle.speed = 0 distance_traveled_this_frame = time_elapsed_last_frame * team.vehicle.speed team.ai_observe_curves(race.circuit.track_layout, int(team.vehicle.distance_traveled + distance_traveled_this_frame) - int(team.vehicle.distance_traveled)) # This HAS to come first team.vehicle.distance_traveled += distance_traveled_this_frame # Check for collisions vehicles_collided.clear() handle_collisions(race, vehicles_collided, barricade_locations) first_frame = False # Render tcod.console_clear(main_viewport) print_race(main_viewport, race, int(teams[player_team_index].vehicle.y), int(teams[player_team_index].vehicle.distance_traveled), barricade_locations) tcod.console_blit(main_viewport, 0, 0, g.screen_width, g.screen_height, 0, 0, 0,) tcod.console_clear(bottom_viewport) if not song_completed: print_lyrics(bottom_viewport, race.lyrics[verse], active_lyrics_character) tcod.console_blit(bottom_viewport, 0, 0, main_viewport_width, bottom_viewport_height, 0, 0, bottom_viewport_y) tcod.console_clear(side_viewport) print_panel_side(side_viewport, build_race_stats(race), side_viewport_width) tcod.console_blit(side_viewport, 0, 0, side_viewport_width, g.screen_height - bottom_viewport_height, 0, side_viewport_x, 0) if not race_started: # This structure is pretty ugly, but no time to clean it up if current_intro_line == len(intro_lines): time.sleep(intro_lines[current_intro_line - 1][1]) elif current_intro_line >= len(intro_lines): race_started = True else: tcod.console_clear(intro_window) tcod.console_hline(intro_window, 0, 0, intro_w) tcod.console_hline(intro_window, 0, intro_h - 1, intro_w) tcod.console_vline(intro_window, 0, 0, intro_h) tcod.console_vline(intro_window, intro_w - 1, 0, intro_h) tcod.console_print_rect_ex(intro_window, int(intro_w/2), 1, intro_w - 3, intro_h - 2, tcod.BKGND_SET, tcod.CENTER, intro_lines[current_intro_line][0]) tcod.console_blit(intro_window, 0, 0, intro_w, intro_h, 0, intro_x, intro_y) if current_intro_line > 0: time.sleep(intro_lines[current_intro_line - 1][1]) current_intro_line += 1 tcod.console_flush() # Race is finished if exit_game: tcod.console_clear(main_viewport) tcod.console_blit(main_viewport, 0, 0, g.screen_width, g.screen_height, 0, 0, 0,) tcod.console_clear(bottom_viewport) tcod.console_blit(bottom_viewport, 0, 0, main_viewport_width, bottom_viewport_height, 0, 0, bottom_viewport_y) tcod.console_flush() g.context = Context.MAIN_MENU else: final_stats = build_race_stats(race) place = 1 for stat in final_stats: race.places[place] = stat.team place += 1 g.season.races.append(race) g.lexicon_counter += 1 if g.lexicon_counter >= len(lex.genres_lexicons): g.lexicon_counter = 0 g.context = Context.POST_RACE
def console_set_alignment(self,con,align): if con==0: libtcod.console_set_alignment(con,align) else: libtcod.console_set_alignment(self.mConsole[con-1],align)
libtcod.console_print(0, SCREEN_WIDTH/2, SCREEN_HEIGHT-2, 'By Nathaniel Berens') #show options and wait for the player's choice choice = menu(' ', ['Play a new game', 'Continue last game', 'Quit'], 24) if choice == 0: #new game new_game() play_game() elif choice == 1: #load last game try: load_game() except: msgbox('\n No saved game to load. \n', 24) continue play_game() elif choice == 2: #quit break #INITIALIZATION AND MAIN LOOP #------------------------------------------------------------# libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) #init custom font libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'PURGATORIO', False) #init console con = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT) #init off-screen console top_panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT) #init top panel bottom_panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT) #init bottom panel libtcod.sys_set_fps(LIMIT_FPS) #limit fps libtcod.console_set_alignment(0, libtcod.CENTER) #set console alignment main_menu()
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, fov_torchx if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #torch flickers (using noise generator) fov_torchx += 0.2 tdx = [fov_torchx + 20.0] dx = libtcod.noise_get(fov_noise, tdx) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(fov_noise, tdx) * 1.5 di = 0.2 * libtcod.noise_get(fov_noise, [fov_torchx]) # Iterate through rendering queue for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x, y].block_sight # check if tile is a wall if not visible: # if it's not visible right now, the player can only # see it if it's explored if map[x, y].explored: # It's out of the player's FOV if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: # It's visible if wall: base = color_dark_wall light = color_light_wall else: base = color_dark_ground light = color_light_ground #Let the torch actually flicker r = float(x - player.x + dx) * (x - player.x + dx) + \ (y - player.y + dy) * (y - player.y + dy) if r < SQUARED_TORCH_RADIUS: l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 # alter base colors to simulate flickering torch base = libtcod.color_lerp(base, light, l) # actually draw the visible tile libtcod.console_set_char_background(con, x, y, base, libtcod.BKGND_SET) #since it's visible, it's explored map[x, y].explored = True # Draw all objects in the list for object in objects: if object != player: object.draw() player.draw() # Blit the contents of con to the root console libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) # Prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print the game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, MSG_X, y, line) y += 1 # Show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, 1, 3, 'Dungeon level {}'.format(str(dungeon_level))) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print(panel, 1, 0, get_names_under_mouse()) # Blit the contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def render_scores(self): tcod.console_set_alignment(0, tcod.CENTER) for i in range(len(self.players)): tcod.console_set_default_foreground(0, config.PLAYER_COLORS[i]) tcod.console_print(0, self.width / 2, i, "P%d: %d" % (i + 1, self.players[i].score))
def do_season_overview(key, mouse): bottom_selector_panel_h = 9 bottom_selector_panel = tcod.console_new(g.screen_width, bottom_selector_panel_h) tcod.console_set_alignment(bottom_selector_panel, tcod.CENTER) tcod.console_set_default_foreground(bottom_selector_panel, tcod.lightest_magenta) nearly_full_panel = tcod.console_new(g.screen_width, g.screen_height - bottom_selector_panel_h) tcod.console_set_alignment(nearly_full_panel, tcod.LEFT) tcod.console_set_default_foreground(nearly_full_panel, tcod.sea) print_season_overview(nearly_full_panel, g.screen_width, g.season) tcod.console_blit(nearly_full_panel, 0, 0, g.screen_width, g.screen_height - bottom_selector_panel_h, 0, 0, 0) confirm = False selected_option = 1 while not confirm: tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) spacing = 6 options = [] if g.season.current_race >= len(g.season.circuits): options = [ ('exit', 'Retire!'), ('start over', 'Play again') ] else: options = [ ('exit', 'I quit'), ('go forward', 'Let\'s rock') ] selection = options[0][0] tcod.console_clear(bottom_selector_panel) option0_text = options[0][1] option1_text = options[1][1] # Currently only supports exactly 2 options xy0 = (int(g.screen_width / 2) - len(option0_text) - int(spacing / 2), int(bottom_selector_panel_h / 2)) xy1 = (int(g.screen_width / 2) + int(spacing / 2), int(bottom_selector_panel_h / 2)) tcod.console_print_ex(bottom_selector_panel, xy0[0], xy0[1], tcod.BKGND_SET, tcod.LEFT, option0_text) tcod.console_print_ex(bottom_selector_panel, xy1[0], xy1[1], tcod.BKGND_SET, tcod.LEFT, option1_text) if selected_option == 0: tcod.console_hline(bottom_selector_panel, xy0[0] - 2, xy0[1] - 1, len(option0_text) + 4) tcod.console_hline(bottom_selector_panel, xy0[0] - 2, xy0[1] + 1, len(option0_text) + 4) elif selected_option == 1: tcod.console_hline(bottom_selector_panel, xy1[0] - 2, xy1[1] - 1, len(option1_text) + 4) tcod.console_hline(bottom_selector_panel, xy1[0] - 2, xy1[1] + 1, len(option1_text) + 4) action = handle_keys(key, 'simple selection') select = action.get('select') enter = action.get('enter') if select: selected_option += select if selected_option > len(options) - 1: selected_option = 0 elif selected_option < 0: selected_option = len(options) - 1 if enter: selection = options[selected_option][0] confirm = True tcod.console_blit(bottom_selector_panel, 0, 0, g.screen_width, bottom_selector_panel_h, 0, 0, g.screen_height - bottom_selector_panel_h) tcod.console_flush() if selection == 'exit': g.context = Context.MAIN_MENU tcod.console_clear(bottom_selector_panel) tcod.console_clear(nearly_full_panel) tcod.console_blit(bottom_selector_panel, 0, 0, g.screen_width, bottom_selector_panel_h, 0, 0, g.screen_height - bottom_selector_panel_h) tcod.console_blit(nearly_full_panel, 0, 0, g.screen_width, g.screen_height - bottom_selector_panel_h, 0, 0, 0) tcod.console_flush() elif selection == 'go forward': g.context = Context.RACE tcod.console_clear(bottom_selector_panel) tcod.console_clear(nearly_full_panel) tcod.console_blit(bottom_selector_panel, 0, 0, g.screen_width, bottom_selector_panel_h, 0, 0, g.screen_height - bottom_selector_panel_h) tcod.console_blit(nearly_full_panel, 0, 0, g.screen_width, g.screen_height - bottom_selector_panel_h, 0, 0, 0) tcod.console_flush() elif selection == 'start over': g.context = Context.TEAM_CREATION tcod.console_clear(bottom_selector_panel) tcod.console_clear(nearly_full_panel) tcod.console_blit(bottom_selector_panel, 0, 0, g.screen_width, bottom_selector_panel_h, 0, 0, g.screen_height - bottom_selector_panel_h) tcod.console_blit(nearly_full_panel, 0, 0, g.screen_width, g.screen_height - bottom_selector_panel_h, 0, 0, 0) tcod.console_flush()
def main_menu(type='basic'): libtcod.console_set_default_foreground(var.MainMenu, var.TextColor) # No background to allow for an image, one day. #libtcod.console_set_default_background(var.MainMenu, libtcod.BKGND_NONE) libtcod.console_set_alignment(var.MainMenu, libtcod.CENTER) libtcod.console_clear(var.MainMenu) # Name of the game. libtcod.console_set_default_foreground(var.MainMenu, libtcod.dark_violet) libtcod.console_print(var.MainMenu, (var.MainWidth / 2), 1, var.GameName) # Restore text color. libtcod.console_set_default_foreground(var.MainMenu, var.TextColor) index = ord('a') options = [] if type == 'basic': options = [ "Quick Start", "Create Character", "Continue Game", "Tutorial", "Options", #"Credits", "Quit" ] elif type == 'gameplay': options = [ "Save and Quit", "Quit and Abandon", "Options", "Help", "Return to Game" ] elif type == 'help': options = [ "Basic Commands", "Command List", #"Character", #"Combat", ] elif type == 'warn': options = [ "Unable to do so.", "Something is not working.", "Do not try again." ] else: return None y = 3 for option in options: text = chr(index) + ') ' + option libtcod.console_print(var.MainMenu, (var.MainWidth / 2), y, text) index += 1 y += 1 # Add version number and my name, or course. :P libtcod.console_set_default_foreground(var.MainMenu, libtcod.grey) libtcod.console_print_ex(var.MainMenu, 1, var.MainHeight - 1, libtcod.BKGND_NONE, libtcod.LEFT, var.VersionNumber) libtcod.console_set_default_foreground(var.MainMenu, var.TextColor) libtcod.console_print_ex(var.MainMenu, var.MainWidth - 2, var.MainHeight - 1, libtcod.BKGND_NONE, libtcod.RIGHT, "by red_kangaroo") libtcod.console_blit(var.MainMenu, 0, 0, var.MainWidth, var.MainHeight, 0, (var.ScreenWidth - var.MainWidth) / 2, (var.ScreenHeight - var.MainHeight) / 2) # Draw it and wait for input: libtcod.console_flush() while True: Key = libtcod.console_wait_for_keypress(True) what = Key.c - ord('a') if what in range(0, len(options)): return what elif Key.vk == libtcod.KEY_ESCAPE: return None
def handle_main_menu(): tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, GAME_TITLE, False) # the player controls to send to the Game class # 'ai' = controlled by an ai player_controls = [controls[0], 'ai', ] conway_speed = CONWAY_SPEED map_width, map_height = CONWAY_SIZE max_fps = MAX_FPS color = False paddle_size = PADDLE_SIZE seamless = True while not tcod.console_is_window_closed(): tcod.console_clear(0) tcod.console_set_default_foreground(0, tcod.white) tcod.console_set_alignment(0, tcod.CENTER) tcod.console_print(0, SCREEN_WIDTH / 2, 2, 'Conway\'s Game of Pong') tcod.console_set_default_foreground(0, tcod.grey) tcod.console_print(0, SCREEN_WIDTH / 2, 3, 'by Spferical ([email protected])') tcod.console_print(0, SCREEN_WIDTH / 2, 4, 'Version %s' % VERSION) tcod.console_set_default_foreground(0, tcod.white) tcod.console_set_alignment(0, tcod.LEFT) tcod.console_print(0, 2, 6, '(a) Play') tcod.console_print(0, 2, 7, '(b) Exit') player_keys = ['c', 'd'] y = 8 x = 2 playernum = -1 for c in player_controls: y += 1 playernum += 1 k = player_keys[playernum] if c: tcod.console_set_default_foreground( 0, PLAYER_COLORS[playernum]) else: tcod.console_set_default_foreground(0, tcod.grey) if c and not c == 'ai': if c == [tcod.KEY_UP, tcod.KEY_DOWN]: str_controls = '[arrow keys]' else: str_controls = str(c) text = '(' + k + ') ' + str(playernum + 1) + ' Player ' + str_controls elif c == 'ai': text = '(' + k + ') ' + str(playernum + 1) + ' CPU' else: text = '(' + k + ') ' + str(playernum + 1) + ' Not Playing' tcod.console_print(0, x, y, text) tcod.console_set_default_foreground(0, tcod.white) tcod.console_print(0, 2, 13, '(e) Paddle size: ' + str(paddle_size)) tcod.console_print(0, 2, 14, '(f) Conway speed: ' + str(conway_speed)) tcod.console_print(0, 2, 16, '(g) Map width: ' + str(map_width)) tcod.console_print(0, 2, 17, '(h) Map height: ' + str(map_height)) tcod.console_print(0, 2, 19, '(i) FPS: ' + str(max_fps)) tcod.console_print(0, 2, 20, '(j) Fancy color effect (laggy): ' + \ str(color)) tcod.console_print(0, 2, 21, '(k) Game reset after every score: ' + \ str(not seamless)) tcod.console_flush() raw_key = tcod.console_check_for_keypress(tcod.KEY_PRESSED) key = util.get_key(raw_key) if key == 'a': g = game.Game(player_controls, conway_speed, (map_width, map_height), max_fps, color, paddle_size, seamless) g.run() #to clean up after Game tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, GAME_TITLE, False) elif key == 'b': break elif key in player_keys: p = player_keys.index(key) c = player_controls[p] if not c: player_controls[p] = controls[p] elif c == 'ai': player_controls[p] = None else: player_controls[p] = 'ai' elif key == 'e': paddle_size = whole_number_menu( paddle_size, 'The paddles will be how many blocks big?') elif key == 'f': conway_speed = whole_number_menu( conway_speed, 'The Life simulation will update every how many frames?') elif key == 'g': map_width = whole_number_menu( map_width, 'How wide should the map be?', min_var=1) elif key == 'h': map_height = whole_number_menu( map_height, 'How tall should the map be?', min_var=1) elif key == 'i': max_fps = whole_number_menu( max_fps, 'How many frames per second should the game ' + \ 'run at?\n (0 = no limit)') elif key == 'j': color = not color elif key == 'k': seamless = not seamless elif key == tcod.KEY_ESCAPE: break
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, fov_torchx if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #torch flickers (using noise generator) fov_torchx += 0.2 tdx = [fov_torchx + 20.0] dx = libtcod.noise_get(fov_noise, tdx) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(fov_noise, tdx) * 1.5 di = 0.2 * libtcod.noise_get(fov_noise, [fov_torchx]) # Iterate through rendering queue for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x, y].block_sight # check if tile is a wall if not visible: # if it's not visible right now, the player can only # see it if it's explored if map[x, y].explored: # It's out of the player's FOV if wall: libtcod.console_set_char_background( con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, color_dark_ground, libtcod.BKGND_SET) else: # It's visible if wall: base = color_dark_wall light = color_light_wall else: base = color_dark_ground light = color_light_ground #Let the torch actually flicker r = float(x - player.x + dx) * (x - player.x + dx) + \ (y - player.y + dy) * (y - player.y + dy) if r < SQUARED_TORCH_RADIUS: l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 # alter base colors to simulate flickering torch base = libtcod.color_lerp(base, light, l) # actually draw the visible tile libtcod.console_set_char_background(con, x, y, base, libtcod.BKGND_SET) #since it's visible, it's explored map[x, y].explored = True # Draw all objects in the list for object in objects: if object != player: object.draw() player.draw() # Blit the contents of con to the root console libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) # Prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print the game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, MSG_X, y, line) y += 1 # Show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, 1, 3, 'Dungeon level {}'.format(str(dungeon_level))) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print(panel, 1, 0, get_names_under_mouse()) # Blit the contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
elif key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #movement keys # checked this way so you can just hold it down elif libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1 libtcod.console_set_custom_font('terminal8x8_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'window title here', False) while not libtcod.console_is_window_closed(): libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_set_alignment(0, libtcod.LEFT) libtcod.console_print(0, 1, 1, '@') libtcod.console_flush() libtcod.console_print(0, playerx, playery, ' ') exit = handle_keys() if exit: break
def set_alignment(self, alignment): libtcod.console_set_alignment(self.con, alignment)