class Scene(object): """A scene is like a level, or one section of the program. There will be an intro scene, the sokoban scene and an ending scene displaying the users score. This is a TEMPLATE class""" def __init__(self, screenRes=(640, 480)): self.running = True self.surfaceManager = SurfaceManager( ) #Handles the drawing of a scene, managers that have draw functions are added at the bottom of init using .setObjects() self.uiManager = UIManager( ) #UI managers keep all the buttons and text together, and position them self.eventHandlers = EventHandlers() #Event Handlers self.eventManager = EventManager() #Manages event handlers self.surfaceManager.setObjects() #Sets objects that need drawn #events are handled and draw managers draw to main display def run(self, surface, events): #Events self.eventManager.run(events) #Draw self.surfaceManager.draw()
class Level: "Level Base class" def __init__(self,score,time): "Initialises resources and start level" self.background=pygame.image.load("sprites/sky.jpg") self.background.convert() #for blitting more faster self.screen=Constants.SCREEN self.score = score self.display_items = pygame.sprite.Group() self.timeclock = GameTime((0.05,0.05),time) self.display_items.add(self.timeclock,self.score) self.time_speed=pygame.time.Clock() self.time=0.0 self.quit = False self.pause = False self.start = False self.completed = False self.gameover = False self.message = None self.polygon=Polygon(self) self.event_manager = EventManager(self) self.on_enter() def get_score_object(self): return self.score def set_score_object(self, score_object): self.score = score_object def on_enter(self): self.screen.blit(self.background, (0, 0)) pygame.display.flip() self.start = True self.pause = True self.timeclock.delete_timer() self.message = MessageBox(self,3000) self.display_items.add(self.message) def on_exit(self): self.display_items.remove(self.timeclock) del self.polygon del self.timeclock if self.completed == False: #end of game, so music fadeout pygame.mixer.music.fadeout(500) f = FadeTransition(500,Constants.FADECOLOR,"to") del f 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()
class Level: "Level Base class" def __init__(self, score, time): "Initialises resources and start level" self.background = pygame.image.load("sprites/sky.jpg") self.background.convert() #for blitting more faster self.screen = Constants.SCREEN self.score = score self.display_items = pygame.sprite.Group() self.timeclock = GameTime((0.05, 0.05), time) self.display_items.add(self.timeclock, self.score) self.time_speed = pygame.time.Clock() self.time = 0.0 self.quit = False self.pause = False self.start = False self.completed = False self.gameover = False self.message = None self.polygon = Polygon(self) self.event_manager = EventManager(self) self.on_enter() def get_score_object(self): return self.score def set_score_object(self, score_object): self.score = score_object def on_enter(self): self.screen.blit(self.background, (0, 0)) pygame.display.flip() self.start = True self.pause = True self.timeclock.delete_timer() self.message = MessageBox(self, 3000) self.display_items.add(self.message) def on_exit(self): self.display_items.remove(self.timeclock) del self.polygon del self.timeclock if self.completed == False: #end of game, so music fadeout pygame.mixer.music.fadeout(500) f = FadeTransition(500, Constants.FADECOLOR, "to") del f 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()