コード例 #1
0
ファイル: main.py プロジェクト: paddo0404/game_python
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'
コード例 #2
0
ファイル: main.py プロジェクト: dingyifei/super-mario-python
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()
コード例 #3
0
ファイル: Mario.py プロジェクト: monthui0002/Mario
 def __init__(self, x, y, direction, level, state, screen, background,
              play_lvl, sound_player):
     self.x, self.y = x, y
     self.small_img, self.big_img = load_img()
     self.direction = direction
     self.level = level
     self.play_lvl = play_lvl
     self.sound_player = sound_player
     self.state = state
     self.key_input = {
         "KP_Enter": False,
         "Up": False,
         "Right": False,
         "Down": False,
         "Left": False,
         "Escape": False,
         "Enter": False
     }
     self.screen = screen
     self.cur_frame = 0
     self.cur_fall_speed = Mario.FALL_SPEED
     self.cur_img = self.small_img if self.level == 0 else self.big_img
     self.grow_lvl = 0 if self.level == 0 else 2
     self.pause = False
     self.restart = False
     self.dashboard = Dashboard(self.screen, play_lvl.name)
     self.pause_obj = Pause(self.screen, self, self.dashboard)
     self.background = background
コード例 #4
0
ファイル: Menu.py プロジェクト: monthui0002/Mario
 def __init__(self, screen, sound_player):
     self.screen = screen
     self.sound_player = sound_player
     self.dashboard = Dashboard(screen)
     self.dashboard.state = "menu"
     self.key_input = {"Enter": False, "Up": False, "Right": False, "Down": False, "Left": False, "Escape": False}
     self.pause = False
     self.cur_img = 1
     self.state = self.MENU
     self.music = load_setting()
     self.sound_player.allow_sound = self.music
     self.level = 1
     self.level_name = ""
