Exemplo n.º 1
0
class Ship:
    def __init__(self):
        self.pos = Vector.null()
        self.dir = Vector.unit('E')
        self.waypoint = Vector(10, 1)

    def step(self, action, value):
        if action in Vector.directions:
            d = Vector.unit(action)
            self.pos += d * value
        elif action == 'L':
            while value > 0:
                self.dir.rotate_left_90()
                value -= 90
        elif action == 'R':
            while value > 0:
                self.dir.rotate_right_90()
                value -= 90
        elif action == 'F':
            d = self.dir
            self.pos += d * value
        else:
            raise Exception('Unknown action')

    def waypoint_step(self, action, value):
        if action in Vector.directions:
            d = Vector.unit(action)
            self.waypoint += d * value
        elif action == 'L':
            while value > 0:
                self.waypoint.rotate_left_90()
                value -= 90
        elif action == 'R':
            while value > 0:
                self.waypoint.rotate_right_90()
                value -= 90
        elif action == 'F':
            d = self.waypoint
            self.pos += d * value
        else:
            raise Exception('Unknown action')

    def parse_command(self, command):
        action = command[0]
        value = int(command[1:])
        return action, value

    def manhattan(self):
        return self.pos.manhattan()
Exemplo n.º 2
0
    def __init__(self, pygame, anim, xPos, yPos, width, height, tag) :
        '''
        Initialize the sprite.
        :param anim: Animation images.
        :param xPos: X position.
        :param yPos: Y position.
        :param tag: Sprite text tag (Text Object).
        :return: None
        '''

        GameObject.__init__(self, xPos, yPos, width, height)

        self.tag = tag
        self.animContent = anim

        self.hitBoxes = [   [(10,0,30,30), (15,30,20,70)],
                            [(10,50,30,50)]                      ]

        self.currentDir = 0
        self.lastDir = -1
        self.attackStages = ((18,19),(20,21),(22,23))
        self.grounded = True
        self.crouched = False
        self.allowDoubleJump = True
        self.allowStateChange = False
        self.handicap = False
        self.animStage = 0
        self.action = 0

        '''
        Action 0, Anim 0: Stand Stationary
        Action 1, Anim 1: Start running
        Action 2, Anim 2 - 6: Running animation
        Action 3, Anim 7 - 9: Falling animation
        Action 4, Anim 10 - 11: Crouch animation
        Action 5, Anim 12 - 17: Roll animation
        Action 6, Anim 18 - 19: Punch animation
        Action 7, Anim 20 - 21: Crouch Punch animation
        Action 8, Anim 22 - 23: Kick animation
        Action 9, Anim 24: Launch animation
        Action 10, Anim 25: Lay animation
        '''

        self.frameDelay = 3
        self.frameCount = 0

        self.groundSpeed = 5
        self.dashDistance = 2
        self.rollDistance = 6
        self.fallSpeedX = 3
        self.jumpSpeed = -15
        self.defaultLaunchSpeed = 10

        self.damage = 0

        self.lastXPos = self.xPos
        self.lastYPos = self.yPos
        self.vel = Vector(0, 0)
Exemplo n.º 3
0
    def applyDamage(self, dam, di) :
        '''
        Apply damage and manage launch speed.
        :param dam: Damage
        :param di: Direction
        :return: None
        '''

        self.damage += dam
        self.handicap = True
        self.action = 9
        self.crouched = False
        self.currentDir = di*-1

        print self.damage

        if self.damage <= 50 :
            self.vel = Vector(self.defaultLaunchSpeed*di, randint(-30,-20)*di)
        else :
            self.vel = Vector(int(self.defaultLaunchSpeed*(self.damage/50.0))*di, randint(-45,-30)*di)
Exemplo n.º 4
0
 def waypoint_step(self, action, value):
     if action in Vector.directions:
         d = Vector.unit(action)
         self.waypoint += d * value
     elif action == 'L':
         while value > 0:
             self.waypoint.rotate_left_90()
             value -= 90
     elif action == 'R':
         while value > 0:
             self.waypoint.rotate_right_90()
             value -= 90
     elif action == 'F':
         d = self.waypoint
         self.pos += d * value
     else:
         raise Exception('Unknown action')
