def update(next): root.update_idletasks() root.update() if 0 < api.settings('tickSpeed', -1): root.after(int(1000 * 100. / api.settings('tickSpeed')), next) else: root.after(0, next)
def start(registered): """ Start the simulation. + Spawn a queen (and its nest) for each contestant registered (returned from `load`). + Starts the main sequence. + Enter the main loop. """ api.newline() api.info("Seed: " + str(api.settings('randomSeed'))) api.info("(you will need this seed to replay the exact same game.)") api.newline() graph.makeColorMap([main[1] for main in registered]) for main in registered: w, h = api.settings('worldSize') x, y = api.RNG.randrange(w), api.RNG.randrange(h) #x, y = world.generate.nextSpanwPosition() loop.world.addNest(Queen(x, y, main[0], main[1]).nest) loop.counter = 0 loop.mainseq = api.seqstart("game") api.info("------------------- Start of simulation -------------------") graph.start(loop)
def end(): """ End simulation. Also output the seed through `api.info`. """ graph.end() api.info("------------------- End of simulation -------------------") api.seqend(loop.mainseq) api.newline() api.info("Seed: " + str(api.settings('randomSeed'))) api.info("(you will need this seed to replay the exact same game.)")
def test(): """ Returns `True` if the simulation should be ended. Used in `loop` to check if the simulation is finished or should be stopped. In fact, the simulation is killed after `settings: turnsLimit`. Set this setting to a negative value to prevent this behavior. """ loop.counter += 1 l = api.settings('turnsLimit') return loop.world.isFinished() or l < loop.counter + 1 and 0 < l
def load(world): global ant, ant_res, res, rock, empty, wallColor, emptyColor, tileSize tileSize = api.settings('tileSize') ratio = 250 // tileSize dir = api.settings('texturesDirectory') res = tk.PhotoImage(file=dir + "res.png").subsample(ratio) rock = tk.PhotoImage(file=dir + "rock.png").subsample(ratio) empty = tk.PhotoImage(file=dir + "empty.png").subsample(ratio) for c in colors: d_ = dir + c + "/" ant[c] = tk.PhotoImage(file=d_ + "ant.png").subsample(ratio) ant_res[c] = tk.PhotoImage(file=d_ + "ant_res.png").subsample(ratio) queen[c] = [[None, None], [None, None]] for i in range(2): for j in range(2): filename = d_ + "queen{}{}.png".format(1-j, i) queen[c][i][j] = tk.PhotoImage(file=filename).subsample(ratio) wallColor = api.settings('wallColor') emptyColor = api.settings('emptyColor') # configure window vsb = tk.Scrollbar(root, orient=tk.VERTICAL) vsb.grid(row=0, column=1, sticky=tk.N + tk.S) hsb = tk.Scrollbar(root, orient=tk.HORIZONTAL) hsb.grid(row=1, column=0, sticky=tk.E + tk.W) canvas = tk.Canvas(root, yscrollcommand=vsb.set, xscrollcommand=hsb.set) canvas.grid(row=0, column=0, sticky="news") vsb.config(command=canvas.yview) hsb.config(command=canvas.xview) root.grid_rowconfigure(0, weight=1) root.grid_columnconfigure(0, weight=1) frame = tk.Frame(canvas) w, h = api.settings('worldSize') for i in range(w): grid.append([]) for j in range(h): b = tk.Label(frame, bg=wallColor, borderwidth=1, image=empty, \ text=" ", width=tileSize, height=tileSize, \ #command=lambda x=i, y=j: handlePress(world, x, y), \ fg=api.settings('textColor'), compound=tk.CENTER) b.grid(column=i, row=h-j+1) grid[-1].append(b) canvas.create_window(0, 0, window=frame) frame.update_idletasks() canvas.config(scrollregion=canvas.bbox("all")) root.bind_all("<MouseWheel>", lambda e: scroll(canvas, e))
def createInput(self, world): """ Crops map and selects pheromones to input """ s = api.settings('viewDistance') hs = s // 2 rMap = [[~api.UNKNOWN for k in range(s)] for k in range(s)] rAnts = [[False for k in range(s)] for k in range(s)] rPheros = [] rOnPos = None # creating pheromones list for ph in world.pheros: if ph.isInRange(self.x, self.y): rPheros.append(api.APhero(ph, self)) if self.x == ph.x and self.y == ph.y: rOnPos = ph # creating view map for i in range(s): for j in range(s) if i in [0, s - 1] else [0, s - 1]: if (i, j) == (hs, hs): rMap[i][j] = world[world.mapT, self.x, self.y] continue l = abs(i - hs) + abs(j - hs) u, v = float(i - hs) / l, float(j - hs) / l bla = False for k in range(l + 1): a, b = int(self.x + k * u), int(self.y + k * v) c, d = int(hs + k * u), int(hs + k * v) tile = world[world.mapT, a, b] if rMap[c][d] == ~api.UNKNOWN: rMap[c][d] = api.UNKNOWN if bla else tile if world[world.antT, a, b]: rAnts[c][d] = (api.AAnt if \ isinstance(world[world.antT, a,b], \ Ant) else api.AQueen) \ (world[world.antT, a, b], noMem=True) bla |= tile & api.WALL return rMap, rAnts, rPheros, rOnPos
def drawQueen(lowerX, lowerY, name): w, h = api.settings('worldSize') for i in range(2): for j in range(2): x, y = (lowerX + i) % w, (lowerY + j) % h grid[x][y].config(image=queen[colorMap[name]][i][j], bg=emptyColor)
def isInRange(self, posX, posY): w, h = api.settings('worldSize') a = min(abs(posX - self.x), abs(posX - w - self.x)) b = min(abs(posY - self.y), abs(posY - h - self.y)) return abs(a) + abs(b) < api.settings('pheroRange') - self.decay
stopped. In fact, the simulation is killed after `settings: turnsLimit`. Set this setting to a negative value to prevent this behavior. """ loop.counter += 1 l = api.settings('turnsLimit') return loop.world.isFinished() or l < loop.counter + 1 and 0 < l def end(): """ End simulation. Also output the seed through `api.info`. """ graph.end() api.info("------------------- End of simulation -------------------") api.seqend(loop.mainseq) api.newline() api.info("Seed: " + str(api.settings('randomSeed'))) api.info("(you will need this seed to replay the exact same game.)") if __name__ == '__main__': if os.path.isdir('./game/'): os.chdir('./game/') api.loadSettings() start(load(api.settings('playersDirectory'))) input("Press enter to continue. ")