def positionCombatants(self): # Align them and stuff for i in range(len(self.allies)): self.allies[i].setPosition( annchienta.Vector(120 - 20 * i, 75 + (i + 1) * 30)) for i in range(len(self.enemies)): self.enemies[i].setPosition( annchienta.Vector( self.videoManager.getScreenWidth() - 120 + 20 * i, 75 + (i + 1) * 30))
def update( self ): # Update input self.inputManager.update() # Calculate number of ms passed ms = 0.0 if self.lastUpdate is not None: ms = self.engine.getTicks() - self.lastUpdate self.lastUpdate = self.engine.getTicks() # Update background self.backgroundY += self.speed*ms while self.backgroundY >= self.background.getHeight(): self.backgroundY -= self.background.getHeight() # Move raft towards cursor mouse = annchienta.Vector( self.inputManager.getMouseX(), self.inputManager.getMouseY() ) mouse -= self.raft.position mouse.normalize() self.raft.position += mouse*self.speed*ms # Update rocks for rock in self.rocks: rock.position.y += self.speed*ms # New spawns self.nextSpawn -= ms while self.nextSpawn < 0 and self.engine.getTicks()<self.end: surface = self.cacheManager.getSurface( "sprites/rock" + str( self.mathManager.randInt( 1, 3 ) ) + ".png" ) position = annchienta.Vector( self.mathManager.randInt( 0, self.videoManager.getScreenWidth() ) , -surface.getHeight() ) rock = RaftObject( surface, position ) self.rocks.insert( 0, rock ) self.nextSpawn += self.mathManager.randInt( 600, 1500 ) # Remove rocks out of vision self.rocks = filter( lambda r: r.position.y < self.videoManager.getScreenHeight()+r.surface.getHeight(), self.rocks ) # Now check for collision with rocks for rock in self.rocks: dist = 0.2*float( self.raft.surface.getWidth()+self.raft.surface.getHeight()+rock.surface.getWidth()+rock.surface.getHeight() ) if rock.position.distance( self.raft.position ) < dist: self.running = False self.mapManager.stop() self.sceneManager.gameOver() # Stop if we're out the cave if self.engine.getTicks()>=self.end and not len(self.rocks): self.running = False
def takeRowAction(self, combatant): oldPosition = annchienta.Vector(combatant.position) combatant.changeRow() newPosition = annchienta.Vector(combatant.position) # Rever and do a small animation combatant.setPosition(oldPosition) self.addLine(combatant.getName().capitalize() + " moves to the " + combatant.row + " row!") animation = Animation.Animation(None, None) animation.setBattle(self) animation.setCombatant(combatant) animation.move(combatant, newPosition)
def __init__( self ): self.engine = annchienta.getEngine() self.videoManager = annchienta.getVideoManager() self.inputManager = annchienta.getInputManager() self.cacheManager = annchienta.getCacheManager() self.mapManager = annchienta.getMapManager() self.mathManager = annchienta.getMathManager() self.sceneManager = SceneManager.getSceneManager() # Background spriteace self.background = annchienta.Surface( "images/backgrounds/sky.png" ) self.backgroundY = 0.0 # Create a ship for the player self.ship = GameObject( annchienta.Vector( videoManager.getScreenWidth()/2, videoManager.getScreenHeight()/2 ), annchienta.Surface( "sprites/ship_small.png" ) ) # Load sprites into cache self.enemySprite = annchienta.Surface("sprites/ship_pirate.png") # All enemies self.enemies = [] # The final enemy self.captain = None # Number of miliseconds we need to fly to gain victory self.victoryTime = 60000
def play(self): origPosition = annchienta.Vector(self.getCombatant().getPosition()) position = annchienta.Vector(self.getTarget().getPosition()) dx = int(self.target.getWidth() / 2 + self.getCombatant().getWidth() / 2) dx = dx if self.target.isAlly() else -dx position.x += dx self.move(self.getCombatant(), position) if self.getSound(): self.audioManager.playSound(self.getSound()) if self.getSprite(): self.spriteOver(self.getTarget(), self.getSprite()) self.move(self.getCombatant(), origPosition)
def __init__(self, xmlElement): # Stuff for sounds self.cacheManager = annchienta.getCacheManager() self.audioManager = annchienta.getAudioManager() self.soundLevelup = self.cacheManager.getSound('sounds/levelup.ogg') self.soundClickNeu = self.cacheManager.getSound( 'sounds/click-neutral.ogg') # Base constructor Combatant.Combatant.__init__(self, xmlElement) # References self.partyManager = PartyManager.getPartyManager() self.logManager = annchienta.getLogManager() self.inputManager = annchienta.getInputManager() self.videoManager = annchienta.getVideoManager() # Get our weapon weaponElements = xmlElement.getElementsByTagName("weapon") if len(weaponElements): # Get the weapon name and search for the corresponding element weaponName = str(weaponElements[0].getAttribute("name")) self.setWeapon(weaponName) else: self.logManager.warning("No Weapon defined for Ally!") # Get the position of our hand handElements = xmlElement.getElementsByTagName("hand") if len(handElements): self.hand = annchienta.Vector( float(handElements[0].getAttribute("x")), float(handElements[0].getAttribute("y"))) else: self.hand = None # Create a dictionary describing the level grades self.grades = {} gradesElement = xmlElement.getElementsByTagName("grades")[0] for k in gradesElement.attributes.keys(): self.grades[k] = int(gradesElement.attributes[k].value) # Create dictionary describing the level learns self.learn = {} learnElement = xmlElement.getElementsByTagName("learn")[0] text = str(learnElement.firstChild.data) words = text.split() for i in range(int(len(words) / 2)): self.learn[int(words[i * 2])] = words[i * 2 + 1] # Build the menu. self.buildMenu()
def move(self, combatant, position): duration = 400 start = self.engine.getTicks() origPosition = annchienta.Vector(combatant.getPosition()) while self.inputManager.isRunning( ) and self.engine.getTicks() < start + duration: self.battle.update() factor = float(self.engine.getTicks() - start) / duration combatant.setPosition(origPosition * (1.0 - factor) + position * factor) self.videoManager.clear() self.battle.draw() self.videoManager.flip() # Make sure we're in the right position in the end. combatant.setPosition(annchienta.Vector(position))
def draw(self): # Draw ourself Combatant.Combatant.draw(self) # draw the weapon if self.hand and self.getWeapon().getSprite(): self.videoManager.push() weaponPosition = self.getPosition() - annchienta.Vector( self.getWidth() / 2, self.getHeight() / 2) + self.hand - self.getWeapon().getGrip() self.videoManager.translate(weaponPosition.x, weaponPosition.y) self.videoManager.drawSurface(self.weapon.getSprite(), 0, 0) self.videoManager.pop()
def __init__(self): # Currently viewed map. self.currentMap = None # Get a few references. self.videoManager = annchienta.getVideoManager() self.mapManager = annchienta.getMapManager() # Set the video mode self.videoManager.setVideoMode(640, 480, "NATE - Map View") # Initial camera position self.cameraPosition = annchienta.Vector(0, 0) # Draw grid method self.drawGridType = self.SIMPLE_DRAW_GRID
def __init__(self, xmlElement): # Call super constructor. BattleEntity.__init__(self, xmlElement) # Set our name self.description = str(xmlElement.getAttribute("description")) # Get our sprite spriteElements = xmlElement.getElementsByTagName("sprite") if len(spriteElements): cacheManager = annchienta.getCacheManager() self.sprite = cacheManager.getSurface( str(spriteElements[0].getAttribute("filename"))) self.grip = annchienta.Vector( float(spriteElements[0].getAttribute("gripx")), float(spriteElements[0].getAttribute("gripy"))) else: self.sprite = None self.grip = None
def spriteOver(self, combatant, sprite): duration = 800 start = self.engine.getTicks() while self.inputManager.isRunning( ) and self.engine.getTicks() < start + duration: self.battle.update() factor = float(self.engine.getTicks() - start) / duration position = combatant.getPosition() + annchienta.Vector( 0, -30) * factor self.videoManager.clear() self.battle.draw() self.videoManager.drawSurface( sprite, int(position.x - sprite.getWidth() / 2), int(position.y - sprite.getHeight() / 2)) self.videoManager.flip()
def __init__( self ): # Get references self.engine = annchienta.getEngine() self.videoManager = annchienta.getVideoManager() self.inputManager = annchienta.getInputManager() self.cacheManager = annchienta.getCacheManager() self.mapManager = annchienta.getMapManager() self.mathManager = annchienta.getMathManager() self.sceneManager = SceneManager.getSceneManager() # Load images self.background = annchienta.Surface("images/backgrounds/water.png") self.cacheManager.getSurface("sprites/rock1.png") self.cacheManager.getSurface("sprites/rock2.png") # Initial positions self.backgroundY = 0.0 self.speed = 0.1 self.nextSpawn = 500 self.rocks = [] self.raft = RaftObject( annchienta.Surface("sprites/raft.png"), annchienta.Vector( self.videoManager.getScreenWidth()/2, self.videoManager.getScreenHeight()/2 ) )
def __init__(self, xmlElement): # Call super constructor BattleEntity.BattleEntity.__init__(self, xmlElement) # We need to log stuff self.logManager = annchienta.getLogManager() # Get references self.videoManager = annchienta.getVideoManager() self.cacheManager = annchienta.getCacheManager() self.mathManager = annchienta.getMathManager() self.sceneManager = SceneManager.getSceneManager() # Create a dictionary describing the level stuff self.level = {} levelElement = xmlElement.getElementsByTagName("level")[0] for k in levelElement.attributes.keys(): self.level[k] = int(levelElement.attributes[k].value) # Create a dictionary describing the health stats self.healthStats = {} healthStatsElement = xmlElement.getElementsByTagName("healthstats")[0] for k in healthStatsElement.attributes.keys(): self.healthStats[k] = int(healthStatsElement.attributes[k].value) # Get all possible actions. The actual actions are in the first child # of the element, hence the code. <actions> action1 action2 </actions> actionsElement = xmlElement.getElementsByTagName("actions")[0] actionNames = str(actionsElement.firstChild.data).split() # Prepare to get the from the xml data self.actions = [] # Get them for a in actionNames: self.addAction(a) # Create a dictionary describing the elemental properties # Only enemies have them, usually self.primaryElemental = {} elementalElements = xmlElement.getElementsByTagName("elemental") if len(elementalElements): for k in elementalElements[0].attributes.keys(): self.primaryElemental[k] = float( elementalElements[0].attributes[k].value) # Load sprite spriteElement = xmlElement.getElementsByTagName("sprite")[0] # Keep the filename so we can save it later on self.spriteFilename = str(spriteElement.getAttribute("filename")) self.sprite = annchienta.Surface(self.spriteFilename) if spriteElement.hasAttribute("x1"): self.sx1 = int(spriteElement.getAttribute("x1")) self.sy1 = int(spriteElement.getAttribute("y1")) self.sx2 = int(spriteElement.getAttribute("x2")) self.sy2 = int(spriteElement.getAttribute("y2")) else: self.sx1, self.sy1 = 0, 0 self.sx2 = self.sprite.getWidth() self.sy2 = self.sprite.getHeight() # Get width and height from those facts. self.width = self.sx2 - self.sx1 self.height = self.sy2 - self.sy1 self.position = annchienta.Vector(0, 0) # We will draw a mark upon ourselves sometimes self.active = False self.selected = False # Damage done by an attack self.damage = 0 self.damageTimer = 0.0 self.reset()
def play(self): position = annchienta.Vector(self.getCombatant().getPosition()) position.x -= 30 if self.getCombatant().isAlly() else -30 self.move(self.getCombatant(), position)
def __init__( self, position, sprite ): self.videoManager = annchienta.getVideoManager() self.pos = position self.sprite = sprite self.dir = annchienta.Vector( 0, 0 )
def selectTarget(self, battle, selectEnemies=True): done = False mouseSelected = False # select a first enemy (there should always be one, # because it's not victory or game over) targetIndex = 0 if selectEnemies: target = battle.enemies[0] else: target = battle.allies[0] while not done: # Update battle.update(False) # Update input self.inputManager.update() # Keyboard actions if self.inputManager.keyTicked(annchienta.SDLK_DOWN): self.audioManager.playSound(self.soundClickNeu) targetIndex += 1 mouseSelected = False elif self.inputManager.keyTicked(annchienta.SDLK_UP): self.audioManager.playSound(self.soundClickNeu) targetIndex -= 1 mouseSelected = False elif self.inputManager.keyTicked( annchienta.SDLK_LEFT) or self.inputManager.keyTicked( annchienta.SDLK_RIGHT): self.audioManager.playSound(self.soundClickNeu) selectEnemies = not selectEnemies mouseSelected = False elif self.inputManager.isMouseMoved(): # Find out hover target # Just have it point to the closest combatant. distance = 0 oldTarget = target target = None for i in range(len(battle.combatants)): combatant = battle.combatants[i] # Use Vectors to calculate distance between mouse and combatant c. mouse = annchienta.Vector(self.inputManager.getMouseX(), self.inputManager.getMouseY()) pos = annchienta.Vector(combatant.getPosition().x, combatant.getPosition().y) d = mouse.distance(pos) if d < distance or target is None: target = combatant targetIndex = i distance = d if target != oldTarget: self.audioManager.playSound(self.soundClickNeu) mouseSelected = True if not mouseSelected: if selectEnemies: targetIndex = targetIndex % len(battle.enemies) target = battle.enemies[targetIndex] else: targetIndex = targetIndex % len(battle.allies) target = battle.allies[targetIndex] # Set selected mark target.setSelected(True) # Check for input if not self.inputManager.isRunning( ) or self.inputManager.buttonTicked( 1) or self.inputManager.cancelKeyTicked(): target = None done = True if self.inputManager.buttonTicked( 0) or self.inputManager.interactKeyTicked(): done = True # Draw battle.videoManager.clear() battle.draw() # Draw "select target" self.sceneManager.activeColor() self.videoManager.drawString( self.sceneManager.getLargeItalicsFont(), "Select Target", self.sceneManager.getMargin(), 40) battle.videoManager.flip() # Reset selected mark for c in battle.combatants: c.setSelected(False) return target
def update( self ): # Update input self.inputManager.update() if not self.inputManager.isRunning(): return # Number of ms passed ms = 0.0 if self.lastUpdate: ms = float( self.engine.getTicks() - self.lastUpdate ) self.lastUpdate = self.engine.getTicks() # Update background self.backgroundY += ms*1.0 #while( self.backgroundY > self.videoManager.getScreenHeight() ): self.backgroundY %= self.videoManager.getScreenHeight() # Update player mouse = annchienta.Vector( self.inputManager.getMouseX(), self.inputManager.getMouseY() ) mouse -= self.ship.pos mouse.normalize() mouse *= (ms * 0.3) self.ship.pos += mouse # Check if we should spawn enemies if self.engine.getTicks()<self.victoryTime: self.nextEnemySpawn -= ms while self.nextEnemySpawn <= 0 and self.inputManager.isRunning(): # Spawn a new enemy pos = annchienta.Vector( self.mathManager.randInt( 0, videoManager.getScreenWidth() ), videoManager.getScreenHeight() + self.enemySprite.getHeight() ) self.enemies += [ GameObject( pos, self.enemySprite ) ] self.nextEnemySpawn += self.mathManager.randInt( 500, 2000 ) # Check if we should spawn the captain else: # Wait until all enemies are gone if not len(self.enemies) and not self.captain: # Spawn our captain pos = annchienta.Vector( videoManager.getScreenWidth()/2, videoManager.getScreenHeight()+100 ) self.captain = GameObject( pos, annchienta.Surface("sprites/ship_captain.png") ) # Move enemies for enemy in self.enemies: vect = None # Just fly on in current direction when we # already passed the player if enemy.pos.y < self.ship.pos.y: vect = annchienta.Vector( enemy.dir ) # Else, approach the player's ship else: vect = self.ship.pos - enemy.pos vect.normalize() enemy.dir = annchienta.Vector( vect ) vect.y = -1 vect *= ( ms * 0.2 ) enemy.pos += vect # Move captain if self.captain: vect = self.ship.pos - self.captain.pos vect.normalize() vect *= ( ms * 0.2 ) self.captain.pos += vect # Minimum distance between player and enemies minDistance = 0.25*( self.enemySprite.getWidth() + self.enemySprite.getHeight() + self.ship.sprite.getWidth() + self.ship.sprite.getHeight() ) # Check for collision between enemies and # the player for enemy in self.enemies: if enemy.pos.distance( self.ship.pos ) < minDistance: # Game over self.running = False self.sceneManager.fade() self.sceneManager.text("We were caught by some sky pirate and executed...", None) self.mapManager.stop() # Check for collision between player and # the captain if self.captain: if self.captain.pos.distance( self.ship.pos ) < minDistance: self.running = False self.sceneManager.fade() self.sceneManager.text("We were lucky. We were caught by one of their captains, who was impressed by our flying.", None) self.sceneManager.text("He brouht us aboard their mothership...", None) # Remove enemies out of screen self.enemies = filter( lambda e: e.pos.y > -e.sprite.getHeight(), self.enemies )