my player gun rotation towards the mouse is messed up and I dont know how to fix it
VIDEp as you can see there its rotating fine towards the left but when I try to rotate towards right it glitches and guns mouth faces down
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
my full player class
# 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
my full code you could test it you just need that gun above
# 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