def _shoot(self, game): from pygame.locals import K_SPACE if self.stype and game.keystate[K_SPACE]: u = unitVector(self.orientation) newones = game._createSprite([self.stype], (self.lastrect.left + u[0] * self.lastrect.size[0], self.lastrect.top + u[1] * self.lastrect.size[1])) if len(newones) > 0 and isinstance(newones[0], OrientedSprite): newones[0].orientation = unitVector(self.orientation) self._reduceAmmo()
def conveySprite(sprite, partner, game): """ Moves the partner in target direction by some step size. """ tmp = sprite.lastrect v = unitVector(partner.orientation) sprite.physics.activeMovement(sprite, v, speed=partner.strength) sprite.lastrect = tmp game._updateCollisionDict(sprite)
def slipForward(sprite, partner, game, prob=0.5): """ Slip forward in the direction of the current orientation, sometimes.""" if prob > random(): tmp = sprite.lastrect v = unitVector(sprite.orientation) sprite.physics.activeMovement(sprite, v, speed=1) sprite.lastrect = tmp game._updateCollisionDict(sprite)
def _draw(self, game): """ With a triangle that shows the orientation. """ VGDLSprite._draw(self, game) if self.draw_arrow: col = (self.color[0], 255 - self.color[1], self.color[2]) pygame.draw.polygon( game.screen, col, triPoints(self.rect, unitVector(self.orientation)))
def bounceDirection(sprite, partner, game, friction=0): """ The centers of the objects determine the direction""" stepBack(sprite, partner, game) inc = sprite.orientation snorm = unitVector((-sprite.rect.centerx + partner.rect.centerx, - sprite.rect.centery + partner.rect.centery)) dp = snorm[0] * inc[0] + snorm[1] * inc[1] sprite.orientation = (-2 * dp * snorm[0] + inc[0], -2 * dp * snorm[1] + inc[1]) sprite.speed *= (1. - friction)
def activeMovement(self, sprite, action, speed=None): """ Here the assumption is that the controls determine the direction of acceleration of the sprite. """ if speed is None: speed = sprite.speed v1 = action[0] / float(sprite.mass) + sprite.orientation[0] * speed v2 = action[1] / float(sprite.mass) + sprite.orientation[1] * speed sprite.orientation = unitVector((v1, v2)) sprite.speed = vectNorm((v1, v2)) / vectNorm(sprite.orientation)
def windGust(sprite, partner, game): """ Moves the partner in target direction by some step size, but stochastically (step, step-1 and step+1 are equally likely) """ s = choice([partner.strength, partner.strength + 1, partner.strength - 1]) if s != 0: tmp = sprite.lastrect.copy() v = unitVector(partner.orientation) sprite.physics.activeMovement(sprite, v, speed=s) sprite.lastrect = tmp game._updateCollisionDict(sprite)
def _aim(self, game): action = self._readAction(game) if action in [UP, DOWN]: if action == DOWN: angle = self.angle_diff else: angle = -self.angle_diff from math import cos, sin self.orientation = unitVector((self.orientation[0]*cos(angle)-self.orientation[1]*sin(angle), self.orientation[0]*sin(angle)+self.orientation[1]*cos(angle)))
def bounceForward(sprite, partner, game): # FLAG """ The partner sprite pushed, so if possible move in the opposite direction. """ # print "in beginning of bounceForward" # embed() sprite.physics.activeMovement(sprite, unitVector(partner.lastdirection)) game._updateCollisionDict(sprite) # print "in second part of bounceForward" # embed() # return ('bounceForward', colorDict[str(partner.color)], colorDict[str(sprite.color)]) return ('bounceForward', sprite.ID, partner.ID)
def pullWithIt(sprite, partner, game): """ The partner sprite adds its movement to the sprite's. """ if not oncePerStep(sprite, game, 'lastpull'): return tmp = sprite.lastrect v = unitVector(partner.lastdirection) sprite._updatePos(v, partner.speed * sprite.physics.gridsize[0]) if isinstance(sprite.physics, ContinuousPhysics): sprite.speed = partner.speed sprite.orientation = partner.lastdirection sprite.lastrect = tmp
def wallStop(sprite, partner, game, friction=0): # FLAG """ Stop just in front of the wall, removing that velocity component, but possibly sliding along it. """ if not oncePerStep(sprite, game, 'laststop'): return stepBack(sprite, partner, game) if abs(sprite.rect.centerx - partner.rect.centerx) > abs(sprite.rect.centery - partner.rect.centery): sprite.orientation = (0, sprite.orientation[1] * (1. - friction)) else: sprite.orientation = (sprite.orientation[0] * (1. - friction), 0) sprite.speed = vectNorm(sprite.orientation) * sprite.speed sprite.orientation = unitVector(sprite.orientation)
def wallStop(sprite, partner, game, friction=0): """ Stop just in front of the wall, removing that velocity component, but possibly sliding along it. """ if not oncePerStep(sprite, game, 'laststop'): return stepBack(sprite, partner, game) if abs(sprite.rect.centerx - partner.rect.centerx) > abs(sprite.rect.centery - partner.rect.centery): sprite.orientation = (0, sprite.orientation[1] * (1. - friction)) else: sprite.orientation = (sprite.orientation[0] * (1. - friction), 0) sprite.speed = vectNorm(sprite.orientation) * sprite.speed sprite.orientation = unitVector(sprite.orientation)
def bounceForward(sprite, partner, game): """ The partner sprite pushed, so if possible move in the opposite direction. """ sprite.physics.activeMovement(sprite, unitVector(partner.lastdirection)) game._updateCollisionDict(sprite) return ('bounceForward', colorName(partner.color), colorName(sprite.color))
def _draw(self, game): """ With a triangle that shows the orientation. """ VGDLSprite._draw(self, game) if self.draw_arrow: col = (self.color[0], 255 - self.color[1], self.color[2]) pygame.draw.polygon(game.screen, col, triPoints(self.rect, unitVector(self.orientation)))
def bounceForward(sprite, partner, game): """ The partner sprite pushed, so if possible move in the opposite direction. """ sprite.physics.activeMovement(sprite, unitVector(partner.lastdirection)) game._updateCollisionDict(sprite)