def main(): n = 200000 m = 200 print 'How fast can a world with {0} items and {1} creatures run.'.format(n, m) pygame.init() game = Game((256,256,128)) user = Player(game.world) for i in range(n): game.world.additem(Pickax((randint(0,255),randint(0,255),64), choice(Metal.__subclasses__()), choice(Wood.__subclasses__()))) for i in range(m): game.schedule(Dwarf(user, (randint(0,255),randint(0,255),64))) display.set_mode((1400,800), HWSURFACE | RESIZABLE) renderers = [Renderer(game, user, DisplayOptions('FreeMono.ttf', False, 16, 18))] game_acc = 0 render_acc = 0 last = time() while renderers: current = time() delta = min(0.125, max(0, current - last)) renderer = renderers[-1] if renderer.game: game_acc += delta while game_acc > renderer.game.dt: renderer.game.step() game_acc -= renderer.game.dt rest = renderer.game.dt - game_acc else: rest = float('inf') render_acc += delta if render_acc > renderer.dt: child = renderer.step() if child != renderer: if child: renderers.append(child) else: renderers = renderers[:-1] render_acc = 0 last = current sleep(min(rest, renderers[-1].dt if renderers else 0))
def main(): print "Dwarf should not keep moving barrels around." game = Game((10, 6, 128)) renderer = Renderer(game) game.schedule(Dwarf((3, 3, 64))) for i in range(4): barrel = Item(Barrel, (8, i, 64), 1.0) barrel.contents.append(Item(Beverage, None, Barrel.capacity)) game.world.additem(barrel) game.world.addstockpile(Stockpile([(2, 5, 64)], [Beverage.stocktype])) game.world.addstockpile(Stockpile([(4, 5, 64)], [Beverage.stocktype])) while not game.done: game.step() renderer.step()
def main(): print 'Dwarf should store barrel in stockpile, then drink from it.' game = Game((10,6,128)) renderer = Renderer(game) dwarf = Dwarf((3,3,64)) dwarf.hydration = 120 game.schedule(dwarf) barrel = Item(Barrel, (8,4,64), 1.0) barrel.contents.append(Item(Beverage, None, Barrel.capacity)) game.world.additem(barrel) game.world.addstockpile(Stockpile([(2,5,64)], [Beverage.stocktype])) while not game.done: pre = barrel.contents[0].materials[0].amount game.step() if barrel.contents[0].materials[0].amount != pre: print 'Dwarf drank' renderer.step()
class MainMenu(object): dt = 0.01 def __init__(self): self.game = None pygame.init() key.set_repeat(100, 100) self.zoom = DisplayOptions('FreeMono.ttf', False, 16, 18) self.definefont() self.makescreen((1400,800)) display.set_caption(_(u'Hex Grid')) self.child = None self.done = False def definefont(self): self._font = self.zoom.font def makescreen(self, size): self.screen = display.set_mode(size, HWSURFACE | RESIZABLE) self.draw() def draw(self): self.screen.fill((0,0,0)) hotkeys = [] self.buttons = [ Button(self.zoom, hotkeys, _(u'New Game'), self.newgame, True), Button(self.zoom, hotkeys, _(u'Make World'), self.world, True), Button(self.zoom, hotkeys, _(u'Quit'), self.quitgame, True) ] size = self.screen.get_size() y = size[1]/2 for b in self.buttons: b.location = (size[0]/2, y) b.draw(self.screen) y += b.size[1] def newgame(self): self.game = Game((156, 104, 128)) user = Player(self.game.world) for i in range(20): x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] self.game.world.space.maketree((x,y, self.game.world.space.groundlevel(x,y))) kind = (Goblin,Tortoise,SmallSpider) for i in range(20): race = choice(kind) x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] creature = race(None, (x,y,self.game.world.space.groundlevel(x,y))) self.game.schedule(creature) for i in range(7): x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] creature = Dwarf(user, (x,y,self.game.world.space.groundlevel(x,y))) self.game.schedule(creature) x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] loc = x, y, self.game.world.space.groundlevel(x,y) handle = Handle(choice(Wood.__subclasses__()), loc) blade = PickaxHead(choice(Metal.__subclasses__()), loc) self.game.world.additem(handle) self.game.world.additem(blade) x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] loc = x, y, self.game.world.space.groundlevel(x,y) handle = Handle(choice(Wood.__subclasses__()), loc) blade = AxHead(choice(Metal.__subclasses__()), loc) self.game.world.additem(Ax(loc, handle, blade)) x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] self.game.world.additem(Workbench((x,y, self.game.world.space.groundlevel(x,y)), choice(Wood.__subclasses__()))) for i in range(10): x, y = [randint(0, self.game.dimensions[i]-1) for i in range(2)] self.game.world.additem(Barrel((x,y, self.game.world.space.groundlevel(x,y)), choice(Wood.__subclasses__()))) self.child = Renderer(self.game, user, self.zoom) user.foundsettlement('the fortress') def world(self): self.child = RenderWorld(self.zoom) def quitgame(self): self.done = True def step(self): if self.child: self.definefont() self.makescreen(self.screen.get_size()) self.child = None if not self.child: self.world() else: self.done = True for e in event.get(): if e.type == QUIT: self.done = True elif e.type == KEYDOWN: if e.key == K_ESCAPE: self.done = True elif e.type == MOUSEBUTTONUP: if e.button == 4: # zoom in self.zoom.width += 2 self.zoom.height += 2 self.definefont() self.draw() elif e.button == 5: # zoom out self.zoom.width = max(self.zoom.width - 2, 2) self.zoom.height = max(self.zoom.height - 2, 4) self.definefont() self.draw() elif e.type == VIDEORESIZE: self.makescreen(e.size) for b in self.buttons: b.handle(e) display.flip() if self.done: return None elif self.child: return self.child else: return self