def run(self): while 1: self.time += self.time_speed.tick(Constants.FPS) if self.gameover and (not self.pause): self.pause = True self.polygon.reset() self.timeclock.delete_timer() self.message = MessageBox(self, 4000) self.display_items.add(self.message) if self.completed and (not self.pause): self.pause = True self.polygon.reset() self.timeclock.delete_timer() self.message = MessageBox(self, 4000) self.display_items.add(self.message) self.polygon.set_alive(False) self.polygon.reset() if self.quit: self.on_exit() if self.gameover == True: h = HighScores() h.insert_HighScores(Constants.PLAYERNAME, str(self.score.get_score())) hlist = HighScores().get_HighScores() if len(hlist) < 5 or self.score.get_score( ) > hlist[4].get_points(): h.flush() pygame.event.clear() highscores = HighScoresMenu("sprites/sky.jpg") highscores.run() return False else: return self.event_manager.run() self.screen.blit(self.background, (0, 0)) self.display_items.update() self.display_items.draw(self.screen) self.atoms.update() self.atoms.draw(self.screen) if self.start == True and self.pause == False: self.display_items.remove(self.message) self.message = None self.start = False self.polygon.set_alive(False) self.polygon.reset() self.timeclock.add_timer() if not self.pause: self.polygon.draw() pygame.display.flip()
def on_exit(self): Level.on_exit(self) h = HighScores() h.insert_HighScores(Constants.PLAYERNAME, str(self.score.get_score())) hlist = HighScores().get_HighScores() if len(hlist) < 5 or self.score.get_score() > hlist[4].get_points(): h.flush() pygame.event.clear() highscores = HighScoresMenu("sprites/sky.jpg") highscores.run()
def __init__(self): pygame.init() pygame.mixer.init() self.END_MUSIC = pygame.mixer.Sound('end.ogg') self.KILL_SOUND = pygame.mixer.Sound('lose.wav') self.ENEMY_DEAD_SOUND = pygame.mixer.Sound('scream.wav') self.ENEMY_DEAD_SOUND.set_volume(.4) self.WIN_SOUND = pygame.mixer.Sound("win.wav") pygame.display.set_caption(title) pygame.font.init() self.font = pygame.font.Font(FONTNAME, 20) self.fileName = "HighScore.json" # if there is no file then make one if not os.path.isfile(self.fileName): empty_score_file = open(self.fileName, "w") empty_score_file.write("[]") empty_score_file.close() self.running = True self.playerDead = False self.win = False self.highscore = HighScores() self.platformSpriteSheet = Spritesheet(spritesheetPlatformFile) self.enemiesSpriteSheet = Spritesheet(enemyspritesheetFile) self.coinSpriteSheet = Spritesheet(spritesheetPlatformFile) self.cloud_images = [] for cloud in range(1, 4): self.cloud_images.append( pygame.image.load("cloud{}.png".format(cloud)).convert())
def loadSprites(self): self.all_sprites = pygame.sprite.LayeredUpdates() self.platforms = pygame.sprite.Group() self.finalplatform = pygame.sprite.Group() self.coin = pygame.sprite.Group() self.enemyFly = pygame.sprite.Group() self.clouds = pygame.sprite.Group() self.spikeenemy = pygame.sprite.Group() self.brownmob = pygame.sprite.Group() self.skullmob = pygame.sprite.Group() self.digletmob = pygame.sprite.Group() self.EnemyFlyTimer = 0 from Player import Player self.player = Player(self) self.highscore = HighScores() self.playerDead = False # creates the enemy flys for fly in ENEMYFLY_LIST: EnemyFly(self, *fly) # creates the big grass platform for grassbigplatform in GRASS_BIG_PLATFORM_LIST: GrassBigPlatform(self, *grassbigplatform) # creates the small grass platform for grasssmallplatform in GRASS_SMALL_PLATFORM_LIST: GrassSmallPlatform(self, *grasssmallplatform) # creates the big brown cake platform for bcakebigplatform in BCAKE_BIG_PLATFORM_LIST: BCakeBigPlatform(self, *bcakebigplatform) # creates the small brown cake platform for bcakesmallplatform in BCAKE_SMALL_PLATFORM_LIST: BCakeSmallPlatform(self, *bcakesmallplatform) # creates the big sand platform for sandbigplatform in SAND_BIG_PLATFORM_LIST: SandBigPlatform(self, *sandbigplatform) # creates the small sand platform for sandsmallplatform in SAND_SMALL_PLATFORM_LIST: SandSmallPlatform(self, *sandsmallplatform) # creates the big snow platform for snowbigplatform in SNOW_BIG_PLATFORM_LIST: SnowBigPlatform(self, *snowbigplatform) # creates the small snow platform for snowsmallplatform in SNOW_SMALL_PLATFORM_LIST: SnowSmallPlatform(self, *snowsmallplatform) # creates the big stone platform for stonebigplatform in STONE_BIG_PLATFORM_LIST: StoneBigPlatform(self, *stonebigplatform) # creates the small stone platform for stonesmallplatform in STONE_SMALL_PLATFORM_LIST: StoneSmallPlatform(self, *stonesmallplatform) # final platform here for grassfinalplatform in GRASS_FINAL_PLATFORM_LIST: GrassFinalPlatform(self, *grassfinalplatform) # creates clouds on the map for i in range(8): from Cloud import Cloud cloud = Cloud(self) cloud.rect.y += 500
def __init__(self, background): "Set up the HighScores menu" Menu.__init__(self, "MoleFusion HighScore Menu", background) self.parser = make_parser() self.curHandler = Localization() self.parser.setContentHandler(self.curHandler) self.parser.parse( open("languages/HighScoresMenu_" + Constants.LANGUAGE + ".xml")) self.title = GW_Label(self.curHandler.getText("title"), (0.5, 0.1), (27, 22, 24)) self.name_column = GW_Label(self.curHandler.getText("name"), (0.25, 0.25), (212, 224, 130)) self.points_column = GW_Label(self.curHandler.getText("points"), (0.75, 0.25), (212, 224, 130)) self.returnMain = GW_Button(self.curHandler.getText("returnMain"), (0.5, 0.9), (255, 255, 255)) self.returnMain.add_eventhandler("onmouseclick", self.returnMain_onmouseclick) h = HighScores() highscorelist = h.get_HighScores() self.widgetlist = [ self.title, self.name_column, self.points_column, self.returnMain ] for val, i in enumerate(highscorelist[0:5]): self.widgetlist.append( GW_Label(i.get_name(), (0.25, 0.35 + val / 10.0), (250, 254, 210))) self.widgetlist.append( GW_Label(str(i.get_points()), (0.75, 0.35 + val / 10.0), (250, 254, 210))) self.widget_man = GuiWidgetManager(self.widgetlist) self.time_speed = pygame.time.Clock() self.exit = False self.on_enter()
print('=' * 20) import HighScores reload(HighScores) # This is a best practice high_scores = HighScores.HighScores() high_scores.print_scores() print('-' * 20) for score in (-99.6, -99.5, -99, -9, -1, -0, 0, 1, 9, 99, 99.5, 99.6): print('Input: {} ========'.format(score)) if high_scores.is_high_score('int', int(score)): print('Congratulations on your new high score!') #highscores.save_score('str_int', str(int(score))) # strings now raise an exception if high_scores.is_high_score('float', float(score)): print('Congratulations on your new high score!') #highscores.save_score('str_float', str(float(score))) # strings now raise an exception high_scores.print_scores() lessons_learned = '''==================== For positive values: int(n), str(int(n)), float(n), str(float(n)) all work correctly. For negative values: Only int(n) and float(n) work correctly. Recommendation: Always pass an int or float as the second value to highscores.is_high_score() Added a try / except block to reject nonnumeric scores''' print(lessons_learned)
from Gameplay import * from GameOver import * from HighScores import * from LevelScreen import * # load game resources big_font = pygame.font.Font("data/visitor.ttf", 64) small_font = pygame.font.Font("data/visitor.ttf", 48) mesh_list = loadMeshes("data/meshes.dsc", "data/sprites.png") # create & init subsystems screen = pygame.display.set_mode((800, 600)) mainMenu = MainMenu(big_font, (255, 0, 0), (0, 255, 0)) gameplay = Gameplay(mesh_list, big_font) gameOver = GameOver(big_font) highScores = HighScores(big_font) levelScreen = LevelScreen(big_font) mainMenu._gameplay = gameplay mainMenu._highScores = highScores gameplay._mainMenu = mainMenu gameplay._gameOver = gameOver gameOver._highScores = highScores highScores._mainMenu = mainMenu levelScreen._gameplay = gameplay gameplay._levelScreen = levelScreen mainMenu._levelScreen = levelScreen quit = False # game main loop gameState = mainMenu oldTime = pygame.time.get_ticks() * 0.001
optionParser = OptionParser() optionParser.add_option("-e", "--editlevel", help="Edit the level with a specified filename. If no such level exists, create a new one.") optionParser.add_option("-l", "--levellength", help="When editing a level, this is the length of the level") (options, args) = optionParser.parse_args() #### Kick off the graphics/window system kernel = GameKernel() screenSurface = kernel.InitializeDisplay((800, 600)) ticker = kernel.Ticker() #### Initialize game states gsm = GameStateManager() gsm.RegisterState(GS_MainMenu(kernel, gsm)) gsm.RegisterState(GS_EditorMenu(kernel, gsm)) gsm.RegisterState(GS_Game(kernel, gsm)) gsm.RegisterState(HighScores(kernel, gsm)) gsm.RegisterState(GS_LevelSelect(kernel, gsm)) gsm.RegisterState(GS_Tutorial(kernel, gsm)) if (options.editlevel): gsm.RegisterState(GS_Editor(kernel, gsm, options.editlevel)) gsm.SwitchState("Editor") else: gsm.SwitchState("MainMenu") font = pygame.font.SysFont("Helvetica", 12) ## Main Loop while (1): delta = ticker.get_time()