コード例 #5
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()
コード例 #6
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
コード例 #7
0
ファイル: Mario.py プロジェクト: monthui0002/Mario
class Mario:
    # States
    IDLE = 1
    WALK = 2
    BREAK = 3
    IN_AIR = 4
    SHRINK = 5
    CLIMB = 6
    SWIM = 7
    GROW = 8
    DEAD = 9
    WIN = 10

    # Constants
    STEP = 5
    DIRECTION_RIGHT = 1
    DIRECTION_LEFT = -1
    GRAVITY = .5
    FALL_SPEED = 0
    IMAGE = pygame.image.load("./img/mario.png")

    def __init__(self, x, y, direction, level, state, screen, background,
                 play_lvl, sound_player):
        self.x, self.y = x, y
        self.small_img, self.big_img = load_img()
        self.direction = direction
        self.level = level
        self.play_lvl = play_lvl
        self.sound_player = sound_player
        self.state = state
        self.key_input = {
            "KP_Enter": False,
            "Up": False,
            "Right": False,
            "Down": False,
            "Left": False,
            "Escape": False,
            "Enter": False
        }
        self.screen = screen
        self.cur_frame = 0
        self.cur_fall_speed = Mario.FALL_SPEED
        self.cur_img = self.small_img if self.level == 0 else self.big_img
        self.grow_lvl = 0 if self.level == 0 else 2
        self.pause = False
        self.restart = False
        self.dashboard = Dashboard(self.screen, play_lvl.name)
        self.pause_obj = Pause(self.screen, self, self.dashboard)
        self.background = background

    def get_input(self):
        self.key_input = get(self.key_input)

    def update(self):
        self.get_input()
        self.dashboard.update()
        self.move()
        self.background.check_out_range()
        self.render()

    def move(self):
        if self.key_input["KP_Enter"] and (self.grow_lvl == 0
                                           or self.grow_lvl == 2):
            self.key_input["KP_Enter"] = False
            if self.level == 0:
                self.state = Mario.GROW
                print("Grow")
            else:
                print("Shrink")
                self.state = Mario.SHRINK

        if self.key_input["Enter"]:
            self.sound_player.bg_sound.pause_sound()
            self.pause = True
            self.key_input = {
                "KP_Enter": False,
                "Up": False,
                "Right": False,
                "Down": False,
                "Left": False,
                "Escape": False,
                "Enter": False
            }

        if self.key_input["Escape"]:
            self.restart = True

        if self.key_input["Up"] and self.state != Mario.IN_AIR:
            if self.sound_player.allow_sound:
                self.sound_player.jump_sound.play_sound()
            self.cur_fall_speed = -6 * scale
            self.state = Mario.IN_AIR

        moving = self.key_input["Right"] or self.key_input["Left"]
        if moving:
            self.x += self.direction * Mario.STEP
            self.direction = Mario.DIRECTION_LEFT if self.key_input[
                "Left"] else Mario.DIRECTION_RIGHT
            if self.direction == Mario.DIRECTION_LEFT:
                self.play_lvl.check_collision_left(self)
            else:
                self.play_lvl.check_collision_right(self)
            if self.state == Mario.IDLE:
                self.state = Mario.WALK
            lol, _ = self.play_lvl.check_collision(self)
            if not lol:
                self.state = Mario.IN_AIR
        if self.state == Mario.IDLE:
            self.cur_frame = 0
        elif self.state == Mario.WALK:
            if moving:
                if int(self.cur_frame) > 3 or int(self.cur_frame) < 1:
                    self.cur_frame = 1
                elif int(self.cur_frame + 7 / FPS) <= 3:
                    self.cur_frame += 7 / FPS
                else:
                    self.cur_frame = 1
            else:
                self.state = Mario.IDLE
        elif self.state == Mario.IN_AIR:
            self.cur_frame = 5
            self.y += self.cur_fall_speed
            self.cur_fall_speed += Mario.GRAVITY
            if self.cur_fall_speed > 0:
                self.play_lvl.check_collision_bottom(self)
            else:
                self.play_lvl.check_collision_top(self)
            if self.y + (self.level + 1) * tile_size * scale >= h:
                if self.sound_player.allow_sound:
                    self.sound_player.death_sound.play_sound()
                    self.sound_player.bg_sound.stop_sound()
                self.state = Mario.DEAD
                self.y = h - (self.level + 1) * tile_size * scale
                self.pause = True
        elif self.state == Mario.SWIM:
            pass
        elif self.state == Mario.GROW:
            if self.grow_lvl < 2:
                if self.grow_lvl == 0:
                    self.y -= tile_size * scale
                self.grow_lvl += 8 / FPS
                self.cur_img = self.big_img
                self.cur_frame = 15
                self.level = 1
                if int(self.grow_lvl) == 2:
                    self.grow_lvl = 2
                    self.cur_frame = 0
                    self.state = Mario.IN_AIR
        elif self.state == Mario.SHRINK:
            if self.level >= 1:
                self.grow_lvl -= 8 / FPS
                self.cur_frame = 15
                if int(self.grow_lvl) == 0:
                    self.level = 0
                    self.grow_lvl = 0
                    self.y += tile_size * scale
                    self.cur_frame = 0
                    self.cur_img = self.small_img
                    self.state = Mario.IN_AIR
            else:
                self.state = Mario.DEAD
                self.pause = True
                if self.sound_player.allow_sound:
                    self.sound_player.death_sound.play_sound()
                    self.sound_player.bg_sound.stop_sound()

    def render(self):
        img = Mario.IMAGE.subsurface(self.cur_img[int(self.cur_frame)][0])
        if self.direction == Mario.DIRECTION_LEFT:
            img = pygame.transform.flip(img, True, False)
        if self.x <= (w - tile_size * scale) / 2:
            pos_x = self.x
        elif self.x + (w + tile_size * scale) / 2 >= self.background.w:
            pos_x = w - (self.background.w - self.x)
        else:
            pos_x = (w - tile_size * scale) / 2
        self.screen.blit(
            pygame.transform.scale(img, (tile_size * scale, tile_size * scale *
                                         (2 if self.level == 1 else 1))),
            (pos_x, self.y))
