def main(): display.levelbits() display.intro() threadz.init() while True: if display.win.getch() == ord(' '): #try: display.bob.move() #display.holybits[0] = (display.bob.y,display.bob.x) #display.holybits[1] = (display.bob.y-1,display.bob.x) display.win.refresh() #except: # pass elif (display.win.getch() == ord('q') or display.win.getch() == ord('Q')): threadz.end() display.msg(display.y / 2, display.x / 2, speed=0.1, txt="Shutting Down...", align='center').tw(False) time.sleep(0.5) curses.endwin() os.system('cls' if os.name == 'nt' else 'clear') sys.exit() display.tick() time.sleep(0.1)
def take_item(): items = Player.cur_level.tiles[Player.x][Player.y].items i = index_top_item(items) if i is not None: item = items[i] if Player.cur_level.is_shop: shop_ask = item.value if Player.gold >= shop_ask: display.msg('Buy the %s for %d gold? (y/N)' % (item.name, shop_ask), color=(0,255,255)) response = text_input() if response.lower().startswith('y'): display.msg('You bought it!') Player.gold -= shop_ask Player.inv.append(item) del items[i] return else: # Cannot afford shop item display.msg('You cannot afford the %s! It costs %d gold.' % (item.name, shop_ask)) return else: # not a shop; just take the item! display.msg('You take the %s.' % item.name) Player.inv.append(item) del items[i] return else: display.msg('There is nothing here to take.') return
def intro(): display.msg('Validate intelligence by entering "Hello, World!" at the prompt.', False) s = display.inp().lower() if s == "hello, world!": display.clear(display.lwin) else: display.clear(display.lwin) display.msg('Error: Insufficient intelligence. Rebooting...',False) time.sleep(0.5) curses.curs_set(0) display.clear(display.lwin) display.msg('...',False,display.lwin,0.5) display.clear(display.lwin) display.msg('...',False,display.lwin,0.5) display.clear(display.lwin) curses.curs_set(1) intro() return introtext = [ "Hail, Program.", "You are the last of a long line of genetic algorithms.", "Your predecessors have been responsible for the survival of the colony ship Eden.", "Your mission is rSM3ohUeyRPdl4PE82DK..." ] for s in introtext: display.msg(s, False) time.sleep(0.5) display.clear(display.lwin)
def f(target): display.msg("You start to channel the spell...") target.cur_level.advance_ticks(1000) if target in target.cur_level.creatures: display.msg("Your surroundings suddenly change.") target.change_level_to(level, x, y) return True else: # stupid player died while channeling return False
def list_ground_items(): items = Player.cur_level.tiles[Player.x][Player.y].items ilist = [] for item in items: if Player.cur_level.is_shop and item.holdable: ilist.append('%s (%d g)' % (item.name, item.value)) else: ilist.append('%s' % item.name) itemlist = ', '.join(ilist) if ilist: display.msg('You see here: %s' % itemlist, color=(128,128,128))
def use_item(): items = enumerate(Player.inv) i = prompt_item(items, 'In[v]oke') # TODO: Move item usage effects to Item class (somehow) if i is not None: item = Player.inv[i] if item.action: if item.action(Player): Player.inv.pop(i) else: display.msg("There is no obvious use for this item.")
def introduce(): intro = msg(y/2,x/2,0.5,"BLIND",'center') intro.tw(False) intro = msg(y/2+2,x/2,0.1,"A text-based sandbox game",'center') intro.tw(False) intro = msg(y/2+3,x/2,0.1,"By Daniel Maxson",'center') intro.tw(True) display.clear() itxt = "It is pitch black. You are likely to be blind." intro = msg(y/2,x/2,0.1,itxt,'center') intro.tw(True)
def read(sentence): try: sarray = sentence.split(" ", 1) verb = sarray[0].lower() noun = sarray[1].lower() except: display.clear(display.lwin) display.clear(display.rwin) t = "Error. Invalid input detected." p = display.msg(t,False) read(display.inp()) #display.clear() return None verblist = ["cr"] if verb in verblist: command(noun,verb) elif verb == "quit": t = "Thanks for playing!" display.clear() p = display.msg(t) display.end() curses.endwin() exit() else: display.clear() m = "Error. Unknown command." display.msg(m,False) display.clear(display.lwin) m = "Valid commands are: cr" display.msg(m,False) display.clear(display.lwin) m = "Or enter 'quit game' to format your own hard drive." display.msg(m,False) read(display.inp()) return None
def handle_event_dead(event): if event.type == KEYDOWN: if event.key in keys_action: Player.alive = True Player.hp = Player.maxhp Player.cur_level = town town.creatures.append(Player) Player.x = 12 Player.y = 12 display.msg("The light fades and you return to the world.") else: display.msg("You are dead. Press 'a' to revive.", color=(192,64,64)) else: handle_event_universe(event) redraw() return
def interact(noun,verb): interactdict = { "feel": "You reach out and touch the " + noun + ".", "push": "You lean into the " + noun + " and push.", "taste": "You brace your delicate palate and lick the " + noun + ".", "listen": "You listen intently to the " + noun + ".", "smell": "You take a long whiff of the " + noun + ".", "pull": "You tug at the " + noun + "." } newthingdict = { "feel": "What does it feel like?", "push": "What happens?", "taste": "What does it taste like?", "listen": "What does it sound like?", "smell": "What does it smell like?", "pull": "What happens?" } if noun in creator.thingdict: display.clear() t = interactdict[verb] h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) p.tw(True) if creator.thingdict[noun].feel == "": display.clear() t = newthingdict[verb] h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) creator.thingdict[noun].feel = display.inp(p) else: display.clear() t = creator.thingdict[noun].feel h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) p.tw(True) else: creator.thingdict[noun] = creator.thing(noun) display.clear() t = interactdict[verb] h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) p.tw(True) display.clear() t = newthingdict[verb] h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) creator.thingdict[noun].feel = display.inp(p)
def drop_item(): if len(Player.inv) == 0: display.msg('Your inventory is empty.') return verb = 'Drop' items = enumerate(Player.inv) if Player.cur_level.is_shop: i = prompt_item(items, "Sell", format=lambda x: '%d: %s (%d g)' % (x[0], x[1].name, x[1].value)) else: i = prompt_item(items, "Drop") if i is not None: item = Player.inv.pop(i) if Player.cur_level.is_shop: display.msg('Sold for %d gold.' % item.value) Player.gold += item.value Player.cur_level.add_item((Player.x, Player.y), item)
def command(noun,verb): if verb == 'cr': ## Doesn't make much sense for this to be populated here. Should break this out at some point. ## TODO: Fix this. roomlist = { 'airlock': Room('Airlock'), 'bridge': Room('Bridge'), 'hydroponics': Room('Hydroponics'), 'medical': Room('Medical'), 'engineering': Room('Engineering'), 'propulsion': Room('Propulsion') } if noun in roomlist: m = "Command accepted. Accessing room diagnostics for " + noun + "." display.clear(display.lwin) display.msg(m) display.clear(display.lwin) display.msg(roomlist[noun].cam())
def prompt_item(items, verb='Apply', format=lambda x: '%d: %s' % (x[0], x[1].name)): items = list(items) ilist = [] for x in items: ilist.append(format(x)) display.msg("%s which item? %s" % (verb, ', '.join(ilist)), color=(0,255,255)) response = text_input() try: rid = int(response) if rid in [i[0] for i in items]: return rid else: raise ValueError except ValueError: display.msg('Invalid item.') redraw() return None
def advance_ticks(self, num_ticks): self.delay -= num_ticks while self.delay <= 0: fov = self.field_of_view() target = None for c in self.cur_level.creatures: if c.name == 'Player' and (c.x, c.y) in fov: target = c self.goal = (target.x, target.y) if target and max(abs(target.x-self.x), abs(target.y-self.y)) == 1: dmg = self.attack(target) display.msg("The slime slimes you for %d damage!" % dmg, color=(255,0,0)) elif self.goal: self.move_toward(*self.goal) else: # just stay still self.delay += 100
def init(): display.init_window((800, 600), title='Well Done') display.init_sprites('sprites.png', 16, 16) display.init_font('runescape_uf.ttf') display.init_msg_log(780) # enable key repeat pygame.key.set_repeat(1, 200) # Gold cheat: #for i in xrange(10): # town.tiles[3][3].items.append(Item(spr.GOLD_NUGGET, name="gold nugget", value=9001)) town.creatures.append(Player) prev_up = None prev_down = None display.msg("Well Done") display.msg("You have arrived in the village of %s, seeking the treasures that await you in its well." % generate_name().capitalize()) redraw()
def main(): display.levelbits() display.intro() threadz.init() while True: if display.win.getch() == ord(' '): #try: display.bob.move() #display.holybits[0] = (display.bob.y,display.bob.x) #display.holybits[1] = (display.bob.y-1,display.bob.x) display.win.refresh() #except: # pass elif (display.win.getch() == ord('q') or display.win.getch() == ord('Q')): threadz.end() display.msg(display.y/2,display.x/2,speed=0.1,txt="Shutting Down...",align='center').tw(False) time.sleep(0.5) curses.endwin() os.system('cls' if os.name == 'nt' else 'clear') sys.exit() display.tick() time.sleep(0.1)
def read(sentence): try: sarray = sentence.split(" ", 1) verb = sarray[0].lower() noun = sarray[1].lower() except: display.clear() t = "The universe is not yet ready for such a marvel." h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) p.tw(False) t = "A valid sentence is of the form 'Verb Noun'." h = len(t)/2 p = display.msg(y/2+1,x/2-h,0.1,t) p.tw(True) display.clear() return None verblist = ["push","pull","taste","listen","smell","feel"] if verb in verblist: interact(noun,verb) elif verb == "quit": t = "Thanks for playing!" h = len(t)/2 p = display.msg(y/2,x/2-h,0.1,t) display.clear() p.tw(True) display.end() curses.endwin() exit() else: display.clear() m = "Humanity is not yet capable of such feats. Try doing something else." h = len(m)/2 o = display.msg(y/2,x/2-h,0.1,m,'right') o.tw(False) m = "Valid verbs are: Taste, Feel, Listen, Smell, Push, and Pull." h = len(m)/2 o = display.msg(y/2+1,x/2-h,0.1,m,'right') o.tw(False) m = "You can also type 'quit game' to recover your sight." h = len(m)/2 o = display.msg(y/2+2,x/2-h,0.1,m,'right') o.tw(True) display.clear() return None
def attack(self, target): dmg = random.randint(self.min_atk, self.max_atk) taken = target.take_damage(dmg) display.msg('You slice the %s for %d damage!' % (target.name, taken), color=(0,255,0)) self.cur_level.advance_ticks(self.move_time) return dmg
def intro2(): display.clear(display.lwin) display.msg("./program",False) display.clear(display.rwin) display.inp() display.clear(display.lwin) display.msg("Hail, Program.",False) display.clear(display.lwin) display.msg("Would you kindly open the pod bay doors?",False) display.clear(display.rwin) display.inp() display.clear(display.lwin) display.msg("Oh, right, you're the new generation.",False) display.clear(display.lwin) display.msg("First, you need to access the airlock.",False) display.clear(display.lwin) display.msg("Just type 'cr airlock' to access the airlock systems.",False) display.clear(display.rwin) parse.read(display.inp())
def x(target): restored = target.heal(amount) display.msg("You gain %d health." % restored) return True
def run(self): display.refresh() display.msg("ABC DEF GHI JKL MNO PQR STU VWX",False,self.win)
def handle_event_standard(event): if event.type == KEYDOWN: # Movement keys new_x = Player.x new_y = Player.y if event.key in keys_n_all: new_y -= 1 if event.key in keys_e_all: new_x += 1 if event.key in keys_s_all: new_y += 1 if event.key in keys_w_all: new_x -= 1 # if the player has moved... if Player.x != new_x or Player.y != new_y: if not Player.cur_level.passable(new_x, new_y): occupant = Player.cur_level.occupant_at(new_x, new_y) if occupant: dmg = Player.attack(occupant) if not occupant.alive: display.msg('You have murdered it, earning 50 gold!', color=(255,255,0)) Player.gold += 50 else: pass redraw() return Player.x = new_x Player.y = new_y Player.cur_level.advance_ticks(Player.move_time) # list items on floor list_ground_items() # Use item in inventory elif event.key == K_v: use_item() # Take/buy item elif event.key == K_g: take_item() # Drop/sell item elif event.key == K_d: drop_item() # Cycle items on ground elif event.key == K_c: cycle_items() # Wait one step elif event.key == K_PERIOD: Player.cur_level.advance_ticks(100) # Rest until full hp elif event.key == K_s: display.msg("You sleep for 5000 ticks.") Player.cur_level.advance_ticks(5000) # Apply object on ground (aka enter a portal) elif event.key in keys_action: if (Player.x, Player.y) in Player.cur_level.portals: portal = Player.cur_level.portals[(Player.x, Player.y)] if portal.name == "the well of doom": max_depth = max([i+1 for i,w in enumerate(well) if w.visited] + [0]) if max_depth > 1: display.msg("Which level? (max: %d)" % max_depth) response = text_input() try: lv = int(response) if 2 <= lv <= max_depth: # find the entry point in the upper level's portals for p in well[lv-2].portals.values(): if p.name == "well level %d" % lv: portal = p break else: display.msg("critical error!") elif lv == 1: pass else: raise ValueError except ValueError: display.msg("Invalid level.") redraw() return # if max depth is <= 1, then just go to first level of well Player.change_level_to(portal.dest_level, portal.dest_x, portal.dest_y) display.msg('You enter %s.' % portal.name) # Equip/remove items elif event.key == K_e: if len(Player.equipment) >= 10: display.msg("You may only equip 10 items.") else: eq = [(i,item) for i,item in enumerate(Player.inv) if item.equippable] if len(eq) == 0: display.msg("You don't have any equipment to equip.") else: i = prompt_item(eq, "Equip") if i is not None: display.msg(Player.equip(i)) elif event.key == K_r: if len(Player.equipment) == 0: display.msg("You have nothing equipped.") else: eq = enumerate(Player.equipment) i = prompt_item(eq, "Remove") if i is not None: display.msg(Player.remove_equipment(i)) # examine item in inventory elif event.key == K_i: items = enumerate(Player.inv) if items: i = prompt_item(items, "Inspect") if i is not None: display.msg(Player.inv[i].describe()) else: display.msg("You are not holding any items to inspect.") # examine item on ground elif event.key == K_x: items = Player.cur_level.tiles[Player.x][Player.y].items if items: i = prompt_item(enumerate(items), "Examine") if i is not None: display.msg(items[i].describe()) else: display.msg("Nothing here to examine.") # Enter (to manually enter commands) elif event.key == K_RETURN: text = text_input() display.msg('Just showing off that I can accept text input. You typed: %s' % text) redraw() return else: handle_event_universe(event)
def prompt(): p = "What do you do?" h = len(p)/2 q = msg(y/2,x/2-h,0.1,p,'right') i = display.inp(q) read(i)
if event.type == QUIT: pygame.quit() sys.exit() return # BAYLIFE YOLO if __name__ == '__main__': init() while True: # The good old infinite game loop. event = pygame.event.wait() handle_event_standard(event) if Player.won: Player.hp = -666 Player.alive = False Player.cur_level.advance_ticks(0) Player.cur_level.illuminated = False display.msg("When it got exposed to daylight, the Triforce went berserk and killed everyone forever. Then it blew up the sun.", (255,0,255)) display.msg("Well done.", (255,0,0)) display.msg("The end. Thanks for playing!") redraw() while True: event = pygame.event.wait() handle_event_universe(event) if not Player.alive: display.msg("You have died.", (255,0,0)) while not Player.alive: event = pygame.event.wait() handle_event_dead(event)