classMainScene(Scene):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)def_generate_objects_(self):Bird=self.object.BirdImage=self.object.ImageFloor=self.object.FloorFlappy=self.object.FlappyBackground=self.object.Backgroundmessage=Image("assets/images/message.png",(int((self.width-184)//2),int(self.height*0.12)),)yieldBackground()yieldFloor(self.width)yieldBird(120,120)yieldFlappy(self.width,self.height)yieldmessagedefhandle_scene(self,event:Event,*args,**kwargs)->None:""" Handle an event for all objects in the scene. Args: event (pygame.event.Event): The event to handle. """ifevent.type==pygame.MOUSEBUTTONDOWN:self.entergame()super().handle_scene(event,*args,**kwargs)defentergame(self):"""entergame"""# self.sounds.play("wing")self.manager.reset("GameScene")# Notes:# - `_generate_objects_` is called by the Scene manager when the scene is# created or reset. Use it to yield live instances of objects that the# scene should manage.# - To preload sounds or resources, use `on_first_enter` or `on_enter` if# your scene system supports those hooks.
classGameScene(Scene):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)Pipes=self.object.PipesFloor=self.object.FloorScore=self.object.ScoreFlappy=self.object.Flappyself.floor=Floor(self.width)self.score=Score(self.width,self.height)self.pipes=Pipes(self.width,self.height)self.flappy=Flappy(self.width,self.height)def_generate_objects_(self):Background=self.object.Backgroundself.flappy.set_mode(2)# normal modeyieldBackground()yieldself.flooryieldself.pipesyieldself.scoreyieldself.flappydefgameover(self):"""entergame"""# self.sounds.play("die")self.manager.append("OverScene",self.screen,self.score.score)defis_tap_event(self,event)->bool:""" Determine if the event is a flap/tap event. Args: event: Pygame event. Returns: bool: True if event is a tap/flap. """m_left,_,_=pygame.mouse.get_pressed()space_or_up=event.type==pygame.KEYDOWNand(event.key==pygame.K_SPACEorevent.key==pygame.K_UP)screen_tap=event.type==pygame.FINGERDOWNreturnm_leftorspace_or_uporscreen_tapdefhandle_scene(self,event:Event,*args,**kwargs)->None:""" Handle an event for all objects in the scene. Args: event (pygame.event.Event): The event to handle. """ifself.is_tap_event(event):self.flappy.flap()super().handle_scene(event,*args,**kwargs)defupdate_scene(self,deltatime:float,*args,**kwargs)->None:""" Update all objects in the scene, unless paused. Args: deltatime (float): Time since last update (ms). """# Collision check first: when collided, transition to game over.ifself.flappy.collided(self.floor,self.pipes):# play sounds safelytry:self.sounds.play("hit")exceptException:passself.gameover()# Score: when flappy crosses a pipe, increment score. Use a copy of# the pipe list so modifications during iteration are safe.forpipeinlist(self.pipes.upper):ifself.flappy.crossed(pipe):self.score.add()# Continue with default update to tick and draw childrensuper().update_scene(deltatime,*args,**kwargs)
classOverScene(BlurScene):def__init__(self,blur_surface,score,**kwargs):super().__init__(blur_surface,blur_count=2,blur_duration=3,on_blur_complete=None,**kwargs)self.score=scoredef_generate_objects_(self):Image=self.object.ImageScore=self.object.Scorescore=Score(self.width,self.height)score.set(self.score)message=Image("assets/images/gameover.png",(int((self.width-184)//2),int(self.height*0.5)),)yieldmessageyieldscoredefhandle_scene(self,event:Event,*args,**kwargs)->None:""" Handle an event for all objects in the scene. Args: event (pygame.event.Event): The event to handle. """ifevent.type==pygame.MOUSEBUTTONDOWN:self.gotomain()super().handle_scene(event,*args,**kwargs)defgotomain(self):"""gotomain"""# self.sounds.play("wing")self.manager.reset("MainScene")defon_first_enter(self,*args,**kwargs):# self.sounds.play("swoosh")pass# Scene lifecycle & tips-Scenecreation:`_generate_objects_`shouldyieldnewinstanceswhenthe# scene is first created. Keep scene state (like score) on the scene instance# if it needs to persist between resets.-Eventhandling:call`super().handle_scene()`toensurechildobjects# receive events. Use `is_tap_event` to centralize input mapping (mouse, key,# touch).-Audio:usetheengine`Sounds()`helperandguardincaseaudiofailsin# headless environments.