few people have helped me with this problem before but the rotation is still messed up my Cannons wont rotate towards the player good they are all out of place I really need
First of all the cannon image is far too tall. Clip the border and use a smaller image. For instance:
The center of the image is the rotation point of the canon. You can scale the image to any size you want. For instance 100x100:
self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))
See also How to rotate an image(player) to the mouse direction?.
Anyway you have to blit the cannon after rotating the image and computing the rectangle.
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
# [...]
self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))
self.image = self.shootsright
self.rect = self.image.get_rect(center = (self.x, self.y))
self.look_at_pos = (self.x, self.y)
self.hitbox = (*self.rect.topleft, *self.rect.size)
def draw(self):
dx = self.look_at_pos[0] - self.x
dy = self.look_at_pos[1] - self.y
angle = (180/math.pi) * math.atan2(dx, dy)
self.image = pygame.transform.rotate(self.shootsright, angle)
self.rect = self.image.get_rect(center = (self.x, self.y))
window.blit(self.image, self.rect)
self.hitbox = (*self.rect.topleft, *self.rect.size)
def lookAt( self, coordinate ):
self.look_at_pos = coordinate
Minimal example: repl.it/@Rabbid76/PyGame-RotateWithMouse