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()
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'
def main(): # Set window size windowSize = [600, 600] # Create variable keep_going keep_going = True # Create pygame Clock class instance timer = pygame.time.Clock() # Create display screen = pygame.display.set_mode(windowSize) # Create instances of local classes sound = Sound() sound.playMusic(sound.soundtrack) object = Object(screen, windowSize[0] // 2, windowSize[1] // 2, "#e00000") player = Player(2, screen, windowSize[0] // 2, windowSize[1] // 2) background = Background(screen, -windowSize[0], -windowSize[1]) # Play soundtrack in sound class sound.playMusic(sound.soundtrack) # Game loop while keep_going: # Fill screen with blue screen.fill("#4287f5") # Update entities background.update() player.update() object.update() # Flip display pygame.display.flip() # Check if quit for event in pygame.event.get(): if event.type == pygame.QUIT: # Set keep_going to false keep_going = False # Call tick method in timer instance timer.tick(720)
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 __init__(self): self.bg_sound, self.coin_sound, self.jump_sound, self.death_sound, self.grow_up_sound, self.shrink_down_sound, self.mine_sound = ( Sound(i) for i in range(0, 7)) self.allow_sound = True
import time from classes.Sound import Sound s = Sound(0) s.play_sound(True) time.sleep(5)
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)