예제 #1
0
def main():
    pygame.mixer.pre_init(44100, -16, 2, 4096)
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    max_frame_rate = 60

    dashboard = Dashboard("./img/font.png", 8, screen)
    sound = Sound()
    level = Level(screen, sound, dashboard)
    menu = Menu(screen, dashboard, level, sound)

    while not menu.start:
        menu.update()

    mario = Mario(0, 0, level, screen, dashboard, sound)
    clock = pygame.time.Clock()

    while not mario.restart:
        pygame.display.set_caption(
            str(
                round(float("{:.2f} FPS".format(clock.get_fps()).split(" ")
                            [0]))) + " FPS")
        level.drawLevel(mario.camera)
        dashboard.update()
        mario.update()
        pygame.display.update()
        clock.tick(max_frame_rate)
    main()
예제 #2
0
def main():
    pygame.mixer.pre_init(44100, -16, 2, 4096)
    pygame.init()
    screen = pygame.display.set_mode(windowSize)
    max_frame_rate = 60
    dashboard = Dashboard("./img/font.png", 8, screen)
    sound = Sound()
    level = Level(screen, sound, dashboard)
    menu = Menu(screen, dashboard, level, sound)

    while not menu.start:
        menu.update()

    mario = Mario(0, 0, level, screen, dashboard, sound)
    clock = pygame.time.Clock()

    while not mario.restart:
        pygame.display.set_caption("Super Mario running with {:d} FPS".format(int(clock.get_fps())))
        if mario.pause:
            mario.pauseObj.update()
        else:
            level.drawLevel(mario.camera)
            dashboard.update()
            mario.update()
        pygame.display.update()
        clock.tick(max_frame_rate)
    return 'restart'
예제 #3
0
def main():
    pygame.mixer.pre_init(44100, -16, 2, 2048)
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    max_frame_rate = 60

    level = Level(screen)
    dashboard = Dashboard("./img/font.png", 8, screen)
    mario = Mario(0, 0, level, screen, dashboard)
    input = Input(mario)
    clock = pygame.time.Clock()

    while (not mario.restart):
        pygame.display.set_caption("{:.2f} FPS".format(clock.get_fps()))
        level.drawLevel(mario.camera)
        dashboard.update()
        input.checkForInput()
        mario.update()
        pygame.display.update()
        clock.tick(max_frame_rate)
    mario.sound.shutdown()
    main()
예제 #4
0
    def init_game(self, y_position=0, coins=0, points=0, time=0, clock=0):
        if not self.headless:
            pygame.mixer.pre_init(44100, -16, 2, 4096)
            pygame.init()
            self.screen = pygame.display.set_mode((640, 480))
            self.dashboard = Dashboard("./img/font.png",
                                       8,
                                       self.screen,
                                       coins=0,
                                       points=0,
                                       time=0)
            self.sound = Sound()
            self.level = Level(self.screen, self.sound, self.dashboard,
                               self.levelname)
            self.menu = Menu(self.screen, self.dashboard, self.level,
                             self.sound)
            self.menu.update()

            self.mario = Mario(0, y_position / 32, self.level, self.screen,
                               self.dashboard, self.sound)
            if self.experiment == 'FD':
                self.mario2 = Mario(0, 2, self.level, self.screen,
                                    self.dashboard, self.sound)
            self.clock = pygame.time.Clock()

            self.menu.dashboard.state = "play"
            self.menu.dashboard.time = 0
            self.menu.start = True

            pygame.display.update()
        else:
            self.level = LevelHeadless(self.levelname)
            if self.experiment == 'FD':
                self.mario2 = MarioHeadless(0, 2, self.level)
            self.mario = MarioHeadless(0, 0, self.level)
            self.clock = clock
