Still Having Problems With Rotating Cannon's Properly Towards The Player Pygame

前端 未结 1 1608
慢半拍i
慢半拍i 2020-12-21 02:35

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

相关标签:
1条回答
  • 2020-12-21 03:09

    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

    0 讨论(0)
提交回复
热议问题