def EvaluateMap(chromosome=[0.00, 0.10, 0.10, 0.00], economy=Economy(50)): # Init economy and map economy = Economy(50) gameMap = gamemap.GameMap(economy, 10, 10, *chromosome) output = Output() times = 30 mapcopy = copy.deepcopy(gameMap) armies = [ Game.selectArmy( economy, mapcopy, armyColor="white", output=Output(), aUnitPool=['SoldierClass', 'TechnicianClass', 'MageClass']) for _ in range(times) ] score = sum( [Game(economy, mapcopy, army, output, 0.0).run() for army in armies]) print(score / times) return score / times,
def printMap(params): economy = Economy(50) gameMap = gamemap.GameMap(economy, 10, 10, *params) output = ConsoleOutput() output.drawMap(gameMap, [], []) print(gameMap)
class Gamestate_Playing: game_map = gamemap.GameMap(10,10) def __init__(self): self.game_map = gamemap.GameMap(10, 10) def enter(self): None def update(self, events=()): for event in events: if event.type is pygame.QUIT: # quit game (window exit button clicked) env.Game.stopped = True elif event.type is pygame.KEYDOWN: # handle on-key-press logic, per key if event.key is env.Game.bindings["quit"]: env.Game.stopped = True None elif event.type is pygame.KEYUP: # handle on-key-release logic, per key None elif event.type is pygame.MOUSEBUTTONDOWN: # handle on-mouse-press logic, per button None elif event.type is pygame.MOUSEBUTTONUP: # handle on-mouse-release logic, per button None def render(self): self.game_map.render()
def __init__(self, xsize, ysize): self.title_loop = True self.game_loop = False self.pause_loop = False self.inventory_loop = False self.battle_loop = False self.map = gamemap.GameMap(0, xsize, ysize) wrapper(self._Graphics)
def __init__(self, economy, army, building): self.economy = economy self.army = army self.building = building self.curGround = self.building.trainingGround self.curMap = gamemap.GameMap( self.building.mapSize, self.building.mapSize, curGround["traps"], curGround["treasure"], curGround["soldiers"], curGround["towers"])
def generate(width: int, height: int) -> gamemap.GameMap: """Return a randomly generated GameMap.""" room_max_size = 10 room_min_size = 6 max_rooms = 30 AREA_BORDER = 20 gm = gamemap.GameMap(width, height) gm.tiles[...] = FLOOR rooms: List[Room] = [] for i in range(max_rooms): # random width and height w = random.randint(room_min_size, room_max_size) h = random.randint(room_min_size, room_max_size) # random position without going out of the boundaries of the map x = random.randint(AREA_BORDER, width - AREA_BORDER - w) y = random.randint(AREA_BORDER, height - AREA_BORDER - h) new_room = Room(x, y, w, h) if any(new_room.intersects(other) for other in rooms): continue # This room intersects with a previous room. # Mark room inner area as open. gm.tiles[new_room.outer] = WALL gm.tiles[new_room.inner] = FLOOR if rooms: # Open a tunnel between rooms. if random.randint(0, 99) < 80: # 80% of tunnels are to the nearest room. other_room = min(rooms, key=new_room.distance_to) else: # 20% of tunnels are to the previous generated room. other_room = rooms[-1] t_start = new_room.center t_end = other_room.center if random.randint(0, 1): t_middle = t_start[0], t_end[1] else: t_middle = t_end[0], t_start[1] gm.tiles[tcod.line_where(*t_start, *t_middle)] = FLOOR gm.tiles[tcod.line_where(*t_middle, *t_end)] = FLOOR rooms.append(new_room) # Add player to the first room. gm.player = fighter.Player.spawn(gm[5, height - 5], ai_cls=ai.PlayerControl) gm.actors.append(gm.player) for room in rooms: room.place_entities(gm) gm.update_fov() return gm
army[index].strategy.riskiness = { "SoldierClass": evolvedParams[2], "TrapClass": evolvedParams[3], "TowerClass": evolvedParams[4] } # Base probability to get closer to interact army[index].strategy.fasting = evolvedParams[5] # Base probability to return if hungry army[index].strategy.greed = evolvedParams[6] for u in army[:5]: print u.strategy.curiosity # scores.sort(reverse=True) # for score in scores[:5]: # print(score) return army if __name__ == "__main__": economy = Economy() output = Output() curmap = gamemap.GameMap(economy) army = Game.selectArmy( economy, curmap, armyColor="white", output=Output(), aUnitPool=['SoldierClass', 'TechnicianClass', 'MageClass'])
del mapInstance # Actually render pygame.display.flip() def saveToFile(self, sFilename): fOut = open(sFilename, 'w') fOut.write(self.msg) fOut.close() if __name__ == "__main__": # Init economy and map economy = Economy(5000) gameMap = gamemap.GameMap(economy, 20, 20) output = Output() # Init army # Set colors sAttackerColor = (255, 255, 255) army = Game.selectArmy( economy, gameMap, sAttackerColor, output, [ "AssassinClass", "BarbarianClass", "DruidClass", "EnchanterClass", "KnightClass",
def __init__(self): self.running = True # set the game loop good to go config = ConfigParser.ConfigParser() config.readfp(open('main.cfg')) self.fsw = config.getint('main', 'fullscreenwidth') self.fsh = config.getint('main', 'fullscreenheight') self.ww = config.getint('main', 'windowedwidth') self.wh = config.getint('main', 'windowedheight') self.tw = config.getint('main', 'tilewidth') self.mapw = config.getint('main', 'mapwidth') self.maph = config.getint('main', 'mapheight') self.fullscreen = config.getboolean('main', 'fullscreen') self.paused = False self.white = (255, 255, 255) # the colour :) #fullscreen = False self.FPS = 60 pygame.init() #setup the default screen size if self.fullscreen == True: self.screen = pygame.display.set_mode((self.fsw, self.fsh), FULLSCREEN) else: self.screen = pygame.display.set_mode((self.ww, self.wh), RESIZABLE) pygame.display.set_caption('pyDF') #Intro's on by default, will need to add a config file entry for this. self.intro = True self.mainclock = pygame.time.Clock() # various rendering offsets self.vpRenderOffset = (self.tw, self.tw) self.statsOffset = (math.floor(int(0.8 * self.ww) + 2 * self.tw), self.tw) self.menuOffset = (math.floor(int(0.8 * self.ww) + 2 * self.tw), 4 * self.tw) self.menuOffset2 = (math.floor(int(0.8 * self.ww) + 2 * self.tw), 5 * self.tw) self.menuOffset3 = (math.floor(int(0.8 * self.ww) + 2 * self.tw), 6 * self.tw) # testing for ezmenu self.idleOffset = (math.floor(int(0.8 * self.ww) + 2 * self.tw), 0) # testing for ezmenu self.pauseDisplayOffset = (self.tw, 0) self.digTypeOffset = (math.floor(int(0.8 * self.ww) + 2 * self.tw), 4 * self.tw) self.showmenu = False self.currentmenu = None self.vpCoordinate = [0, 0] # Starting coordinates for the view port self.vpDimensions = (math.floor(int(0.8 * self.ww) / self.tw) * self.tw, math.floor(int(0.9 * self.wh) / self.tw) * self.tw) # resolution of the view port self.vpStep = self.tw # move 1 tile over. self.vpShiftStep = self.tw * 10 # move 10 tile over. self.minHorizScrollBound = 0 self.minVertScrollBound = 0 self.maxHorizScrollBound = self.mapw * self.tw self.maxVertScrollBound = self.maph * self.tw self.numXTiles = int(math.ceil(int(self.vpDimensions[0]) / self.tw)) # the number of tiles to be shown at one time for X self.numYTiles = int(math.ceil(int(self.vpDimensions[1]) / self.tw)) # the number of tiles to be shown at one time for y self.startXTile = math.floor(int(self.vpCoordinate[0]) / self.tw) self.startYTile = math.floor(int(self.vpCoordinate[1]) / self.tw) if not pygame.font.get_init(): pygame.font.init() self.arialFnt = pygame.font.SysFont('Arial', 16) self.m = gamemap.GameMap(self.tw, self.mapw, self.maph, self.startXTile, self.startYTile, self.numXTiles, self.numYTiles) self.m.initMap() self.m.initEMap() self.editmode = [None, None] self.selectmode = False self.testmode = False self.roomtiles = [] self.selectcursor = cursor.Cursor(self.tw, self.m.numXTiles / 2 * self.tw, self.m.numYTiles / 2 * self.tw) self.queued_jobs = [] self.mobs = [] self.mapvalue = None self.tilecontent = None #self.currentZlevel = self.m.currentZlevel openspot = self.m.find_open_spot() self.currentZlevel = openspot[2] self.mobs.append(mob.Mob(openspot, self.tw)) self.addTileMob(openspot[0], openspot[1], openspot[2], self.mobs[0]) self.mobs.append(mob.Mob(openspot, self.tw)) self.addTileMob(openspot[0], openspot[1], openspot[2], self.mobs[0]) self.mobs.append(mob.Mob(openspot, self.tw)) self.addTileMob(openspot[0], openspot[1], openspot[2], self.mobs[0]) self.buttons = {} #self.keys = set() self.motion = None
return (self.gameTime % math.floor(1000.0 / actor.attackSpeed)) == 0 def getScore(self, iGameTime, aSurvivors, aDead): dScore = iGameTime for curSurvivor in aSurvivors: dScore += self.economy.cost(curSurvivor) + curSurvivor.score # Also take into account dead for soldier, dieTime in self.dead: dScore += (soldier.score / 2) * float(dieTime) / float(iGameTime) return dScore if __name__ == "__main__": # Init economy and map economy = Economy(500) gameMap = gamemap.GameMap(economy, 10, 10, 0.1, 0.1, 0.05, 0.05) # Init messaging output = ConsoleOutput() # Init army # Set colors sAttackerColor = "white" army = Game.selectArmy(economy, gameMap, sAttackerColor, output) # Output game data sGameStats = ("Traps: %d, Treasures: %d, Foes: %d, Army: %d" % (len( gameMap.traps), len(gameMap.treasures), len(gameMap.foes), len(army))) # Init game g = Game(economy, gameMap, army, output, 0.05) evaluation = g.run() print("Final evaluation of the map", evaluation)
def timeToAct(self, actor): return (self.gameTime % math.floor(1000.0 / actor.attackSpeed)) == 0 def getScore(self, iGameTime, aSurvivors, aDead): dScore = iGameTime for curSurvivor in aSurvivors: dScore += self.economy.cost(curSurvivor) + curSurvivor.score # Also take into account dead for soldier, dieTime in self.dead: dScore += (soldier.score / 2) * float(dieTime) / float(iGameTime) return dScore if __name__ == "__main__": # Init economy and map economy = Economy(50) gameMap = gamemap.GameMap(economy, 30, 30, 0.00, 0.10, 0.10, 0.00) # Init messaging output = ConsoleOutput() # Init army # Set colors sAttackerColor = "white" army = Game.selectArmy(economy, gameMap, sAttackerColor, output, ['BarbarianClass']) # Init game g = Game(economy, gameMap, army, output, 0.05) # Init army # Set colors sAttackerColor = "white" army = Game.selectArmy(economy, gameMap, sAttackerColor, output, ['SoldierClass', 'TechnicianClass', 'MageClass'])
import sys, pygame, character, gamemap, socket, enemy, random pygame.init() pygame.display.set_caption('AP Game') black = 0, 0, 0 size = width, height, = 640, 480 screen = pygame.display.set_mode(size) playerblock = pygame.image.load("player.png") player = character.Character(50, 215, 30, 30, playerblock) enemy1 = pygame.image.load("enemy1.png") background = pygame.image.load("background.png") frame = 300 frames = 0 frames2 = 0 maps = gamemap.GameMap("map.txt", "spots.txt") player2x = -50 player2y = -50 enemies = [] IP = socket.gethostbyname(socket.gethostname()) PORT = 9000 BUFFER_SIZE = 1024 TIME = 600 if len(sys.argv) > 3: player.setX(int(sys.argv[2])) player.setY(int(sys.argv[3])) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) def render():
def __init__(self): self.game_map = gamemap.GameMap(10, 10)
import sys, pygame, character, gamemap, socket pygame.init() pygame.display.set_caption('AP Game') black = 0, 0, 0 size = width, height, = 640, 480 screen = pygame.display.set_mode(size) playerblock = pygame.image.load("player.png") player = character.Character(50, 215, 30, 30, playerblock) background = pygame.image.load("background.png") frame = 300 frames = 0 maps = gamemap.GameMap("map.txt") player2x = -50 player2y = -50 IP = sys.argv[1] PORT = 9000 BUFFER_SIZE = 1024 if len(sys.argv) > 3: player.setX(int(sys.argv[2])) player.setY(int(sys.argv[3])) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) def render(): screen.fill(black) screen.blit(background, (0, 0)) player.render(screen)
def getGameMap(self, bReset = False): if self.gameMap is None or bReset: economy = self.getEconomy(); self.gameMap = gamemap.GameMap(economy, 10, 10); return self.gameMap