예제 #5
0
    def main(self):
        # Initializing the game, graphics and sound
        pygame.mixer.pre_init(44100, -16, 2, 4096)
        pygame.init()
        screen = pygame.display.set_mode(self.windowSize)
        max_frame_rate = 9999
        dashboard = Dashboard("./img/font.png", 8, screen)
        sound = Sound()

        maxReward = 0
        timesMaxedTheReward = 0
        winStreak = 0
        maxWinStreak = 0

        differentialFramesCount = 10
        dxStartFrame = 0
        diffXOverFrames = 0
        dxConstant = 0
        lastX = 0

        epsCoef = 0

        for episodeCounter in range(0, self.experimentsCount):
            print("=============================================")
            print("============WELCOME TO EPISODE {0}============".format(
                episodeCounter))
            print("=============================================")
            framesCounter = 0

            stillLearning = episodeCounter <= self.learningPeriod

            if stillLearning and episodeCounter / self.experimentsCount >= self.globalStep / self.globalStepMax:
                self.decreaseLearningRate()
                self.printLearningRate()

            self.level = Level(screen, sound, dashboard)

            self.level.loadLevel('Level1-1')
            dashboard.coins = 0
            dashboard.points = 0

            self.mario = Mario(0, 0, self.level, screen, dashboard, sound)
            self.mario.setPos(0, 384)
            clock = pygame.time.Clock()

            max_x = 0
            obs = np.append(
                np.append(self.mario.getObservation(), framesCounter),
                self.level.getClosestEntityDistance(self.mario))

            finishedLevel = False

            while framesCounter < self.maxFrames and not finishedLevel:
                #print(framesCounter)
                pygame.display.set_caption(
                    "Super Mario running with {:d} FPS".format(
                        int(clock.get_fps())))
                if self.mario.pause:
                    self.mario.pauseObj.update()
                else:
                    self.level.drawLevel(self.mario.camera)
                    dashboard.update()
                    self.mario.update()
                    #print(self.mario.rect.x, self.mario.rect.y)
                    prevObs = obs
                    obs = np.append(
                        np.append(self.mario.getObservation(), framesCounter),
                        self.level.getClosestEntityDistance(self.mario))

                    reward = 0
                    actionIdentifier = self.player.agent.sample_action(
                        obs, self.eps)

                    if max_x < obs[0]:
                        max_x = obs[0]
                        reward += 1

                    # diffXOverFrames = self.mario.rect.x - dxConstant

                    if self.mario.restart:
                        print("I DIED!")
                        finishedLevel = True
                        reward -= 400
                        winStreak = 0
                    else:
                        if max_x >= self.level.max_X and not finishedLevel:
                            finishedLevel = True
                            timesMaxedTheReward += 1
                            winStreak += 1
                            reward += 200
                        else:
                            if self.mario.rect.x > max_x:
                                reward += 5
                            if framesCounter - dxStartFrame >= differentialFramesCount:
                                dxStartFrame = framesCounter
                                dxConstant = self.mario.rect.x
                    if stillLearning:
                        next = self.player.agent.predict(obs)
                        assert (len(next.shape) == 1)
                        G = reward + self.gamma * np.max(next)
                        self.player.agent.update(prevObs, actionIdentifier, G)

                    self.doAction(actionIdentifier)

                pygame.display.update()
                clock.tick(max_frame_rate)

                framesCounter += 1

            if timesMaxedTheReward > 0:
                print("I've maxed the reward {0} times!".format(
                    timesMaxedTheReward))
                if maxWinStreak < winStreak:
                    maxWinStreak = winStreak
                print("winStreak {0}, maxWinStreak: {1}".format(
                    winStreak, maxWinStreak))

            if stillLearning:
                print("Still learning!")
                self.running_avg[episodeCounter] = self.totalrewards[
                    0:episodeCounter + 1].mean()

                self.eps -= 1 / (
                    (epsCoef + 2) * (epsCoef + 1)
                )  # eps=1/(n+1) for n=epsCoef ~~ episodeCounter
                epsCoef += 1

                if self.running_avg[episodeCounter] < self.running_avg[int(
                        np.ceil(np.sqrt(episodeCounter)))]:
                    self.eps = 1 / (np.ceil(np.sqrt(episodeCounter)) + 1)
                    epsCoef = np.ceil(np.sqrt(episodeCounter))
                    print("Reverting epsilon**: {0}".format(self.eps))
                elif episodeCounter >= self.episodesPerStep and self.running_avg[
                        episodeCounter] < self.running_avg[
                            episodeCounter - self.episodesPerStep]:
                    self.eps = 1 / (
                        (episodeCounter - self.episodesPerStep) + 1)
                    epsCoef = episodeCounter
                    print("Reverting epsilon: {0}".format(self.eps))
            else:
                self.running_avg[episodeCounter] = self.totalrewards[
                    learningPeriod:episodeCounter + 1].mean()

            totalreward = max_x
            if totalreward > maxReward:
                maxReward = totalreward
            self.totalrewards[episodeCounter] = totalreward

            print("Episode: {0}, maxReward: {1} eps: {2} totalReward: {3}".
                  format(episodeCounter, maxReward, self.eps, totalreward))
            print("Avg: {0}".format(self.running_avg[episodeCounter] /
                                    self.level.max_X))

        for reward in self.totalrewards:
            print(reward)
