File size: 3,902 Bytes
e53fda1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import argparse
import pygame, sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..")


from utils.dict_utils import get_with_warning
from game.pygame_base import PygameBase
from utils.config import Config
from game.supermario.classes.Dashboard import Dashboard
from game.supermario.classes.Level import Level
from game.supermario.classes.Menu import Menu
from game.supermario.classes.Sound import Sound
from game.supermario.entities.Mario import Mario

config = Config()


class SuperMarioGame(PygameBase):
    def __init__(
            self,
            output_dir,
    ):
        super(SuperMarioGame, self).__init__(output_dir, "Super Mario")
        
        self.valid_actions = ["UP", "LEFT", "RIGHT", "UP+LEFT", "UP+RIGHT", "NONE"]   
        
        self.sample_frames = 5 
    
    def set_level_config(self, level_config):
        current_level = get_with_warning(level_config, "level", 0)
        self.max_round = get_with_warning(level_config, "max_round", 300)
        
        windowSize = 640, 480
        self.screen = pygame.display.set_mode(windowSize)
        
        self.dashboard = Dashboard("game/supermario/img/font.png", 8, self.screen)
        self.sound = Sound()
        self.level = Level(self.screen, self.sound, self.dashboard)
        self.menu = Menu(self.screen, self.dashboard, self.level, self.sound)
        
        self.menu.levelNames = self.menu.loadLevelNames()
        print("Level Names: ", self.menu.levelNames)
        self.menu.inChoosingLevel = False
        self.menu.dashboard.state = "start"
        self.menu.dashboard.time = 0
        self.menu.level.loadLevel(self.menu.levelNames[current_level])
        
        print(len(self.menu.level.level))
        print(len(self.level.level))
        
        self.menu.dashboard.levelName = self.menu.levelNames[current_level].split("Level")[1]
        self.menu.start = True
        
        self.mario = Mario(0, 0, self.level, self.screen, self.dashboard, self.sound)
        
    def step(self, action, dt=None):
        print("step Action: ", action)
        self.level.drawLevel(self.mario.camera)
        self.dashboard.update()
        self.mario.update(action)
        pygame.display.update()
        print("len(self.game_frames): ", len(self.game_frames))
        if len(self.game_frames) >= self.max_round or self.mario.restart:
            self.over = True
        
        self.score = self.dashboard.points
        if self.over:
            info = f"Game Over, You got {self.score} scores."    
        else:
            info = "Game is running."
        return self.over, info
    
        
    def human_mode_action(self, event):
        action = None
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                action = "UP"
            elif event.key == pygame.K_DOWN:
                action = "DOWN"
            elif event.key == pygame.K_LEFT:
                action = "LEFT"
            elif event.key == pygame.K_RIGHT:
                action = "RIGHT"
            else:
                action = "NONE"
        return action

    def get_score(self):
        return {
            "score" : self.score,  # win or lose
            "frames" : len(self.game_frames),
            "valid rate" : len(self.game_frames) / ( len(self.game_frames) + self.invalid_action_count ),
        }

if __name__ == '__main__':
    print(os.path.dirname(os.path.abspath(__file__)) + "/..")
    
    parser = argparse.ArgumentParser()
    parser.add_argument("--levelConfig", type=str, default="./config/level_config/supermariogame/level1.json", help="The path to the level config file.")
    args = parser.parse_args()
    
    levelConfig = args.levelConfig
    
    config.load_level_config(levelConfig)
    
    supermario_game = SuperMarioGame("")
    
    supermario_game.run(None, human_mode=True)
    
# python game/supermario_game.py