示例#1
0
文件: player.py 项目: BrettMcNeff/TBA
 def __init__(self, hud):
     self.img = pygame.image.load("Images/Ships/Player.png").convert_alpha()
     self.rect = self.img.get_rect()
     self.moving = [False, False, False, False, False]
     self.ang = math.pi / 2
     self.accel = [0.0, 0.0]
     self.velocity = [0.0, 0.0]
     self.momentum = [0.0, 0.0]
     self.pos = [200.0, 200.0]
     self.timer = pygame.time.get_ticks()
     self.elapsed = 0
     
     self.bladeRange = [(0,60),(120,220),(300,360)]
     self.bladeRadius = 24
     
     self.hudRef = hud
     
     #Boundry Extra
     self.boundsExtra = 32
     self.killBounds = 64
     
     #Movement
     self.accelFactor = 0.8
     self.momentumFactor = 1
     self.turnRate = 45
     
     #Thrusting State
     self.thrusting = False
     self.thrustImage = pygame.image.load("Images/Effects/LoFlame.png").convert_alpha()
     self.thrustRect = self.thrustImage.get_rect()
     self.thrustImage2 = pygame.image.load("Images/Effects/HiFlame.png").convert_alpha()
     self.thrustRect2 = self.thrustImage2.get_rect()
     self.thrust1 = 1
     
     self.boosting = False
     self.boostBonus = 3
     
     
     
     #Killing
     self.numLives = 3
     self.dead = False
     self.canMakeDeadEffect = True
     self.currentDeadTime = 0
     self.deadTime = 2
     self.reformEffect = False
     self.invincTime = 3
     self.currentInvincTime = 31
     
     #Motion Trails
     self.motionTrail1_angle = math.radians(-146)
     self.motionTrail1_dist = 28
     self.motionTrail1 = MotionTrailGenerator(  self.motionTrail1_dist * math.cos( -self.ang + self.motionTrail1_angle ),
                                                 self.motionTrail1_dist * math.sin( -self.ang + self.motionTrail1_angle ),
                                                 1024, 768,
                                                 17, 10,
                                                 255, 255, 155, 5 )
     self.motionTrail2_angle = math.radians(146)
     self.motionTrail2_dist = 28
     self.motionTrail2 = MotionTrailGenerator(  self.motionTrail2_dist * math.cos( -self.ang + self.motionTrail1_angle ),
                                                 self.motionTrail2_dist * math.sin( -self.ang + self.motionTrail1_angle ),
                                                 1024, 768,
                                                 17, 10,
                                                 255, 255, 155, 5 )
