def Run(self): self.DT = Libtcod.sys_get_last_frame_length() if self.State == "Start": if not self.Start.Instant: self.Start.Duration -= self.DT else: self.State = "Middle" return True if self.Start.Duration <= 0: self.State = "Middle" self.Middle.Duration += self.Start.Duration return True return True if self.State == "Middle": # Needs expanding to run event functions: self.Middle.Duration -= Libtcod.sys_get_last_frame_length() if self.OnEnd: if self.Middle.Duration <= 0: self.Function(self, *self.Parameters) self.State = "End" return True else: self.DT = Libtcod.sys_get_last_frame_length() self.Function(self, *self.Parameters) if self.Middle.Duration <= 0: self.State = "End" return True dt = 1 return True return False if self.State == "End": if not self.End.Instant: # not instant self.End.Duration -= self.DT else: if not self.Unique: if self.Stacked and self.Stack > 0: self.Stack -= 1 self.Start.Duration = self.Start.MaxDuration self.Middle.Duration = self.Middle.MaxDuration self.End.Duration = self.End.MaxDuration self.State = "Start" return True self.State = "Finished" return True if self.End.Duration <= 0: if not self.Unique: if self.Stacked and self.Stack > 0: self.Stack -= 1 self.Start.Duration = self.Start.MaxDuration + self.End.Duration self.Middle.Duration = self.Middle.MaxDuration self.End.Duration = self.End.MaxDuration self.State = "Start" return True else: self.State = "Finished" return True self.State = "Finished" return True else: return False
def test_sys_time(console): libtcodpy.sys_set_fps(0) libtcodpy.sys_get_fps() libtcodpy.sys_get_last_frame_length() libtcodpy.sys_sleep_milli(0) libtcodpy.sys_elapsed_milli() libtcodpy.sys_elapsed_seconds()
def handle_keys(self): key = libtcod.console_check_for_keypress() #real-time is_char = (key.vk == libtcod.KEY_CHAR) is_space = (key.vk == libtcod.KEY_SPACE) if is_char or is_space: letter = (chr(key.c) if key.c else key.vk) return letter # if key.vk == libtcod.KEY_ENTER and key.lalt: # libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if key.vk == libtcod.KEY_ESCAPE: return "quit game" elif key.vk == libtcod.KEY_TAB: print libtcod.sys_get_last_frame_length() elif key.vk == libtcod.KEY_ENTER: return "test word" elif key.vk == libtcod.KEY_RIGHT: return "switch context"
def play_game(): global key, mouse, player_turn mouse = libtcod.Mouse() key = libtcod.Key() start_time = libtcod.sys_elapsed_seconds() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) delta_time = libtcod.sys_get_last_frame_length() #render the screen if not local: ##Clear the characters from screen. for object_ in R.world_obj: object_.clear(cam_x, cam_y) for city in cities: for merchant in city.trade_house.caravans_out: merchant.clear(cam_x, cam_y) #handles the keys and exit if needed. player_action = handle_keys() if player_action == "exit": save_game() break if not pause: #and not player_turn: advance_time() #player_turn = True handle_mouse() render_all() else: # for object_ in R.locale_obj: # object_.clear(cam_x,cam_y) # # you.clear(cam_x, cam_y) #handles the keys and exit if needed. player_action = handle_keys() if player_action == "exit": save_game() break handle_mouse() render_local() if R.msg_redraw == True: update_msg_bar()
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 stats(): libtcod.console_set_default_foreground(None, libtcod.grey) libtcod.console_print_ex ( None, 79, 46, libtcod.BKGND_NONE, libtcod.RIGHT, 'last frame : %3d ms (%3d fps)' % ( int(libtcod.sys_get_last_frame_length() * 1000.0), libtcod.sys_get_fps() ) ) libtcod.console_print_ex ( None, 79, 47, libtcod.BKGND_NONE, libtcod.RIGHT, 'elapsed : %8d ms %4.2fs' % ( libtcod.sys_elapsed_milli(), libtcod.sys_elapsed_seconds() ) )
def get_last_frame_length(): return libtcod.sys_get_last_frame_length()
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
import libtcodpy as libtcod from logic.menu_main import MenuMain from logic.menu_manager import MenuManager screen_width = 75 screen_height = 75 limit_fps = 30 menu_manager = MenuManager(MenuMain(screen_width, screen_height)) libtcod.console_disable_keyboard_repeat() libtcod.console_set_custom_font('cp437_10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(screen_width, screen_height, 'RogueP pre-dev', False) libtcod.sys_set_fps(limit_fps) while not libtcod.console_is_window_closed(): #behavior_manager.update_behaviors(entities) if not menu_manager.loop(round(libtcod.sys_get_last_frame_length()*1000)): break