def _draw_internal(self, console, board_data, move_history): # Draw y-axis legend win = tdl.Window(console, 0, 0, 1, 10) win.draw_str(0, 0, ' 87654321', Colors.LIGHT_GREY) # Draw x-axis legend win = tdl.Window(console, 0, 9, 10, 1) win.draw_str(0, 0, ' abcdefgh', Colors.LIGHT_GREY) # Draw board win = tdl.Window(console, 1, 1, 8, 8) for i, p in enumerate(board_data): row = i // 8 col = i % 8 # Set square color bg = Colors.LIGHT_GREY if (i + row) % 2: bg = Colors.GREY # Set piece color fg = Colors.WHITE if p.islower(): fg = Colors.DARK_GREY win.draw_str(col, row, char_to_semigraphic(p), fg, bg) # Draw move history win = tdl.Window(console, 0, 11, 10, 10) for i, history in enumerate(reversed(move_history[-10:])): move = history.move x, y = move row = 7 - (x // 8) + 1 col = x % 8 col = chr(ord('a') + col) x = '{}{}'.format(col, row) row = 7 - (y // 8) + 1 col = y % 8 col = chr(ord('a') + col) y = '{}{}'.format(col, row) move = '{}{}'.format(x, y) name = history.player_name fg = history.player_color if fg == Colors.WHITE: fg = Colors.LIGHT_GREY move_color = Colors.WHITE if not history.valid: move_color = Colors.GREY fg = tuple(map(lambda x: int(x / 2), fg)) win.draw_str(0, i, move, fg=move_color) win.draw_str(4, i, name[:6], fg=fg)
def draw_spotted_window(): ''' draws a window next to the cursor if any objects are in the area ''' cx, cy = gv.cursor.pos() spotted = [ obj for obj in gv.gameobjects if [obj.x, obj.y] == [cx, cy] and not obj == gv.cursor and not obj == gv.player ] if spotted: # if more than one object is present, output the names as a message lines = [] #gv.cursor.color = colors.yellow width = max( len(obj.name) for obj in spotted ) + 6 # Window width is adapted to longest object name in list for obj in spotted: # Go through the object names and wrap them according to the window's width line_wrapped = textwrap.wrap(obj.name, width) for text in line_wrapped: lines.append(text) window = tdl.Window(gv.root, cx + 2, cy - 2, width, 4 + len(lines)) window.caption = 'I spot:' window.border_color = settings.PANELS_BORDER_COLOR setup_panel(window) y = 2 for text in lines: window.draw_str(1, y, text.title(), bg=None, fg=colors.white) y += 1
def __init__( self, game, width=200, height=200, seed=random.randint(1, 50000) ): self.game = game self.seed = seed self.timer, self.fps = 0, 15 self.width, self.height = width, height self.bg_console = tdl.Console(width, height) self.path_console = tdl.Console(width, height) self.console = tdl.Console(width, height) self.window = tdl.Window( self.console, x=width // 2 - (game.width - 2) // 2, y=height // 2 - (game.height - 2) // 2, width=game.width - 2, height=game.height - 2 ) self.parameters = dict( pher_evap_rate=0.005, pher_amount_walk=0.05, pher_amount_refill=0.50, pher_sensitivity=5., terrain_awareness=1., max_pher=1., rand_dir_chance=10 ) self.colonies = [] self.generate_world() self.camera = Camera( self, x=width // 2, y=height // 2 )
def setUpClass(cls): cls.console = tdl.init(WIDTH, HEIGHT, 'TDL UnitTest', False, renderer='GLSL') # make a small window in the corner cls.window = tdl.Window(cls.console, 0, 0, WINWIDTH, WINHEIGHT)
def __init__(self, console, size, destination, fg, bg): self.console = console width, height = size self.x, self.y = destination self.window = tdl.Window(self.console, self.x, self.y, width, height) self.width, self.height = self.window.get_size() self.fg, self.bg = fg, bg self.window.set_colors(fg=self.fg, bg=self.bg)
def __init__(self, main_console): self.main_console = main_console self.window = tdl.Window( self.main_console, int(math.floor(self.main_console.width / 2)) + 1, 0, width=int(math.floor(self.main_console.width / 2)), height=self.main_console.height ) self.control = None self.chosen_item = None
def __init__(self, main_console): self.main_console = main_console self.window = tdl.Window( self.main_console, 0, 0, width=int(math.floor(self.main_console.width / 2)), height=self.main_console.height ) self.wielded_items_control = None self.worn_items_control = None self.inventory_items_control = None self.chosen_item = None
def game_loop(con): '''The main game loop.''' tdl.event.set_key_repeat(500, 100) game_map = eldmapgen.new_map() ent_mgr = EntityManager() player_coords = eldmap.random_unoccupied(ent_mgr, game_map, eldmapgen.DEFAULT_MAP_SEED) player = ents.new_player(ent_mgr, game_map, player_coords) torch_coords = eldmap.random_unoccupied(ent_mgr, game_map, eldmapgen.DEFAULT_MAP_SEED) client_list = [] for _ in range(random.randint(1, MAX_CLIENTS)): for client_coords in utils.adjacent8(player_coords): if eldmap.passable(ent_mgr, game_map, client_coords): client_list.append( (ents.new_client(ent_mgr, game_map, client_coords), client_coords)) break ents.new_torch(ent_mgr, game_map, torch_coords) # ents.new_client(ent_mgr, game_map, (8, 3)) main_display = tdl.Window(con, 0, 0, 25, 15) ents.new_tracking_camera(ent_mgr, game_map, main_display, player) sys_mgr = SystemManager(ent_mgr) sys_mgr.add_system(systems.UpdateWorldSys()) sys_mgr.add_system(systems.LightingSys(game_map)) sys_mgr.add_system(systems.FOVSys()) sys_mgr.add_system(systems.FOWSys()) fog_sys = systems.FogSys((main_display.width, main_display.height)) sys_mgr.add_system(fog_sys) event_sys = systems.EventSys(CONSOLE_WIDTH, CONSOLE_HEIGHT) sys_mgr.add_system(event_sys) sys_mgr.add_system(systems.AISys()) sys_mgr.add_system(systems.ActorSys()) sys_mgr.add_system(systems.FollowEntitySys()) sys_mgr.add_system(systems.RenderDisplaySys(fog_sys.get_fogmap)) start_time = utils.cur_time_ms() prev_time = start_time cur_time = prev_time while not event_sys.game_ended: cur_time = utils.cur_time_ms() sys_mgr.update(cur_time - prev_time) # 0 is a placeholder value prev_time = cur_time
def draw_options_window(caption, options, x, y): ''' draws a window listing the passed options ''' width = max(len(option) for option in options) + 7 height = len(options) + 4 window = tdl.Window(gv.root, x, y, width, height) window.caption = caption window.border_color = settings.PANELS_BORDER_COLOR_ACTIVE setup_panel(window) y = 2 letter_index = ord('a') #ord returns the ascii code for a string-letter for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text window.draw_str(1, y, text, fg=colors.white, bg=None) y += 1 letter_index += 1 #by incrementing the ascii code for the letter, we go through the alphabet window.draw_str(center_x_for_text(window, '<ESC TO CANCEL>'), window.height - 1, '<ESC TO CANCEL>')
def draw_item_window(item, px, py, width, height): ''' draws a window containing an item's description and related options ''' window = tdl.Window(gv.root, px, py, width, height) window.caption = item.name.title() window.border_color = settings.PANELS_BORDER_COLOR_ACTIVE setup_panel(window) wrapped_descr = textwrap.wrap(item.description, window.width - 2) y = 2 for line in wrapped_descr: window.draw_str(1, y, line, bg=None, fg=colors.white) y += 1 y += 2 window.draw_str(1, y, '(u)se?', bg=None, fg=colors.white) y += 2 window.draw_str(1, y, '(e)quip?', bg=None, fg=colors.white) y += 2 window.draw_str(1, y, '(d)rop?', bg=None, fg=colors.white) y += 2 window.draw_str(center_x_for_text(window, '<ESC TO CANCEL>'), y, '<ESC TO CANCEL>')
#!/usr/bin/env python """ An interactive example of what events are available. """ import sys sys.path.insert(0, '../') import tdl WIDTH, HEIGHT = 80, 60 console = tdl.init(WIDTH, HEIGHT) # the scrolling text window textWindow = tdl.Window(console, 0, 0, WIDTH, -2) # slow down the program so that the user can more clearly see the motion events tdl.setFPS(24) while 1: event = tdl.event.wait() print(event) if event.type == 'QUIT': raise SystemExit() elif event.type == 'MOUSEMOTION': # clear and print to the bottom of the console console.draw_rect(0, HEIGHT - 1, None, None, ' ') console.draw_str(0, HEIGHT - 1, 'MOUSEMOTION event - pos=%i,%i cell=%i,%i motion=%i,%i cellmotion=%i,%i' % (event.pos + event.cell + event.motion + event.cellmotion)) continue # prevent scrolling
(y + self.y) / height * self.zoom, self.z) bgcolor = (int(val * 255), ) * 2 + (min( 255, int(val * 2 * 255)), ) samplewin.drawChar(x, y, ' ', (255, 255, 255), bgcolor) WIDTH, HEIGHT = 80, 40 SAMPLE_WINDOW_RECT = (20, 10, 46, 20) FONT = '../fonts/X11/8x13.png' if __name__ == '__main__': tdl.setFont(FONT) console = tdl.init(WIDTH, HEIGHT, renderer='opengl') samplewin = tdl.Window(console, *SAMPLE_WINDOW_RECT) samples = [cls() for cls in [TrueColorSample, NoiseSample]] while 1: console.clear() samples[sampleIndex].runOnce() for i, sample in enumerate(samples): bgcolor = (0, 0, 0) if sampleIndex == i: bgcolor = (0, 0, 192) console.drawStr(0, -5 + i, '%s' % sample.name, (255, 255, 255), bgcolor) console.drawStr(0, -1, '%i FPS' % tdl.getFPS()) tdl.flush()