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
def __init__(self, definition_path, map_path, init_music_path=None, music_path=None, music_loops=-1, has_timer=True, should_fade_in=True, mid=0): self.has_timer = has_timer self.keyCode = None self.definition_path = definition_path self.map_path = map_path self.tile_engine = TileEngine(join(Level.MAP_BASE, definition_path), join(Level.MAP_BASE, map_path)) self.camera = Camera(self.tile_engine, pygame.Rect(0, 0, Globals.WIDTH, Globals.HEIGHT)) self.tile_rect = self.tile_engine.get_tile_rect() self.enemySprites = pygame.sprite.Group() self.playerSprites = pygame.sprite.Group() self.turrets = list() self.lights = list() self.init_player() self.init_enemies() self.timer = None if Globals.HEALTH_BAR is None: Globals.HEALTH_BAR = HealthBar() if Globals.HUD_MANAGER is None: Globals.HUD_MANAGER = HUDManager() self.black_surf = pygame.Surface( (Globals.WIDTH, Globals.HEIGHT)).convert() self.black_surf.fill((0, 0, 0)) self.fade_in = False self.fade_out = False self.showing_subtitle = False self.alpha_factor = 300 self.should_fade_in = should_fade_in self.pausing = False self.going_back = False self.score_counted = False self.respawn_coords = [-1, -1] self.timer = None self.first_occur = True self.find_respawn() self.loader = AssetLoader('images', 'sounds') self.switch_sound = self.loader.load_sound(Level.SWITCH_SOUND_PATH) self.channel = None self.music_loops = music_loops self.music_handle = None if music_path is not None: self.background_music_handle = self.loader.load_sound(music_path) self.music_handle = self.background_music_handle else: self.background_music_handle = None if init_music_path is not None: self.init_music_handle = self.loader.load_sound(init_music_path) self.music_handle = self.init_music_handle else: self.init_music_handle = None self.time_init = 0 self.start_on_stop = True self.switched_sound = False self.music_end_id = Level.MUSIC_END_ID_BASE + mid self.can_open_doors = True
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()