class GameState(State): ''' State for game playing mode. ''' bgGroup = pygame.sprite.OrderedUpdates() playerGroup = pygame.sprite.RenderPlain() guiGroup = pygame.sprite.OrderedUpdates() enemyGroup = pygame.sprite.RenderPlain() weaponGroup = pygame.sprite.RenderPlain() player = None terrainLayer = None cachedPathGraph = None curPathGraph = None def getPlayer(): assert(player != None) return GameState.player @staticmethod def getCurrentAtMap(): assert(GameState.terrainLayer != None) return GameState.terrainLayer.getMap().getAtLayer() def __init__(self, main): # transition from another state super(GameState, self).__init__(main) self.loadPlayer() # Initialize World self.wl = WorldLoader(config.WORLD_NAME) # Initialize World Map self.worldMap = WorldMap(self.wl) startMap = os.path.join("tygra", "0_0.map") # Starting map self.environment = self.wl.getMap(startMap, self.worldMap) self.currentMap = startMap # Initialize HUD self.hud = HUDManager() #TODO: FIX MUSIC pygame.mixer.init() filename = "worldAmbient.ogg" GameState.enemyGroup.add(Enemy(self, self.player.rect.left - 50, self.player.rect.top - 50, "skeleton")) GameState.enemyGroup.sprites()[0].movetowards(self.player.rect.left, self.player.rect.top) #''' npc_one = NPC(self, 30, 30, "Skeleton") ''' '''TODO: FIX MUSIC pygame.mixer.init() filename = "worldAmbient.ogg" path = os.path.join(util.GAME_SOUNDS, filename) path = util.filepath(path) pygame.mixer.music.load(path) pygame.mixer.music.play() ''' def __del__(self): # transition to another state super(GameState, self).__del__() def loadPlayer(self): self.player = Player(self) self.player.mapPos = [0,0] GameState.playerGroup.add(self.player) def update(self, clock): super(GameState, self).update(clock) GameState.guiGroup.update(clock) enemies = [enemy for enemy in GameState.enemyGroup] surfaces = [surface for surface in self.environment.atGroup] GameState.playerGroup.update(clock, self.player, enemies, surfaces) GameState.enemyGroup.update(clock, self.player, enemies, surfaces) GameState.weaponGroup.update(clock, self.player, enemies, surfaces) self.worldMap.update(clock) self.hud.update(clock, self.player) def handleEvent(self): super(GameState, self).handleEvent() self.sudoNext() # handle mouse mousePos = Vector2(pygame.mouse.get_pos()) self.player.orient(mousePos) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[0]: self.player.swingSword() if pygame.mouse.get_pressed()[2]: self.player.useMagic() def sudoNext(self): #Used for debugging, from config import keyboard, keymap mmap = None if keyboard.downup(keymap.DUP): mmap = self.wl.north[self.currentMap] elif keyboard.downup(keymap.DDOWN): mmap = self.wl.south[self.currentMap] elif keyboard.downup(keymap.DLEFT): mmap = self.wl.west[self.currentMap] elif keyboard.downup(keymap.DRIGHT): mmap = self.wl.east[self.currentMap] if mmap is not None: self.currentMap = mmap print "MAP: ", mmap self.environment = self.wl.getMap(mmap, self.worldMap) # Added for debugging purposes. Remove when not needed print "MAP: ", mmap def nextMap(self, direction, pos): # print "moving to: " + direction + " via: " + str(pos) mmap = None if direction == 'up': mmap = self.wl.north[self.currentMap] # update player mapPos self.player.mapPos[1] -= 1 # position player at bottom minus almost half a tile if mmap is not None: self.player.setPos(pos[0], config.HEIGHT - 17) elif direction == 'down': mmap = self.wl.south[self.currentMap] self.player.mapPos[1] += 1 if mmap is not None: self.player.setPos(pos[0], 17) elif direction == 'right': self.player.mapPos[0] += 1 mmap = self.wl.east[self.currentMap] if mmap is not None: self.player.setPos(64 + 17, pos[1]) # just not touching the hud elif direction == 'left': self.player.mapPos[0] -= 1 mmap = self.wl.west[self.currentMap] if mmap is not None: self.player.setPos(config.WIDTH - (64 + 17), pos[1]) if mmap is not None: self.currentMap = mmap self.environment = self.wl.getMap(mmap, self.worldMap) # Added for debugging purposes. Remove when not needed print "MAP: ", mmap GameState.enemyGroup.empty() GameState.enemyGroup.add(Enemy(self, randrange(1, config.WIDTH), randrange(1, config.HEIGHT), "skeleton")) GameState.enemyGroup.add(Enemy(self, randrange(1, config.WIDTH), randrange(1, config.HEIGHT), "skeleton")) GameState.enemyGroup.add(Enemy(self, randrange(1, config.WIDTH), randrange(1, config.HEIGHT), "skeleton")) GameState.weaponGroup.empty() def draw(self): #draw environment self.environment.drawBackground(self.main.screen); # draw player GameState.playerGroup.draw(self.main.screen) # draw enemies GameState.enemyGroup.draw(self.main.screen) # draw weapons GameState.weaponGroup.draw(self.main.screen) # draw gui self.hud.draw(self.main.screen) # draw foreground self.environment.drawForeground(self.main.screen) # draw world map self.worldMap.draw(self.main.screen) # flip screen super(GameState, self).draw()
class GameState(State): ''' State for game playing mode. ''' bgGroup = pygame.sprite.OrderedUpdates() playerGroup = pygame.sprite.RenderPlain() guiGroup = pygame.sprite.OrderedUpdates() def __init__(self, main): # transition from another state State.__init__(self,main) self.loadPlayer() self.hud = Actor(IMG_HUD,-1) self.hud2 = Actor(IMG_HUD2) self.hud.setPos(32,HEIGHT/2) self.hud2.setPos(WIDTH-32,HEIGHT/2) GameState.guiGroup.add(self.hud) GameState.guiGroup.add(self.hud2) self.health = 7 self.hudHearts = [] self.hudHeartsHalf = Actor(IMG_HEART2,-1) self.hudSlot = [None]*3 self.wl = WorldLoader('new.world') self.background = TerrainLayer("0_0.map") self.currentMap = "0_0.map" for i in range(0,3): self.hudSlot[i] = Actor(IMG_SLOT,-1) self.hudSlot[i].setPos(50,120+i*120) self.guiGroup.add(self.hudSlot[i]) self.updateHudHealth() pygame.mixer.init() filename = "worldAmbient.mp3" path = os.path.join(util.GAME_SOUNDS, filename) path = util.filepath(path) pygame.mixer.music.load(path) pygame.mixer.music.play() def __del__(self): # transition to another state pass def loadPlayer(self): self.player = Player(self) GameState.playerGroup.add(self.player) def update(self): self.checkCollisions() State.update(self); GameState.guiGroup.update() GameState.playerGroup.update() def updateHudHealth(self): if self.health < 1 or self.health > 20: return full = self.health/2 halve = self.health%2 if len(self.hudHearts) != full: while len(self.hudHearts) < full: self.hudHearts.append(Actor(IMG_HEART,-1)) GameState.guiGroup.add(self.hudHearts[-1]) while len(self.hudHearts) > full: GameState.guiGroup.remove(self.hudHearts.pop()) for i in range(0,full): self.hudHearts[i].setPos(WIDTH-25,HEIGHT-50-i*60) if halve == 1: GameState.guiGroup.add(self.hudHeartsHalf) self.hudHeartsHalf.setPos(WIDTH-25, HEIGHT-50-full*60) else: self.hudHeartsHalf.kill() def handleEvent(self): # handle mouse mousePos = Vector2(pygame.mouse.get_pos()) self.player.orient(mousePos) # For each event that occurs this frame for event in pygame.event.get(): # If user exits the window if event.type == QUIT: sys.exit(0) # monitor keyboard self.handleKey(event) def handleKey(self, event): ''' Handle input from user keyboard ''' if event.type == pygame.KEYDOWN: # exit game if event.key == K_ESCAPE: sys.exit(1) if event.key == MOVEMENT_KEYS[0]: self.player.move(0) if event.key == MOVEMENT_KEYS[1]: self.player.move(1) if event.key == MOVEMENT_KEYS[2]: self.player.move(2) if event.key == MOVEMENT_KEYS[3]: self.player.move(3) if event.key == MAGIC_ATTACK_KEY: self.player.useMagic() # testing if event.key == K_DOWN: self.health -= 1 self.updateHudHealth() if event.key == K_UP: self.health += 1 self.updateHudHealth() elif event.type == pygame.KEYUP: if event.key == MOVEMENT_KEYS[0]: self.player.unMove(0) if event.key == MOVEMENT_KEYS[1]: self.player.unMove(1) if event.key == MOVEMENT_KEYS[2]: self.player.unMove(2) if event.key == MOVEMENT_KEYS[3]: self.player.unMove(3) elif event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[0]: self.player.swingSword() if pygame.mouse.get_pressed()[2]: self.player.shootBow() def checkCollisions(self): # Check for atLayer collisions for hit in pygame.sprite.spritecollide(self.player, self.background.atGroup, 0): self.player.collideWall(hit) def nextMap(self, direction, pos): # print "moving to: " + direction + " via: " + str(pos) mmap = "" if direction == 'up': mmap = self.wl.north[self.currentMap] # position player at bottom minus almost half a tile if mmap is not 'none': self.player.setPos(pos[0], HEIGHT-17) elif direction == 'down': mmap = self.wl.south[self.currentMap] if mmap is not 'none': self.player.setPos(pos[0], 17) elif direction == 'right': mmap = self.wl.east[self.currentMap] if mmap is not 'none': self.player.setPos(64+17, pos[1]) # just not touching the hud elif direction == 'left': mmap = self.wl.west[self.currentMap] if mmap is not 'none': self.player.setPos(WIDTH-(64+17), pos[1]) if not mmap == 'none': self.currentMap = mmap self.background = TerrainLayer(mmap) # Added for debugging purposes. Remove when not needed print "MAP: ",mmap def draw(self): #draw background #self.main.screen.blit(self.background, self.background.get_rect()) self.background.drawTerrain(self.main.screen); # draw player GameState.playerGroup.draw(self.main.screen) # draw gui GameState.guiGroup.draw(self.main.screen) # flip screen State.draw(self)