1
\$\begingroup\$

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(-dy, dx)
        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)) 
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I don't think this will fix the issue, but on the for loop you should add enemyshoot.isLookingAtPlayer = True to make sure you do not rotate it when it is not needed.

It would look like this

        for enemyshoot in enemyshooting:
            if not enemyshoot.isLookingAtPlayer:
                enemyshoot.lookAt((playerman.x, playerman.y))
                enemyshoot.isLookingAtPlayer = True
\$\endgroup\$
0
\$\begingroup\$

Try using vectors and vector math instead of your own equations:

delta = Vec2d(self.look_at_pos) - self.rect.center
angle = delta.get_angle()

Also make sure cannon image is looking right (literally).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .