4

enter image description herefew 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 help with this problem its my first time trying to make something rotate towards a player I am trying to make my cannon mouth rotate towards player, but the rotation is messed up and up I don't know how to fix it.

    class enemyshoot:
        def __init__(self,x,y,height,width,color):
        # [...............]
             self.look_at_pos = (x,y)

     def draw(self):
          # [............]
            self.rect = self.shootsright.get_rect(topleft = (self.x, self.y))

            dx = self.look_at_pos[0] - self.rect.centerx
            dy = self.look_at_pos[1] - self.rect.centery 
            angle = (180/math.pi) * math.atan2(-dx, dy)
            (window.blit(self.image, self.rect))
            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 enemyshoot class

    shotsright = pygame.image.load("canss.png")
    class enemyshoot:
        def __init__(self,x,y,height,width,color):
            self.x = x
            self.y =y
            self.height = height
            self.width = width
            self.color = color
            self.health = 10
            self.hitbox = (self.x + -20, self.y + 30, 31, 57)
           #-------------------------------------------------------
            # Make a Reference Copy of the bitmap for later rotation
            self.shootsright = pygame.image.load("canss.png")
            self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()-150,self.shootsright.get_height()-150))            
            self.image    = self.shootsright
            self.rect     = self.image.get_rect()
            self.position = pygame.math.Vector2( (x, y) )
            self.isLookingAtPlayer = False
            self.look_at_pos = (x,y)
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            window.blit(self.image, self.rect)
            self.rect = self.shootsright.get_rect(topleft = (self.x, self.y))

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

            # ------------
            self.hits = (self.x + 20, self.y, 28,60)
            pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 100, 10)) # NEW
            pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 100 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 200, self.y + 200, 51, 65)
        def lookAt( self, coordinate ):
            self.look_at_pos = coordinate


            
    black = (0,0,0)
    enemyshooting = []
    platformGroup = pygame.sprite.Group
    platformList = []
    level = ["                                                                                                                     p               p           p                         p                        p        ",
             "                                       ",
             "                             ",
             "                                      ",
             "                                  ",
             "                           ",
             "                                      ",
             "                                      ",
             "                                    ",
             "                                   ",
             "                    ",]
    for iy, row in enumerate(level):
        for ix, col in enumerate(row):
            if col == "p":
                new_platforms = enemyshoot(ix*10, iy*50, 10,10,(255,255,255))
                enemyshooting.append(new_platforms)

This is where the cannons rotate towards the player, wherever they are

            for enemyshoot in enemyshooting:
                if not enemyshoot.isLookingAtPlayer:
                    enemyshoot.lookAt((playerman.x, playerman.y)) 
Habib Ismail
  • 69
  • 5
  • 16
  • [video](https://gyazo.com/619a7bca7c2a6ef303a967329985f9e2) the rotation makes the image move like I just want it to tilt its mouth towards the player now the image is starting from the bottom – Habib Ismail Jun 24 '20 at 19:38

1 Answers1

6

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • yeah I did that and its still acting weird [video](https://gyazo.com/fbb0f326244fae22d5c391a088a11329) – Habib Ismail Jun 24 '20 at 19:43
  • here I have something you can debugg you just need to download the cannons I provided in the video you can see the cannons moving towards the mouse but there moving is kinda messed up like `is there a way for you to make the cannons mouth rotate toward the mouse without it moving?` [the script](https://pastebin.com/wYUWYsN1) and this is the video [video](https://gyazo.com/8f814e0dc824b6b9422240d8048ab947) and here is the cannon image [here is the cannon image](file:///C:/Users/Habib/Desktop/PYTHONGGAME/canss.png) – Habib Ismail Jun 24 '20 at 20:16
  • oh sorry alraight give me a minut – Habib Ismail Jun 24 '20 at 20:20
  • how do I do that? – Habib Ismail Jun 24 '20 at 20:42
  • 1
    @HabibIsmail I've changed the answer and I've add a canon image. – Rabbid76 Jun 24 '20 at 20:53
  • it works! but how can I change its position to lower it [image](https://gyazo.com/2ab994a49925c5a12bb8f5e6f150cd01) its out of my screens heightTHANK YOU SO MUCH !!! – Habib Ismail Jun 24 '20 at 21:04
  • @HabibIsmail You can do something like `self.y = y + 100` in `__init__`. If the cannon is to small, then you have to change the scale e.g. `self.shootsright = pygame.transform.smoothscale(self.shootsright, (200, 200))` – Rabbid76 Jun 24 '20 at 21:11
  • nVM THANKS I know WWWW – Habib Ismail Jun 24 '20 at 21:12