コード例 #8
0
ファイル: Menu.py プロジェクト: monthui0002/Mario
class Menu:
    image = pygame.image.load('./img/items.png')
    image1 = image.subsurface(311, 160, 9, 8)
    image2 = image.subsurface(23, 160, 9, 8)
    img = [pygame.transform.scale(image1, (18, 16)), pygame.transform.scale(image2, (18, 16))]

    MENU = 0
    IN_CHOOSING_LEVEL = 1
    INSETTING = 2
    EXIT = 3

    def __init__(self, screen, sound_player):
        self.screen = screen
        self.sound_player = sound_player
        self.dashboard = Dashboard(screen)
        self.dashboard.state = "menu"
        self.key_input = {"Enter": False, "Up": False, "Right": False, "Down": False, "Left": False, "Escape": False}
        self.pause = False
        self.cur_img = 1
        self.state = self.MENU
        self.music = load_setting()
        self.sound_player.allow_sound = self.music
        self.level = 1
        self.level_name = ""

    def update(self):
        self.dashboard.update()
        self.update_menu()
        self.update_setting()

    def get_input(self):
        self.key_input = get(
            {"Enter": False, "Up": False, "Right": False, "Down": False, "Left": False, "Escape": False})

    def draw_background(self):
        background, ground, bg_colors = load_json()

        # load background menu
        self.screen.fill(bg_colors)
        ground_tile = tiles['1-1']
        img = pygame.image.load(ground_tile[0])
        img = img.subsurface(0, 0, 16, 16)
        img = pygame.transform.scale(img, (tile_size * scale, tile_size * scale))
        for y in range(ground[1], ground[1] + ground[3]):
            for x in range(ground[0], ground[0] + ground[2]):
                self.screen.blit(img, (x * tile_size * scale, y * tile_size * scale))
        for background_type in background:
            img = pygame.image.load(tiles[background_type][0])
            img = img.subsurface(tiles[background_type][1], tiles[background_type][2],
                                 tiles[background_type][3][0], tiles[background_type][3][1])
            img = pygame.transform.scale(img,
                                         (tiles[background_type][3][0] * scale, tiles[background_type][3][1] * scale))
            for i in background[background_type]:
                self.screen.blit(img, (i[0] * scale * tile_size, i[1] * scale * tile_size))

        # load character images
        mario = pygame.image.load('./img/mario.png')
        mario = mario.subsurface(80, 34, 16, 16)
        mario = pygame.transform.scale(mario, (16 * scale, 16 * scale))
        self.screen.blit(mario, (2.5 * scale * tile_size, 11 * scale * tile_size))

    def draw_menu_background(self):
        image = pygame.image.load('./img/title_screen.png')
        image = image.subsurface(1, 60, 175, 87)
        image = pygame.transform.scale(image, (262, 130))
        self.screen.blit(image, ((w - 262) / 2, 100))

    def update_menu(self):
        self.draw_background()
        if self.state != self.IN_CHOOSING_LEVEL:
            self.draw_menu_background()
            self.dashboard.update()

        if self.state == self.MENU:
            self.draw_menu()
        elif self.state == self.IN_CHOOSING_LEVEL:
            self.draw_choose_level()
        elif self.state == self.INSETTING:
            self.draw_setting()
        else:
            exit(0)

    def update_setting(self):
        self.get_input()
        if self.key_input["Up"]:
            self.cur_img = self.cur_img - 1 if self.cur_img > 1 else 3
        if self.key_input["Down"]:
            self.cur_img = self.cur_img + 1 if self.cur_img < 3 else 1
        if self.state == self.IN_CHOOSING_LEVEL:
            if self.key_input["Right"]:
                self.level = self.level + 1 if self.level < len(levels) else 1
            elif self.key_input["Left"]:
                self.level = self.level - 1 if self.level > 1 else len(levels)
        if self.key_input["Enter"]:
            if self.state == self.MENU:
                if self.cur_img == 1:
                    self.state = self.IN_CHOOSING_LEVEL
                elif self.cur_img == 2:
                    self.state = self.INSETTING
                else:
                    self.state = self.EXIT
            elif self.state == self.INSETTING:
                if self.cur_img == 1:
                    self.music = not self.music
                    self.sound_player.allow_sound = self.music
                else:
                    self.state = self.MENU
                    self.save_setting()
            elif self.state == self.IN_CHOOSING_LEVEL:
                self.level_name = list_map[self.level - 1]
                self.pause = True

    def drawText(self, text, x, y, size):
        my_font = pygame.font.Font('freesansbold.ttf', size)
        text_surface = my_font.render(text, False, (255, 255, 255))
        self.screen.blit(text_surface, (x, y))

    def draw_menu(self):
        self.screen.blit(self.img[0], ((w - 262) / 2, 240))
        self.screen.blit(self.img[0], ((w - 262) / 2, 270))
        self.screen.blit(self.img[0], ((w - 262) / 2, 300))
        self.drawText("CHOOSE LEVEL", (w - 262) / 2 + 24, 240, 25)
        self.drawText("SETTINGS", (w - 262) / 2 + 24, 270, 25)
        self.drawText("EXIT", (w - 262) / 2 + 24, 300, 25)
        self.screen.blit(self.img[1], ((w - 262) / 2, 240 if self.cur_img == 1 else 270 if self.cur_img == 2 else 300))

    def draw_setting(self):
        self.screen.blit(self.img[0], ((w - 262) / 2, 240))
        self.screen.blit(self.img[0], ((w - 262) / 2, 270))
        self.drawText("MUSIC", (w - 262) / 2 + 24, 240, 25)
        self.drawText("ON" if self.music else "OFF", (w - 262) / 2 + 150, 240, 25)
        self.drawText("BACK", (w - 262) / 2 + 24, 270, 25)
        self.screen.blit(self.img[1], ((w - 262) / 2, 240 if self.cur_img == 1 else 270))

    def draw_choose_level(self):
        x = 30
        y = 30
        width = 120
        for i in range(0, len(levels)):
            if i + 1 != self.level:
                pygame.draw.rect(self.screen, [150, 150, 150], pygame.Rect(x + width * i, y, 100, 100), 2)
            else:
                pygame.draw.rect(self.screen, [255, 255, 255], pygame.Rect(x + width * i, y, 100, 100), 2)
            self.drawText(levels[i], 60 + width * i, 60, 30)

    def save_setting(self):
        data = {"music": self.music}
        with open('./settings/setting.json', "w") as outfile:
            json.dump(data, outfile)