예제 #6
0
class MainWindow:
    def __init__(self):
        # Initializing ML parameters
        self.techniqueIdentifier = 0
        self.startingLearningRate = 0.1
        self.param1 = 0.9
        self.param2 = 0.99
        self.learningPeriod = 300

        self.gamma = 0.97
        self.eps = 1.0

        self.experimentsCount = 20000
        self.maxFrames = 3000

        self.windowSize = (640, 480)
        self.mario = None

        self.episodesPerStep = 10
        self.globalStepMax = int(self.experimentsCount / self.episodesPerStep)
        self.globalStep = 1
        self.decayRate = 0.99
        self.learningRate = tf.train.exponential_decay(
            self.startingLearningRate,
            self.globalStep,
            1,
            self.decayRate,
            staircase=True)

        self.player = MLPlayer(self.techniqueIdentifier, self.maxFrames, 1888,
                               self.learningRate, self.globalStep, self.param1,
                               self.param2)

        self.totalrewards = np.empty(self.experimentsCount)
        self.running_avg = np.empty(self.experimentsCount)

        #(lambda: os.system('cls'))()    # Clear console

    def decreaseLearningRate(self):
        self.globalStep += 1

    def calculateLearningRate(self):
        return self.startingLearningRate * (self.decayRate
                                            **(self.globalStep / 1))

    def printLearningRate(self):
        print("Learning rate is:{0}".format(self.calculateLearningRate()))

    def main(self):
        # Initializing the game, graphics and sound
        pygame.mixer.pre_init(44100, -16, 2, 4096)
        pygame.init()
        screen = pygame.display.set_mode(self.windowSize)
        max_frame_rate = 9999
        dashboard = Dashboard("./img/font.png", 8, screen)
        sound = Sound()

        maxReward = 0
        timesMaxedTheReward = 0
        winStreak = 0
        maxWinStreak = 0

        differentialFramesCount = 10
        dxStartFrame = 0
        diffXOverFrames = 0
        dxConstant = 0
        lastX = 0

        epsCoef = 0

        for episodeCounter in range(0, self.experimentsCount):
            print("=============================================")
            print("============WELCOME TO EPISODE {0}============".format(
                episodeCounter))
            print("=============================================")
            framesCounter = 0

            stillLearning = episodeCounter <= self.learningPeriod

            if stillLearning and episodeCounter / self.experimentsCount >= self.globalStep / self.globalStepMax:
                self.decreaseLearningRate()
                self.printLearningRate()

            self.level = Level(screen, sound, dashboard)

            self.level.loadLevel('Level1-1')
            dashboard.coins = 0
            dashboard.points = 0

            self.mario = Mario(0, 0, self.level, screen, dashboard, sound)
            self.mario.setPos(0, 384)
            clock = pygame.time.Clock()

            max_x = 0
            obs = np.append(
                np.append(self.mario.getObservation(), framesCounter),
                self.level.getClosestEntityDistance(self.mario))

            finishedLevel = False

            while framesCounter < self.maxFrames and not finishedLevel:
                #print(framesCounter)
                pygame.display.set_caption(
                    "Super Mario running with {:d} FPS".format(
                        int(clock.get_fps())))
                if self.mario.pause:
                    self.mario.pauseObj.update()
                else:
                    self.level.drawLevel(self.mario.camera)
                    dashboard.update()
                    self.mario.update()
                    #print(self.mario.rect.x, self.mario.rect.y)
                    prevObs = obs
                    obs = np.append(
                        np.append(self.mario.getObservation(), framesCounter),
                        self.level.getClosestEntityDistance(self.mario))

                    reward = 0
                    actionIdentifier = self.player.agent.sample_action(
                        obs, self.eps)

                    if max_x < obs[0]:
                        max_x = obs[0]
                        reward += 1

                    # diffXOverFrames = self.mario.rect.x - dxConstant

                    if self.mario.restart:
                        print("I DIED!")
                        finishedLevel = True
                        reward -= 400
                        winStreak = 0
                    else:
                        if max_x >= self.level.max_X and not finishedLevel:
                            finishedLevel = True
                            timesMaxedTheReward += 1
                            winStreak += 1
                            reward += 200
                        else:
                            if self.mario.rect.x > max_x:
                                reward += 5
                            if framesCounter - dxStartFrame >= differentialFramesCount:
                                dxStartFrame = framesCounter
                                dxConstant = self.mario.rect.x
                    if stillLearning:
                        next = self.player.agent.predict(obs)
                        assert (len(next.shape) == 1)
                        G = reward + self.gamma * np.max(next)
                        self.player.agent.update(prevObs, actionIdentifier, G)

                    self.doAction(actionIdentifier)

                pygame.display.update()
                clock.tick(max_frame_rate)

                framesCounter += 1

            if timesMaxedTheReward > 0:
                print("I've maxed the reward {0} times!".format(
                    timesMaxedTheReward))
                if maxWinStreak < winStreak:
                    maxWinStreak = winStreak
                print("winStreak {0}, maxWinStreak: {1}".format(
                    winStreak, maxWinStreak))

            if stillLearning:
                print("Still learning!")
                self.running_avg[episodeCounter] = self.totalrewards[
                    0:episodeCounter + 1].mean()

                self.eps -= 1 / (
                    (epsCoef + 2) * (epsCoef + 1)
                )  # eps=1/(n+1) for n=epsCoef ~~ episodeCounter
                epsCoef += 1

                if self.running_avg[episodeCounter] < self.running_avg[int(
                        np.ceil(np.sqrt(episodeCounter)))]:
                    self.eps = 1 / (np.ceil(np.sqrt(episodeCounter)) + 1)
                    epsCoef = np.ceil(np.sqrt(episodeCounter))
                    print("Reverting epsilon**: {0}".format(self.eps))
                elif episodeCounter >= self.episodesPerStep and self.running_avg[
                        episodeCounter] < self.running_avg[
                            episodeCounter - self.episodesPerStep]:
                    self.eps = 1 / (
                        (episodeCounter - self.episodesPerStep) + 1)
                    epsCoef = episodeCounter
                    print("Reverting epsilon: {0}".format(self.eps))
            else:
                self.running_avg[episodeCounter] = self.totalrewards[
                    learningPeriod:episodeCounter + 1].mean()

            totalreward = max_x
            if totalreward > maxReward:
                maxReward = totalreward
            self.totalrewards[episodeCounter] = totalreward

            print("Episode: {0}, maxReward: {1} eps: {2} totalReward: {3}".
                  format(episodeCounter, maxReward, self.eps, totalreward))
            print("Avg: {0}".format(self.running_avg[episodeCounter] /
                                    self.level.max_X))

        for reward in self.totalrewards:
            print(reward)

    # Controls
    def doAction(self, actionIdentifier):
        if (actionIdentifier == 0):
            # Left
            self.mario.traits['jumpTrait'].jump(True)
            self.mario.traits["goTrait"].direction = -1
        elif (actionIdentifier == 1):
            # Right
            self.mario.traits['jumpTrait'].jump(True)
            self.mario.traits["goTrait"].direction = 1
        if (actionIdentifier == 2):
            # Left
            self.mario.traits['jumpTrait'].jump(False)
            self.mario.traits["goTrait"].direction = -1
        elif (actionIdentifier == 3):
            # Right
            self.mario.traits['jumpTrait'].jump(False)
            self.mario.traits["goTrait"].direction = 1
예제 #7
0
파일: main.py 프로젝트: monthui0002/Mario
screen = pygame.display.set_mode(window_size)
bg = pygame.transform.scale(screen, (w, h))
sound_player = SoundPlayer()

while True:
    menu = Menu(screen, sound_player)
    while not menu.pause:
        menu.update()
        pygame.display.update()
        fps_clock.tick(FPS / 6)
        if sound_player.allow_sound:
            if not sound_player.bg_sound.is_playing():
                sound_player.bg_sound.play_sound(True)
        else:
            sound_player.bg_sound.stop_sound()

    level = Level("levels/" + menu.level_name, screen)
    background = Background(0, 0, screen, level)
    mario = Mario(0, 0, Mario.DIRECTION_RIGHT, 0, Mario.IN_AIR, screen,
                  background, level, sound_player)
    background.set_character(mario)

    while not mario.restart:
        if mario.pause:
            mario.pause_obj.update()
        else:
            background.update()
            mario.update()
        pygame.display.update()
        fps_clock.tick(FPS)