Beispiel #1
0
class Trainer():
    def __init__(self, withGrapics=True):
        self.bestbest = 0
        self.done = False
        self.pool = Pool(Config.WIDTH, Config.HEIGHT, self.callback)
        self.graphics = Graphics()
        if withGrapics:
            self.graphics.init("PoolGame", Config.SIZE)

    def callback(self, gen, avg_score, best_score, best_json):
        line = "Generation\t%d\tAverage Score\t%f\tBest Score\t%f" % (
            gen, avg_score, best_score)
        self.write_log(Config.JSON_FOLDER + "/" + "logfile.txt", line)
        filename = "brain-g%03d-%04d.json" % (gen, best_score * 1000)

        if best_score >= self.bestbest:
            if best_json != None:
                self.write_file(Config.JSON_FOLDER + "/" + filename, best_json)
            self.bestbest = best_score

        if gen == Config.GENERATIONS:
            self.done = True

    def write_log(self, filename, line):
        print(line)
        with open(filename, "a") as outfile:
            outfile.write(line + "\n")

    def write_file(self, filename, data):
        with open(filename, "w") as outfile:
            outfile.write(data + "\n")

    def run(self):
        # Loop until the user clicks the close button.
        while not self.done:

            self.done = self.graphics.queryQuit()

            # Set the screen background
            self.graphics.fill(Config.GREEN)
            self.graphics.print("Clock: {}".format(self.graphics.fps()))

            # Do physics
            self.pool.tick()

            # Draw everything
            self.pool.draw(self.graphics)

            # Update screen
            self.graphics.flip()

        # Exit
        self.graphics.quit()
Beispiel #2
0
def main():
    """
    Main starting point for the program

    :return: N/A
    """
    Graphics.init()
    FPS.init()

    drawer = Drawer()

    key_down_listeners = [drawer.on_key_down]

    mouse_down_listeners = [drawer.on_mouse_down]

    mouse_up_listeners = [drawer.on_mouse_up]

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                if event.key == pygame.K_F10:
                    Graphics.toggle_fullscreen()

                for delegate in key_down_listeners:
                    delegate(event.key, event.mod)

            if event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:
                mouse_position = (event.pos[0] / Graphics.get_scale()[0],
                                  event.pos[1] / Graphics.get_scale()[1])

                if event.type == pygame.MOUSEBUTTONDOWN:
                    for delegate in mouse_down_listeners:
                        delegate(mouse_position, event.button)

                if event.type == pygame.MOUSEBUTTONUP:
                    for delegate in mouse_up_listeners:
                        delegate(mouse_position, event.button)

        drawer.update()

        drawer.draw()

        FPS.update()
        Graphics.flip()
Beispiel #3
0
class Doku():
    def __init__(self):
        self.solver = Solver(Config.SIZE, self.callback)
        self.done = False
        self.graphics = Graphics()
        self.graphics.init("Doku Solver", Config.WINDOWSIZE)

        if Config.SIZE == 4:
            self.solver.setGrid([
                "3002",
                "0410",
                "0320",
                "4001",
            ])

        if Config.SIZE == 9:
            self.solver.setGrid([
                "000000000",
                "000000000",
                "000000000",
                "384000000",
                "000000000",
                "000000000",
                "000000000",
                "000000000",
                "000000002",
            ])

            #self.solver.setGrid([
            #	"100904082",
            #	"052680300",
            #	"864200910",
            #	"010049806",
            #	"498300701",
            #	"607010093",
            #	"086035209",
            #	"509002130",
            #	"030497008"
            #]);

        if Config.SIZE == 16:

            self.solver.setGrid([
                " D0F  63C 7 1E  ", "  74    B 3 D   ", "E 4  97 0    3AF",
                " 2     E  516B9 ", "B A   E  8   F  ", "  F CA 6        ",
                "      4     5 E ", " 6C7  8  5B   2 ", " F B9   4 C  D 2",
                " 41D 6  5     C9", "2   7 1 D   B  8", "      A83   E   ",
                "C    B9 6  20   ", "  2  E30   C 546", " A 8 C   4     1",
                " 7   5       8D "
            ])

        if Config.SIZE == 25:
            for i in range(Config.SIZE):
                self.solver.setNumber(i, i, i + 1, True)
                self.solver.setNumber(0, i, Config.SIZE - (i + 1) + 1, True)

    def callback(self):
        # self.done = True
        pass

    def run(self):
        # Loop until the user clicks the close button.
        while not self.done:

            self.done = self.graphics.queryQuit()

            # Set the screen background
            self.graphics.fill(Config.WHITE)
            # self.graphics.print("Clock: {}".format(self.graphics.fps()))

            if Config.SPEED == Config.FAST:
                # Draw everything
                self.solver.draw(self.graphics)
                # Update screen
                self.graphics.flip()

            # Do physics
            self.solver.tick()

            # Draw everything
            self.solver.draw(self.graphics)

            # Update screen
            self.graphics.flip()

        # Exit
        self.graphics.quit()
class GameWindow:
    def __init__(self):
        self._window = None
        self._graphics = None
        self._sprite = None 
        self._running = True
        self._renderer = SDL_Renderer()
        self._player = None
        self._input = Input() 

    def gameLoop(self):
        sdl2.ext.init()
        self._window = sdl2.ext.Window("Game Window", size=(800, 600))
        self._graphics = Graphics(self._window)
        self._player = Player(self._graphics, 100, 100)
        self._window.show()

        LAST_UPDATE_TIME = 0

        while self._running:

            self._input.beginNewFrame()
            events = sdl2.ext.get_events()
            for event in events:
                if event.type == SDL_KEYDOWN:
                    if event.key.repeat == 0:
                        self._input.keyDownEvent(event)
                elif event.type == SDL_KEYUP:
                    self._input.keyUpEvent(event)
                
                elif event.type == sdl2.SDL_QUIT:
                    self._running = False
                    break

            if self._input.wasKeyPressed(SDL_SCANCODE_LEFT):
                self._player.moveLeft()
            elif self._input.wasKeyPressed(SDL_SCANCODE_RIGHT):
                self._player.moveRight()
            if not self._input.isKeyHeld(SDL_SCANCODE_LEFT) and not self._input.isKeyHeld(SDL_SCANCODE_RIGHT):
                self._player.stopMoving()

            CURRENT_TIME_MS = sdl2.SDL_GetTicks()
            ELAPSED_TIME_MS = CURRENT_TIME_MS - LAST_UPDATE_TIME
            self.update(min([ELAPSED_TIME_MS, MAX_FRAME_TIME]))
            LAST_UPDATE_TIME = CURRENT_TIME_MS

            self.draw()
            

        return 0
    
    def draw(self):
        self._graphics.clear()

        self._player.draw()
        self._graphics.flip()
    
    def update(self, elapsed_time):
        self._player.update(elapsed_time)
    
    def start(self):
        sys.exit(self.gameLoop())