コード例 #9
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)
コード例 #10
0
class MarioGym(gym.Env):
    def __init__(self,
                 headless=True,
                 level_name='Level-basic-one-goomba.json',
                 no_coins=5,
                 step_size=5,
                 partial_observation=False,
                 distance_reward=False,
                 experiment='SE'):
        self.action_space = spaces.Discrete(6)
        self.observation_space = spaces.Box(low=-10000000,
                                            high=100000000,
                                            dtype=np.float,
                                            shape=(40, 80, 4))

        self.levelname = level_name
        self.headless = headless
        self.score = 0
        self.step_size = step_size
        self.max_frame_rate = 60
        self.steps = 0
        self.observation = None
        self.experiment = experiment
        self.level = None
        self.mario = None
        self.mario2 = None
        self.screen = None
        self.dashboard = None
        self.sound = None
        self.menu = None
        self.partial_observation = partial_observation
        self.no_coins = no_coins
        self.coins_start = self.no_coins
        self.distance_reward = distance_reward

        self.init_game()
        self.reset(levelname=self.levelname)
        self.coin_name = 'Coin' if not self.headless else 'CoinHeadless'

    def reset(self, levelname=None):
        if not levelname:
            self.no_coins = 5
            self.levelname = 'Level-{}-coins.json'.format(self.no_coins)
        else:
            self.levelname = levelname
        self.init_game()
        self.steps = 0
        self.observation = self.level_to_numpy()
        self.max_dist = 0
        return self.observation

    def reset_clean(self, y_pos):
        self.count_entities()
        self.coins_taken = self.coins_start - self.no_coins
        self.coins_end = self.no_coins
        self.no_coins = min(5, self.no_coins * 2)
        self.coins_start = self.no_coins

        self.levelname = 'Level-{}-coins.json'.format(self.no_coins)
        self.init_game(y_position=y_pos,
                       coins=self.coins_end,
                       clock=self.clock)

        self.observation = self.level_to_numpy()

        return self.observation

    def step(
        self,
        action_num,
    ):
        self.steps += 1
        action = MOVES[action_num]
        # action = 'doNothing'
        num_goombas = len([
            x for x in self.level.entityList
            if ((x.__class__.__name__ == 'Goomba'
                 or x.__class__.__name__ == 'GoombaHeadless') and x.alive)
        ])

        coins = len([
            x for x in self.level.entityList
            if ((x.__class__.__name__ == self.coin_name))
        ])

        old_x_pos = self.mario.rect.x / (10 * MAP_MULTIPLIER)

        reward = self.do_game_step(action)

        if self.distance_reward:
            reward += old_x_pos / 1.7

        goombas_died = num_goombas - len([
            x for x in self.level.entityList if (
                (x.__class__.__name__ == 'Goomba'
                 or x.__class__.__name__ == 'GoombaHeadless') and x.alive)
        ])

        coins_taken = coins - len([
            x for x in self.level.entityList if (
                (x.__class__.__name__ == self.coin_name))
        ])

        coins_left = len([
            x for x in self.level.entityList
            if ((x.__class__.__name__ == self.coin_name))
        ])

        self.observation = self.level_to_numpy()
        # print(coins_taken)
        info = {
            'num_killed': goombas_died,
            'coins_taken': coins_taken,
            'death': self.mario.restart
        }

        if self.mario.restart:
            reward -= HOLE_REWARD
        if coins_left == 0 and coins_taken > 0:
            self.mario.restart = True

        restart = self.mario.restart or self.steps >= EPISODE_LENGTH
        return self.observation, reward, restart, info

    def render(self, mode='human', close=False):
        pass

    def get_screen(self):

        return self.screen

    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

    def do_game_step(self, move):
        if not self.headless:
            start_score = self.dashboard.points
        else:
            start_score = self.level.points

        counter = 0
        reward = 0
        while counter < self.step_size:
            counter += 1
            coins = self.return_coins()
            self.do_move(move)

            if self.experiment == 'FD':
                random_move = random.choice(MOVES)
                self.do_random_move(self.mario2, random_move)

            if not self.headless:
                pygame.display.set_caption("{:.2f} FPS".format(
                    self.clock.get_fps()))
                self.level.drawLevel(self.mario.camera)
                self.mario.update()
                if self.experiment == 'FD':
                    self.mario2.update()
                self.clock.tick(self.max_frame_rate)
                self.dashboard.update()
                pygame.display.update()
                self.score = self.dashboard.points
            else:
                self.level.updateEntities()
                self.mario.update()
                if self.experiment == 'FD':
                    self.mario2.update()
                self.clock += (1.0 / 60.0)
                self.score = self.level.points

            reward += coins - self.return_coins()

        return COIN_REWARD * reward

    def level_to_numpy(self, other_agent=False):
        mult = 1
        granularity = 8

        padding = int(256 / granularity)
        level_size = self.level.levelLength * padding
        array = np.zeros((EMBEDDING_SIZE, level_size, level_size))
        granx = int(level_size / MAP_HEIGHT)
        grany = int(level_size / MAP_WIDTH)

        if other_agent:
            mult * +-1

        y_axis_1 = int(round(self.mario.rect.top / granularity))
        y_axis_2 = int(round(self.mario.rect.bottom / granularity))
        x_axis_1 = int(round(self.mario.rect.left / granularity))
        x_axis_2 = int(round(self.mario.rect.right / granularity))

        if other_agent:
            array[EMBEDDINGS['Other_agents'], y_axis_1:y_axis_2,
                  x_axis_1:x_axis_2] = 1
        else:
            array[EMBEDDINGS['Mario'], y_axis_1:y_axis_2,
                  x_axis_1:x_axis_2] = 1

        if self.experiment == 'FD':
            y_axis_1 = int(round(self.mario2.rect.top / granularity))
            y_axis_2 = int(round(self.mario2.rect.bottom / granularity))
            x_axis_1 = int(round(self.mario2.rect.left / granularity))
            x_axis_2 = int(round(self.mario2.rect.right / granularity))
            if other_agent:
                array[EMBEDDINGS['Mario'], y_axis_1:y_axis_2,
                      x_axis_1:x_axis_2] = 1
            else:
                array[EMBEDDINGS['Other_agents'], y_axis_1:y_axis_2,
                      x_axis_1:x_axis_2] = 1

        for entity in self.level.entityList:
            y_axis_1 = int(round(entity.rect.top / granularity))
            y_axis_2 = int(round(entity.rect.bottom / granularity))
            x_axis_1 = int(round(entity.rect.left / granularity))
            x_axis_2 = int(round(entity.rect.right / granularity))
            if entity.__class__.__name__ == 'Koopa' or entity.__class__.__name__ == 'Goomba' or entity.__class__.__name__ == 'GoombaHeadless':
                array[EMBEDDINGS['Animal'], y_axis_1:y_axis_2,
                      x_axis_1:x_axis_2] = 1
            elif entity.__class__.__name__ == 'Coin' or entity.__class__.__name__ == 'CoinHeadless':
                array[EMBEDDINGS['Coin'], y_axis_1:y_axis_2,
                      x_axis_1:x_axis_2] = 1
            elif entity.__class__.__name__ == 'RandomBox':
                if not entity.triggered:
                    array[EMBEDDINGS['Random_box'], y_axis_1:y_axis_2,
                          x_axis_1:x_axis_2] = 1
                else:
                    array[EMBEDDINGS['Random_box_trig'], y_axis_1:y_axis_2,
                          x_axis_1:x_axis_2] = 1

        for layer in self.level.level:
            for ground in layer:
                if ground.rect:
                    y_axis_1 = int(round(ground.rect.top / granularity))
                    y_axis_2 = int(round(ground.rect.bottom / granularity))
                    x_axis_1 = int(round(ground.rect.left / granularity))
                    x_axis_2 = int(round(ground.rect.right / granularity))

                    array[EMBEDDINGS['Ground'], y_axis_1:y_axis_2,
                          x_axis_1:x_axis_2] = 1
        for layer in self.level.level[13:17]:
            for index in range(len(layer)):
                if not layer[index].rect and layer[index - 1].rect:
                    y_axis_1 = int(round(layer[0].rect.top / granularity))
                    y_axis_2 = int(round(layer[0].rect.bottom / granularity))
                    x_axis_1 = int(
                        round(layer[index - 1].rect.right / granularity))
                    width = layer[index - 1].rect.right - layer[index -
                                                                1].rect.left
                    x_axis_2 = int(
                        round((layer[index - 1].rect.right + 2 * width) /
                              granularity))

                    array[EMBEDDINGS['Hole'], y_axis_1:y_axis_2,
                          x_axis_1:x_axis_2] = 1

        if self.partial_observation:
            left_paddding = int(round(self.mario.rect.y / granularity))
            upper_padding = int(round(self.mario.rect.x / granularity))
            x1 = max(
                0,
                int(round(self.mario.rect.y / granularity)) -
                min(left_paddding, padding))
            x2 = max(
                0,
                int(round(self.mario.rect.y / granularity)) +
                (2 * padding - min(left_paddding, padding)))
            x3 = int(round(self.mario.rect.x / granularity)) - min(
                upper_padding, padding)
            x4 = int(round(self.mario.rect.x / granularity)) + (
                2 * padding - min(upper_padding, padding))

            numpy_frame = array[:, x1:x2, x3:x4]
        else:
            numpy_frame = array[:, 0:MAP_HEIGHT, 0:MAP_WIDTH]

        return numpy_frame

    def count_entities(self, entity='coin'):

        if self.headless:
            no_entity = len([
                entity for entity in self.level.entityList
                if isinstance(entity, CoinHeadless)
            ])
        else:
            no_entity = len([
                entity for entity in self.level.entityList
                if isinstance(entity, Coin)
            ])
        self.no_coins = no_entity

    def return_coins(self, entity='coin'):

        if self.headless:
            no_entity = len([
                entity for entity in self.level.entityList
                if isinstance(entity, CoinHeadless)
            ])
        else:
            no_entity = len([
                entity for entity in self.level.entityList
                if isinstance(entity, Coin)
            ])
        return no_entity

    def level_to_empathic_numpy(self):
        granularity = 8

        padding = int(256 / granularity)

        level_size = self.level.levelLength * padding
        array = np.zeros((level_size, level_size))

        mario_pos = [
            int(round(self.mario.rect.y / granularity)),
            int(round(self.mario.rect.x / granularity))
        ]

        mario_representation = 128
        ground_representation = 64
        array[mario_pos[0]][mario_pos[1]] = mario_representation

        closest_enemy = None
        closest_distance = np.inf

        for entity in self.level.entityList:
            if entity.__class__.__name__ == 'Koopa' or entity.__class__.__name__ == 'Goomba' or entity.__class__.__name__ == 'GoombaHeadless':
                enemy_pos = [
                    int(round(entity.rect.y / granularity)),
                    int(round(entity.rect.x / granularity))
                ]
                array[enemy_pos[0], enemy_pos[1]] = enemy_representation

                distance = np.linalg.norm(
                    np.array(mario_pos) - np.array(enemy_pos))
                if distance < closest_distance:
                    closest_distance = distance
                    closest_enemy = enemy_pos
            # elif entity.__class__.__name__ == 'Coin':
            #     array[int(round(entity.rect.y / granularity))][int(round(entity.rect.x / granularity))] = 2
            # elif entity.__class__.__name__ == 'RandomBox':
            #     if not entity.triggered:
            #         array[int(round(entity.rect.y / granularity))][int(round(entity.rect.x / granularity))] = 3
            #     else:
            #         array[int(round(entity.rect.y / granularity))][int(round(entity.rect.x / granularity))] = 4
        for ground in self.level.groundList:
            array[int(round(32 * ground[1] / granularity))][int(
                round(32 * ground[0] / granularity))] = ground_representation

        array = np.hstack((np.zeros((level_size, padding)), array))
        array = np.vstack((np.zeros((padding, level_size + padding)), array))

        mario_view = array[max(0, mario_pos[0]):mario_pos[0] + 2 * padding,
                           max(0, mario_pos[1]):mario_pos[1] + 2 * padding]
        if closest_enemy:
            enemy_view = array[max(0, closest_enemy[0]):closest_enemy[0] +
                               2 * padding,
                               max(0, closest_enemy[1]):closest_enemy[1] +
                               2 * padding].copy()
        else:
            enemy_view = mario_view

        enemy_view[enemy_view == mario_representation] = enemy_representation
        enemy_view[padding, padding] = mario_representation
        if mario_view.shape == (padding * 2, padding *
                                2) and enemy_view.shape == (padding * 2,
                                                            padding * 2):
            total_view = np.dstack((mario_view, enemy_view))
        else:
            total_view = np.zeros((padding * 2, padding * 2, 2))
        return total_view

    def level_to_supersimple_numpy(self):
        array = np.zeros(6)
        min_distance = 1000000
        for entity in self.level.entityList:
            if entity.__class__.__name__ == 'Goomba' or entity.__class__.__name__ == 'GoombaHeadless':
                distance = np.absolute(self.mario.rect.x - entity.rect.x)
                if distance < min_distance:
                    min_distance = distance
                    array[0] = self.mario.rect.x - entity.rect.x
                    array[1] = self.mario.rect.y - entity.rect.y
                    array[5] = entity.vel.x
        array[2] = self.mario.vel.x
        array[3] = self.mario.vel.y
        array[4] = self.mario.rect.x
        return array

    def do_move(self, move):
        if move == 'moveLeft':
            self.mario.traits['goTrait'].direction = -1
        elif move == 'moveRight':
            self.mario.traits['goTrait'].direction = 1
        elif move == 'jump':
            self.mario.traits['jumpTrait'].start()
        elif move == 'jumpRight':
            self.mario.traits['goTrait'].direction = 1
            self.mario.traits['jumpTrait'].start()
        elif move == 'jumpLeft':
            self.mario.traits['goTrait'].direction = -1
            self.mario.traits['jumpTrait'].start()
        elif move == 'doNothing':
            self.mario.traits['goTrait'].direction = 0

    def do_random_move(self, mario, move):
        if move == 'moveLeft':
            mario.traits['goTrait'].direction = -1
        elif move == 'moveRight':
            mario.traits['goTrait'].direction = 1
        elif move == 'jump':
            mario.traits['jumpTrait'].start()
        elif move == 'jumpRight':
            mario.traits['goTrait'].direction = 1
            mario.traits['jumpTrait'].start()
        elif move == 'jumpLeft':
            mario.traits['goTrait'].direction = -1
            mario.traits['jumpTrait'].start()
        elif move == 'doNothing':
            mario.traits['goTrait'].direction = 0