def boot(): global SCREEN framework.events.register_event('draw', blit) tcod.console_set_custom_font(os.path.join('data', 'tiles', 'dejavu_wide12x12_gs_tc.png'),#,'consolas10x10_gs_tc.png') flags=tcod.FONT_LAYOUT_TCOD|tcod.FONT_TYPE_GREYSCALE) tcod.console_init_root(constants.WINDOW_WIDTH, constants.WINDOW_HEIGHT, constants.WINDOW_TITLE, fullscreen='--fullscreen' in sys.argv, renderer=tcod.RENDERER_GLSL) tcod.console_set_keyboard_repeat(200, 0) tcod.sys_set_fps(constants.FPS) tcod.mouse_show_cursor(constants.SHOW_MOUSE) SCREEN['c'] = numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int32) SCREEN['d'] = '0'*(constants.WINDOW_HEIGHT*constants.WINDOW_WIDTH) SCREEN['r'] = [] SCREEN['f'] = [] SCREEN['f'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['f'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['f'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'] = [] SCREEN['b'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'].append(numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16))
def init_libtcod(terraform=False, window_size=WINDOW_SIZE, map_view_size=MAP_WINDOW_SIZE): global ITEM_WINDOW, CONSOLE_WINDOW, MESSAGE_WINDOW, PREFAB_WINDOW, X_CUTOUT_WINDOW, Y_CUTOUT_WINDOW _font_file = os.path.join(DATA_DIR, 'tiles', FONT) if '_incol' in FONT: _layout = tcod.FONT_LAYOUT_ASCII_INCOL elif '_inrow' in FONT: _layout = tcod.FONT_LAYOUT_ASCII_INROW if '_tiles' in FONT: _layout = _layout|tcod.FONT_TYPE_GRAYSCALE if '--worldmap' in sys.argv: MAP_WINDOW_SIZE[0] = 450 MAP_WINDOW_SIZE[1] = 450 window_size[0] = 450 window_size[1] = 450 tcod.console_set_custom_font(_font_file, _layout) tcod.console_init_root(window_size[0], window_size[1], WINDOW_TITLE, renderer=RENDERER) if terraform: PREFAB_WINDOW = tcod.console_new(PREFAB_WINDOW_SIZE[0],PREFAB_WINDOW_SIZE[1]) X_CUTOUT_WINDOW = tcod.console_new(X_CUTOUT_WINDOW_SIZE[0],X_CUTOUT_WINDOW_SIZE[1]) Y_CUTOUT_WINDOW = tcod.console_new(Y_CUTOUT_WINDOW_SIZE[0],Y_CUTOUT_WINDOW_SIZE[1]) PREFAB_CHAR_BUFFER[0] = numpy.zeros((PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) PREFAB_CHAR_BUFFER[1] = numpy.zeros((PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_CHAR_BUFFER[0] = numpy.zeros((X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_CHAR_BUFFER[1] = numpy.zeros((X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_CHAR_BUFFER[0] = numpy.zeros((Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_CHAR_BUFFER[1] = numpy.zeros((Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) tcod.console_set_keyboard_repeat(200, 0) tcod.sys_set_fps(FPS) for i in range(3): if terraform: PREFAB_RGB_BACK_BUFFER[i] = numpy.zeros((PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) PREFAB_RGB_FORE_BUFFER[i] = numpy.zeros((PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_RGB_BACK_BUFFER[i] = numpy.zeros((X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_RGB_FORE_BUFFER[i] = numpy.zeros((X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_RGB_BACK_BUFFER[i] = numpy.zeros((Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_RGB_FORE_BUFFER[i] = numpy.zeros((Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) SETTINGS['light mesh grid'] = numpy.meshgrid(range(map_view_size[0]), range(map_view_size[1]))
def new_game(): #create object representing the player fighter_component = entities.Fighter(hp=300, defense=10, power=20, xp=0, xpvalue=0, clan='monster', death_function=entities.player_death, speed = 10) Game.player = entities.Object(data.SCREEN_WIDTH/2, data.SCREEN_HEIGHT/2, '@', 'Roguetato', libtcod.white, tilechar=data.TILE_MAGE, blocks=True, fighter=fighter_component) Game.player.dungeon_level = 1 Game.game_state = data.STATE_PLAYING Game.player.game_turns = 0 Game.dungeon_levelname = data.maplist[Game.player.dungeon_level] Game.map = {} Game.objects = {} Game.upstairs = {} Game.downstairs = {} Game.tick = 0 if data.FREE_FOR_ALL_MODE: #turn on SQL junk and kill player. Game.entity_sql = logging.Sqlobj(data.ENTITY_DB) Game.message_sql = logging.Sqlobj(data.MESSAGE_DB) Game.sql_commit_counter = data.SQL_COMMIT_TICK_COUNT Game.player.fighter.alive = False Game.player.fighter.hp = 0 #generate map (at this point it's not drawn to screen) maplevel.make_dungeon(Game) Game.tick = 1 Game.fov_recompute = True Game.player.fighter.fov = Game.map[Game.dungeon_levelname].fov_map libtcod.console_clear(Game.con) #initial equipment if not data.AUTOMODE: equipment_component = entities.Equipment(slot='wrist', max_hp_bonus = 5) obj = entities.Object(0, 0, '-', 'wristguards of the whale', libtcod.gold, equipment=equipment_component) obj.always_visible = True Game.player.fighter.add_item(obj) equipment_component.equip(Game, Game.player) Game.player.fighter.hp = Game.player.fighter.max_hp(Game) #a warm welcoming message! message('Welcome to MeFightRogues! Good Luck! Don\'t suck!', Game, libtcod.blue) libtcod.console_set_keyboard_repeat(data.KEYS_INITIAL_DELAY,data.KEYS_INTERVAL)
def boot(): global SCREEN framework.events.register_event('draw', blit) tcod.console_set_custom_font( os.path.join( 'data', 'tiles', 'dejavu_wide12x12_gs_tc.png'), #,'consolas10x10_gs_tc.png') flags=tcod.FONT_LAYOUT_TCOD | tcod.FONT_TYPE_GREYSCALE) tcod.console_init_root(constants.WINDOW_WIDTH, constants.WINDOW_HEIGHT, constants.WINDOW_TITLE, fullscreen='--fullscreen' in sys.argv, renderer=tcod.RENDERER_GLSL) tcod.console_set_keyboard_repeat(200, 0) tcod.sys_set_fps(constants.FPS) tcod.mouse_show_cursor(constants.SHOW_MOUSE) SCREEN['c'] = numpy.zeros( (constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int32) SCREEN['d'] = '0' * (constants.WINDOW_HEIGHT * constants.WINDOW_WIDTH) SCREEN['r'] = [] SCREEN['f'] = [] SCREEN['f'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['f'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['f'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'] = [] SCREEN['b'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16)) SCREEN['b'].append( numpy.zeros((constants.WINDOW_HEIGHT, constants.WINDOW_WIDTH), dtype=numpy.int16))
def __init__(self, width, height, map_file): tcod.console_set_custom_font( 'terminal8x8_gs_tc.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD ) tcod.console_init_root(width, height, 'test', False, tcod.RENDERER_GLSL) tcod.sys_set_fps(20) tcod.console_set_keyboard_repeat(300, 10) self.width = width self.height = height self.exit = False self.map = Map.from_file(self, map_file, map_char_data) self.console = tcod.console_new(self.map.width, self.map.height) self.player = Object( self.map.width / 2, self.map.height / 2, character='@', game=self, color=tcod.green, background=tcod.Color(40, 40, 40), walkable=True ) self.fov = Fov(self.map.width, self.map.height, game=self, radius=30) self.objects = [self.player] self.step = 0 self.fov.recompute() for i in range(10): x, y = random.randrange(self.map.width), random.randrange(self.map.height) if self.fov.is_walkable(x, y): self.spawn_npc(x, y) self.fov.dirty = True self.keys = { tcod.KEY_UP: self.action_north, tcod.KEY_DOWN: self.action_south, tcod.KEY_RIGHT: self.action_east, tcod.KEY_LEFT: self.action_west, tcod.KEY_ESCAPE: self.action_exit, tcod.KEY_KP8: self.action_north, tcod.KEY_KP2: self.action_south, tcod.KEY_KP6: self.action_east, tcod.KEY_KP4: self.action_west, tcod.KEY_KP7: self.action_northwest, tcod.KEY_KP1: self.action_southwest, tcod.KEY_KP9: self.action_northeast, tcod.KEY_KP3: self.action_southeast, tcod.KEY_KP5: self.action_pass, }
def main_menu(): while not libtcod.console_is_window_closed(): libtcod.console_disable_keyboard_repeat() libtcod.console_set_default_background(0, libtcod.black) libtcod.console_set_default_foreground(0, libtcod.black) libtcod.console_clear(0) # Show the game's title, and some credits! libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_print_ex(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 4, libtcod.BKGND_NONE, libtcod.CENTER, 'BALL LABYRINTH') libtcod.console_print_ex(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Eric Williams') # Show options and wait for the player's choice. choice = menu('', ['Play a new game', 'Load saved game', 'Play custom level', 'Quit'], 24) if choice == 0: # New game. new_game() libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 1: # Load saved game. loaded = load_game() if loaded: libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 2: # Open the test arena level. custom = new_custom_game() if custom: libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 3: # Quit. break
def init_program(self): """Setup method that is run when program starts.""" libt.console_set_custom_font(config.get_img_path('char_sheet'), libt.FONT_TYPE_GREYSCALE | libt.FONT_LAYOUT_TCOD) libt.console_init_root(config.SCREEN_WIDTH, config.SCREEN_HEIGHT, "BOGEY", False) libt.console_credits() libt.console_set_keyboard_repeat(50, 100) libt.sys_set_fps(60) # Screen consoles self.game_map = libt.console_new(config.MAP_WIDTH, config.MAP_HEIGHT) self.gui = libt.console_new(config.GUI_WIDTH, config.GUI_HEIGHT) # Set up input self.key = libt.Key() self.mouse = libt.Mouse() # Create main menu self.main_menu = gui.MainMenu() self.main_menu.draw() self.main_menu.select()
def menu(header, options, width, Game, letterdelim=None): if len(options) > data.MAX_NUM_ITEMS: message('Cannot have a menu with more than ' + str(data.MAX_NUM_ITEMS) + ' options.', Game) #calculate total height of the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(Game.con, 0, 0, width, data.SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create 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_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height letter_index = ord('a') for obj in options: text = obj.text color = obj.color char = obj.char if color is None: color = libtcod.white if char is None: char = '' if letterdelim is None: letterchar = '' else: letterchar = chr(letter_index) + letterdelim libtcod.console_set_default_foreground(window, color) libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, letterchar + ' ' + char + ' ' + text) y += 1 letter_index += 1 #blit contents of window to root console x = data.SCREEN_WIDTH / 2 - width / 2 y = data.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 keypress libtcod.console_flush() libtcod.console_set_keyboard_repeat(0,0) #turn off key repeat goodchoice = False while not goodchoice: key = libtcod.console_wait_for_keypress(True) if key.pressed == False: continue if key.vk == libtcod.KEY_ENTER and key.lalt: # full screen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #convert ASCII code to an index. if it's valid, return it index = key.c - ord('a') if index >= 0 and index < len(options): goodchoice = True retval = index elif key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_SPACE: goodchoice = True retval = None libtcod.console_set_keyboard_repeat(data.KEYS_INITIAL_DELAY,data.KEYS_INTERVAL) return retval
def menu(header, options, width): if len(options) > 26: raise ValueError("Cannot have a menu with more than 26 options.") # Disable repeat to avoid selecting wrong chocies. Re-enable it when leaving. libtcod.console_disable_keyboard_repeat() #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 if len(options) > 0: opt_height = len(options) else: opt_height = 1 height = opt_height + header_height + 2 # Create an off-screen console that represents the menu's window. window = libtcod.console_new(width, height) while True: # Don't leave menu until a valid choice has been made. # Print a nice border around the menu. frame = '#' * width * height back = ' ' * (width - 1) * (height - 1) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, frame) libtcod.console_print_rect_ex(window, 1, 1, width - 2, height - 2, libtcod.BKGND_NONE, libtcod.LEFT, back) # Print the header with auto-wrap. libtcod.console_print_rect_ex(window, 1, 1, width - 2, height - 2, libtcod.BKGND_NONE, libtcod.LEFT, header) # Print all of the options. y = header_height + 1 letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ')' + option_text libtcod.console_print_ex(window, 1, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 # Blit the contents of "window" to the root console. x = SCREEN_WIDTH / 2 - width / 2 y = 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. libtcod.console_flush() # Wait for a keypress. key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) # Convert the ASCII code to an index. index = key.c - ord('a') # Handle keys. if key.vk == libtcod.KEY_ENTER and key.lalt: # Special case: Alt + Enter toggles fullscreen. libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if len(options) > 0: if key.vk == libtcod.KEY_ESCAPE: # Cancel and exit. libtcod.console_set_keyboard_repeat(5, 5) return None elif index >= 0 and index < len(options): # Return the option. libtcod.console_set_keyboard_repeat(5, 5) return index elif key.vk == libtcod.KEY_ENTER or key.vk == libtcod.KEY_ESCAPE: libtcod.console_set_keyboard_repeat(5, 5) return None
def select_target(mode, x, y): # Set the loop. key = libtcod.Key() libtcod.console_disable_keyboard_repeat() while True: # Function loop. # Render the screen. render_all() # Print the 'look' character. libtcod.console_set_default_foreground(0, libtcod.yellow) libtcod.console_put_char(0, x, y, 'X', libtcod.BKGND_NONE) # Update the screen. libtcod.console_flush() # Process key input. key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ENTER: # Select the target tile and leave the loop. libtcod.console_set_keyboard_repeat(5, 5) return mode, x, y elif key.vk == libtcod.KEY_ESCAPE: # Cancel and leave the loop. libtcod.console_set_keyboard_repeat(5, 5) mode = False return mode, x, y # Move the cursor around. elif key.vk in [libtcod.KEY_UP, libtcod.KEY_KP8]: if y > 0: y -= 1 elif key.vk in [libtcod.KEY_DOWN, libtcod.KEY_KP2]: if y < MAP_HEIGHT - 1: y += 1 elif key.vk in [libtcod.KEY_LEFT, libtcod.KEY_KP4]: if x > 0: x -=1 elif key.vk in [libtcod.KEY_RIGHT, libtcod.KEY_KP6]: if x < MAP_WIDTH - 1: x += 1 elif key.vk == libtcod.KEY_KP7: if x > 0 and y > 0: x -= 1 y -= 1 elif key.vk == libtcod.KEY_KP9: if x < MAP_WIDTH - 1 and y > 0: x +=1 y -=1 elif key.vk == libtcod.KEY_KP1: if x > 0 and y < MAP_HEIGHT - 1: x -= 1 y += 1 elif key.vk == libtcod.KEY_KP3: if x < MAP_WIDTH - 1 and y < MAP_HEIGHT - 1: x += 1 y += 1
import os.path import yaml import textwrap import math import libtcodpy as libtcod import glob libtcod.console_set_keyboard_repeat(500, 50) for fil in glob.glob('./data/namegen/*.cfg'): libtcod.namegen_parse(fil) help = ''' 'i': Inventory 'd': Drop 'g': Get item (Pick up) '?': Help Alt+Escape: Exit Arrow Keys for movement / selecting Name of item under the mouse shown above the health bar ''' from game import GameBase import levels import objects import utilities if __name__ == 'main': class Null: pass class SettingsObject(object): def __init__(self, setting_name, default=Null): self.setting_name = setting_name
def init_libtcod(terraform=False, window_size=WINDOW_SIZE, map_view_size=MAP_WINDOW_SIZE): global ITEM_WINDOW, CONSOLE_WINDOW, MESSAGE_WINDOW, PREFAB_WINDOW, X_CUTOUT_WINDOW, Y_CUTOUT_WINDOW _font_file = os.path.join(DATA_DIR, 'tiles', FONT) if '_incol' in FONT: _layout = tcod.FONT_LAYOUT_ASCII_INCOL elif '_inrow' in FONT: _layout = tcod.FONT_LAYOUT_ASCII_INROW if '_tiles' in FONT: _layout = _layout | tcod.FONT_TYPE_GRAYSCALE if '--worldmap' in sys.argv: MAP_WINDOW_SIZE[0] = 450 MAP_WINDOW_SIZE[1] = 450 window_size[0] = 450 window_size[1] = 450 tcod.console_set_custom_font(_font_file, _layout) tcod.console_init_root(window_size[0], window_size[1], WINDOW_TITLE, renderer=RENDERER) if terraform: PREFAB_WINDOW = tcod.console_new(PREFAB_WINDOW_SIZE[0], PREFAB_WINDOW_SIZE[1]) X_CUTOUT_WINDOW = tcod.console_new(X_CUTOUT_WINDOW_SIZE[0], X_CUTOUT_WINDOW_SIZE[1]) Y_CUTOUT_WINDOW = tcod.console_new(Y_CUTOUT_WINDOW_SIZE[0], Y_CUTOUT_WINDOW_SIZE[1]) PREFAB_CHAR_BUFFER[0] = numpy.zeros( (PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) PREFAB_CHAR_BUFFER[1] = numpy.zeros( (PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_CHAR_BUFFER[0] = numpy.zeros( (X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_CHAR_BUFFER[1] = numpy.zeros( (X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_CHAR_BUFFER[0] = numpy.zeros( (Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_CHAR_BUFFER[1] = numpy.zeros( (Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) tcod.console_set_keyboard_repeat(200, 0) tcod.sys_set_fps(FPS) for i in range(3): if terraform: PREFAB_RGB_BACK_BUFFER[i] = numpy.zeros( (PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) PREFAB_RGB_FORE_BUFFER[i] = numpy.zeros( (PREFAB_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_RGB_BACK_BUFFER[i] = numpy.zeros( (X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) X_CUTOUT_RGB_FORE_BUFFER[i] = numpy.zeros( (X_CUTOUT_WINDOW_SIZE[1], X_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_RGB_BACK_BUFFER[i] = numpy.zeros( (Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) Y_CUTOUT_RGB_FORE_BUFFER[i] = numpy.zeros( (Y_CUTOUT_WINDOW_SIZE[1], Y_CUTOUT_WINDOW_SIZE[0]), dtype=numpy.int8) SETTINGS['light mesh grid'] = numpy.meshgrid(range(map_view_size[0]), range(map_view_size[1]))
# for obj in objects: # obj.clear() action = m.handle() print action # debug message log to console if action == 'new game': new_game() if action == 'continue': load_game() if action == 'exit': break print 'Thank you for playing%s\nGoodbye!'%GAME_TITLE if __name__ == '__main__': # initialization tl.console_set_custom_font('arial12x12.png', tl.FONT_TYPE_GREYSCALE | tl.FONT_LAYOUT_TCOD) tl.console_init_root(SCREEN_W, SCREEN_H, GAME_TITLE, False) # window tl.sys_set_fps(LIMFPS) # frame rate limit tl.console_set_keyboard_repeat(250, 30) # init', rep. delay[ms] # globals' IDs: scr(een), key(board), mouse scr = tl.console_new(SCREEN_W, SCREEN_H) key, mouse = tl.Key(), tl.Mouse() # show intro if it is set up to be shown if INTRO: tl.console_credits() # enter main loop main()
def handle_keys(): #for real-time, uncomment #key = libtcod.console_check_for_keypress() #for turn-based, uncomment #key = libtcod.console_wait_for_keypress(True) key_char = chr(Game.key.c) if Game.key.vk == libtcod.KEY_ENTER and Game.key.lalt: #ALT + ENTER: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif Game.key.vk == libtcod.KEY_ESCAPE: return data.STATE_EXIT #exit game if Game.game_state == data.STATE_PLAYING: #rest if Game.key.vk == libtcod.KEY_KPDEC or Game.key.vk == libtcod.KEY_KP5: player_resting(Game) Game.fov_recompute = True pass #movement keys elif Game.key.vk == libtcod.KEY_UP or key_char == 'k' or Game.key.vk == libtcod.KEY_KP8 : return player_move_or_attack(0, -1, Game) elif Game.key.vk == libtcod.KEY_DOWN or key_char == 'j' or Game.key.vk == libtcod.KEY_KP2 : return player_move_or_attack(0, 1, Game) elif Game.key.vk == libtcod.KEY_LEFT or key_char == 'h' or Game.key.vk == libtcod.KEY_KP4 : return player_move_or_attack(-1, 0, Game) elif Game.key.vk == libtcod.KEY_RIGHT or key_char == 'l' or Game.key.vk == libtcod.KEY_KP6 : return player_move_or_attack(1, 0, Game) #handle diagonal. 11 oclock -> clockwise elif key_char == 'y' or Game.key.vk == libtcod.KEY_KP7 : return player_move_or_attack(-1, -1, Game) elif key_char == 'u' or Game.key.vk == libtcod.KEY_KP9 : return player_move_or_attack(1, -1, Game) elif key_char == 'n' or Game.key.vk == libtcod.KEY_KP3 : return player_move_or_attack(1, 1, Game) elif key_char == 'b' or Game.key.vk == libtcod.KEY_KP1 : return player_move_or_attack(-1, 1, Game) else: #test for other keys if key_char == 'g': #pick up an item for object in Game.objects[data.maplist[Game.player.dungeon_level]]: #look for items in the player's title on the same floor of the player if object.x == Game.player.x and object.y == Game.player.y and object.item: Game.player.game_turns += 1 return object.item.pick_up(Game, Game.player) #break if key_char == 'i': #show inv. if an item is selected, use it chosen_item = inventory_menu('Press the key next to an item to use it. \nPress ESC to return to game\n', Game, Game.player) if chosen_item is not None: Game.player.game_turns += 1 return chosen_item.use(Game, user=Game.player) if key_char == 'd': #show the inventory. if item is selected, drop it chosen_item = inventory_menu('Press the key next to the item to drop. \nPress ESC to return to game\n', Game, Game.player) if chosen_item is not None: Game.player.game_turns += 1 chosen_item.drop(Game, Game.player) if key_char == 'c': #show character info level_up_xp = data.LEVEL_UP_BASE + Game.player.xplevel * data.LEVEL_UP_FACTOR msgbox('Character Information\n\nLevel: ' + str(Game.player.xplevel) + '\nExperience: ' + str(Game.player.fighter.xp) + '\nExperience to level up: ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(Game.player.fighter.max_hp(Game)) + '\nAttack: ' + str(Game.player.fighter.power(Game)) + '\nDefense: ' + str(Game.player.fighter.defense(Game)), Game, data.CHARACTER_SCREEN_WIDTH) if key_char == 'x': #debug key to automatically level up msgbox('You start to meditate!', Game, data.CHARACTER_SCREEN_WIDTH) level_up_xp = data.LEVEL_UP_BASE + Game.player.xplevel * data.LEVEL_UP_FACTOR Game.player.fighter.xp = level_up_xp check_level_up(Game) Game.player.game_turns += 1 if key_char == 'a': #debug key to set all objects to visible msgbox('You can smell them all!', Game, data.CHARACTER_SCREEN_WIDTH) set_objects_visible(Game) if key_char == 'q': #go down stairs, if the player is on them msgbox('You feel your inner dwarf admiring the dungeon walls!', Game, data.CHARACTER_SCREEN_WIDTH) Game.map[Game.dungeon_levelname].set_map_explored() Game.fov_recompute = True if key_char == 'z': #debug key to automatically go to next level msgbox('You start digging at your feet!', Game, data.CHARACTER_SCREEN_WIDTH) map.next_level(Game) if key_char == '>': #go down stairs, if the player is on them if Game.downstairs[data.maplist[Game.player.dungeon_level]].x == Game.player.x and Game.downstairs[data.maplist[Game.player.dungeon_level]].y == Game.player.y: Game.player.game_turns +=1 map.next_level(Game) if key_char == '<': #go up stairs, if the player is on them if Game.upstairs[data.maplist[Game.player.dungeon_level]].x == Game.player.x and Game.upstairs[data.maplist[Game.player.dungeon_level]].y == Game.player.y: Game.player.game_turns +=1 map.prev_level(Game) if key_char == 's': #general status key #debug key to automatically go to prev level msgbox('You start digging above your head!', Game, data.CHARACTER_SCREEN_WIDTH) map.prev_level(Game) if key_char == 'p': #display log width = data.SCREEN_WIDTH height = data.SCREEN_HEIGHT history = [[]] count = 0 page = 1 numpages = int(float(len(Game.msg_history))/data.MAX_NUM_ITEMS + 1) for thepage in range(numpages): history.append([]) for obj in reversed(Game.msg_history): line = obj.text color = obj.color history[page].append(Menuobj(line, color = color)) count += 1 if count >= data.MAX_NUM_ITEMS: page +=1 count = 0 for thepage in range(numpages): window = libtcod.console_new(width, height) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, '') libtcod.console_blit(window, 0, 0, width, height, 0, 0, 0, 1.0, 1) menu ('Message Log: (Sorted by Most Recent Turn) Page ' + str(thepage+1) + '/' + str(numpages), history[thepage+1], data.SCREEN_WIDTH, Game, letterdelim=None) Game.fov_recompute = True if key_char == 'r': print 'SYSTEM--\t RELOADING GAME DATA' reload(data) reload(entitydata) #update_entities() #need to find a way to update all objects to current data Game.fov_recompute = True libtcod.console_set_keyboard_repeat(data.KEYS_INITIAL_DELAY,data.KEYS_INTERVAL) buff_component = entities.Buff('Super Strength', power_bonus=20) Game.player.fighter.add_buff(buff_component) msgbox ('YOU ROAR WITH BERSERKER RAGE!', Game, data.CHARACTER_SCREEN_WIDTH) if key_char == 'w': #give all items msgbox('You fashion some items from the scraps at your feet', Game, data.CHARACTER_SCREEN_WIDTH) give_items(Game) return data.STATE_NOACTION
LIGHTNING_RANGE = 5 CONFUSE_NUM_TURNS = 3 CONFUSE_RANGE = 5 FIREBALL_RADIUS = 3 FIREBALL_DAMAGE = 12 FOV_ALGO = 0 #default FOV algorithm FOV_LIGHT_WALLS = True BACK_COLOR = libtcod.black LIMIT_FPS = 20 libtcod.console_set_custom_font( 'tiles18x18_gs_ro.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, 16, 25) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False) libtcod.console_set_keyboard_repeat(80, 25) con = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT) libtcod.sys_set_fps(LIMIT_FPS) color_dark_wall = libtcod.Color(50, 50, 50) color_light_wall = libtcod.Color(255, 255, 255) color_dark_ground = libtcod.Color(50, 50, 50) color_light_ground = libtcod.white fov = True player_action = None #create the list of game messages and their colors, starts empty game_msgs = [] ######################################################################### ### Game State Class ### #########################################################################
libtcod.console_set_default_foreground(msg, colour_default) libtcod.console_print(msg, 4, SCREEN_HEIGHT - 18, "Controls:") libtcod.console_print(msg, 4, SCREEN_HEIGHT - 17, " arrow keys - move & attack") libtcod.console_print(msg, 4, SCREEN_HEIGHT - 16, " , (comma) - pick up") libtcod.console_print(msg, 4, SCREEN_HEIGHT - 10, "Press Enter to start") libtcod.console_print(msg, 4, SCREEN_HEIGHT - 4, "ENGHack 2014 @ University of Waterloo") bg = libtcod.image_load("bg.png") libtcod.image_blit_rect(bg, msg, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, libtcod.BKGND_ADD) libtcod.console_blit(blank, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_blit(msg, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1, 1) libtcod.console_flush() libtcod.console_set_custom_font("terminal12x12_gs_ro.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, "Rogue@UW - ENGHack 2014", False) libtcod.console_set_keyboard_repeat(KBD_RPT_INIT_DELAY, KBD_RPT_INTERVAL_DELAY) buf = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT) blank = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) msg = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) player_char = Character(M.M['player'][M.HP], M.M['player'][M.ATK], M.M['player'][M.DEF], M.M['player'][M.ACC], M.M['player'][M.AVO], M.M['player'][M.VIS], M.M['player'][M.COL], M.M['player'][M.EXP], M.M['player'][M.RATE], death_func=player_death) player = Object(P_X, P_Y, M.M['player'][M.NAME], M.M['player'][M.CHAR], M.M['player'][M.COL], True, Object.ALIGN_ALLY, character=player_char) objects.append(player) make_map() # Title loop while not libtcod.console_is_window_closed(): make_title()
if self.player_ship.laser_firing: self.player_ship.fire_laser() if self.player_ship.reversing: self.player_ship.reverse_direction() elif self.player_ship.turning_left: self.player_ship.turn_left() elif self.player_ship.turning_right: self.player_ship.turn_right() if __name__ == '__main__': # libtcod setup libtcod.sys_set_fps(60) libtcod.sys_set_renderer(libtcod.RENDERER_GLSL) # libtcod.sys_set_renderer(libtcod.RENDERER_OPENGL) # libtcod.sys_set_renderer(libtcod.RENDERER_SDL) libtcod.console_set_keyboard_repeat(1, 10) # libtcod.console_set_custom_font('fonts/8x8_limited.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, nb_char_horiz=16, nb_char_vertic=16) # game = Game(160, 90) # libtcod.console_set_custom_font('fonts/10x10_limited.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, nb_char_horiz=16, nb_char_vertic=16) # game = Game(128, 72) libtcod.console_set_custom_font('fonts/12x12_limited.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, nb_char_horiz=16, nb_char_vertic=16) game = Game(106, 60) game.main_loop()
def menu(header, options, width, Game, letterdelim=None): if len(options) > data.MAX_NUM_ITEMS: message( 'Cannot have a menu with more than ' + str(data.MAX_NUM_ITEMS) + ' options.', Game) #calculate total height of the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(Game.con, 0, 0, width, data.SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create 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_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height letter_index = ord('a') for obj in options: text = obj.text color = obj.color char = obj.char if color is None: color = libtcod.white if char is None: char = '' if letterdelim is None: letterchar = '' else: letterchar = chr(letter_index) + letterdelim libtcod.console_set_default_foreground(window, color) libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, letterchar + ' ' + char + ' ' + text) y += 1 letter_index += 1 #blit contents of window to root console x = data.SCREEN_WIDTH / 2 - width / 2 y = data.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 keypress libtcod.console_flush() libtcod.console_set_keyboard_repeat(0, 0) #turn off key repeat goodchoice = False while not goodchoice: key = libtcod.console_wait_for_keypress(True) if key.pressed == False: continue if key.vk == libtcod.KEY_ENTER and key.lalt: # full screen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #convert ASCII code to an index. if it's valid, return it index = key.c - ord('a') if index >= 0 and index < len(options): goodchoice = True retval = index elif key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_SPACE: goodchoice = True retval = None libtcod.console_set_keyboard_repeat(data.KEYS_INITIAL_DELAY, data.KEYS_INTERVAL) return retval
MAIN_MENU_HEIGHT = 10 MAIN_MENU_X = (constant.SCREEN_WIDTH / 2) - (MAIN_MENU_WIDTH / 2) # Co-ordinates to draw main menu at MAIN_MENU_Y = (constant.SCREEN_HEIGHT / 2) - (MAIN_MENU_HEIGHT / 2) tcod.console_set_custom_font(os.path.join('fonts', 'terminal8x8_aa_ro.png'), tcod.FONT_LAYOUT_ASCII_INROW) tcod.console_init_root(constant.SCREEN_WIDTH, constant.SCREEN_HEIGHT, 'S.T.A.L.K.E.R RL', False) # Console for any temporary UI elements (inventory, equipment, etc) ui_con = tcod.console_new(constant.SCREEN_WIDTH, constant.SCREEN_HEIGHT) # Main console that the map and constant UI elements are rendered to game_con = tcod.console_new(constant.SCREEN_WIDTH, constant.SCREEN_HEIGHT) tcod.sys_set_fps(constant.FPS_CAP) tcod.console_set_keyboard_repeat(10, 50) tcod.mouse_show_cursor(False) tcod.console_credits() img = tcod.image_load(os.path.join('images', 'menu_background.png')) tcod.image_blit_2x(img, game_con, 0, 0) tcod.console_blit(game_con, 0, 0, 0, 0, 0, 0, 0) tcod.console_flush() main_menu_index = 0 while not tcod.console_is_window_closed(): tcod.image_blit_2x(img, game_con, 0, 0) tcod.console_blit(game_con, 0, 0, 0, 0, 0, 0, 0) tcod.console_clear(ui_con) ui.draw_menu(ui_con, "S.T.A.L.K.E.R. RL", ['New Game', 'Load Game', 'Highscores', 'Exit'],
_font = sys.argv[sys.argv.index('-font')+1] libtcod.console_set_custom_font(_font, libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) except: logging.error('Failed to load font!') sys.exit() else: var.window_size = (80,50) libtcod.console_set_custom_font(os.path.join('data','terminal16x16_aa_tc.png'), libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(var.window_size[0], var.window_size[1], 'Rogue Caves - %s' % __version__, False) var.view = libtcod.console_new(var.window_size[0], var.window_size[1]-6) var.tree = libtcod.console_new(var.window_size[0], var.window_size[1]-6) var.splatter = libtcod.console_new(var.window_size[0], var.window_size[1]-6) libtcod.console_set_key_color(var.splatter,libtcod.Color(0,0,0)) libtcod.console_set_key_color(var.tree,libtcod.Color(0,0,0)) var.log = libtcod.console_new(var.window_size[0], 6) libtcod.console_set_keyboard_repeat(100,1) libtcod.sys_set_fps(var.max_fps) _logofile = open(os.path.join('data','logo.txt'),'r') _y=18 for line in _logofile.readlines(): _i = 1 for char in line: if char == '\n': continue libtcod.console_set_char_foreground(0,_i,_y,libtcod.Color(_y*6,_y*6,_y*6)) #libtcod.console_print(0, _i, _y, char) libtcod.console_set_char(0, _i, _y, char) _i+=1 _y+=1 libtcod.console_print(0,(var.window_size[0]/2)-(len(__version__)/2), 28,