示例#2
0
文件: player.py 项目: BrettMcNeff/TBA
class Player(object):
    def __init__(self, hud):
        self.img = pygame.image.load("Images/Ships/Player.png").convert_alpha()
        self.rect = self.img.get_rect()
        self.moving = [False, False, False, False, False]
        self.ang = math.pi / 2
        self.accel = [0.0, 0.0]
        self.velocity = [0.0, 0.0]
        self.momentum = [0.0, 0.0]
        self.pos = [200.0, 200.0]
        self.timer = pygame.time.get_ticks()
        self.elapsed = 0
        
        self.bladeRange = [(0,60),(120,220),(300,360)]
        self.bladeRadius = 24
        
        self.hudRef = hud
        
        #Boundry Extra
        self.boundsExtra = 32
        self.killBounds = 64
        
        #Movement
        self.accelFactor = 0.8
        self.momentumFactor = 1
        self.turnRate = 45
        
        #Thrusting State
        self.thrusting = False
        self.thrustImage = pygame.image.load("Images/Effects/LoFlame.png").convert_alpha()
        self.thrustRect = self.thrustImage.get_rect()
        self.thrustImage2 = pygame.image.load("Images/Effects/HiFlame.png").convert_alpha()
        self.thrustRect2 = self.thrustImage2.get_rect()
        self.thrust1 = 1
        
        self.boosting = False
        self.boostBonus = 3
        
        
        
        #Killing
        self.numLives = 3
        self.dead = False
        self.canMakeDeadEffect = True
        self.currentDeadTime = 0
        self.deadTime = 2
        self.reformEffect = False
        self.invincTime = 3
        self.currentInvincTime = 31
        
        #Motion Trails
        self.motionTrail1_angle = math.radians(-146)
        self.motionTrail1_dist = 28
        self.motionTrail1 = MotionTrailGenerator(  self.motionTrail1_dist * math.cos( -self.ang + self.motionTrail1_angle ),
                                                    self.motionTrail1_dist * math.sin( -self.ang + self.motionTrail1_angle ),
                                                    1024, 768,
                                                    17, 10,
                                                    255, 255, 155, 5 )
        self.motionTrail2_angle = math.radians(146)
        self.motionTrail2_dist = 28
        self.motionTrail2 = MotionTrailGenerator(  self.motionTrail2_dist * math.cos( -self.ang + self.motionTrail1_angle ),
                                                    self.motionTrail2_dist * math.sin( -self.ang + self.motionTrail1_angle ),
                                                    1024, 768,
                                                    17, 10,
                                                    255, 255, 155, 5 )
                                                    
    def vec_len(self, vec):
        return math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
        
    def move_forward(self):
        self.accel = [math.cos(-self.ang), math.sin(-self.ang)]
        
    def rotate_ccw(self):
        tempBonus = 1
        if not self.boosting:
            tempBonus = 1.5#1.5
        self.ang += math.pi / self.turnRate * tempBonus
        
    def rotate_cw(self):
        tempBonus = 1
        if not self.boosting:
            tempBonus = 1.5
        self.ang -= math.pi / self.turnRate * tempBonus
        
    def kill(self):
        if not self.dead:
            self.dead = True
            self.currentDeadTime = self.deadTime
            self.canMakeDeadEffect = True
            self.currentInvincTime = self.invincTime
            if ( self.hudRef != None ):
                self.hudRef.updateDeath(True)
            
    def borderHit(self, index, percent=1, deathOk=True):
        if (self.currentInvincTime > 0):
            self.velocity[index] *= -1 * percent
            self.momentum[index] *= -1 * percent
        else:
            if ( deathOk ):
                self.kill()
    
    def getPointDistance(self, x1, y1, x2, y2):
        return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
    def getAngleOfLineBetweenTwoPoints(self, p1, p2): 
        xDiff = p2[0] - p1[0] 
        yDiff = p2[1] - p1[1] 
        return math.degrees(math.atan2(yDiff, xDiff))
        
    def checkColl(self, enemy, cam, gameCon):
        #If in range
        if ( self.getPointDistance( self.rect.centerx - cam.rect.x, self.rect.centery - cam.rect.y, enemy.rect.centerx - cam.rect.x, enemy.rect.centery - cam.rect.y ) <= (self.bladeRadius + enemy.bladeRadius) and not self.dead):
            angleToEnemy = self.getAngleOfLineBetweenTwoPoints((self.rect.centerx - cam.rect.x, self.rect.centery - cam.rect.x), (enemy.rect.centerx - cam.rect.x, enemy.rect.centery - cam.rect.x))
            angleToEnemy = ((angleToEnemy * -1) + 360) % 360
            
            playerAngleOffset = ((math.degrees(self.ang) % 360) + 360 - 90) % 360
            enemyAngleOffset = ((math.degrees(enemy.ang) % 360) + 360 - 90) % 360
            #print str(enemyAngleOffset) + ", " + str(playerAngleOffset) + " | " + str(angleToEnemy)
            #if enemy is in a kill zone
            enemyInKillZone = False
            adjustedAngle = (angleToEnemy - playerAngleOffset + 360) % 360
            for angleRange in self.bladeRange:
                if ( adjustedAngle >= angleRange[0] and adjustedAngle <= angleRange[1] ):
                    enemyInKillZone = True
            
            #if player in enemy kill zone
            playerInKillZone = False
            angleToPlayer = (angleToEnemy + 180) % 360
            adjustedAngle2 = (angleToPlayer - enemyAngleOffset + 360) % 360
            for angleRange in enemy.bladeRange:
                if ( adjustedAngle2 >= angleRange[0] and adjustedAngle2 <= angleRange[1]):
                    playerInKillZone = True
                    
            #Resolve Collision
            if ( enemyInKillZone and playerInKillZone ):
                self.borderHit(0, 1, False)
                self.borderHit(1, 1, False)
                enemy.borderHit(0, 1, False)
                enemy.borderHit(1, 1, False)
                gameCon.shakeScreen(16)
                gameCon.clashSound.play()
            elif ( not enemyInKillZone and not playerInKillZone ):
                self.borderHit(0, 1, False)
                self.borderHit(1, 1, False)
                enemy.borderHit(0, 1, False)
                enemy.borderHit(1, 1, False)
                gameCon.shakeScreen(16)
                gameCon.clashSound.play()
            elif ( enemyInKillZone and not playerInKillZone ):
                enemy.deathFlag = True
                enemy.deathAngle = -angleToEnemy
                gameCon.shakeScreen(32)
            else:
                if self.currentInvincTime <= 0:
                    self.kill()
                else:
                    self.borderHit(0, 0.3)
                    self.borderHit(1, 0.3)
                    enemy.borderHit(0, 0.3)
                    enemy.borderHit(1, 0.3)
                    gameCon.shakeScreen(16)
    
    def update(self, cam):

        self.thrusting = False
        self.boosting = False
        
        self.elapsed = (pygame.time.get_ticks() - self.timer) / 1000.0
        self.accel = [0, 0]
        if not self.dead:
            if self.moving[0] == True or self.moving[4] == True:
                self.thrusting = True
                self.move_forward()
            if self.moving[4]:
                self.boosting = True
            if self.moving[2] == True:
                self.rotate_ccw()
            if self.moving[3] == True:
                self.rotate_cw()
                
            #Bounds
            if (self.currentInvincTime > 0):
                if ( self.rect.centerx >= cam.bounds.right - self.boundsExtra ):
                    self.borderHit(0)
                if ( self.rect.centerx <=  self.boundsExtra ):
                    self.borderHit(0)
                
                if ( self.rect.centery >= cam.bounds.bottom - self.boundsExtra ):
                    self.borderHit(1)
                if ( self.rect.centery <=  self.boundsExtra ):
                    self.borderHit(1)
            else:
                if ( self.rect.centerx >= cam.bounds.right - self.killBounds ):
                    self.borderHit(0)
                if ( self.rect.centerx <=  self.killBounds ):
                    self.borderHit(0)
                
                if ( self.rect.centery >= cam.bounds.bottom - self.killBounds ):
                    self.borderHit(1)
                if ( self.rect.centery <=  self.killBounds ):
                    self.borderHit(1)
            #Apply forces    
            self.momentum[0] += self.accel[0] * 0.15
            self.momentum[1] += self.accel[1] * 0.15
            
            tempBoostBonus = 1
            if ( self.boosting ):
                tempBoostBonus = self.boostBonus
            self.velocity[0] = self.momentum[0] * self.momentumFactor * tempBoostBonus + self.accel[0] * self.accelFactor * tempBoostBonus#0.6, 0.4
            self.velocity[1] = self.momentum[1] * self.momentumFactor * tempBoostBonus + self.accel[1] * self.accelFactor * tempBoostBonus
            
            self.pos[0] += self.velocity[0]
            self.pos[1] += self.velocity[1]
            
            self.rect.center = (self.pos[0], self.pos[1])
            
            self.momentum[0] *= 0.99
            self.momentum[1] *= 0.99
                

            #Motion Trails
            self.motionTrail1.xPos = self.rect.centerx + self.motionTrail1_dist * math.cos( -self.ang + self.motionTrail1_angle )
            self.motionTrail1.yPos = self.rect.centery + self.motionTrail1_dist * math.sin( -self.ang + self.motionTrail1_angle )
            
            self.motionTrail2.xPos = self.rect.centerx + self.motionTrail2_dist * math.cos( -self.ang + self.motionTrail2_angle )
            self.motionTrail2.yPos = self.rect.centery + self.motionTrail2_dist * math.sin( -self.ang + self.motionTrail2_angle )

            self.motionTrail1.update()
            self.motionTrail2.update()
            
            #Invincibile
            if ( self.currentInvincTime > 0 ):
                self.currentInvincTime -= self.elapsed
            
        else:
            self.currentDeadTime -= self.elapsed
            if ( self.currentDeadTime <= 0 ):
                self.dead = False
                self.reformEffect = True
                
        
        
        self.timer = pygame.time.get_ticks()
        
        
    def draw(self, screen, cam):
        if not self.dead:
            #Motion Trails
            if ( self.boosting ):
                self.motionTrail1.draw( screen, cam.rect.x, cam.rect.y )
                self.motionTrail2.draw( screen, cam.rect.x, cam.rect.y )
            
            #Thrust
            if ( self.thrusting ):
                thrust_trans = pygame.transform.rotate(self.thrustImage, self.ang * 180 / math.pi - 90)
                if self.thrust1 <= 0:
                    thrust_trans = pygame.transform.rotate(self.thrustImage2, self.ang * 180 / math.pi - 90)
                self.thrust1 *= -1
                thrust_rect_trans = thrust_trans.get_rect()
                thrust_rect_trans.center = (self.pos[0] - cam.rect.x, self.pos[1] - cam.rect.y)
                screen.blit( thrust_trans, thrust_rect_trans )
        
            img_trans = pygame.transform.rotate(self.img, self.ang * 180 / math.pi - 90)
            rect_trans = img_trans.get_rect()
            rect_trans.center = (self.pos[0] - cam.rect.x, self.pos[1] - cam.rect.y)
            screen.blit( img_trans, rect_trans )