Python 玩家的枪朝着鼠标旋转时出现问题

Python 玩家的枪朝着鼠标旋转时出现问题,python,pygame,Python,Pygame,我的游戏者枪朝鼠标的旋转是乱七八糟的,我不知道如何修复它 正如你们看到的,它向左旋转很好,但当我试图向右旋转时,它会出现小故障,枪口朝下 class player: def __init__(self,x,y,height,width,color): #[......] self.shootsright = pygame.image.load("gun.png") self.image = self.shootsr

我的游戏者枪朝鼠标的旋转是乱七八糟的,我不知道如何修复它

正如你们看到的,它向左旋转很好,但当我试图向右旋转时,它会出现小故障,枪口朝下

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


我的全部代码你可以测试你只需要上面的枪

# 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



如果枪围绕其中心旋转,则必须在
self.rect
而不是
self.gunhitbox
处绘制枪

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

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

如果枪必须绕着玩家旋转一周,请阅读

添加将图像绕偏心轴旋转到应用程序的功能:

def blitRotate(表面、图像、位置、原始位置、角度):
#计算旋转图像的轴对齐边界框
w、 h=图像。获取大小()
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])
#计算轴的平移
pivot=pygame.math.Vector2(originPos[0],-originPos[1])
枢轴旋转=枢轴旋转(角度)
枢轴移动=枢轴旋转-枢轴
#计算旋转图像的左上角原点
原点=(位置[0]-原点位置[0]+最小x轴移动[0],位置[1]-原点位置[1]-最小y轴移动[1])
#得到一个旋转的图像
旋转的_image=pygame.transform.rotate(图像,角度)
#旋转并闪烁图像
冲浪板(旋转的_图像,原点)
播放器中使用旋转和弹射枪的功能。绘制

职业玩家:
# [...]
def牵引(自):
# [...]
#旋转枪
dx=self.look_位置[0]-self.rect.centerx
dy=自我。查看位置[1]-自我纠正中心
角度=(300/math.pi)*math.atan2(-dy,dx)
gun\u size=self.image.get\u size()
枢轴=(8,枪尺寸[1]//2)
blitRotate(窗口、self.image、self.rect.center、枢轴、角度)

非常感谢您一直以来对我的帮助!!!!