图像以错误的方式旋转

Image rotating the wrong way

在我的游戏中,我正在创建炮塔(一种在地面上使用的机枪)。问题是我正在使用操纵杆移动角色,当操纵杆向下时,y 速度为正(因此它可以向下移动),如果向上移动则相反。然后 if 检查您当前的角度并查看您指向的方向,如果其中一个 if 语句发生,那么它将允许您移动。主要问题是,当我向上移动操纵杆时,枪口指向下方。

我已经尝试制作一个存储方向的变量,但这会导致同样的问题,所以我放弃了这个想法并回到了我之前的想法。还有一个炮塔架,炮塔画在上面

joystick = pygame.joystick.Joystick(0)
    joystick.init()

turret_stand_pic = pygame.image.load("C:/knuckles_pictures/turret_stand.png").convert_alpha()
class Turret_stand():
    def __init__(self, x, y, picture, picture_tag):
        self.xpos = x
        self.ypos = y
        self.picture = picture
        super().__init__()
        self.picture = pygame.transform.scale(self.picture, (200, 200))
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos
        self.tag = picture_tag

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


turret_gun_pic = pygame.image.load("C:/knuckles_pictures/turret_gun.png").convert_alpha()
class Turret_gun():
    def __init__(self, x, y, picture, picture_tag):
        self.xpos = x
        self.ypos = y
        self.picture = picture
        super().__init__()
        self.picture = pygame.transform.scale(self.picture, (200, 80))
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos
        self.tag = picture_tag
        self.previous_angle = 0
        self.angle = -90
        self.speed_x = 0
        self.speed_y = 0
        self.facing = "left"


    def rotate(self, angle):
        if angle != self.angle:
            self.picture = pygame.transform.rotate(self.picture, angle)
            """self.angle += angle
            self.previous_angle = self.angle"""





    def draw(self):
        if self.angle == 0:
            screen.blit(self.picture, (self.xpos+70, self.ypos-70))

        elif self.angle == -90:
            screen.blit(self.picture, (self.xpos, self.ypos))

turret = Turret_gun(500, 370, turret_gun_pic, "turret")
turret_stand = Turret_stand(500, 400, turret_stand_pic, "turret stand")

while True:
    [...]

if joystick:
                move = 3
                axis_x_two, axis_y_two = (joystick.get_axis(3), joystick.get_axis(2))
                if abs(axis_x_two) > 0.1:
                    turret.speed_x = move * axis_x_two
                    turret.speed_y = move * axis_y_two

                turret.speed_y = round(int(turret.speed_y))
                turret.speed_x = round(int(turret.speed_x))


if turret.angle == -90 and turret.speed_y == -3 and turret.speed_x <= 1 and turret.speed_x >= -1:
            turret.rotate(90)

        if turret.angle == 0 and turret.speed_x == -3 and turret.speed_y <= 1 and turret.speed_y >= -1:
            turret.rotate(-90)

turret.update()
turret.draw()

实际效果是摇杆向上推时机枪指向下方。我的意思是:

[![在此处输入图片描述][1]][1]

在这种情况下,炮塔最终指向下方: [1]: https://i.stack.imgur.com/Aheix.jpg

[![在此处输入图片描述][2]][2]

我应该期待的是,当我向上移动操纵杆时,炮塔指向上方。 [2]: https://i.stack.imgur.com/jFyg5.jpg

有时摇杆向右时枪不显示:

基本上,您需要反转图像的旋转方向。所以,要翻转它,再旋转 180 度。 将 rotate 方法中的行更改为

self.picture = pygame.transform.rotate(self.picture, angle+180)

我不知道您的确切设置,但这可能仅适用于 up/down 情况。如果你把棍子放在左边,枪可能仍然指向右边。如果发生这种情况,请将其更改为

self.picture = pygame.transform.rotate(self.picture, 180-angle)

这不只是翻转而已,旋转的方向是反的。增加角度实际上减少了旋转。

同样,这可能行不通。如果您向上移动操纵杆,它可能指向正确。如果是这样,请尝试将 180 更改为另一个数字,例如 90 或 270。这会使旋转在一个方向或另一个方向偏移 90 度。

self.picture = pygame.transform.rotate(self.picture, 90-angle)

self.picture = pygame.transform.rotate(self.picture, 270-angle)