Esempio n. 1
0
    def __init__(self, seed, setContextFunc, population=None, train=True):
        BaseContext.__init__(self, setContextFunc)
        self.seed = seed
        self.pop = Population(seed, 100) if population is None else population
        if train:
            self.worlds = []
            for net in sorted(self.pop.current_generation, key=lambda x: x.fitness, reverse=True):
                nWorld = NeuronalWorld(self.pop.seed, net)
                nWorld.renderer = RenderNeuronalWorld(nWorld)
                self.worlds.append(nWorld)
                nWorld.generatePlatform()
        else:
            best_nn = max((n for n in self.pop.current_generation), key=lambda x: x.fitness)
            nWorld = NeuronalWorld(self.pop.seed, best_nn)
            nWorld.renderer = RenderNeuronalWorld(nWorld)
            self.worlds = [nWorld]
        self.drawmode = 0
        self._train = train

        # the gui overlays
        self._overlays = texhandler.adjustedSurface(texhandler.Textures.overlays, height=48)

        fontObj = SysFont("Monospace", 30, bold=True)
        # mode switch buttons
        self.addElements({
            "bNext": GuiButton(constants.screenWidth - 100, constants.screenHeight - 70, fontObj, "->",
                               width=70).connect(self.buttonModeSwitch, 1),
            "bPrevious": GuiButton(30, constants.screenHeight - 70, fontObj, "<-", width=70).connect(
                self.buttonModeSwitch, -1)
        })
    def __init__(self, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._background = texturehandler.fillSurface(
            pygame.Surface(const.screenSize),
            texturehandler.Textures.cobblestone, (64, 64))
        # adjust logo to dimensions (adjustedWidth, 120)
        self._logo = texturehandler.adjustedSurface(
            texturehandler.Textures.logo, height=130)

        fontObj = Font(None, 40)
        self.addElements({
            "bStartGame":
            GuiButton(240, 200, fontObj,
                      "Start Game").connect(self.buttonStartGame),
            "bNeuronalNetworking":
            GuiButton(240, 280, fontObj,
                      "Neuronal Networking").connect(self.buttonNetworks),
            "bOptions":
            GuiButton(240, 440, fontObj,
                      "Options").connect(self.buttonOptions),
            "bHelp":
            GuiButton(240, 520, fontObj, "Help").connect(self.buttonHelp),
            "bQuit":
            GuiButton(240, 600, fontObj, "Quit").connect(self.closeApp)
        })

        Music.setVolume(Entries.MusicVolume.getCurrentValue())
        Music.play(Music.bgMusic, 0)
    def __init__(self, networkContext, setContextFunc, popFileName):
        BaseContext.__init__(self, setContextFunc)
        self._networkContext = networkContext
        self._popFileName = popFileName
        self._pop = Population.load_from_file(
            constants.res_loc("networks") + popFileName)

        self._background = texturehandler.fillSurface(
            pygame.Surface(constants.screenSize),
            random.choice(texturehandler.blocks), (64, 64))

        best_fitness = max(n.fitness for n in self._pop.current_generation)
        fontObj = Font(None, 40)
        self.addElements({
            "lCaption":
            GuiLabel.createCentered(10, Font(None, 60), self._pop.name),
            "lSeed":
            GuiLabel(240, 90, fontObj, "Current seed:"),
            "tfSeed":
            GuiNumberTextfield(450,
                               87,
                               SysFont("Monospace", 24, bold=True),
                               width=140,
                               text=str(self._pop.seed)),
            "bSeed":
            GuiButton(600, 87, fontObj, "Set Seed", width=200,
                      height=32).connect(self.buttonSetSeed),
            "lSize":
            GuiLabel(
                240, 130, fontObj, "Population size: {}".format(
                    len(self._pop.current_generation))),
            "lFitness":
            GuiLabel(240, 170, fontObj,
                     "Highest fitness: {0:.2f}".format(best_fitness)),
            "lGeneration":
            GuiLabel(240, 210, fontObj,
                     "Generation: {}".format(self._pop.generation_count)),
            "bDelete":
            GuiButton(390,
                      470,
                      fontObj,
                      "Delete (hold CTRL)",
                      width=300,
                      height=40,
                      startColor=(255, 50, 50),
                      endColor=(255, 100, 100)).connect(self.buttonDelete),
            "bShowResult":
            GuiButton(240, 530, fontObj, "Show Result",
                      width=285).connect(self.buttonShowResult),
            "bResumeTraining":
            GuiButton(555, 530, fontObj, "Resume Training",
                      width=285).connect(self.buttonResumeTraining),
            "bBack":
            GuiButton(240, 600, fontObj, "Back").connect(self.buttonBack)
        })

        # enable key repeats
        pygame.key.set_repeat(500, 50)
Esempio n. 4
0
 def draw(self, screen):
     if self.worlds:
         [self.drawSimple, self.drawNetwork, self.drawSummary, self.drawOverview][self.drawmode](screen)
     BaseContext.draw(self, screen)
     # draw current generation
     renderedGen = SysFont("Monospace", 40, bold=True).render(str(self.pop.generation_count), 1, (50, 50, 50))
     # draw generation overlay (dinosaur egg)
     screen.blit(self._overlays, (485, 650), (336, 0, 48, 48))
     screen.blit(renderedGen, (540, 655))
    def __init__(self, mainMenuContext, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._mainMenuContext = mainMenuContext
        self._background = texturehandler.fillSurface(pygame.Surface(screenSize), random.choice(texturehandler.blocks),
                                                      (64, 64))
        self._files = []
        self.initGuiElements()

        # enable key repeats
        pygame.key.set_repeat(500, 50)
    def __init__(self, parent, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._parent = parent
        self._background = texturehandler.fillSurface(
            pygame.Surface(screenSize), random.choice(texturehandler.blocks),
            (64, 64))

        fontObj = Font(None, 40)
        self.addElements({
            "lCaption":
            GuiLabel.createCentered(10, Font(None, 60), "Options"),
            "cEntries":
            GuiContainer(240, 77, 600, 450, fontObj),
            "bRestoreDefaults":
            GuiButton(390, 580, fontObj, "Restore Defaults",
                      width=300).connect(self.buttonRestoreDefaults),
            "bBack":
            GuiButton(240, 650, fontObj, "Cancel",
                      width=285).connect(self.buttonBack),
            "bSave":
            GuiButton(555, 650, fontObj, "Save",
                      width=285).connect(self.buttonSave)
        })

        # add config Entry buttons
        y = 10
        for entry in config.Entries:
            self._elements["cEntries"].addElement(
                "l" + entry.name,
                GuiLabel(50, y + 16, fontObj, entry.desc + ":"))

            if entry.entryType == config.EntryType.Key or entry.entryType == config.EntryType.Toggle:
                self._elements["cEntries"].addElement(
                    "b" + entry.name,
                    GuiButton(300, y, fontObj, str(entry),
                              width=200).connect(self.buttonConfigEntry,
                                                 entry))
            elif entry.entryType == config.EntryType.Scroll:
                self._elements["cEntries"].addElement(
                    "b" + entry.name,
                    GuiScrollbar(300,
                                 y + 5,
                                 200,
                                 40,
                                 fontObj,
                                 value=entry.getCurrentValue(),
                                 barLength=20).connect(self.scrollConfigEntry,
                                                       entry))
            y += 70
 def handleEvent(self, event):
     if BaseContext.handleEvent(self, event):
         return True
     elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
         self._setContextFunc(self._gameContext)
         return True
     return False
    def __init__(self, gameContext, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._gameContext = gameContext

        fontObj = Font(None, 40)
        self.addElements({
            "bResume":
            GuiButton(240, 200, fontObj, "Resume").connect(self.buttonResume),
            "bOptions":
            GuiButton(240, 300, fontObj,
                      "Options").connect(self.buttonOptions),
            "bHelp":
            GuiButton(240, 400, fontObj, "Help").connect(self.buttonHelp),
            "bMainMenu":
            GuiButton(240, 500, fontObj,
                      "Back to main menu").connect(self.buttonMainMenu)
        })
    def __init__(self, parent, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._parent = parent
        self._background = texturehandler.fillSurface(
            Surface(constants.screenSize),
            random.choice(texturehandler.blocks), (64, 64))
        self._overlays = texturehandler.adjustedSurface(
            texturehandler.Textures.overlays, height=48)

        self.addElements({
            "lCaption":
            GuiLabel.createCentered(10, Font(None, 60), "Help"),
            "bBack":
            GuiButton(240, 600, Font(None, 40),
                      "Back").connect(self.buttonBack)
        })
        self.addIconHelp()
Esempio n. 10
0
    def update(self, t):
        BaseContext.update(self, t)

        done = True
        for world in self.worlds:
            if world.update(constants.UPS):
                done = False

        if done and self._train:
            self.pop.save_to_file(constants.res_loc("networks") + self.pop.name + ".pop")
            self.pop.create_next_generation()
            self.pop.generation_count += 1

            self.worlds = []
            for net in self.pop.current_generation:
                nWorld = NeuronalWorld(self.pop.seed, net)
                nWorld.renderer = RenderNeuronalWorld(nWorld)
                self.worlds.append(nWorld)
                nWorld.generatePlatform()
    def handleEvent(self, event):
        if BaseContext.handleEvent(self, event):
            return True

        from context.gamepausecontext import GamePauseContext
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self._setContextFunc(
                    GamePauseContext(self, self._setContextFunc))

        return False
Esempio n. 12
0
    def handleEvent(self, event):
        if BaseContext.handleEvent(self, event):
            return True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                from context.gamepausecontext import GamePauseContext
                self._setContextFunc(GamePauseContext(self, self._setContextFunc))
            elif event.key == pygame.K_TAB:
                self.drawmode = (self.drawmode + 1) % 4
        return False
    def __init__(self, gameContext, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._gameContext = gameContext
        self._fontObj = Font(None, 40)

        self.addElements({
            "lGameOver":
            GuiLabel.createCentered(120, Font(None, 130), "Game Over!",
                                    (255, 0, 0)),
            "lSeed":
            GuiLabel.createCentered(
                220, self._fontObj,
                "Your score is " + str(int(gameContext._world.points)) +
                ", Your seed: " + str(gameContext._world.seed), (0, 0, 0)),
            "bRetry":
            GuiButton(240, 520, self._fontObj,
                      "Retry").connect(self.buttonRetry),
            "bMainMenu":
            GuiButton(240, 600, self._fontObj,
                      "Back to main menu").connect(self.buttonMainMenu)
        })
Esempio n. 14
0
    def __init__(self, mainMenuContext, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self._mainMenuContext = mainMenuContext
        fontObj = Font(None, 40)
        self._background = texturehandler.fillSurface(
            pygame.Surface(screenSize), random.choice(texturehandler.blocks),
            (64, 64))

        self.addElements({
            "lCaption":
            GuiLabel.createCentered(10, Font(None, 60), "Starting new Game"),
            "lSeed":
            GuiLabel.createCentered(280, fontObj,
                                    "Seed for the World Generator"),
            "tfSeed":
            GuiNumberTextfield(430,
                               325,
                               SysFont("Monospace", 38, bold=True),
                               width=220),
            "lHint":
            GuiLabel.createCentered(400,
                                    fontObj,
                                    "leave blank for a random seed",
                                    color=(200, 200, 200)),
            "bBack":
            GuiButton(240, 500, fontObj, "Back",
                      width=285).connect(self.buttonBack),
            "bStartGame":
            GuiButton(555, 500, fontObj, "Start Game",
                      width=285).connect(self.buttonStartGame)
        })

        self._elements["tfSeed"].setFocused(True)

        # enable key repeats
        pygame.key.set_repeat(500, 50)
 def update(self, t):
     BaseContext.update(self, t)
     if not self._world.update(t):
         self._setContextFunc(GameOverContext(self, self._setContextFunc))
    def __init__(self, seed, setContextFunc):
        BaseContext.__init__(self, setContextFunc)
        self.setWorld(World(seed))

        # disable key repeats
        pygame.key.set_repeat()
 def draw(self, screen):
     screen.blit(self._background, (0, 0))
     BaseContext.draw(self, screen)
Esempio n. 18
0
 def calculateDelta(self, clock):
     if self._train:
         return constants.UPS
     else:
         return BaseContext.calculateDelta(self, clock)
 def draw(self, screen):
     self._gameContext.draw(screen)
     BaseContext.draw(self, screen)
    def draw(self, screen):
        screen.blit(self._background, (0, 0))
        BaseContext.draw(self, screen)

        for i, (x, y) in enumerate(self._positions):
            screen.blit(self._overlays, (50 + x, 70 + y), (i * 48, 0, 48, 48))
 def draw(self, screen):
     screen.blit(self._background, (0, 0))
     screen.blit(self._logo,
                 ((const.screenWidth - self._logo.get_width()) // 2, 35))
     BaseContext.draw(self, screen)