def reset(self): if self.freezeState == True: self.entLifeCounter.set(3) self.entLifeCounter.Text.setText(self.entLifeCounter.Lives) self.freezeState = False self.DisplayEngine.PlayerDisplayLayers.empty() for safeZone in self.listSafeZones: safeZone.markUnoccupied() defaultEntityFactory = EntityFactory(self.DisplayEngine) entNewFrog = defaultEntityFactory.buildFrog() entNewFrog.draw() self.DisplayEngine.addUserControlledLayer(entNewFrog) self.CollisionEngine.setControlledEntity(entNewFrog) self.safeFrogs = []
def handleCollision(self): ''' This is the function responsible for providing the means of handling a collision of between a SafeZone and a Frog. ''' if self.safeZoneCollidedWith.IsOccupied : self.PlayerLifeCounter.remove() self.PlayerLifeCounter.Text.setText(self.playerLifeCounter.Lives) self.FrogCollidedWith.Controller.resetFrogToStartingPosition() else : self.safeZoneCollidedWith.markOccupied() defaultEntityFactory = EntityFactory(self.gameEngine.engDisplay) self.gameEngine.safeFrogs.append(self.frogCollidedWith) # Unhook the controller from the Frog that landed safely. self.frogCollidedWith.setController(None) entNewFrog = defaultEntityFactory.buildFrog() entNewFrog.draw() self.gameEngine.DisplayEngine.addUserControlledLayer(entNewFrog) self.gameEngine.CollisionEngine.setControlledEntity(entNewFrog)
@property def Controller(self): return self.myController if __name__ == "__main__": from src.core.validationframework import ValidationFramework from src.entities.entityfactory import EntityFactory Validation = ValidationFramework() # Create a new EntityFactory defaultEntityFactory = EntityFactory(Validation.engDisplay) # Draw entities ... do stuff that needs to be rendered to the Main Display # Create Static Entities (i.e. visual aspects of the Game Screen) listStaticBackgroundEntities = defaultEntityFactory.buildBackground() # Create Controlled Entities (i.e. those Entities that will be controllable via User Input) entFrog = defaultEntityFactory.buildFrog() entFrog.draw() # Add each of the update methods of the static entites to the run queue for staticEntity in listStaticBackgroundEntities:
def init(self): # Get some constants which we will need later self.GameConfig = IniParser("engine.ini") intDisplayWidth = int(self.GameConfig.get("EngineCore", "displayWidth")) intDisplayHeight = int(self.GameConfig.get("EngineCore", "displayHeight")) self.gameWidth = intDisplayWidth self.gameHeight = intDisplayHeight # a List containing all the safe frogs self.safeFrogs = [] # Cheat sequence self.cheatCharInput = [] # Init the main display engine self.engDisplay = DisplayEngine((intDisplayWidth, intDisplayHeight)) defaultEntityFactory = EntityFactory(self.DisplayEngine) listStaticBackgroundEntities = defaultEntityFactory.buildBackground() self.engCollision = CollisionEngine(self) self.entLifeCounter = defaultEntityFactory.buildLifeCounter() entFrog = defaultEntityFactory.buildFrog() entFrog.draw() # Create the Animated Cars entCar1 = defaultEntityFactory.buildCar([20, 189], uniform(2, 7), generateRandomColor()) entCar2 = defaultEntityFactory.buildCar([20, 219], uniform(2, 7), generateRandomColor()) entCar3 = defaultEntityFactory.buildCar([20, 249], uniform(2, 7), generateRandomColor()) entCar4 = defaultEntityFactory.buildCar([20, 279], uniform(2, 7), generateRandomColor()) entCar5 = defaultEntityFactory.buildCar([20, 309], uniform(2, 7), generateRandomColor()) # Create an animated Log listLastLogCoordinates = [1, 70] intLastLogSpeed = 3 entLastLog = LogEntity(listLastLogCoordinates, intLastLogSpeed, LogEntity.LEFT_TO_RIGHT) entLastLog.setGameScreen(self.DisplayEngine) contLastLogAnimation = LogController(entLastLog) entLastLog.setController(contLastLogAnimation) listSecondLogCoordinates = [485, 100] intSecondLogSpeed = 2 entSecondLog = LogEntity(listSecondLogCoordinates, intSecondLogSpeed, LogEntity.RIGHT_TO_LEFT) entSecondLog.setGameScreen(self.DisplayEngine) contSecondLogAnimation = LogController(entSecondLog) entSecondLog.setController(contSecondLogAnimation) listFirstLogCoordinates = [1, 130] intFirstLogSpeed = 1 entFirstLog = LogEntity(listFirstLogCoordinates, intFirstLogSpeed, LogEntity.LEFT_TO_RIGHT) entFirstLog.setGameScreen(self.DisplayEngine) contFirstLogAnimation = LogController(entFirstLog) entFirstLog.setController(contFirstLogAnimation) # Create the Large Hazard Zone listHazardZones = [] LARGE_HAZARD_LOCATION = [18, 65] LARGE_HAZARD_DIMENSIONS = [464, 95] entLargeHazardZone = HazardEntity(LARGE_HAZARD_LOCATION, LARGE_HAZARD_DIMENSIONS, COLOR_NAVY_BLUE) entLargeHazardZone.setGameScreen(self.DisplayEngine) listHazardZones.append(entLargeHazardZone) # Create the Smaller Intermediate Hazard Zones HAZARD_ZONEA_LOCATION = [80, 30] HAZARD_ZONEA_DIMENSIONS = [70, 30] entHazardZoneA = HazardEntity(HAZARD_ZONEA_LOCATION, HAZARD_ZONEA_DIMENSIONS, COLOR_BACKGROUND_GREEN) entHazardZoneA.setGameScreen(self.DisplayEngine) listHazardZones.append(entHazardZoneA) HAZARD_ZONEB_LOCATION = [210, 30] HAZARD_ZONEB_DIMENSIONS = [90, 30] entHazardZoneB = HazardEntity(HAZARD_ZONEB_LOCATION, HAZARD_ZONEB_DIMENSIONS, COLOR_BACKGROUND_GREEN) entHazardZoneB.setGameScreen(self.DisplayEngine) listHazardZones.append(entHazardZoneB) HAZARD_ZONEC_LOCATION = [360, 30] HAZARD_ZONEC_DIMENSIONS = [70, 30] entHazardZoneC = HazardEntity(HAZARD_ZONEC_LOCATION, HAZARD_ZONEC_DIMENSIONS, COLOR_BACKGROUND_GREEN) entHazardZoneC.setGameScreen(self.DisplayEngine) listHazardZones.append(entHazardZoneC) # Create the SafeZones listSafeZones = [] SAFE_ZONEA_LOCATION = [20, 30] SAFE_ZONEA_DIMENSIONS = [60, 30] entSafeZoneA = SafeZoneEntity(SAFE_ZONEA_LOCATION, SAFE_ZONEA_DIMENSIONS, COLOR_WHITE) entSafeZoneA.setGameScreen(self.DisplayEngine) listSafeZones.append(entSafeZoneA) SAFE_ZONEB_LOCATION = [150, 30] SAFE_ZONEB_DIMENSIONS = [60, 30] entSafeZoneB = SafeZoneEntity(SAFE_ZONEB_LOCATION, SAFE_ZONEB_DIMENSIONS, COLOR_WHITE) entSafeZoneB.setGameScreen(self.DisplayEngine) listSafeZones.append(entSafeZoneB) SAFE_ZONEC_LOCATION = [300, 30] SAFE_ZONEC_DIMENSIONS = [60, 30] entSafeZoneC = SafeZoneEntity(SAFE_ZONEC_LOCATION, SAFE_ZONEC_DIMENSIONS, COLOR_WHITE) entSafeZoneC.setGameScreen(self.DisplayEngine) listSafeZones.append(entSafeZoneC) SAFE_ZONED_LOCATION = [430, 30] SAFE_ZONED_DIMENSIONS = [52, 30] entSafeZoneD = SafeZoneEntity(SAFE_ZONED_LOCATION, SAFE_ZONED_DIMENSIONS, COLOR_WHITE) entSafeZoneD.setGameScreen(self.DisplayEngine) listSafeZones.append(entSafeZoneD) # The GameEngine should be aware of how many safe zones we have total self.listSafeZones = listSafeZones # Add the Hazard Boundary Borders listHazardBoundaryBorders = [] LEFT_HAZARD_BOUNDARY_LOCATION = [0, 30] LEFT_HAZARD_BOUNDARY_DIMENSIONS = [19, 130] entLeftHazardBoundary = StaticEntity( LEFT_HAZARD_BOUNDARY_LOCATION, LEFT_HAZARD_BOUNDARY_DIMENSIONS, COLOR_BACKGROUND_GREEN ) entLeftHazardBoundary.setGameScreen(self.DisplayEngine.Surface) listHazardBoundaryBorders.append(entLeftHazardBoundary) RIGHT_HAZARD_BOUNDARY_LOCATION = [482, 30] RIGHT_HAZARD_BOUNDARY_DIMENSIONS = [19, 130] entRightHazardBoundary = StaticEntity( RIGHT_HAZARD_BOUNDARY_LOCATION, RIGHT_HAZARD_BOUNDARY_DIMENSIONS, COLOR_BACKGROUND_GREEN ) entRightHazardBoundary.setGameScreen(self.DisplayEngine.Surface) listHazardBoundaryBorders.append(entRightHazardBoundary) # Anything that can collide with the frog should be appended here listCollisionEntities = [entCar1, entCar2, entCar3, entCar4, entCar5] listCollisionEntities.append(entFirstLog) listCollisionEntities.append(entSecondLog) listCollisionEntities.append(entLastLog) for hazardZone in listHazardZones: listCollisionEntities.append(hazardZone) for safeZone in listSafeZones: listCollisionEntities.append(safeZone) # Add a Background Entities to the Game Layer self.DisplayEngine.addLayer(listStaticBackgroundEntities) # Add the HazardZones to the Game Layer self.DisplayEngine.addLayer(listHazardZones) # Add the SafeZones to the Game Layer self.DisplayEngine.addLayer(listSafeZones) # Add the logs to the Game Layer self.DisplayEngine.addLayer(entFirstLog) self.DisplayEngine.addLayer(entSecondLog) self.DisplayEngine.addLayer(entLastLog) # Add the Hazard Boundary Borders self.DisplayEngine.addLayer(listHazardBoundaryBorders) # Add the universal game controller UniversalGameController = GameController(self) self.DisplayEngine.addUserControlledLayer(entFrog) self.DisplayEngine.addGameController(UniversalGameController) self.DisplayEngine.addLayer(entCar1) self.DisplayEngine.addLayer(entCar2) self.DisplayEngine.addLayer(entCar3) self.DisplayEngine.addLayer(entCar4) self.DisplayEngine.addLayer(entCar5) TitleText = Text(FONT_BLOX, "PyFrogger", COLOR_FROG_GREEN, 20) TitleText.setGameScreen(self.DisplayEngine.Surface) TitleText.draw((400, 4)) LivesText = Text(FONT_BLOX, "Lives ", COLOR_FROG_GREEN, 20) LivesText.setGameScreen(self.DisplayEngine.Surface) LivesText.draw((10, 4)) LivesCounterText = Text(FONT_BLOX, "3", COLOR_FROG_RED, 20) LivesCounterText.setGameScreen(self.DisplayEngine.Surface) LivesCounterText.draw((70, 4)) self.entLifeCounter.setLifeTextEntity(LivesCounterText) entText = [TitleText, LivesText, LivesCounterText] self.DisplayEngine.addLayer(entText) self.CollisionEngine.setPlayerLifeCounter(self.entLifeCounter) # Adding these entities into the collision engine will let the engine monitor # their position and on the action of a rectangle collision, the controlled # entity will be handled based on it's collision based methods self.CollisionEngine.setControlledEntity(entFrog) for collisionEntity in listCollisionEntities: self.CollisionEngine.addCollisionEntity(collisionEntity) # This variable keeps the run active self.running = True self.freezeState = False