Exemplo n.º 5
0
class Sprite (GameObject) :

    def __init__(self, pygame, anim, xPos, yPos, width, height, tag) :
        '''
        Initialize the sprite.
        :param anim: Animation images.
        :param xPos: X position.
        :param yPos: Y position.
        :param tag: Sprite text tag (Text Object).
        :return: None
        '''

        GameObject.__init__(self, xPos, yPos, width, height)

        self.tag = tag
        self.animContent = anim

        self.hitBoxes = [   [(10,0,30,30), (15,30,20,70)],
                            [(10,50,30,50)]                      ]

        self.currentDir = 0
        self.lastDir = -1
        self.attackStages = ((18,19),(20,21),(22,23))
        self.grounded = True
        self.crouched = False
        self.allowDoubleJump = True
        self.allowStateChange = False
        self.handicap = False
        self.animStage = 0
        self.action = 0

        '''
        Action 0, Anim 0: Stand Stationary
        Action 1, Anim 1: Start running
        Action 2, Anim 2 - 6: Running animation
        Action 3, Anim 7 - 9: Falling animation
        Action 4, Anim 10 - 11: Crouch animation
        Action 5, Anim 12 - 17: Roll animation
        Action 6, Anim 18 - 19: Punch animation
        Action 7, Anim 20 - 21: Crouch Punch animation
        Action 8, Anim 22 - 23: Kick animation
        Action 9, Anim 24: Launch animation
        Action 10, Anim 25: Lay animation
        '''

        self.frameDelay = 3
        self.frameCount = 0

        self.groundSpeed = 5
        self.dashDistance = 2
        self.rollDistance = 6
        self.fallSpeedX = 3
        self.jumpSpeed = -15
        self.defaultLaunchSpeed = 10

        self.damage = 0

        self.lastXPos = self.xPos
        self.lastYPos = self.yPos
        self.vel = Vector(0, 0)

    def update(self, physics, objs) :
        '''
        Update the sprite.
        :param physics: Physics instance.
        :param objs: Environment objects.
        :return: Attack position (x,y), or None.
        '''

        ### POSITIONS
        self.frameCount += 1

        if self.currentDir != 0 :
            self.lastDir = self.currentDir

        if self.action in (2,3) :
            if self.grounded :
                self.vel.setMagX(self.groundSpeed * self.currentDir)
            else :
                self.vel.setMagX(self.fallSpeedX * self.currentDir)
        else :
            if self.grounded :
                self.vel.setMagX(self.vel.getMagX()/2.0)
                if abs(self.vel.getMagX()) < 1 :
                    self.vel.setMagX(0)

        self.vel = physics.applyGravity(self.vel)

        self.lastXPos = self.xPos
        self.lastYPos = self.yPos

        if 1 <= self.action <= 3 :
            self.xPos += self.vel.getMagX()
        elif self.action in (0,4):
            self.xPos += self.vel.getMagX()
            self.currentDir = 0
        elif self.action == 5 :
            self.xPos += self.rollDistance * self.lastDir
        elif self.action in (6,8) :
            self.xPos += self.dashDistance * self.lastDir
        elif self.action == 9 :
            self.xPos += self.vel.getMagX()

        self.yPos += self.vel.getMagY()
        self.tag.setPos(self.xPos + 17, self.yPos - 20)
        ###

        ### COLLISIONS
        self.checkCollisions(objs, self.hitBoxes[int(self.crouched)][int(not self.crouched)])

        if self.grounded :
            if self.action == 3 :
                if self.currentDir != 0 :
                    self.action = 1
                else :
                    self.action = 0
            elif self.action == 9 :
                self.action = 0

            self.handicap = False
        else :
            if self.action <= 4 :
                self.action = 3

        ### ANIMATIONS
        if self.damage >= 300 :
            if self.grounded :
                self.action = 10
                self.animStage = 25
            self.allowStateChange = False

        if self.frameCount >= self.frameDelay :
            self.frameCount = 0

            if self.action == 0 :
                if 10 < self.animStage <= 12 :
                    self.animStage -= 1
                else :
                    self.animStage = 0

            elif self.action == 1:
                self.animStage = 1
                self.action = 2

            elif self.action == 2:
                if self.animStage < 6 :
                    self.animStage += 1
                else :
                    self.animStage = 3

            elif self.action == 3:
                if self.vel.getMagY() < -3 :
                    self.animStage = 7
                elif self.vel.getMagY() > 3 :
                    self.animStage = 9
                else :
                    self.animStage = 8

            elif self.action == 4 :
                if not self.crouched :
                    if self.currentDir == 0 :
                        self.action = 0
                    else :
                        self.action = 1
                if self.animStage < 10 :
                    self.animStage = 10
                elif 10 <= self.animStage < 11 :
                    self.animStage += 1
                else :
                    self.animStage = 11

            elif self.action == 5:
                if self.animStage < 11 or self.animStage > 17 :
                    self.animStage = 11
                    self.allowStateChange = False
                elif 11 <= self.animStage < 17 :
                    self.animStage += 1
                    self.allowStateChange = False
                else :
                    self.animStage = 11
                    self.allowStateChange = True
                    if self.currentDir == 0 :
                        self.action = 4
                    else :
                        self.action = 1

            elif self.action == 6 :
                if self.animStage < self.attackStages[0][0] :
                    self.animStage = self.attackStages[0][0]
                    self.allowStateChange = False
                elif self.attackStages[0][0] <= self.animStage < self.attackStages[0][1]  :
                    self.animStage += 1
                else :
                    self.allowStateChange = True
                    if self.currentDir == 0 :
                        self.action = 0
                    else :
                        self.action = 1

            elif self.action == 7 :
                if self.animStage < self.attackStages[1][0] :
                    self.animStage = self.attackStages[1][0]
                    self.allowStateChange = False
                elif self.attackStages[1][0] <= self.animStage < self.attackStages[1][1]  :
                    self.animStage += 1
                else :
                    self.allowStateChange = True
                    self.action = 4

            elif self.action == 8 :
                if self.animStage < self.attackStages[2][0] :
                    self.animStage = self.attackStages[2][0]
                    self.allowStateChange = False
                elif self.attackStages[2][0] <= self.animStage < self.attackStages[2][1] :
                    self.animStage += 1
                else :
                    self.allowStateChange = True
                    if self.currentDir == 0 :
                        self.action = 0
                    else :
                        self.action = 1

            elif self.action == 9 :
                self.allowStateChange = True
                if self.animStage != 24 :
                    self.animStage = 24

            elif self.action == 10 :
                self.allowStateChange = False
                if self.animStage != 25 :
                    self.animStage = 25
        ###

        ### ATTACKS
        if self.lastDir == 1 :
            if self.animStage == self.attackStages[0][1] :
                return self.xPos + self.width, self.yPos + 40, self.xPos + self.width/2.0, self.lastDir
            elif self.animStage == self.attackStages[1][1] :
                return self.xPos + self.width, self.yPos + 75, self.xPos + self.width/2.0, self.lastDir
            elif self.animStage == self.attackStages[2][1] :
                return self.xPos + self.width, self.yPos + 70, self.xPos + self.width/2.0, self.lastDir
            else :
                return None
        else :
            if self.animStage == self.attackStages[0][1] :
                return self.xPos, self.yPos + 40, self.xPos + self.width/2.0, self.lastDir
            elif self.animStage == self.attackStages[1][1] :
                return self.xPos, self.yPos + 75, self.xPos + self.width/2.0, self.lastDir
            elif self.animStage == self.attackStages[2][1] :
                return self.xPos, self.yPos + 70, self.xPos + self.width/2.0, self.lastDir
            else :
                return None
        ###

    def draw(self, screen, pygame, camera) :
        '''
        Draw the sprite.
        :param screen: Surface to draw to.
        :param pygame: Pygame instance.
        :param camera: Camera instance.
        :return: None
        '''

        if self.animStage == 25 :
            w = int(self.width*2)
        else :
            w = int(self.width)

        if self.lastDir == -1 :
            screen.blit(pygame.transform.smoothscale(self.animContent[0][self.animStage],
                                                        camera.zoomToGameScreen(w, int(self.height))),
                                                        camera.transToGameScreen(self.xPos, self.yPos))
        else :
            screen.blit(pygame.transform.smoothscale(self.animContent[1][self.animStage],
                                                        camera.zoomToGameScreen(w, int(self.height))),
                                                        camera.transToGameScreen(self.xPos, self.yPos))

        self.tag.drawToCamera(screen, pygame, camera)

    def goDirection(self, direction) :
        '''
        Move in the direction specified.
        :param direction: (Int) Direction.
        :return: None
        '''
        if not self.allowStateChange :
            return

        self.currentDir = direction
        if self.grounded :
            if self.action == 4:
                self.action = 5
            else :
                self.action = 1
        else :
            self.action = 3

    def jump(self) :
        '''
        Make the player jump.
        :return: None
        '''
        if not self.allowStateChange or self.action == 9 :
            return

        self.action = 3
        if self.grounded :
            self.vel.setMagY(self.jumpSpeed)
            self.grounded = False
        elif self.allowDoubleJump :
            self.vel.setMagY(self.jumpSpeed)
            self.allowDoubleJump = False

    def stop(self) :
        '''
        Stops the player from running.
        :return: None
        '''

        if not self.allowStateChange and self.action not in (4, 5, 6, 8) :
            return

        self.currentDir = 0

        if self.action not in (4, 5, 6, 8) :
            self.action = 0

    def punch(self) :
        '''
        Make the sprite punch.
        :return: None
        '''
        if not self.allowStateChange :
            return

        if self.action == 4 :
            self.action = 7
        else :
            self.action = 6
        return

    def kick(self) :
        '''
        Make the sprite kick.
        :return: None
        '''
        if not self.allowStateChange :
            return
        self.action = 8

    def crouch(self) :
        '''
        Make the sprite crouch.
        :return: None
        '''
        self.crouched = True

        if not self.allowStateChange :
            return

        if self.currentDir == 0 :
            self.action = 4
        else :
            self.action = 5

    def stand(self) :
        '''
        Make the sprite stand.
        :return: None
        '''
        self.crouched = False

        if not self.allowStateChange :
            return

        if self.action == 9 :
            self.action = 0
            return

        if self.currentDir == 0 :
            self.action = 0
        else :
            self.action = 1

    def checkCollisions(self, objs, hitbox) :
        '''
        Check for any collisions with the given objects.
        :param objs: Objects.
        :param hitbox: HItbox to check collisions with.
        :return: None
        '''

        self.grounded = False

        for i in objs :
            boundary = i.getBoundary()
            if boundary == 'L' and self.xPos + hitbox[0] < i.getXPos() + i.getWidth() :
                self.xPos = i.getXPos() + i.getWidth() - hitbox[0]
                if self.action == 9 :
                    self.vel.setMagX(self.vel.getMagX()/-2.0)
                    self.currentDir *= -1
                else :
                    self.vel.setMagX(0)

            elif boundary == 'R' and self.xPos + hitbox[0] + hitbox[2] > i.getXPos() :
                self.xPos = i.getXPos() - hitbox[0] - hitbox[2]
                if self.action == 9 :
                    self.vel.setMagX(self.vel.getMagX()/-2.0)
                    self.currentDir *= -1
                else :
                    self.vel.setMagX(0)

            elif boundary == 'B' and self.yPos + self.height > i.getYPos() :
                self.yPos = i.getYPos() - self.height
                self.vel.setMagY(0)
                self.grounded = True
                self.allowDoubleJump = True

            elif boundary == 'D' and self.yPos + self.height > i.getYPos() :
                self.damage = 300

            elif boundary == 'NA' :
                if (i.getXPos() < self.xPos + hitbox[0] + hitbox[2] and
                            self.xPos + hitbox[0] < i.getXPos() + i.getWidth() and
                            self.lastYPos + self.height <= i.getYPos() < self.yPos + self.height and
                            self.vel.getMagY() > 0) :

                    self.yPos = i.getYPos() - self.height
                    self.vel.setMagY(0)
                    self.grounded = True
                    self.allowDoubleJump = True

    def opposingAttacks(self, a) :
        '''
        Check and handle any opposing attacks that were made by the other player.
        :param a: Attack position (x,y).
        :return: None
        '''

        if a is None or self.handicap :
            return

        if self.crouched and self.checkAttackCollisions(a, self.hitBoxes[1][0]) :
            self.applyDamage(randint(15,20), a[3])

        elif (not self.crouched) and self.checkAttackCollisions(a, self.hitBoxes[0][0]) :
            self.applyDamage(randint(15,20), a[3])

        elif (not self.crouched) and self.checkAttackCollisions(a, self.hitBoxes[0][1]) :
            self.applyDamage(randint(7,12), a[3])

    def applyDamage(self, dam, di) :
        '''
        Apply damage and manage launch speed.
        :param dam: Damage
        :param di: Direction
        :return: None
        '''

        self.damage += dam
        self.handicap = True
        self.action = 9
        self.crouched = False
        self.currentDir = di*-1

        print self.damage

        if self.damage <= 50 :
            self.vel = Vector(self.defaultLaunchSpeed*di, randint(-30,-20)*di)
        else :
            self.vel = Vector(int(self.defaultLaunchSpeed*(self.damage/50.0))*di, randint(-45,-30)*di)

    def checkAttackCollisions(self, a, hitbox) :
        '''
        Checks to see if an attack landed a hit.
        :param a: Position of the attack (x,y).
        :param hitbox: Current hitbox.
        :return: Boolean - True if hit, False if not.
        '''

        if (((a[2] <= self.xPos + hitbox[0] <= a[0] and a[3] == 1) or
                (a[0] <= self.xPos + hitbox[0] + hitbox[2] <= a[2] and a[3] == -1)) and
                self.yPos + hitbox[1] <= a[1] <= self.yPos + hitbox[1] + hitbox[3]) :
            return True
        return False

    def disable(self) :
        '''
        Disable character controls.
        :return: None
        '''
        self.allowStateChange = False

    def enable(self) :
        '''
        Enable character controls.
        :return: None
        '''
        self.allowStateChange = True

    def getXCenter(self) :
        return self.getXPos() + self.getWidth()/2.0

    def getYCenter(self) :
        return self.getYPos() + self.getHeight()/2.0

    def getDamage(self) :
        return self.damage
Exemplo n.º 6
0
 def __init__(self):
     self.pos = Vector.null()
     self.dir = Vector.unit('E')
     self.waypoint = Vector(10, 1)