播放器枪向鼠标Pygame旋转的问题

时间:2020-07-03 22:22:38

标签: python pygame

我的玩家的枪支朝向鼠标的旋转被弄乱了,我不知道如何解决

VIDEp,您可以看到它向左旋转良好,但是当我尝试向右旋转时,它会出现故障,并且枪口朝下

class player:
    def __init__(self,x,y,height,width,color):
         #[......]
        self.shootsright = pygame.image.load("gun.png")


        self.image = self.shootsright
        self.rect  = self.image.get_rect(center = (self.x, self.y))
        self.look_at_pos = (self.x, self.y)

        self.isLookingAtPlayer = False
        self.look_at_pos = (x,y)
      # -----------------------------
    def draw(self):
   #[....]
     
        # rotatiing the gun
        dx = self.look_at_pos[0] - self.rect.centerx
        dy = self.look_at_pos[1] - self.rect.centery 
        angle = (300/math.pi) * math.atan2(dx, dy)
        self.image = pygame.transform.rotate(self.shootsright, angle)
        self.rect  = self.image.get_rect(center = self.rect.center)
 
    def lookAt( self, coordinate ):
        self.look_at_pos = coordinate

我的完整玩家课程

# the block class
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        # for it to jump
        self.isJump = False
        self.JumpCount = 10
        # for making it fall
        self.fall = 0
        # how fast it is moving
        self.speed = 4
        self.rect = pygame.Rect(x,y,height,width)
        # players health
        self.health = 10
        # gun image
           #-------------------------------------------------------
            # Make a Reference Copy of the bitmap for later rotation
        self.shootsright = pygame.image.load("gun.png")


        self.image = self.shootsright
        self.rect  = self.image.get_rect(center = (self.x, self.y))
        self.look_at_pos = (self.x, self.y)

        self.isLookingAtPlayer = False
        self.look_at_pos = (x,y)
        
        # hitboxes
        self.hitbox = (self.x + 20, self.y, 28,60)
        self.gunhitbox = (self.x + 20, self.y, 28,60)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.hitbox)
        # draw the players health
        pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 80, 10)) # NEW
        pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 40, 80 - (5 * (10 - self.health)), 10))
        self.hitbox = (self.x + -8, self.y + 20 , 41,41)
        # the guns hitbox

        # rotatiing the gun
        dx = self.look_at_pos[0] - self.rect.centerx
        dy = self.look_at_pos[1] - self.rect.centery 
        angle = (300/math.pi) * math.atan2(dx, dy)
        self.image = pygame.transform.rotate(self.shootsright, angle)
        self.rect  = self.image.get_rect(center = self.rect.center)

        self.gunhitbox = (self.x + 8, self.y + -20 , 31,57)
        window.blit(self.image,self.gunhitbox)
    def lookAt( self, coordinate ):
        self.look_at_pos = coordinate


enter image description here

我的完整代码可以测试,只需要上面的那把枪

# the block class
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        # for it to jump
        self.isJump = False
        self.JumpCount = 10
        # for making it fall
        self.fall = 0
        # how fast it is moving
        self.speed = 4
        self.rect = pygame.Rect(x,y,height,width)
        # players health
        self.health = 10
        # gun image
           #-------------------------------------------------------
            # Make a Reference Copy of the bitmap for later rotation
        self.shootsright = pygame.image.load("gun.png")


        self.image = self.shootsright
        self.rect  = self.image.get_rect(center = (self.x, self.y))
        self.look_at_pos = (self.x, self.y)

        self.isLookingAtPlayer = False
        self.look_at_pos = (x,y)
        
        # hitboxes
        self.hitbox = (self.x + 20, self.y, 28,60)
        self.gunhitbox = (self.x + 20, self.y, 28,60)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.hitbox)
        # draw the players health
        pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 80, 10)) # NEW
        pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 40, 80 - (5 * (10 - self.health)), 10))
        self.hitbox = (self.x + -8, self.y + 20 , 41,41)
        # the guns hitbox

        # rotatiing the gun
        dx = self.look_at_pos[0] - self.rect.centerx
        dy = self.look_at_pos[1] - self.rect.centery 
        angle = (300/math.pi) * math.atan2(dx, dy)
        self.image = pygame.transform.rotate(self.shootsright, angle)
        self.rect  = self.image.get_rect(center = self.rect.center)

        self.gunhitbox = (self.x + 8, self.y + -20 , 31,57)
        window.blit(self.image,self.gunhitbox)
    def lookAt( self, coordinate ):
        self.look_at_pos = coordinate


1 个答案:

答案 0 :(得分:1)

如果枪支绕其中心旋转,则必须在self.rect而非self.gunhitbox处拉动枪支。

window.blit(self.image,self.gunhitbox)

window.blit(self.image, self.rect)

如果枪支必须使玩家旋转一圈,则请阅读How can you rotate an image around an off center pivot in Pygame

添加功能,使图像围绕偏离中心的轴旋转到应用程序:

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h         = image.get_size()
    sin_a, cos_a = math.sin(math.radians(angle)), math.cos(math.radians(angle)) 
    min_x, min_y = min([0, sin_a*h, cos_a*w, sin_a*h + cos_a*w]), max([0, sin_a*w, -cos_a*h, sin_a*w - cos_a*h])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_x - pivot_move[0], pos[1] - originPos[1] - min_y + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

player.draw中使用旋转并枪支 的功能:

class player:  
    # [...]

    def draw(self):
        # [...]

        # rotatiing the gun
        dx = self.look_at_pos[0] - self.rect.centerx
        dy = self.look_at_pos[1] - self.rect.centery 
        
        angle = (300/math.pi) * math.atan2(-dy, dx)
  
        gun_size = self.image.get_size()
        pivot = (8, gun_size[1]//2)
        blitRotate(window, self.image, self.rect.center, pivot, angle)

相关问题