def setup(self): """ Generates a hero, monster and door at random locations. """ player_start = GameMap.random_location() # Generate monster location. Make sure it is not the same as player location while True: monster_start = GameMap.random_location() if monster_start == player_start: continue else: break # Generate door location. Make sure it is not the same as player and monster location. while True: door_start = GameMap.random_location() if door_start == monster_start or door_start == player_start: continue else: break self.player = Hero(position=player_start) self.monster = Monster(position=monster_start) self.door = Door(position=door_start) self.player.visit_location(player_start)
def simulate_battle(config): guild = Guild() guild.players = [ Player(player_id=n + 1, strenght=calculate_value_with_randomness( float(config["PLAYER_STRENGHT"]), float(config["PLAYER_STRENGHT_RANDOMNESS"])), logistics=calculate_value_with_randomness( float(config["PLAYER_LOGISTICS"]), float(config["PLAYER_LOGISTICS_RANDOMNESS"])), gold=calculate_value_with_randomness( float(config["PLAYER_GOLD"]), float(config["PLAYER_GOLD_RANDOMNESS"])), gained_war_exp_buff=0.1) for n in range(int(config["N_PLAYERS"])) ] monsters = [ Monster(monster_id=n + 1, strength=calculate_value_with_randomness( float(config["MONSTER_STRENGHT"]), float(config["MONSTER_STRENGHT_RANDOMNESS"])), life_points=calculate_value_with_randomness( float(config["MONSTER_LIFE_POINTS"]), float(config["MONSTER_LIFE_RANDOMNESS"])), first_attack_chance=0.5) for n in range(int(config["N_MONSTERS"])) ] battle = Battle(guild.players, monsters) battle.run() battle.results.print_results() return battle.results
def simulate_battle(config): # TODO: add saving log file guild_exporter = GuildExporter() guild_stats, guild_stats_for_player = guild_exporter.export_guild_tsv( 'real_data/BAZA_DANYCH - GILDIA.tsv') exporter = PlayersExporter(buffs_from_guild=guild_stats_for_player) players = exporter.export_players_tsv('real_data/BAZA_DANYCH - GRACZE.tsv') monsters = [ Monster(monster_id=n + 1, strength=calculate_value_with_randomness( float(config["MONSTER_STRENGHT"]), float(config["MONSTER_STRENGHT_RANDOMNESS"])), life_points=calculate_value_with_randomness( float(config["MONSTER_LIFE_POINTS"]), float(config["MONSTER_LIFE_RANDOMNESS"])), first_attack_chance=0.5) for n in range(int(config["N_MONSTERS"])) ] battle = Battle(players, monsters) battle.run() battle.results.print_results() return battle.results
def __init__(self): super().__init__(self) pman.shim.init(self) player = playerdata.PlayerData() self.blackboard = { 'player': player, } default_monster = Monster.make_new('player_monster', 'Default', 'mine') self.blackboard['player'].monsters = [default_monster] self.camera = p3d.NodePath('camera') class Pipeline: def __init__(self): self.exposure = 6 self.render_pipeline = Pipeline() self.ui = MagicMock() self.event_mapper = MagicMock()
from random import randint import pygame import sys from game.game_map import GameMap from game.monster import Monster from game.player_character import PlayerCharacter from game.video import Video __author__ = 'TriD' video = Video() game_map = GameMap() player_character = PlayerCharacter() monster = Monster() game_state = 'running' last_tick = pygame.time.get_ticks() monster_update = 0 next_level = None def init_level(level_name): global map_file, next_level, game_map game_map.map_data = [] with open('res/maps/' + level_name) as map_file: while True: line = map_file.readline() if line == '\n': break game_map.map_data.append(list(line)) player_x, player_y = map_file.readline().split(',') player_character.x, player_character.y = int(player_x), int(player_y)
def __init__(self): ShowBase.__init__(self) pman.shim.init(self) base.enable_particles() gdb = gamedb.get_instance() # Render pipeline self.set_background_color((0, 0, 0, 1)) self.render.set_antialias(p3d.AntialiasAttrib.MAuto) self.render_pipeline = simplepbr.init( max_lights=4, msaa_samples=p3d.ConfigVariableInt('msaa-samples', 4).get_value(), enable_shadows=p3d.ConfigVariableBool('enable-shadows', True).get_value(), exposure=5, ) # Controls self.event_mapper = eventmapper.EventMapper() self.disable_mouse() self.accept('quit', sys.exit) self.accept('toggle-buffer-viewer', self.bufferViewer.toggleEnable) self.accept('toggle-oobe', self.oobe) self.accept('save-screenshot', self.screenshot) # Global storage self.blackboard = {} default_save = p3d.ConfigVariableString('mercury-default-save', '').get_value() if default_save: saveloc = os.path.join( pathutils.get_saves_dir(), default_save, ) if not saveloc.endswith('.sav'): saveloc += '.sav' if os.path.exists(saveloc): with open(saveloc) as savefile: self.blackboard['player'] = PlayerData.load(savefile) default_monster_id = p3d.ConfigVariableString( 'mercury-default-monster', '').get_value() if default_monster_id: default_monster = Monster(gdb['monsters'][default_monster_id]) else: default_form = p3d.ConfigVariableString('mercury-default-form', 'mine').get_value() default_monster = Monster.make_new('player_monster', form_id=default_form) if 'player' not in self.blackboard: self.blackboard['player'] = PlayerData() self.blackboard['player'].monsters = [default_monster] # UI default_font = self.loader.load_font('fonts/BalooThambi2-Medium.ttf', pixelsPerUnit=90) p3d.TextNode.set_default_font(default_font) # Game states initial_state = p3d.ConfigVariableString('mercury-initial-state', 'Title').get_value() self.gman = gamestates.StateManager(initial_state) def update_state(task): self.gman.update() return task.cont self.taskMgr.add(update_state, 'GameState Update') # Get volume levels from config self.musicManager.set_volume( p3d.ConfigVariableDouble('audio-music-volume', 1.0).get_value()) self.sfxManagerList[0].set_volume( p3d.ConfigVariableDouble('audio-sfx-volume', 1.0).get_value())
def process(self, data): if data['type'] == 'events': for event in data['events']: self.process(event) elif data['type'] == 'loginsucceeded': self.log(data) self.play() # OK, we can switch to playing mode elif data['type'] == 'refresh': self.log(data) self.game.load(data['player_name'], data['zone_source'], data['players'], data['monsters'], data['npcs'], data['containers'], data['zone_data']) elif data['type'] == 'addplayer': self.log(data) self.game.players[data['name']] = Player( data['title'], data['gender'], data['body'], data['hairstyle'], data['haircolor'], data['armor'], data['head'], data['weapon'], data['x'], data['y']) message = "%s is here." % (data['name']) self.chatManager.add_message(message) elif data['type'] == 'dropplayer': self.log(data) self.game.players[data['name']].unload() del self.game.players[data['name']] message = "%s left." % (data['name']) self.chatManager.add_message(message) elif data['type'] == 'addmonster': self.log(data) self.game.monsters[data['name']] = Monster(data['title'], data['source'], data['x'], data['y']) elif data['type'] == 'addcontainer': self.log(data) self.game.containers[data['name']] = Container( data['title'], data['x'], data['y'], data['source'], data['source_w'], data['source_h'], data['source_x'], data['source_y']) elif data['type'] == 'dropcontainer': self.log(data) if self.game.containers.has_key(data['name']): del self.game.containers[data['name']] elif data['type'] == 'playerpath': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].path = data['path'] elif data['type'] == 'playermove': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].go(data['direction'], data['start'], data['end']) elif data['type'] == 'addnpc': self.log(data) self.game.npcs[data['name']] = Npc(data['title'], data['gender'], data['body'], data['hairstyle'], data['haircolor'], data['armor'], data['head'], data['weapon'], data['x'], data['y'], data['villan']) elif data['type'] == 'npcmove': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].go(data['direction'], data['start'], data['speed']) elif data['type'] == 'monstermove': self.log(data) if self.game.monsters.has_key(data['name']): self.game.monsters[data['name']].go(data['direction'], data['start'], data['speed']) elif data['type'] == 'playerstop': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].wait() elif data['type'] == 'playerslash': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].slash() #self.sounds.play('sword') title = self.game.players[data['name']].title dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s slashes %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'playerthrust': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].thrust() title = self.game.players[data['name']].title #self.sounds.play('melee') dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s stabs %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'playerbow': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].bow() title = self.game.players[data['name']].title dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s shoots %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'playercast': self.log(data) if self.game.players.has_key(data['name']): title = self.game.players[data['name']].title self.chatManager.add_message("%s casts!" % (title)) elif data['type'] == 'npcslash': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].slash() #self.sounds.play('sword') title = self.game.npcs[data['name']].title dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s slashes %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'npcthrust': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].thrust() title = self.game.npcs[data['name']].title #self.sounds.play('melee') dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s stabs %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'npcbow': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].bow() title = self.game.npcs[data['name']].title dam = data['dam'] target_title = data['target_title'] self.chatManager.add_message("%s shoots %s for %s damage!" % (title, target_title, dam)) elif data['type'] == 'npccast': self.log(data) if self.game.npcs.has_key(data['name']): title = self.game.npcs[data['name']].title self.chatManager.add_message("%s casts!" % (title)) elif data['type'] == 'inventory': self.log(data) self.inventoryManager = None self.inventoryManager = InventoryManager(self, data['inventory']) if self.characterManager: self.get_stats() elif data['type'] == 'shop': self.log(data) self.shopManager = ShopManager( self, data['inventory'], data['player_inventory']['inventory']) elif data['type'] == 'container': self.log(data) self.containerManager = ContainerManager(self, data['inventory']) elif data['type'] == 'questdialog': self.log(data) self.questDialogManager = None self.questDialogManager = QuestDialogManager( self, data['name'], data['title'], data['dialog']) elif data['type'] == 'questlog': self.log(data) self.questLogManager = None self.questLogManager = QuestLogManager(self, data['quests']) elif data['type'] == 'playerstats': self.log(data) self.characterManager = None self.characterManager = CharacterManager(self, data['stats']) elif data['type'] == 'setplayerhead': self.log(data) self.game.players[data['name']].set_head(data['head']) elif data['type'] == 'setplayerarmor': self.log(data) self.game.players[data['name']].set_armor(data['armor']) elif data['type'] == 'setplayerweapon': self.log(data) self.game.players[data['name']].set_weapon(data['weapon']) elif data['type'] == 'playerdie': self.log(data) if self.game.players.has_key(data['name']): self.game.players[data['name']].die() elif data['type'] == 'monsterattack': self.log(data) if self.game.monsters.has_key(data['name']): title = data['title'] dam = data['dam'] target = data['target'] self.game.monsters[data['name']].attack() self.chatManager.add_message("%s hits %s for %s damage!" % (title, target, dam)) elif data['type'] == 'monsterstop': self.log(data) if self.game.monsters.has_key(data['name']): self.monsters[data['name']].stop() elif data['type'] == 'monsterdie': self.log(data) if self.game.monsters.has_key(data['name']): self.game.monsters[data['name']].die() self.chatManager.add_message("%s dies!" % data['title']) elif data['type'] == 'dropmonster': self.log(data) if self.game.monsters.has_key(data['name']): self.game.monsters[data['name']].unload() del self.game.monsters[data['name']] elif data['type'] == 'npcdie': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].die() self.chatManager.add_message("%s dies!" % data['title']) elif data['type'] == 'dropnpc': self.log(data) if self.game.npcs.has_key(data['name']): self.game.npcs[data['name']].unload() del self.game.npcs[data['name']] elif data['type'] == 'settarget': self.log(data) if data['objtype'] == 'Monster': self.game.player_target = self.game.monsters[data['name']] elif data['objtype'] == 'Npc': self.game.player_target = self.game.npcs[data['name']] elif data['objtype'] == 'Player': self.game.player_target = self.game.players[data['name']] elif data['objtype'] == 'Container': self.game.player_target = self.game.containers[data['name']] elif data['type'] == 'unsettarget': self.log(data) self.game.player_target = None elif data['type'] == 'playerchat': self.log(data) message = "<%s> %s" % (data['name'], data['message']) self.chatManager.add_message(message) elif data['type'] == 'message': self.log(data) self.chatManager.add_message(data['message']) elif data['type'] == 'tick': self.log(data) elif data['type'] == 'loginfailed': self.log(data)
class Game: def setup(self): """ Generates a hero, monster and door at random locations. """ player_start = GameMap.random_location() # Generate monster location. Make sure it is not the same as player location while True: monster_start = GameMap.random_location() if monster_start == player_start: continue else: break # Generate door location. Make sure it is not the same as player and monster location. while True: door_start = GameMap.random_location() if door_start == monster_start or door_start == player_start: continue else: break self.player = Hero(position=player_start) self.monster = Monster(position=monster_start) self.door = Door(position=door_start) self.player.visit_location(player_start) def player_turn(self): """ Asks the user to take an action to either attack or recover """ while True: action = input("Attack or Heal: ").lower().strip() if action == "heal": self.player.heal() print("new hp {}".format(self.player.hp)) break elif action == "attack": damage = self.player.make_attack() print("\nYou hit the monster for {} damage.".format(damage)) self.monster.hp -= damage break else: print("Invalid action, try again.\n") def monster_turn(self): """ The monster attemps to attack the player """ damage = self.monster.make_attack() print("The monster hits you for {} damage.\n".format(damage)) self.player.hp -= damage def fight(self): """ Handles the fight between the player and the monster. """ while True: print('-' * 30) print(self.player) print(self.monster) print('-' * 30) self.player_turn() if self.monster.hp <= 0: print("You have slain the monster and taken the key from it's dead body.\n") self.player.key = True self.monster.position = None break self.monster_turn() if self.player.hp <= 0: print("You have been killed by the monster. Game over!\n") sys.exit() def clear(self): """ Clears the screen """ os.system('cls' if os.name == 'nt' else 'clear') def __init__(self): self.clear() self.setup() message = None while True: self.clear() print("Hero: " + self.player.name.title() + " / Weapon: " + self.player.weapon.title()) self.player.draw_map(hero = self.player.position, monster=self.monster.position, door = self.door.position, visited=self.player.visited_list) if message: print(message) message = None if self.player.key == True: print("Inventory: Key") move = input("{}: ".format(self.player.get_valid_moves(self.player.position))).upper().strip() if self.player.is_valid_move(move, self.player.position): print("You moved {}.\n\n".format(move)) self.player.change_position(move) else: message="That is not a valid move." if self.player.position == self.door.position: if self.player.key: print("You found the exit. You win!") break else: message = "You found the exit, but the door is locked." elif self.player.position == self.monster.position: print("You have found the monster!\n") self.fight() print("Game over. You made {} moves.\n\n".format(len(self.player.visited_list)-1))
def monster(): from game.monster import Monster return Monster.make_new('id', 'test', 'bobcatshark')