def wait(timeout=None, flush=True): """Wait for an event. @type timeout: int or None @param timeout: The time in seconds that this function will wait before giving up and returning None. With the default value of None, this will block forever. @type flush: boolean @param flush: If True a call to L{tdl.flush} will be made before listening for events. @rtype: L{Event} or None @return: Returns an instance derived from L{Event}, or None if the function has timed out. Anything added via L{push} will also be returned. @since: 1.4.0 """ if timeout is not None: timeout = timeout + _time.clock() # timeout at this time while True: if _eventQueue: return _eventQueue.pop(0) if flush: # a full 'round' of events need to be processed before flushing _tdl.flush() if timeout and _time.clock() >= timeout: return None # return None on timeout _time.sleep(0.001) # sleep 1ms _processEvents()
def promptFolderName(escapable=False): #width, height = 26, 3 #window = tdl.Console(width, height) quit = False letters = [] while not (quit or tdl.event.is_window_closed()): root.clear() #update() text = '_' name = '' for letter in letters: name += letter if len(name) < 24: text = name + '_' else: text = name root.draw_str(80, 62, text) #root.blit(window, 57, 39, width, height) tdl.flush() print(name) userInput = tdl.event.key_wait() if userInput.keychar in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890": if len(letters) < 24: letters.append(userInput.keychar) elif userInput.keychar.upper() == 'BACKSPACE' and len(letters) > 0: letters.pop() elif userInput.keychar.upper() == 'ENTER': if name == '': pass else: return name elif userInput.keychar.upper() == 'ESCAPE' and escapable: return
def Update(toClear = False): if toClear: mainConsole.clear() print("Main clear") message = "Key Pressed = " + curKey mainConsole.draw_str(MID_WIDTH - len(message)//2, MID_HEIGHT, message, fg = colors.green) #mainConsole.blit(shiftConsole, 0 ,3, width = WIDTH, height = 1) tdl.flush()
def update(showDark=False): root.clear() root.draw_str(2, 62, '1: turn into wall', fg=colors.yellow) root.draw_str(2, 64, '2: turn into ground', fg=colors.yellow) root.draw_str(2, 66, '3: toggle block status', fg=colors.yellow) root.draw_str(2, 68, '4: toggle block LOS status', fg=colors.yellow) root.draw_str(2, 70, '5: toggle exploration status', fg=colors.yellow) root.draw_str(2, 72, '6: explore all', fg=colors.yellow) root.draw_str(2, 74, '7: explore None', fg=colors.yellow) root.draw_str(2, 76, '9: toggle lit/dark', fg=colors.yellow) root.draw_str(34, 62, 'c: copy tile', fg=colors.yellow) root.draw_str(34, 64, 'v: paste tile', fg=colors.yellow) root.draw_str(34, 66, 'w: turn all into wall', fg=colors.yellow) root.draw_str(34, 68, 'g: turn all into ground', fg=colors.yellow) root.draw_str(34, 70, 'r: load an existing map folder', fg=colors.yellow) root.draw_str(34, 72, 's: select a square', fg=colors.yellow) root.draw_str(70, 62, 'n: create or modify an NPC', fg=colors.yellow) root.draw_str(70, 64, 'd: delete an NPC', fg=colors.yellow) root.draw_str(70, 66, 'l: copy an NPC', fg=colors.yellow) root.draw_str(70, 68, 'm: paste an NPC', fg=colors.yellow) for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): if myMap[x][y] in selectedTiles and not showDark: root.draw_char(x, y, myMap[x][y].character, myMap[x][y].fg, colors.dark_violet) elif myMap[x][y] in selectedTiles and showDark: root.draw_char(x, y, myMap[x][y].character, myMap[x][y].dark_fg, colors.dark_violet) elif not showDark: root.draw_char(x, y, myMap[x][y].character, myMap[x][y].fg, myMap[x][y].bg) else: root.draw_char(x, y, myMap[x][y].character, myMap[x][y].dark_fg, myMap[x][y].dark_bg) for object in objects: if myMap[object.x][object.y] in selectedTiles and not showDark: root.draw_char(object.x, object.y, object.char, object.color, colors.dark_violet) elif myMap[object.x][object.y] in selectedTiles and showDark: root.draw_char(object.x, object.y, object.char, object.color, colors.dark_violet) elif not showDark: root.draw_char(object.x, object.y, object.char, object.color, myMap[object.x][object.y].bg) else: root.draw_char(object.x, object.y, object.char, object.color, myMap[object.x][object.y].dark_bg) tdl.flush()
def update(mapToUse=myMap): root.clear() for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): try: if mapToUse[x][y].blocked: root.draw_char(x, y, '#', colors.grey, colors.darker_grey) elif mapToUse[x][y].door: root.draw_char(x, y, '+', colors.darker_orange, colors.sepia) else: root.draw_char(x, y, None, bg=colors.sepia) except IndexError: print('___PROBLEM___:', x, y) tdl.flush()
def update(): root.clear() for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): if myMap[x][y].blocked and not (x, y) in roomEdges and not ( x, y) in tunnelEdges: root.draw_char(x, y, '#', colors.grey, colors.darker_grey) elif myMap[x][y].blocked and (x, y) in roomEdges: root.draw_char(x, y, '#', colors.dark_sepia, colors.darkest_sepia) elif myMap[x][y].blocked and (x, y) in tunnelEdges: root.draw_char(x, y, chr(254), colors.darkest_sepia, colors.sepia) else: root.draw_char(x, y, None, bg=colors.sepia) tdl.flush()
def update(mapToUse): root.clear() try: for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): root.draw_char(x, y, char = mapToUse[x][y].character, fg = mapToUse[x][y].fg, bg = mapToUse[x][y].bg) if (x, y) == (firstX, firstY): root.draw_char(x, y, char = 'X', fg = colors.red, bg = mapToUse[x][y].bg) if (x, y) == (lastX, lastY): root.draw_char(x, y, char = 'X', fg = colors.green, bg = mapToUse[x][y].bg) except IndexError: traceback.print_exc() os._exit(-1) tdl.flush()
def createPassage(roomA, roomB, tileA, tileB): ''' if not roomB in roomA.connectedRooms: #roomA.connectedRooms.append(roomB) linkRooms(roomA, roomB) if not roomA in roomB.connectedRooms: #roomB.connectedRooms.append(roomA) linkRooms(roomB, roomA) ''' linkRooms(roomA, roomB) (xA, yA) = tileA (xB, yB) = tileB passage = tdl.map.bresenham(xA, yA, xB, yB) for (x, y) in passage: openTile(x, y, myMap) myMap[x][y].usedForPassage = True otherTileA = findNeighbours(xA, yA) otherTileB = findNeighbours(xB, yB) bugged = False try: (xOA, yOA) = otherTileA except TypeError: traceback.print_exc() root.draw_char(xB, yB, char="X", fg=colors.green) tdl.flush() bugged = True try: (xOB, yOB) = otherTileB except TypeError: traceback.print_exc() root.draw_char(xB, yB, char="X", fg=colors.green) tdl.flush() bugged = True if not bugged: otherPassage = tdl.map.bresenham(xOA, yOA, xOB, yOB) for (x, y) in otherPassage: openTile(x, y, myMap) myMap[x][y].usedForPassage = True
def update(mapToUse=myMap): root.clear() for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): tile = mapToUse[x][y] try: root.draw_char(x, y, tile.character, tile.fg, tile.bg) ''' if myMap[x][y].blocked and not (x, y) in roomEdges and not (x, y) in tunnelEdges: root.draw_char(x, y, '#', colors.grey, colors.darker_grey) elif myMap[x][y].blocked and (x, y) in roomEdges: root.draw_char(x, y, '#', colors.dark_sepia, colors.darkest_sepia) elif myMap[x][y].blocked and (x, y) in tunnelEdges: root.draw_char(x, y, chr(254), colors.darkest_sepia, colors.sepia) elif mapToUse[x][y].chasm: root.draw_char(x, y, None, bg = (16, 16, 16)) elif mapToUse[x][y].door: root.draw_char(x, y, '+', colors.darker_orange, colors.sepia) else: root.draw_char(x, y, None, bg = colors.sepia) ''' except IndexError: print('___PROBLEM___:', x, y) tdl.flush()
def update(mapToUse): root.clear() try: for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): if mapToUse[x][y].blocked: root.draw_char(x, y, char='#', fg=colors.lighter_gray) if visuEdges and (x, y) in visuEdges and (state != 'normal' or dispDebug): root.draw_char(x, y, char='#', fg=colors.purple) if confTiles and (x, y) in confTiles and (state != 'normal' or dispDebug): root.draw_char(x, y, char='#', fg=colors.red) else: root.draw_char(x, y, char=None, bg=colors.green) if visuTiles and (x, y) in visuTiles and (state != 'normal' or dispDebug): root.draw_char(x, y, char=None, bg=colors.red) if visuEdges and (x, y) in visuEdges and (state != 'normal' or dispDebug): root.draw_char(x, y, char=None, bg=colors.purple) if dispEmpty: for (x, y) in emptyTiles: root.draw_char(x, y, char=".", bg=colors.cyan) print(len(emptyTiles)) if state in ("floodfillPrep", "edgeDetectionPrep", "roomMergePrep"): drawCentered(root, 70, "Ready to continue, press a key to proceed...", fg=colors.green) elif state == "floodfill": drawCentered(root, 70, "Filling...", fg=colors.green) elif state == "normal": drawCentered( root, 70, "Done ! Press ENTER to restart or SPACE to toggle between map and canvas. ", fg=colors.green) try: for (x, y) in rooms[curRoomIndex].tiles: root.draw_char(x, y, None, bg=colors.yellow) except IndexError: print("Rooms length : {}".format(len(rooms))) print("CurRoomIndex : {}".format(curRoomIndex)) print("X, Y : {} ; {}".format(x, y)) try: for (x, y) in rooms[curRoomIndex].borders: root.draw_char(x, y, "#", fg=colors.orange) except IndexError: print("Rooms length : {}".format(len(rooms))) print("CurRoomIndex : {}".format(curRoomIndex)) print("X, Y : {} ; {}".format(x, y)) elif state == "base": drawCentered(root, 70, "Loading...", fg=colors.gray) except IndexError: traceback.print_exc() #pdb.set_trace() os._exit(-1) tdl.flush()
def openDetails(x, y): tile = myMap[x][y] width, height = 36, 19 window = tdl.Console(width, height) quit = False index = 0 ascii = ord(tile.character) fg = tile.fg dark_fg = tile.dark_fg bg = tile.bg dark_bg = tile.dark_bg blocked = tile.blocked block_sight = tile.block_sight explored = tile.explored baseIndex = range(-1, 7) Red, Green, Blue = 0, 0, 0 while not quit: window.clear() if index == 0 or index == 7: window.draw_str(1, 1, 'Character:', colors.black, colors.green) else: window.draw_str(1, 1, 'Character:', colors.green) window.draw_char(12, 1, chr(ascii)) window.draw_str(14, 1, str(ascii)) if index == 1 or index in range(8, 11): window.draw_str(1, 3, 'FG (lit):', colors.black, colors.green) else: window.draw_str(1, 3, 'FG (lit):', colors.green) if index in range(8, 11): window.draw_str( 12, 3, '({}, {}, {})'.format(str(Red), str(Green), str(Blue))) window.draw_str(30, 3, ' ', fg=None, bg=(Red, Green, Blue)) else: R, G, B = fg window.draw_str(12, 3, '({}, {}, {})'.format(str(R), str(G), str(B))) window.draw_str(30, 3, ' ', fg=None, bg=(R, G, B)) if index == 2 or index in range(11, 14): window.draw_str(1, 5, 'FG (dark):', colors.black, colors.green) else: window.draw_str(1, 5, 'FG (dark):', colors.green) if index in range(11, 14): window.draw_str( 12, 5, '({}, {}, {})'.format(str(Red), str(Green), str(Blue))) window.draw_str(30, 5, ' ', fg=None, bg=(Red, Green, Blue)) else: R, G, B = dark_fg window.draw_str(12, 5, '({}, {}, {})'.format(str(R), str(G), str(B))) window.draw_str(30, 5, ' ', fg=None, bg=(R, G, B)) if index == 3 or index in range(14, 17): window.draw_str(1, 7, 'BG (lit):', colors.black, colors.green) else: window.draw_str(1, 7, 'BG (lit):', colors.green) if index in range(14, 17): window.draw_str( 12, 7, '({}, {}, {})'.format(str(Red), str(Green), str(Blue))) window.draw_str(30, 7, ' ', fg=None, bg=(Red, Green, Blue)) else: R, G, B = bg window.draw_str(12, 7, '({}, {}, {})'.format(str(R), str(G), str(B))) window.draw_str(30, 7, ' ', fg=None, bg=(R, G, B)) if index == 4 or index in range(17, 20): window.draw_str(1, 9, 'BG (dark):', colors.black, colors.green) else: window.draw_str(1, 9, 'BG (dark):', colors.green) if index in range(17, 20): window.draw_str( 12, 9, '({}, {}, {})'.format(str(Red), str(Green), str(Blue))) window.draw_str(30, 9, ' ', fg=None, bg=(Red, Green, Blue)) else: R, G, B = dark_bg window.draw_str(12, 9, '({}, {}, {})'.format(str(R), str(G), str(B))) window.draw_str(30, 9, ' ', fg=None, bg=(R, G, B)) if index == 5 or index == 20: window.draw_str(1, 11, 'Blocked:', colors.black, colors.green) else: window.draw_str(1, 11, 'Blocked:', colors.green) window.draw_str(12, 11, str(blocked)) if index == 6 or index == 21: window.draw_str(1, 13, 'Block LOS:', colors.black, colors.green) else: window.draw_str(1, 13, 'Block LOS:', colors.green) window.draw_str(12, 13, str(block_sight)) if index == -1 or index == 22: window.draw_str(1, 15, 'Explored', colors.black, colors.green) else: window.draw_str(1, 15, 'Explored', colors.green) window.draw_str(12, 15, str(explored)) window.draw_str(1, 17, 'x: {}, y: {}'.format(x, y)) root.blit(window, 65, 24, width, height) tdl.flush() userInput = tdl.event.key_wait() if userInput.keychar.upper() == 'ESCAPE': if index in baseIndex: quit = True elif index == 7: tile.character = chr(ascii) index = 0 elif index in range(8, 11): index = 1 fg = Red, Green, Blue tile.fg = fg elif index in range(11, 14): index = 2 dark_fg = Red, Green, Blue tile.dark_fg = dark_fg elif index in range(14, 17): index = 3 bg = Red, Green, Blue tile.bg = bg elif index in range(17, 20): index = 4 dark_bg = Red, Green, Blue tile.dark_bg = dark_bg elif index == 20: tile.blocked = blocked index = 5 elif index == 21: tile.block_sight = block_sight index = 6 elif index == 22: tile.explored = explored index = -1 elif userInput.keychar.upper() == 'UP': if index in baseIndex: index -= 1 if index < -1: index = 6 elif index == 7: ascii += 1 if ascii > 255: ascii = 0 elif index == 8 or index == 11 or index == 14 or index == 17: #W.H.Y ? Red += 1 if Red > 255: Red = 0 elif index == 9 or index == 12 or index == 15 or index == 18: Green += 1 if Green > 255: Green = 0 elif index == 10 or index == 13 or index == 16 or index == 19: Blue += 1 if Blue > 255: Blue = 0 elif index == 20: blocked = not blocked elif index == 21: block_sight = not block_sight elif index == 22: explored = not explored elif userInput.keychar.upper() == 'DOWN': if index in baseIndex: index += 1 if index > 6: index = -1 elif index == 7: ascii -= 1 if ascii < 0: ascii = 255 elif index == 8 or index == 11 or index == 14 or index == 17: Red -= 1 if Red < 0: Red = 255 elif index == 9 or index == 12 or index == 15 or index == 18: Green -= 1 if Green < 0: Green = 255 elif index == 10 or index == 13 or index == 16 or index == 19: Blue -= 1 if Blue < 0: Blue = 255 elif index == 20: blocked = not blocked elif index == 21: block_sight = not block_sight elif index == 22: explored = not explored elif userInput.keychar.upper() == 'RIGHT': if index in range(8, 11): index += 1 if index > 10: index = 8 if index in range(11, 14): index += 1 if index > 13: index = 11 if index in range(14, 17): index += 1 if index > 16: index = 14 if index in range(17, 20): index += 1 if index > 19: index = 17 elif userInput.keychar.upper() == 'LEFT': if index in range(8, 11): index -= 1 if index < 8: index = 10 if index in range(11, 14): index -= 1 if index < 11: index = 13 if index in range(14, 17): index -= 1 if index < 14: index = 16 if index in range(17, 20): index -= 1 if index < 17: index = 19 elif userInput.keychar.upper() == 'ENTER': if index == 0: index = 7 elif index == 1: index = 8 Red, Green, Blue = fg elif index == 2: index = 11 Red, Green, Blue = dark_fg elif index == 3: index = 14 Red, Green, Blue = bg elif index == 4: index = 17 Red, Green, Blue = dark_bg elif index == 5: index = 20 elif index == 6: index = 21 elif index == -1: index = 22
def createNPC(x, y): global objects found = False for object in objects: if object.x == x and object.y == y: npc = object found = True if not found: npc = Object(x, y) width, height = 36, 17 window = tdl.Console(width, height) quit = False index = 0 ascii = ord(npc.char) name = npc.name color = npc.color dialog = npc.dialog shop = npc.shop baseIndex = range(-1, 4) Red, Green, Blue = 0, 0, 0 while not quit: window.clear() if index == 0 or index == 4: window.draw_str(1, 1, 'Character:', colors.black, colors.green) else: window.draw_str(1, 1, 'Character:', colors.green) window.draw_char(12, 1, chr(ascii)) if index == 1 or index == 5: window.draw_str(1, 3, 'Name:', colors.black, colors.green) else: window.draw_str(1, 3, 'Name:', colors.green) window.draw_str(12, 3, name) if index == 2 or index in range(6, 9): window.draw_str(1, 5, 'FG:', colors.black, colors.green) else: window.draw_str(1, 5, 'FG:', colors.green) if index in range(6, 9): window.draw_str( 12, 5, '({}, {}, {})'.format(str(Red), str(Green), str(Blue))) window.draw_str(30, 5, ' ', fg=None, bg=(Red, Green, Blue)) else: R, G, B = color window.draw_str(12, 5, '({}, {}, {})'.format(str(R), str(G), str(B))) window.draw_str(30, 5, ' ', fg=None, bg=(R, G, B)) if index == 3 or index == 9: window.draw_str(1, 7, 'Dialog:', colors.black, colors.green) else: window.draw_str(1, 7, 'Dialog:', colors.green) window.draw_str(12, 7, dialog) if index == -1 or index == 10: window.draw_str(1, 9, 'Shop:', colors.black, colors.green) else: window.draw_str(1, 9, 'Shop:', colors.green) window.draw_str(12, 9, shop) root.blit(window, 65, 24, width, height) tdl.flush() userInput = tdl.event.key_wait() if userInput.keychar.upper() == 'ESCAPE': if index in baseIndex: quit = True elif index == 4: npc.char = chr(ascii) index = 0 elif index == 5: npc.name = name index = 1 elif index in range(6, 9): index = 2 color = Red, Green, Blue npc.color = color elif index == 9: npc.dialog = dialog index = 3 elif index == 10: npc.shop = shop index = -1 elif userInput.keychar.upper() == 'UP': if index in baseIndex: index -= 1 if index < -1: index = 3 elif index == 4: ascii += 1 if ascii > 255: ascii = 0 elif index == 6: Red += 1 if Red > 255: Red = 0 elif index == 7: Green += 1 if Green > 255: Green = 0 elif index == 8: Blue += 1 if Blue > 255: Blue = 0 elif userInput.keychar.upper() == 'DOWN': if index in baseIndex: index += 1 if index > 3: index = -1 elif index == 4: ascii -= 1 if ascii < 0: ascii = 255 elif index == 6: Red -= 1 if Red < 0: Red = 255 elif index == 7: Green -= 1 if Green < 0: Green = 255 elif index == 8: Blue -= 1 if Blue < 0: Blue = 255 elif userInput.keychar.upper() == 'RIGHT': if index in range(6, 9): index += 1 if index > 8: index = 6 elif userInput.keychar.upper() == 'LEFT': if index in range(6, 9): index -= 1 if index < 6: index = 8 elif userInput.keychar.upper() == 'ENTER': if index == 0: index = 4 elif index == 1: index = 5 elif index == 2: index = 6 Red, Green, Blue = color elif index == 3: index = 9 elif index == -1: index = 10 elif userInput.keychar in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890': if index == 5: name += userInput.keychar elif index == 9: dialog += userInput.keychar elif index == 10: shop += userInput.keychar elif userInput.keychar.upper() == 'SPACE': if index == 5: name += ' ' elif index == 9: dialog += ' ' elif index == 10: shop += ' ' elif userInput.keychar.upper() == 'BACKSPACE': if index == 5 and name != '': nameList = list(name) nameList.pop() name = '' for letter in nameList: name += letter if index == 9 and dialog != '': dialogList = list(dialog) dialogList.pop() dialog = '' for letter in dialogList: dialog += letter if index == 10 and shop != '': shopList = list(shop) shopList.pop() shop = '' for letter in shopList: shop += letter objects.append(npc)
foundBlocked = True break else: foundBlocked = True mapToUse[x][y].clearance = clearance return mapToUse makeMap() ''' squareComp = Square(10, 10, 3) fatherSquare = SquareObject(squareComp=squareComp, generateSmaller=True) print(squareComp) print(fatherSquare.Square) print(fatherSquare.smallerSquareObject.Square) fatherSquare.Square.s += 3 print(fatherSquare.Square) print(fatherSquare.smallerSquareObject.Square) ''' while not tdl.event.is_window_closed(): root.clear() for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): char = myMap[x][y].clearance if char >= 10: char = '+' else: char = str(char) root.draw_str(x, y, char, myMap[x][y].fg, myMap[x][y].bg) tdl.flush()