class PlayState(): def __init__(self, gsm): self.gsm = gsm self.background = Background( os.path.join("res", "PlayState", "background.png")) self.camera = Camera(self) self.HUD = HUD(self.gsm) # Key handling self.leftKeyDown = False self.rightKeyDown = False self.upKeyDown = False self.spriteSheet = SpriteSheet( os.path.join("res", "PlayState", "Spritesheet.png"), 1) # Holds all the objects in the game self.allObjectList = pygame.sprite.Group() self.activeObjectList = pygame.sprite.Group() self.terrainList = pygame.sprite.Group() self.interactableList = pygame.sprite.Group() self.enemyList = pygame.sprite.Group() self.enemyAIList = pygame.sprite.Group() self.player = None self.level = currentLevel # Sets the current level self.loadLevel(self.level) # Loads the level def update(self): # Fixes the bug where if you instantly switch directions you will stop moving if self.leftKeyDown: self.player.moveLeft() if self.rightKeyDown: self.player.moveRight() self.camera.update(self.player) self.HUD.update() # PERFORMANCE FIX # This block makes it so that only the objects that are within a certain range of the player will it update it and draw it. # Runs through all the objects in the game and if it is within the range of the camera then add it to the active objects list. for object in self.allObjectList: if object.rect.right > -160 and object.rect.left < 960: self.activeObjectList.add(object) else: self.activeObjectList.remove(object) self.activeObjectList.update() # Updates all the active objects self.background.update() def draw(self, window): window.fill(Color.CYAN) self.background.draw(window) self.activeObjectList.draw(window) # Draws all the active objects self.HUD.draw(window) def keyHandler(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.leftKeyDown = True if event.key == pygame.K_RIGHT: self.rightKeyDown = True if event.key == pygame.K_UP: self.player.jump(9.5) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: self.player.stopMoving() self.leftKeyDown = False if event.key == pygame.K_RIGHT: self.player.stopMoving() self.rightKeyDown = False def loadLevel( self, level ): # Reads the pixels of an image for their RGB Values and depending on these values, create a object at that position if level == 1: mapData = Image.open( os.path.join("res", "PlayState", "Level1Map(sand).png")) # Loads level1 elif level == 2: mapData = Image.open( os.path.join("res", "PlayState", "Level2Map(sand).png")) elif level == 3: mapData = Image.open( os.path.join("res", "PlayState", "Level3Map.png")) elif level == 4: mapData = Image.open( os.path.join("res", "PlayState", "Level4Map.png")) elif level == 5: mapData = Image.open( os.path.join("res", "PlayState", "Level5Map.png")) mapWidth, mapHeight = mapData.size # Gets the width and height of the image print("WIDTH: " + str(mapWidth) + " HEIGHT: " + str(mapHeight)) # For debugging for x in range(mapWidth): # Runs through all the colomns for y in range(mapHeight): # Runs through all the rows currentPixel = mapData.getpixel((x, y)) red = currentPixel[0] # RED VALUE green = currentPixel[1] # GREEN VALUE blue = currentPixel[2] # BLUE VALUE alpha = currentPixel[3] # ALPHA VALUE # Checks for player spawner if red == 255 and green == 0 and blue == 255 and alpha == 255: self.player = Player( x * 32, y * 32, self, self.gsm, self.spriteSheet, ) self.allObjectList.add(self.player) # Check for Grass blocks if red == 0 and green == 255 and blue == 0 and alpha == 255: self.terrainList.add( Grass(x * 32, y * 32, self.spriteSheet)) # Checks for dirt blocks if red == 222 and green == 144 and blue == 61 and alpha == 255: self.terrainList.add(Dirt(x * 32, y * 32, self.spriteSheet)) # Checks for sand blocks if red == 243 and green == 225 and blue == 179 and alpha == 255: self.terrainList.add(Sand(x * 32, y * 32, self.spriteSheet)) # Check for coin blocks if red == 255 and green == 255 and blue == 0 and alpha == 255: self.interactableList.add( Coin(x * 32, y * 32, self.spriteSheet)) # Checks for enemy AI detection if red == 0 and green == 0 and blue == 0 and alpha == 255: self.enemyAIList.add(EnemyAIBox(x * 32, y * 32, self)) # Checks for Crab enemy if red == 255 and green == 0 and blue == 0 and alpha == 255: self.enemyList.add( Crab(x * 32, y * 32, self, self.spriteSheet)) # Checks for map piece if red == 0 and green == 0 and blue == 255 and alpha == 255: self.interactableList.add( Map(x * 32, y * 32, self.spriteSheet)) if red == 0 and green == 255 and blue == 255 and alpha == 255: self.interactableList.add( Chest(x * 32, y * 32, self, self.spriteSheet)) print("Chest addded") # Adds all the groups into one master group self.allObjectList.add(self.terrainList) self.allObjectList.add(self.interactableList) self.allObjectList.add(self.enemyList) self.allObjectList.add(self.enemyAIList) def removeObject(self, object): # Removes a game object from the game self.allObjectList.remove(object) self.activeObjectList.remove(object) def nextLevel(self): global highestLevelComplete global currentLevel # This makes it so that if you play a level you already beat, it doesn't unlock a new level if currentLevel > highestLevelComplete: highestLevelComplete += 1 def reset(self): global highestLevelComplete global currentLevel highestLevelComplete = 0 currentLevel = 1 self.HUD.reset()