一尘不染

仍然无法正确地将Cannon旋转至玩家Pygame

python

在此处输入图片说明以前很少有人帮助我解决此问题,但旋转仍然混乱,我的大炮不会向玩家旋转,他们都错位了。我真的很需要此问题的帮助,这是我第一次尝试使物体向玩家旋转。我试图使我的大炮嘴向玩家旋转,但是旋转变得一团糟,我不知道该如何解决。

    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

我完整的敌人射击课程

    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)

无论大炮在哪里,这都是大炮向玩家旋转的地方

            for enemyshoot in enemyshooting:
                if not enemyshoot.isLookingAtPlayer:
                    enemyshoot.lookAt((playerman.x, playerman.y))

阅读 200

收藏
2021-01-20

共1个答案

一尘不染

首先,加农炮的图像太高了。剪裁边框并使用较小的图像。例如:

图像的中心是佳能的旋转点。您可以将图像缩放到所需的任何大小。例如100x100:

self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))

无论如何,在旋转图像并计算矩形后,您必须将大炮 变灰

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

最小示例: [![](https://i.stack.imgur.com/5jD0C.png) repl.it/@Rabbid76/PyGame- RotateWithMouse](https://repl.it/@Rabbid76/PyGame-RotateWithMouse#main.py)

2021-01-20