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
Example #2
0
    def __init__(self, config):
        self._config = config
        if self._config.dataPath[-1] != '/':
            self._config.dataPath += '/'
	if self._config.startLevel > 1:
		self._config.cheat = True
        self._screen = None
        self._clock = None
        self._terminated = False
        self._display = None
        self._world = None
        self._gameState = model.GameState()
        self._player = None
        self._worldLoader = WorldLoader(self._config.dataPath)
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()
Example #4
0
class Game:
    titleDemo = [3,0,0,3,0,1,2,1,1,3,0,2,0,3,1,2,1,3,0,2,0,3,1,2,1,3,0,2,0,0,3,2,1,3,2,1,3,3,2,1,3,1,2,2,2,2,2,2,2,1,2,1,1,0,3,1,2,3,0,2,0,3,1,2,1,1,3,2,0,3,2,0,3,3,2,0,3,1,1,3,3,3,0,0,1,2,2,0,0,0,3,1,1,2,3,3,2,0,2,3,3,2,0,3,1,1,3,3,0,0,1,2,2,0,0,0,3,1,3,2,2,1,3,2,1,3,2,0,0,3,0,3,1,2,1,1,0,0,3,3,1,2,2,3,1,1,2]
    
    def __init__(self, config):
        self._config = config
        if self._config.dataPath[-1] != '/':
            self._config.dataPath += '/'
	if self._config.startLevel > 1:
		self._config.cheat = True
        self._screen = None
        self._clock = None
        self._terminated = False
        self._display = None
        self._world = None
        self._gameState = model.GameState()
        self._player = None
        self._worldLoader = WorldLoader(self._config.dataPath)

    def _init(self):
        sound.soundManager.init(self._config)
        pygame.init()
        self._initDisplay()
        pygame.display.set_caption('Node Reviver - by Vincent Petry (MiniLD#33)')
        pygame.mouse.set_visible(False) 
        self._screen = pygame.display.get_surface()
        self._clock = pygame.time.Clock()
        self._display = view.Display(self._config, self._screen, self._gameState)
        sound.soundManager.loadSounds()

    def _quit(self):
        sound.soundManager.release()
        pygame.quit()
        
    def _initDisplay(self):
        flags = 0
        if self._config.fullScreen:
            flags = pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF

        pygame.display.set_mode(self._config.screenSize, flags)

    def _movePlayer(self, direction):
        if self._player.moving:
            return

        movements = [(0, -1), (0, 1), (-1, 0), (1, 0)]
        movement = movements[direction]
        # Check whether player is allowed to move to this direction
        edge = self._player.currentNode.getEdgeByDirection(movement)
        if edge:
            self._player.moveAlong(edge)

    def _handleInputEvent(self, event):
        if event.type == pygame.locals.QUIT: 
            self._terminated = True
        elif event.type == pygame.locals.KEYDOWN:
            if event.key in self._config.keymap.pause:
                self._gameState.pause = not self._gameState.pause
            elif event.key in self._config.keymap.start:
                mods = pygame.key.get_mods()
                if mods & pygame.locals.KMOD_ALT:
                    self._config.fullScreen = not self._config.fullScreen
                    self._initDisplay()
                elif self._gameState.state == GameState.TITLE: 
                    self._showStory()
                elif self._gameState.state == GameState.STORY: 
                    self._startGame()
                elif self._gameState.state == GameState.ENDGAME: 
                    self._startTitle()
            elif self._gameState.state == GameState.GAME and not self._gameState.pause:
                directionKeys = self._config.keymap.directions
                for direction in range(4):
                    if event.key in directionKeys[direction]:
                        self._movePlayer(direction)
                        break

        elif event.type == pygame.locals.KEYUP:
            if event.key in self._config.keymap.quit:
                self.onBack()

    def onBack(self):
        if self._gameState.state == GameState.TITLE:
            self._terminated = True
        else:
            self._startTitle()

    def _handleInput(self):
        for event in pygame.event.get():
            self._handleInputEvent(event)

    def _handleDemo(self):
        if self._player.moving:
            return
        
        nextDirection = self._titleDemo[0]
        self._titleDemo = self._titleDemo[1:]
        self._movePlayer(nextDirection)
        
    def _handleLogic(self):
        state = self._gameState 
        state.update()
        if state.state == GameState.NEXT_LEVEL or state.state == GameState.RESTART_LEVEL:
            if state.state == GameState.NEXT_LEVEL:
                state.worldNum += 1
                if state.worldNum > self._config.levelsCount:
                    state.setState(GameState.ENDGAME)
                    return;
            state.dirty = True
            self._initWorld(state.worldNum)
            state.setState(GameState.LEVEL_START, self._config.fps)

        if state.state == GameState.TITLE:
            # plays title demo
            self._handleDemo()

        if state.pause:
            return

        self._gameState.elapsed += 1

        if state.state in [GameState.GAME, GameState.TITLE, GameState.LEVEL_END, GameState.LEVEL_START]:
            self._world.update()
            
        if state.state in [GameState.GAME, GameState.TITLE] and self._world.hasAllEdgesMarked():
            self.onLevelEnd()

        if state.state == GameState.GAME:
            # check for player collision
            for entity in self._world.entities:
                if entity == self._player:
                    continue            
                dist = vectorDiff(entity.pos, self._player.pos)
                if abs(dist[0]) < 10 and abs(dist[1]) < 10:
                    self._player.die()
                    state.setState(GameState.DEAD, self._config.fps, GameState.RESTART_LEVEL);
                    sound.soundManager.play(sound.soundManager.DEAD)

    def onLevelEnd(self):
        if self._gameState.state == GameState.TITLE:
            self._startTitle()
            return
        self._gameState.setState(GameState.LEVEL_END, self._config.fps, GameState.NEXT_LEVEL);

    def _showStory(self):
        self._gameState.setState(GameState.STORY)

    def _startGame(self, worldNum = None):
        if worldNum == None:
            worldNum = self._config.startLevel        
        self._gameState.setState(GameState.LEVEL_START, self._config.fps)
        self._gameState.worldNum = worldNum
        self._initWorld(self._gameState.worldNum)
        sound.soundManager.enable()

    def _startTitle(self):
        self._gameState.state = GameState.TITLE
        self._initWorld(0)

    def _initWorld(self, worldNum):
        self._player = model.Player()
        if self._gameState.state == GameState.TITLE:
            self._player.speed = 4
            self._world = self._worldLoader.loadWorld(0)
            self._titleDemo = list(self.titleDemo)
            sound.soundManager.enable(False)
        else:
            self._world = self._worldLoader.loadWorld(worldNum)
        self._world.centerInView(self._config.screenSize)
        self._player.setCurrentNode(self._world.startNode)
        self._display.setWorld(self._world, self._player)
        self._display.addEntityView( view.PlayerView(self._player) )
        self._world.addEntity(self._player)

        # set tracking foes to track the player
        for entity in self._world.entities:
            if entity.entityType == 1 and entity.foeType == 1:
                entity.track(self._player)

    def run(self):
        self._init()
        self._gameState.state = GameState.TITLE
        self._initWorld(self._gameState.worldNum)

        while not self._terminated:
            self._handleInput()
            self._handleLogic()
            self._display.render()
            pygame.display.flip()
            self._clock.tick(self._config.fps)
            
        self._quit()