def memoBedroomUse(stdscr, item, location, inv): if item == playerItem: stdscr.addstr(0, 0, "You mess around with the note. It has some writing on it. If you looked at the note, you might be able to read it.", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr)
def hedgeclippersUse(stdscr, item, location, inv): if item == playerItem: stdscr.addstr(0, 0, "They look sharp. It's probably best not to do that.", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr)
def pennyUse(stdscr, item, location, inv): if item == playerItem: stdscr.addstr(0, 0, "You flip the penny. It comes up " + random.choice(["heads", "tails"]) + ".", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr)
def drawBoxPopup(screen, text, xsize=40): '''Draws a pop-up box.''' screen.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) ypos = 1 for option in options: screen.addstr(ypos, 0, option + " " * (xsize - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) screen.addstr(ypos, xsize, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 screen.addstr(ypos, 0, "-" * xsize, unicurses.color_pair(0) | unicurses.A_BOLD)
def initattrs(self): unicurses.init_pair(1, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) unicurses.init_pair(3, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) unicurses.init_pair(4, unicurses.COLOR_RED, unicurses.COLOR_BLACK) self.attr = {} self.attr['ttl'] = unicurses.A_BOLD self.attr['key'] = unicurses.A_BOLD | unicurses.color_pair(1) self.attr['val'] = unicurses.A_BOLD | unicurses.color_pair(2) self.attr['slp'] = unicurses.A_BOLD | unicurses.color_pair(3) self.attr['err'] = unicurses.A_BOLD | unicurses.color_pair(4)
def pennyUse(stdscr, item, location, inv): if item == playerItem: stdscr.addstr( 0, 0, "You flip the penny. It comes up " + random.choice(["heads", "tails"]) + ".", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr)
def memoBedroomUse(stdscr, item, location, inv): if item == playerItem: stdscr.addstr( 0, 0, "You mess around with the note. It has some writing on it. If you looked at the note, you might be able to read it.", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr)
def drawBoxMenu(screen, options, xsize=40): '''Draws a menu of options.''' screen.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) ypos = 1 numCounter = 1 for option in options: option = "(" + str(numCounter) + ") " + option screen.addstr(ypos, 0, option + " " * (xsize - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) screen.addstr(ypos, xsize, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 numCounter += 1 screen.addstr(ypos, 0, "-" * xsize, unicurses.color_pair(0) | unicurses.A_BOLD) return ypos
def pennyUse(stdscr, item, location, inv): if item == playerItem: # The coin actually flips :o stdscr.addstr(0, 0, "You flip the penny. It comes up " + random.choice(["heads", "tails"]) + ".", unicurses.color_pair(0) | unicurses.A_BOLD) else: stdscr.addstr(0, 0, "That doesn't seem like it will do anything.", unicurses.color_pair(0) | unicurses.A_BOLD) nextMenu(stdscr)
def create_color(cls, text_color: int, background_color: int) -> int: """Creates and returns a Color Combination of text color and background color.""" global _COLOR_COUNTER unicurses.init_pair(_COLOR_COUNTER, text_color, background_color) color = unicurses.color_pair(_COLOR_COUNTER) _COLOR_COUNTER += 1 return color
def color(fg="w"): fg = fg.lower() colors = ["n","k","db","dg","dc","dr","dp","dy","dw","dn","b","g","c","r","p","y","w"] if fg not in colors: fg = "w" i = colors.index(fg) if fg == "w": return curses.color_pair(0) #log.log("Couleur : "+str(i)) return curses.color_pair(i)
def set_colors(self, foreground, background): self.color = make_color(foreground, background) self.foreground = foreground self.background = background curses.waddstr(self.window, self.body, curses.color_pair(self.color) + self.attribute) self.show_changes()
def create_color_and_id(cls, text_color: int, background_color: int) -> _Tuple[int, int]: """Creates and returns the color and id of a Color Combination of text color and background color.""" global _COLOR_COUNTER unicurses.init_pair(_COLOR_COUNTER, text_color, background_color) color_id = _COLOR_COUNTER color = unicurses.color_pair(color_id) _COLOR_COUNTER += 1 return color, color_id
def display_text(self, text, username='******'): y, x = self.displayWindow.getyx() if (y == self.height - 5): self.clear_display_text() y, x = self.displayWindow.getyx() self.displayWindow.move(y + 1, 1) self.displayWindow.addstr("{}:".format(username), unicurses.color_pair(1)) self.displayWindow.addstr(text) self.displayWindow.refresh()
def display_info(self, text, type="info"): self.clear_info_window() y, x = self.infoWindow.getyx() if (y == self.height - 5): self.init_info_screen() y, x = self.infoWindow.getyx() self.infoWindow.move(y + 1, 1) self.infoWindow.addstr("{}:".format(type), unicurses.color_pair(1)) self.infoWindow.addstr(text) self.infoWindow.refresh()
def main(): ## Curses normal init sequence stdscr = curses.initscr() curses.noecho() # no echo, but we still see the cursor curses.curs_set(False) #turns off the cursor drawing stdscr.keypad(True) # allows special keys and arrow keys try: curses.start_color() curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_GREEN) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_GREEN) dude = curses.newwin(1, 1, 10, 30) curses.waddstr(dude, "@", curses.color_pair(2) + curses.A_BOLD) dude_panel = curses.new_panel(dude) grass = curses.newwin(10, 50, 5, 5) grass.bkgd(" ", curses.color_pair(1)) grass_panel = curses.new_panel(grass) curses.top_panel(dude_panel) curses.update_panels() curses.doupdate() while True: key = curses.getch() if key == 27: break curses.update_panels() curses.doupdate() except Exception as e: stdscr.addstr(0, 0, str(e)) stdscr.getch() finally: curses.endwin() return 0
def main(): if not curses.has_colors(): print("Your terminal emulator needs to have colors.") return 0 ## Curses normal init sequence stdscr = curses.initscr() curses.noecho() # no echo, but we still see the cursor curses.curs_set(False) #turns off the cursor drawing stdscr.keypad(True) # allows special keys and arrow keys try: curses.start_color() avatar = Player(stdscr, "@", curses.COLOR_RED, curses.COLOR_BLACK, curses.A_BOLD) curses.attron(curses.color_pair(1)) curses.vline("|", 10) curses.hline("-", 10) curses.attroff(curses.color_pair(1)) while True: key = curses.getch() avatar.move(key) if key == 27: break curses.update_panels() curses.doupdate() except Exception as e: stdscr.addstr(0, 0, str(e)) stdscr.getch() finally: curses.endwin() return 0
def main(): ## Curses normal init sequence stdscr = curses.initscr() try: curses.start_color() curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_WHITE) stdscr.addstr("Hello World!", curses.color_pair(1)) stdscr.addstr("\nHello World!", curses.color_pair(2) + curses.A_BLINK) stdscr.addstr("\nHello World!", curses.color_pair(1) + curses.A_REVERSE) stdscr.getch() except Exception as e: stdscr.addstr(0, 0, str(e)) stdscr.getch() finally: curses.endwin() return 0
def initattrs(self): unicurses.init_pair(1, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) unicurses.init_pair(3, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) unicurses.init_pair(4, unicurses.COLOR_RED, unicurses.COLOR_BLACK) unicurses.init_pair(5, unicurses.COLOR_WHITE, unicurses.COLOR_CYAN) unicurses.init_pair(6, unicurses.COLOR_WHITE, unicurses.COLOR_BLUE) self.attr = {} self.attr['ttl'] = unicurses.A_BOLD self.attr['key'] = unicurses.A_BOLD | unicurses.color_pair(1) self.attr['val'] = unicurses.A_BOLD | unicurses.color_pair(2) self.attr['slp'] = unicurses.A_BOLD | unicurses.color_pair(3) self.attr['err'] = unicurses.A_BOLD | unicurses.color_pair(4) self.attr['pfg'] = unicurses.A_BOLD | unicurses.color_pair(5) self.attr['pbg'] = unicurses.A_BOLD | unicurses.color_pair(6)
def create_attribute(self, reverse=False, underline=False, bold=False, color=0, alt_background=False, highlight=False): """ Return the attribute corresponding to the given properties. """ result = 0 if bold: result |= curses.A_BOLD if underline: result |= curses.A_UNDERLINE if reverse: result |= curses.A_REVERSE if not terminal.HAS_COLORS: if alt_background: result ^= curses.A_REVERSE if highlight: result ^= curses.A_REVERSE else: colorpair = color % (terminal.COLOR_PAIRS + 1) if alt_background: if terminal.HAS_BACKGROUND_COLORS: colorpair = color + terminal.COLOR_PAIRS + 1 else: result ^= curses.A_REVERSE if highlight: if terminal.HAS_BACKGROUND_COLORS: colorpair = color + terminal.COLOR_PAIRS + terminal.COLOR_PAIRS + 1 else: result ^= curses.A_REVERSE result |= curses.color_pair(colorpair) return result
import sys import time, random import unicurses as curses screen = curses.initscr() screen.nodelay(1) screen.border() curses.noecho() curses.curs_set(0) dims = screen.getmaxyx() height,width = dims[0]-1, dims[1]-1 curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) row,col= 0,0 for i in range(256): screen.addch(row,col,i,curses.color_pair(0) ) col +=1 if col>75: row +=1 col = 1 screen.refresh() time.sleep(0.051) curses.endwin()
def gameLoop(stdscr): global locations, saveGame, loadGame stdscr.clear() stdscr.refresh() if not args.nointro: stdscr.addstr(0, 0, 'Tymeventure', unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, "Version {}".format(version), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to begin --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) if not args.name: # If we didn't set a name already, prompt the user now stdscr.addstr(0, 0, "May I ask what your name is? (max 30 characters)", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, 'Name: ', unicurses.color_pair(0) | unicurses.A_BOLD) playerName = stdscr.getstr(1, 6, 30).decode('utf8') stdscr.clear() stdscr.refresh() else: playerName = args.name # Load the data from the player's save hasSave, saveData = loadGame(playerName) if hasSave: currentLocation = saveData[1] inventory = saveData[2] locations = saveData[3] else: currentLocation = yourBedroom inventory = list() if not args.nointro and not hasSave: stdscr.addstr(0, 0, "All right, {}, let's go...".format(playerName), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, "It's a sunny day outside and you wake up.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(3, 0, "\"I think there was something going on today, a big press conference...?\"", unicurses.color_pair(3) | unicurses.A_BOLD) stdscr.addstr(4, 0, "After getting dressed and having breakfast, you get ready to take on the day.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(5, 0, "\"Well, better get started.\"", unicurses.color_pair(3) | unicurses.A_BOLD) stdscr.addstr(7, 0, "-- Press any key to begin --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) continueGame = True while continueGame: topbar = playerName # May be expanded in the future stdscr.addstr(0, 0, topbar, unicurses.color_pair(0) | unicurses.A_BOLD) location = "Current Location: " + currentLocation.printName stdscr.addstr(1, 0, location, unicurses.color_pair(0) | unicurses.A_BOLD) description = currentLocation.desc stdscr.addstr(2, 0, description, unicurses.color_pair(0) | unicurses.A_BOLD) # Detect how many items are here if currentLocation.itemsHere == []: itemsHereBar = "There are no items here." elif len(currentLocation.itemsHere) == 1: itemsHereBar = "There is one item here." else: itemsHereBar = "There are " + str(len(currentLocation.itemsHere)) + " items here." stdscr.addstr(3, 0, itemsHereBar, unicurses.color_pair(0) | unicurses.A_BOLD) # Options stdscr.addstr(5, 0, "Save and (Q)uit", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(6, 0, "(M)ove", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(7, 0, "(T)hings Here", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(8, 0, "(I)nventory", unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr).lower() # Use nextMenu for nice, easy clearing if choice == "q": # Save and Quit saveGame(playerName, currentLocation, inventory, locations) continueGame = False elif choice == "m": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 for place in currentLocation.connections: label = "|(" + str(keyCounter) + ") " + place.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press the key next to where you want to move --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) if choice in "123456789": # Make sure it's a valid location number if int(choice) - 1 < len(currentLocation.connections): moveTo = currentLocation.connections[int(choice) - 1] for index, item in enumerate(locations): if item.printName == currentLocation.printName: locations[index] = currentLocation currentLocation = moveTo # Move us elif choice == "t": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if currentLocation.itemsHere == []: stdscr.addstr(ypos, 0, "|There is nothing here. |", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) else: for item in currentLocation.itemsHere: label = "|(" + str(keyCounter) + ") " + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press the key next to the item you want to use --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a valid item number if int(choice) - 1 < len(currentLocation.itemsHere): checkItem = True itemInQuestion = currentLocation.itemsHere[int(choice) - 1] else: checkItem = False if checkItem: drawBoxMenu(stdscr, ["Take Item"]) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": currentLocation.itemsHere.remove(itemInQuestion) inventory.append(itemInQuestion) elif choice == "i": ypos = 1 stdscr.addstr(ypos - 1, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if inventory == []: stdscr.addstr(ypos, 0, "|You have nothing in your inventory. |", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 2, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) else: itemNames = [i.printName for i in inventory] ypos = drawBoxMenu(stdscr, itemNames) stdscr.addstr(ypos, 0, "-- Press an item's key to do something with it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": if int(choice) - 1 < len(inventory): checkItem = True itemInQuestion = inventory[int(choice) - 1] else: checkItem = False if checkItem: drawBoxMenu(stdscr, ["Look At Item", "Drop Item", "Use Item"]) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": stdscr.addstr(0, 0, itemInQuestion.printName, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, itemInQuestion.desc, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to exit --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) elif choice == "2": currentLocation.itemsHere.append(itemInQuestion) inventory.remove(itemInQuestion) elif choice == "3": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) for item in inventory: if not item == itemInQuestion: label = "|(" + str(keyCounter) + ") " + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 keyCounter += 1 label = "|(0) Yourself" stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press an item's key to use it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a number, the game crashes otherwise if int(choice) - 1 < len(inventory): checkItem = True itemToUseWith = inventory[int(choice) - 1] else: checkItem = False elif choice == "0": # If it's the player being used... itemToUseWith = playerItem # ...then use the special player item. checkItem = True else: checkItem = False if checkItem: itemInQuestion.useWith(stdscr, itemToUseWith, currentLocation, inventory) else: pass else: pass
def get_color(cls, id: int) -> int: return unicurses.color_pair(id)
import sys import time, random import unicurses as curses screen = curses.initscr() screen.nodelay(1) screen.border() curses.noecho() curses.curs_set(0) dims = screen.getmaxyx() height, width = dims[0] - 1, dims[1] - 1 curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) row, col = 0, 0 for i in range(256): screen.addch(row, col, i, curses.color_pair(0)) col += 1 if col > 75: row += 1 col = 1 screen.refresh() time.sleep(0.051) curses.endwin()
if sys.platform == 'win32': # Windows: set codepage to 65001 for unicode support os.system('chcp 65001') # Settings broadcaster_names = { # Broadcaster names for the 'BCST' bar 'djprofessork': 'DJ Professor K', 'noisetanks': 'Noise Tanks', 'seaman': 'Seaman' } unicurses.init_pair(1, unicurses.COLOR_BLUE, unicurses.COLOR_BLACK) # default user color pair unicurses.init_pair(2, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) # registered user color pair unicurses.init_pair(3, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) # DJPK color pair default_color = unicurses.color_pair(1) | unicurses.A_BOLD # default user color registered_color = unicurses.color_pair(2) | unicurses.A_BOLD # registered user color djpk_color = unicurses.color_pair(3) | unicurses.A_BOLD # DJPK color login_text = open('./screens/login.txt', 'r').read() # login text loaded from file chat_text = open('./screens/chat.txt', 'r').read() # chat text loaded from file # Core functions and classes def write(line, x, y, effect=0): """ Function to write text to coordinates (x, y) with optional effect Args: line (str): Line to write to coordinates (x, y)
def gameLoop(stdscr): locations = world.locations stdscr.clear() stdscr.refresh() if not args.nointro: stdscr.addstr(0, 0, 'TYMEVENTURE', unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(0, 3, 'E', unicurses.color_pair(2) | unicurses.A_BOLD) versionBar = "You have version " + version + "." stdscr.addstr(1, 0, versionBar, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to advance --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) if not args.name: # If we didn't set a name already, prompt the user now stdscr.addstr(0, 0, "May I ask what your name is? (max 30 characters)", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, 'Name: ', unicurses.color_pair(0) | unicurses.A_BOLD) playerName = stdscr.getstr(1, 6, 30).decode('utf8') stdscr.clear() stdscr.refresh() else: playerName = args.name # Load the data from the player's save #allData = loadGame( playerName ) hasSave = False savename = "".join([playerName.rstrip().lstrip(), "_tymeventuresave"]) if os.path.exists("".join([os.getcwd(), "/", savename])): savefile_open = open("".join([os.getcwd(), "/", savename]), "rb") allData = pickle.load(savefile_open) hasSave = True if not hasSave: currentLocation = yourComputer inventory = list() else: currentLocation = allData[0] inventory = allData[1] locations = allData[2] if not args.nointro: adventureAnnounce = "OK " + playerName + ", get ready to play..." stdscr.addstr(0, 0, adventureAnnounce, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, "It's a sunny day outside and you wake up. Yawn.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(3, 0, "Once again, like every morning, you log onto the internet and check for new messages.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(4, 0, "A couple of forum posts, a new follower, a friend request. Slow day.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(5, 0, "But today feels... different.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(6, 0, "Something compels you to go outside today, as if you know something's about to happen.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(7, 0, "You decide to close the computer, and head outside, ready to explore the world...", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(8, 0, "-- Press any key to begin --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) continueGame = True while continueGame: topbar = playerName # May be expanded in the future stdscr.addstr(0, 0, topbar, unicurses.color_pair(0) | unicurses.A_BOLD) location = "Current Location: " + currentLocation.printName stdscr.addstr(1, 0, location, unicurses.color_pair(0) | unicurses.A_BOLD) description = currentLocation.desc stdscr.addstr(2, 0, description, unicurses.color_pair(0) | unicurses.A_BOLD) # Detect how many items are here if currentLocation.itemsHere == []: itemsHereBar = "There are no items here." elif len(currentLocation.itemsHere) == 1: itemsHereBar = "There is one item here." else: itemsHereBar = "There are " + str(len(currentLocation.itemsHere)) + " items here." stdscr.addstr(3, 0, itemsHereBar, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(4, 0, "(Q)uit", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(5, 0, "(M)ove", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(6, 0, "(T)hings here", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(7, 0, "(I)nventory", unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr).lower() # Case doesn't matter, and we clear anyway, so nextMenu is OK here if choice == "q": #saveGame( playerName, currentLocation, inventory, locations ) allData = [currentLocation, inventory, locations] # Clone locations so we can keep the positions of items # Temp file for safety placename = currentLocation.printName savename = "".join([playerName.rstrip().lstrip(), "_tymeventuresave"]) tmpname = "".join([playerName.rstrip().lstrip(), "_tymeventuretmp"]) savefile_out = open(tmpname, "wb") pickle.dump(allData, savefile_out) os.rename(tmpname, savename) continueGame = False elif choice == "m": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 for place in currentLocation.connections: label = "|(" + str(keyCounter) + ")" + place.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "Press the key next to where you want to move.", unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr) if choice in "123456789": # Make sure it's a number, the game crashes otherwise if int(choice) - 1 < len(currentLocation.connections): moveTo = currentLocation.connections[int(choice) - 1] for index, item in enumerate(locations): if item.printName == currentLocation.printName: locations[index] = currentLocation currentLocation = moveTo # Move us elif choice == "t": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if currentLocation.itemsHere == []: stdscr.addstr(ypos, 0, "|There is nothing here. |", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 else: for item in currentLocation.itemsHere: label = "|(" + str(keyCounter) + ")" + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a number, the game crashes otherwise if int(choice) - 1 < len(currentLocation.itemsHere): checkItem = True itemInQuestion = currentLocation.itemsHere[int(choice) - 1] else: checkItem = False if checkItem: stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) option = "(1)Take Item" stdscr.addstr(1, 0, option + " " * (40 - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" stdscr.addstr(2, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": currentLocation.itemsHere.remove(itemInQuestion) inventory.append(itemInQuestion) elif choice == "i": ypos = 1 stdscr.addstr(ypos - 1, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if inventory == []: stdscr.addstr(ypos, 0, "|You have nothing in your inventory. |", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 else: for item in inventory: label = "|(" + str(keyCounter) + ")" + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press an item's key to do something with it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": if int(choice) - 1 < len(inventory): checkItem = True itemInQuestion = inventory[int(choice) - 1] else: checkItem = False if checkItem: stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) option = "(1)Look At Item" stdscr.addstr(1, 0, option + " " * (40 - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) option = "(2)Drop Item" stdscr.addstr(2, 0, option + " " * (40 - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) option = "(3)Use Item" stdscr.addstr(3, 0, option + " " * (40 - len(option)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(3, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(4, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": stdscr.addstr(0, 0, itemInQuestion.printName, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, itemInQuestion.desc, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to exit --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) elif choice == "2": currentLocation.itemsHere.append(itemInQuestion) inventory.remove(itemInQuestion) elif choice == "3": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) for item in inventory: if not item == itemInQuestion: label = "|(" + str(keyCounter) + ")" + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 keyCounter += 1 label = "|(0) Yourself" stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press an item's key to use it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a number, the game crashes otherwise if int(choice) - 1 < len(inventory): checkItem = True itemToUseWith = inventory[int(choice) - 1] else: checkItem = False elif choice == "0": # If it's the player being used... itemToUseWith = playerItem # ...then use the special player item. checkItem = True else: checkItem = False if checkItem: itemInQuestion.useWith(stdscr, itemToUseWith, currentLocation, inventory) else: pass else: pass
def gameLoop(stdscr): global locations, saveGame, loadGame stdscr.clear() stdscr.refresh() if not args.nointro: stdscr.addstr(0, 0, 'Tymeventure', unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, "Version {}".format(version), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to begin --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) if not args.name: # If we didn't set a name already, prompt the user now stdscr.addstr(0, 0, "May I ask what your name is? (max 30 characters)", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, 'Name: ', unicurses.color_pair(0) | unicurses.A_BOLD) playerName = stdscr.getstr(1, 6, 30).decode('utf8') stdscr.clear() stdscr.refresh() else: playerName = args.name # Load the data from the player's save hasSave, saveData = loadGame(playerName) if hasSave: currentLocation = saveData[1] inventory = saveData[2] locations = saveData[3] else: currentLocation = yourBedroom inventory = list() if not args.nointro and not hasSave: stdscr.addstr(0, 0, "All right, {}, let's go...".format(playerName), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, "It's a sunny day outside and you wake up.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr( 3, 0, "\"I think there was something going on today, a big press conference...?\"", unicurses.color_pair(3) | unicurses.A_BOLD) stdscr.addstr( 4, 0, "After getting dressed and having breakfast, you get ready to take on the day.", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(5, 0, "\"Well, better get started.\"", unicurses.color_pair(3) | unicurses.A_BOLD) stdscr.addstr(7, 0, "-- Press any key to begin --", unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) continueGame = True while continueGame: topbar = playerName # May be expanded in the future stdscr.addstr(0, 0, topbar, unicurses.color_pair(0) | unicurses.A_BOLD) location = "Current Location: " + currentLocation.printName stdscr.addstr(1, 0, location, unicurses.color_pair(0) | unicurses.A_BOLD) description = currentLocation.desc stdscr.addstr(2, 0, description, unicurses.color_pair(0) | unicurses.A_BOLD) # Detect how many items are here if currentLocation.itemsHere == []: itemsHereBar = "There are no items here." elif len(currentLocation.itemsHere) == 1: itemsHereBar = "There is one item here." else: itemsHereBar = "There are " + str(len( currentLocation.itemsHere)) + " items here." stdscr.addstr(3, 0, itemsHereBar, unicurses.color_pair(0) | unicurses.A_BOLD) # Options stdscr.addstr(5, 0, "Save and (Q)uit", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(6, 0, "(M)ove", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(7, 0, "(T)hings Here", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(8, 0, "(I)nventory", unicurses.color_pair(0) | unicurses.A_BOLD) choice = nextMenu( stdscr).lower() # Use nextMenu for nice, easy clearing if choice == "q": # Save and Quit saveGame(playerName, currentLocation, inventory, locations) continueGame = False elif choice == "m": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 for place in currentLocation.connections: label = "|(" + str(keyCounter) + ") " + place.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr( ypos + 1, 0, "-- Press the key next to where you want to move --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) if choice in "123456789": # Make sure it's a valid location number if int(choice) - 1 < len(currentLocation.connections): moveTo = currentLocation.connections[int(choice) - 1] for index, item in enumerate(locations): if item.printName == currentLocation.printName: locations[index] = currentLocation currentLocation = moveTo # Move us elif choice == "t": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if currentLocation.itemsHere == []: stdscr.addstr(ypos, 0, "|There is nothing here. |", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) else: for item in currentLocation.itemsHere: label = "|(" + str(keyCounter) + ") " + item.printName stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) # Make a "box" ypos += 1 keyCounter += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr( ypos + 1, 0, "-- Press the key next to the item you want to use --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a valid item number if int(choice) - 1 < len(currentLocation.itemsHere): checkItem = True itemInQuestion = currentLocation.itemsHere[int(choice) - 1] else: checkItem = False if checkItem: drawBoxMenu(stdscr, ["Take Item"]) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": currentLocation.itemsHere.remove(itemInQuestion) inventory.append(itemInQuestion) elif choice == "i": ypos = 1 stdscr.addstr(ypos - 1, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) keyCounter = 1 if inventory == []: stdscr.addstr(ypos, 0, "|You have nothing in your inventory. |", unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 1, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos + 2, 0, "-- Press any key to continue --", unicurses.color_pair(1) | unicurses.A_BOLD) else: itemNames = [i.printName for i in inventory] ypos = drawBoxMenu(stdscr, itemNames) stdscr.addstr( ypos, 0, "-- Press an item's key to do something with it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": if int(choice) - 1 < len(inventory): checkItem = True itemInQuestion = inventory[int(choice) - 1] else: checkItem = False if checkItem: drawBoxMenu(stdscr, ["Look At Item", "Drop Item", "Use Item"]) choice = nextMenu(stdscr) # Number doesn't matter here, I'm not converting it to int or anything if choice == "1": stdscr.addstr(0, 0, itemInQuestion.printName, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(1, 0, itemInQuestion.desc, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(2, 0, '-- Press any key to exit --', unicurses.color_pair(1) | unicurses.A_BOLD) nextMenu(stdscr) elif choice == "2": currentLocation.itemsHere.append(itemInQuestion) inventory.remove(itemInQuestion) elif choice == "3": ypos = 1 stdscr.addstr(0, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) for item in inventory: if not item == itemInQuestion: label = "|(" + str( keyCounter) + ") " + item.printName stdscr.addstr( ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr( ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 keyCounter += 1 label = "|(0) Yourself" stdscr.addstr(ypos, 0, label + (" " * (len(label) - 40)), unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr(ypos, 40, "|", unicurses.color_pair(0) | unicurses.A_BOLD) ypos += 1 stdscr.addstr(ypos, 0, "-" * 40, unicurses.color_pair(0) | unicurses.A_BOLD) stdscr.addstr( ypos + 1, 0, "-- Press an item's key to use it, or anything else to exit --", unicurses.color_pair(1) | unicurses.A_BOLD) choice = nextMenu(stdscr) checkItem = False if choice in "123456789": # Make sure it's a number, the game crashes otherwise if int(choice) - 1 < len(inventory): checkItem = True itemToUseWith = inventory[int(choice) - 1] else: checkItem = False elif choice == "0": # If it's the player being used... itemToUseWith = playerItem # ...then use the special player item. checkItem = True else: checkItem = False if checkItem: itemInQuestion.useWith(stdscr, itemToUseWith, currentLocation, inventory) else: pass else: pass