def game_loop(): game_started = False while not game_started: user_input = input('New game(1)\nLoad game (2)\n') if user_input == '1': world.create_world(10) player_pos = world.spawn_player() game_started = True logger.warning('New game started') elif user_input == '2': try: player_pos = world.load() game_started = True logger.warning('Game loaded') except RuntimeError as error: logger.warning(error) won = False lose = False while not (won or lose): if world.is_trap_around(player_pos): logger.warning('There is a trap within one square from you!') else: logger.warning('No traps around.') if world.is_treasure_around(player_pos): logger.warning('There is a treasure within one square from you!') else: logger.warning('No treasures around.') input_is_valid = False while not input_is_valid: logger.warning( 'Your move (w/a/s/d)? (Input \'save\' to save game)') user_input = input() input_is_valid = True if user_input in directions.keys(): world.move_player(player_pos, directions[user_input]) elif user_input == 'save': world.save(player_pos) logger.warning('Game saved.') else: logger.warning('Invalid input. Try again.') input_is_valid = False won = world.is_found_treasure(player_pos) lose = world.is_trapped(player_pos) if won: logger.warning('You won!') else: logger.warning('You lose!') world.print_world(player_pos)
def run(self): self.netThread.start() self.aiThread.start() try: for line in sys.stdin: print "Line %s"%line except KeyboardInterrupt: pass finally: print "Terminating" self.terminate=True world.save()
def update(self): offset_x = 10 if self.shop: if Rect(self.left + offset_x, self.bot, 60, 20).collidepoint(commons.MOUSE_POS): if not self.shop_hover: self.shop_hover = True game_data.play_sound("fg.sound.menu_select") else: self.shop_hover = False offset_x += 60 if self.button_1_name is not None: if Rect(self.left + offset_x, self.bot, 60, 20).collidepoint(commons.MOUSE_POS): if not self.button_1_hover: self.button_1_hover = True game_data.play_sound("fg.sound.menu_select") if pygame.mouse.get_pressed()[0]: self.button_1_pressed = True else: self.button_1_hover = False offset_x += commons.DEFAULTFONT.size(self.button_1_name)[0] + 20 if Rect(self.left + offset_x, self.bot, 60, 20).collidepoint(commons.MOUSE_POS): if not self.close_hover: self.close_hover = True game_data.play_sound("fg.sound.menu_select") if pygame.mouse.get_pressed()[0]: game_data.play_sound("fg.sound.menu_close") self.close = True else: self.close_hover = False if pygame.mouse.get_pressed()[0] and not commons.WAIT_TO_USE: if not Rect(self.left, self.top, self.width, self.height).collidepoint(commons.MOUSE_POS): game_data.play_sound("fg.sound.menu_close") self.close = True if self.name == "Exit": if self.button_1_pressed: entity_manager.client_player.save() world.save() commons.GAME_STATE = "MAINMENU" commons.GAME_SUB_STATE = "MAIN" world.terrain_surface = pygame.Surface((1, 1)) menu_manager.update_active_menu_buttons() entity_manager.kill_all_entities() self.close = True
def run(self): global main try: while not main.terminate: try: p = self.sock.recv(1024*128).strip().split(" ") if p[0] == 'res': i = int(p[1]) if i in self.callback: self.callback[i](p[2:]) del self.callback[i] elif p[0] == 'toggle': print "Flush" self.condition.acquire() world.save() self.pause = not self.pause if self.pause: self.sock.send("stop") self.condition.notify() self.condition.release() except socket.timeout: if len(self.callback) < 10: self.async("pos", self.__loc_help) self.async("list_inventory", self.__inventory_help) if main.loc: r=10 p = tuple(map(lambda x: int(math.floor(x)), main.loc)) p2=p about = world.getAbout(p, r) self.async("query", lambda res: self.__query_help(p2, r, res), *map(lambda (x,y,z): "%d %d %d"%(x,y,z), about)) finally: self.condition.acquire() self.condition.notify() self.condition.release() print "Net thread exit"
def create(this, mapname, size = (64,64), density=5): """ Generates a dungeon given a map size and room concentration. The concentration ranges from 0 to 9, inclusively, with 0 being sparse and 9 being dense. Note that high density will result in more rooms/corridors that will most likely overlap causing large open areas. Returns a World object. """ ### A slightly more detailed description of the algorithm ### # 1) A blank world is generated and filled with walls # 2) Random rooms are generated # - Note that the rooms are not written yet # - All the points the rooms cover are stored in memory # - Same with the corridors # 3) Merge any rooms that are generated on top of eachother # 4) Connect all the rooms with corridors # - Corridors are recursively generated (and can be buggy) # - The corridor generation is completely random # - It only stops when it reaches a room or another corridor # - It is theoretically possible for the entire map # to be one corridor # 5) Check if all rooms are connected # - If not, redo map entire generation process from start # 6) Write all these points into the world as floors # 7) Place random objects throughout the map # 8) Write again and save ############################################################# # Generate a new world filled with only walls world = World() world.new(size, '#') # Create lists of rooms, corridors and points of the map # that are no longer occupied by a wall (aka "dug out") rooms = [] halls = [] cleared = [] # locations that have been dug out # Calculate number of rooms based on map size and difficulty #avgHeight = roomCount = int(math.ceil(((world.size[1]-2)*(world.size[0]-2) / (this.avgRoomSize[0]*this.avgRoomSize[1])) * (density*0.65+1)/12)) # Make sure the number of rooms is even # (Algorithm fails if it isnt...) if roomCount % 2 != 0: roomCount + 1 tries = 0 # This infinite while loop is here to repeat the map generation # process in case an "invalid" map has been generated while True: tries += 1 # clear any old data del rooms[:] del halls[:] del cleared[:] #~ print len(rooms) #~ print len(halls) #~ print len(cleared) # Create rooms (areas) for i in range(roomCount): # Generate a new room area = Area() loc = (random.randint(1,world.size[0]-this.avgRoomSize[0]-4), random.randint(1,world.size[1]-this.avgRoomSize[1]-4)) size = (random.randint(this.avgRoomSize[0]-3, this.avgRoomSize[0]+3), random.randint(this.avgRoomSize[1]-3, this.avgRoomSize[1]+3)) area.create(loc, size) # Add the new room to the list of rooms rooms.append(area) # add the all the locations the room occupies to [cleared] cleared += area.points # Remove duplicates from the tuple of cleared areas # This is also performed again later, but cleared will # be accessed a lot. Clearing duplicates now will save cpu cleared[:] = list(set(cleared)) # After generating rooms, merge rooms that are connected to each other i = 0 while i < len(rooms): # n = i + 1 because all the rooms before i have already been checked n = i+1 while n < len(rooms): if rooms[i].isOverlap(rooms[n]): # Merge the two rooms rooms[i].merge(rooms[n]) # Remove the second room (room[n]) since that one # is now a part of the current room (room[i]) rooms.pop(n) # Recheck the current room with the rest again n = i+1 continue n+=1 i+=1 # Calculate locations of walls for each room for r in rooms: r.calcWalls(cleared) # Now connect rooms via corridors for i in range(len(rooms)): if len(rooms[i].corridors) == 0: while True: hall = Corridor() loc, d = random.choice(rooms[i].walls) hall.endrooms.append(rooms[i]) # if this hall generated properly if hall.generate(world, rooms, halls, loc, d): # Add this corridor to all the rooms # it connected to for room in hall.endrooms: room.corridors.append(hall) break # exit hall.endrooms[:] = list(set(hall.endrooms)) #halls.append(hall) # Appened corridors to cleared areas cleared += hall.points # Remove duplicates from the tuple of cleared areas cleared[:] = list(set(cleared)) # Check if all the rooms are connected while True: tries += 1 connected = rooms[0].getConnectedRooms() connected[:] = list(set(connected)) print len(connected),"is connected" print len(rooms),"were generated" if len(connected) < len(rooms): print "Invalid map detected" print "connecting unconnected rooms..." # Get list of all the rooms that are not connected unconnected = map(lambda x: x if x not in connected else None, rooms) unconnected = list(set(unconnected)) unconnected.remove(None) print len(unconnected),"rooms are unconnected" # Choose a connected room and connect with an unconnected one room = random.choice(connected) while True: hall = Corridor() loc, d = random.choice(rooms[i].walls) hall.endrooms.append(room) # if this hall generated properly if hall.generate(world, unconnected, halls, loc, d, 10, False): print "Connected",len(hall.endrooms)-1 # Add this corridor to all the rooms # it connected to for room in hall.endrooms: room.corridors.append(hall) break # exit hall.endrooms[:] = list(set(hall.endrooms)) #halls.append(hall) # Appened corridors to cleared areas cleared += hall.points cleared[:] = list(set(cleared)) del connected[:] del unconnected[:] continue elif len(connected) > len(rooms): print "Something is wrong here..." raise ValueError break # dig out rooms and corridors world.tile('.',cleared) ## Randomly place objects and monsters ## # TODO: create a way to calculate how many of each object to place nchests = 3 nlowlvl = 9 nhighlvl = 2 nportal = 1 nplayer = 1 # Place objects and monsters break print "- - - - - - - - - - - - - - - - - - -" print "Generated world with",len(rooms),"rooms in",tries,"tries" world.save(mapname) return world
def make(make_what): # World generation if ("+tutorial " in make_what) or (("+all " in make_what) and ("-tutorial " not in make_what)): print("Making tutorial maps", end="") Tutorial.start.generate() world.save("tutorial.start") print(".", end="") Tutorial.tut1.generate() world.save("tutorial.1") print(".", end="") Tutorial.tut2.generate() world.save("tutorial.2") print(".", end="") Tutorial.tut2killed.generate() world.save("tutorial.2-killed") print(".", end="") Tutorial.tutboss.generate() world.save("tutorial.boss") print(".", end="") Tutorial.tutbosskilled.generate() world.save("tutorial.boss-killed") print(".", end="") Tutorial.tutfinal.generate() world.save("tutorial.final") print(".") if ("+town " in make_what) or (("+all " in make_what) and ("-town " not in make_what)): print("Making town", end="") town.generate() world.save("town") print(".") if ("+stonedungeon " in make_what) or (("+all " in make_what) and ("-stonedungeon " not in make_what)): print("Making Stone Dungeon", end="") StoneDungeon.stonboss.generate() world.save("stonedungeon.boss") print(".", end="") StoneDungeon.stonstart.generate() world.save("stonedungeon.start") print(".", end="") StoneDungeon.ston1.generate() world.save("stonedungeon.1") print(".", end="") StoneDungeon.ston2.generate() world.save("stonedungeon.2") print(".") if ("+lavadungeon " in make_what) or (("+all " in make_what) and ("-lavadungeon " not in make_what)): print("Making LavaDungeon", end="") LavaDungeon.start.generate() world.save("lavadungeon.start") print(".", end="") LavaDungeon.map1.generate() world.save("lavadungeon.1") print(".", end="") LavaDungeon.map2.generate() world.save("lavadungeon.2") print(".") if ("+credits " in make_what) or (("+all " in make_what) and ("-credits " not in make_what)): print("Making credits", end="") credits.generate() world.save("credits") print(".")
def place(this, world, cleared, level = 0): """ This function places objects (e.g. stairs, chests, monsters) randomly in the world. The type of items and monsters that are placed depends on the level of the dungeon (better items and harder monsters the higher the level) """ ## TODO: Implement everything print "cleared:",len(cleared) nchests = len(cleared)/(200) + random.randint(0,4) # Locations of where each object is placed placed = [] # Monsters nbats = 80-level*5 nskel = 300-level*15 nreap = 800-level*20 ndrag = 550-level*20 nbats = len(cleared)/( 55 if nbats < 55 else nbats ) nskel = len(cleared)/( 75 if nskel < 75 else nskel ) nreap = len(cleared)/( 220 if nreap < 220 else nreap ) ndrag = len(cleared)/( 120 if ndrag < 120 else ndrag ) # Set player location loc = random.choice(cleared) placed.append(loc) placed += this.getArea(world,loc, 10) world.placeObject("player", loc[0], loc[1]) for item in world.inventory: if item.equipped == True: world.player.weapon = item break # Set staircase/boss location while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break if world.level >= 10: world.placeObject("boss", loc[0], loc[1]) else: world.placeObject("stair", loc[0], loc[1]) # Set chest locations for chest in range(nchests): while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break world.placeObject("chest", loc[0], loc[1]) # Set monster locations for i in range(nbats): while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break world.placeObject("bat", loc[0], loc[1]) for i in range(nskel): while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break world.placeObject("skel", loc[0], loc[1]) for i in range(ndrag): while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break world.placeObject("dragon", loc[0], loc[1]) for i in range(nreap): while(True): loc = random.choice(cleared) if loc in placed: continue placed.append(loc) break world.placeObject("reaper", loc[0], loc[1]) world.save()
def make(make_what): # World generation if ("+start " in make_what) or (("+all " in make_what) and ("-tutorial " not in make_what)): print("Making start", end = "") maps.start.generate() world.save("start") print(".") if ("+war_tut " in make_what) or ("+tutorial " in make_what and "-war_tut " not in make_what) or (("+all " in make_what) and ("-tutorial " not in make_what or "-war_tut " not in make_what)): print("Making warrior tutorial", end = "") maps.tutorial.war_tut.warrior_start.generate() world.save("warrior_start") print(".", end="") maps.tutorial.war_tut.war_tut_1.generate() world.save("war_tut_1") print('.') if ("+mage_tut " in make_what) or ("+tutorial " in make_what and "-mage_tut " not in make_what) or (("+all " in make_what) and ("-tutorial " not in make_what or "-mage_tut " not in make_what)): print("Making mage tutorial", end="") maps.tutorial.mage_tut.mage_start.generate() world.save("mage_start") print(".", end="") maps.tutorial.mage_tut.mage_start_upper.generate() world.save("mage_start_upper") print(".", end = "") maps.tutorial.mage_tut.mage_tut_1.generate() world.save("mage_tut_1") print(".", end = "") maps.tutorial.mage_tut.mage_tut_2.generate() world.save("mage_tut_2") print(".", end = "") maps.tutorial.mage_tut.mage_tut_boss.generate() world.save("mage_tut_boss") print(".", end = "\n") if ("+test_dungeon " in make_what) or ("+all " in make_what and "-test_dungeon " not in make_what): print("Making test dungeon", end = "") maps.test_dungeon.start.generate() world.save("test_dungeon_start") print(".", end="") maps.test_dungeon.test_dungeon_1.generate() world.save("test_dungeon_1") print(".")
def update_menu_buttons(): for menu_button in menu_buttons: if menu_button.active: menu_button.update() if menu_button.clicked: menu_button.clicked = False temp_game_sub_state = commons.GAME_SUB_STATE if menu_button.text == "Play": commons.GAME_SUB_STATE = "PLAYERSELECTION" load_menu_player_data() elif menu_button.text == "Settings": commons.GAME_SUB_STATE = "SETTINGS" elif menu_button.text == "Changes": commons.GAME_SUB_STATE = "CHANGES" elif menu_button.text == "PyGame Page": entity_manager.client_prompt = prompt.Prompt( "browser opened", "PyGame page opened in a new tab.", size=(5, 2)) webbrowser.open("https://www.pygame.org/project/3451") elif menu_button.text == "GitHub Repo": entity_manager.client_prompt = prompt.Prompt( "browser opened", "GitHub page opened in a new tab.", size=(5, 2)) webbrowser.open( "https://github.com/FergusGriggs/Fegaria-Remastered") elif menu_button.text == "YouTube Channel": entity_manager.client_prompt = prompt.Prompt( "browser opened", "YouTube page opened in a new tab.", size=(5, 2)) webbrowser.open( "https://www.youtube.com/channel/UC_7e1qyqA39URIlV89MByug" ) elif menu_button.text == "Trello Board": entity_manager.client_prompt = prompt.Prompt( "browser opened", "Trello board opened in a new tab.", size=(5, 2)) webbrowser.open( "https://trello.com/b/FS1rej7y/fegaria-remastered") elif menu_button.text == "Credits": commons.GAME_SUB_STATE = "CREDITS" elif menu_button.text == "Quit": pygame.quit() sys.exit() elif menu_button.text == "Back": commons.GAME_SUB_STATE = commons.GAME_SUB_STATE_STACK.pop() elif menu_button.text == "New Player": commons.GAME_SUB_STATE = "PLAYERCREATION" commons.PLAYER_MODEL_DATA = [ 0, 0, [(127, 72, 36), None, None], [(62, 22, 0), None, None], [(0, 0, 0), None, None], [(95, 125, 127), None, None], [(48, 76, 127), None, None], [(129, 113, 45), None, None], [(80, 100, 45), None, None] ] commons.PLAYER_MODEL = player.Model( commons.PLAYER_MODEL_DATA[0], commons.PLAYER_MODEL_DATA[1], commons.PLAYER_MODEL_DATA[2][0], commons.PLAYER_MODEL_DATA[3][0], commons.PLAYER_MODEL_DATA[4][0], commons.PLAYER_MODEL_DATA[5][0], commons.PLAYER_MODEL_DATA[6][0], commons.PLAYER_MODEL_DATA[7][0], commons.PLAYER_MODEL_DATA[8][0]) commons.PLAYER_FRAMES = player.render_sprites( commons.PLAYER_MODEL, directions=1, arm_frame_count=1, torso_frame_count=1) elif menu_button.text == "Hair Type": if commons.PLAYER_MODEL.hair_id < 8: commons.PLAYER_MODEL.hair_id += 1 else: commons.PLAYER_MODEL.hair_id = 0 commons.PLAYER_MODEL_DATA[1] = commons.PLAYER_MODEL.hair_id commons.PLAYER_FRAMES = player.render_sprites( commons.PLAYER_MODEL, directions=1, arm_frame_count=1, torso_frame_count=1) elif menu_button.text == "Hair Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 3 elif menu_button.text == "Eye Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 4 elif menu_button.text == "Skin Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 2 elif menu_button.text == "Clothes": commons.GAME_SUB_STATE = "CLOTHES" elif menu_button.text == "Shirt Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 5 elif menu_button.text == "Undershirt Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 6 elif menu_button.text == "Trouser Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 7 elif menu_button.text == "Shoe Colour": commons.GAME_SUB_STATE = "COLOURPICKER" commons.PLAYER_MODEL_COLOUR_INDEX = 8 elif menu_button.text == "Randomize": commons.PLAYER_MODEL_DATA = [ 0, random.randint(0, 8), [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None], [(random.randint(0, 128), random.randint(0, 128), random.randint(0, 128)), None, None] ] player.update_player_model_using_model_data() commons.PLAYER_FRAMES = player.render_sprites( commons.PLAYER_MODEL, directions=1, arm_frame_count=1, torso_frame_count=1) elif menu_button.text == "Create": commons.GAME_SUB_STATE = "PLAYERNAMING" commons.TEXT_INPUT = "" elif menu_button.text == "Set Player Name": date = datetime.datetime.now() commons.PLAYER_DATA = [ commons.TEXT_INPUT, commons.PLAYER_MODEL, None, None, 100, 100, 0, date, date ] # Create player array pickle.dump( commons.PLAYER_DATA, open("res/players/" + commons.TEXT_INPUT + ".player", "wb")) # Save player array commons.GAME_SUB_STATE = "PLAYERSELECTION" load_menu_player_data() elif menu_button.text == "New World": commons.GAME_SUB_STATE = "WORLDCREATION" elif menu_button.text == "Tiny (100x350)": commons.GAME_SUB_STATE = "WORLDNAMING" commons.TEXT_INPUT = "" world.WORLD_SIZE_X = 100 world.WORLD_SIZE_Y = 350 elif menu_button.text == "Small (200x400)": commons.GAME_SUB_STATE = "WORLDNAMING" commons.TEXT_INPUT = "" world.WORLD_SIZE_X = 200 world.WORLD_SIZE_Y = 400 elif menu_button.text == "Medium (400x450)": commons.GAME_SUB_STATE = "WORLDNAMING" commons.TEXT_INPUT = "" world.WORLD_SIZE_X = 400 world.WORLD_SIZE_Y = 450 elif menu_button.text == "Large (700x550)": commons.GAME_SUB_STATE = "WORLDNAMING" commons.TEXT_INPUT = "" world.WORLD_SIZE_X = 700 world.WORLD_SIZE_Y = 550 elif menu_button.text == "Set World Name": world.WORLD_NAME = commons.TEXT_INPUT world.generate_terrain("DEFAULT", blit_progress=True) world.save() commons.GAME_SUB_STATE = "WORLDSELECTION" load_menu_player_data() if commons.GAME_SUB_STATE == "COLOURPICKER": if commons.PLAYER_MODEL_DATA[ commons.PLAYER_MODEL_COLOUR_INDEX][1] is not None: entity_manager.client_colour_picker.selected_colour = tuple( commons.PLAYER_MODEL_DATA[ commons.PLAYER_MODEL_COLOUR_INDEX][0]) entity_manager.client_colour_picker.selected_x = commons.PLAYER_MODEL_DATA[ commons.PLAYER_MODEL_COLOUR_INDEX][1] entity_manager.client_colour_picker.selected_y = commons.PLAYER_MODEL_DATA[ commons.PLAYER_MODEL_COLOUR_INDEX][2] # Update last game sub state variable is the sub state changed if temp_game_sub_state != commons.GAME_SUB_STATE: if menu_button.text != "Back": commons.GAME_SUB_STATE_STACK.append( temp_game_sub_state) if menu_button.text == "Set Player Name": commons.GAME_SUB_STATE_STACK = commons.GAME_SUB_STATE_STACK[: 1] if menu_button.text == "Set World Name": commons.GAME_SUB_STATE_STACK = commons.GAME_SUB_STATE_STACK[: 2] update_active_menu_buttons()
CLIENTS[addr] = data print "%s %s" % (addr, data) cmd_args = data.split(':', 1) cmd = cmd_args[0] if cmd not in ('save', 'load'): args = cmd_args[1] if cmd == 'connect': CLIENTS[addr] = args print '%s connected' % args start = 'world:' + world.get_all() s.sendto(start, addr) elif cmd == 'move': world.update_player(args) for client in CLIENTS: if addr == client: continue s.sendto(data, client) elif cmd == 'update': world.update(args) # naively echo to all other clients for client in CLIENTS: if addr == client: continue s.sendto(data, client) elif cmd == 'save': world.save() elif cmd == 'load': # Any clients that are connected now disagree with the server. world.load() finally: print 'Goodbye world' s.close()