Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像旋转方向错误_Python_Image_Pygame_Rotation - Fatal编程技术网

Python 图像旋转方向错误

Python 图像旋转方向错误,python,image,pygame,rotation,Python,Image,Pygame,Rotation,在我的游戏中,我创建了一个炮塔(你在地面上使用的一种机枪)。问题是,我正在使用操纵杆移动角色,当操纵杆向下时,y速度为正(因此它可以向下移动),如果向上移动,则相反。然后,如果检查您当前的角度,并看到您所指的方向,如果其中一个if语句发生,它将允许您移动。主要问题是,当我向上移动操纵杆时,枪口指向dowanrds 我已经尝试过创建一个存储方向的变量,但这会导致相同的问题,所以我放弃了这个想法,回到了以前的想法。还有一个塔架,塔架被拉到那里 joystick = pygame.joystick.J

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

我已经尝试过创建一个存储方向的变量,但这会导致相同的问题,所以我放弃了这个想法,回到了以前的想法。还有一个塔架,塔架被拉到那里

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()
mogage=pygame.mogage.mogage(0)
init()文件
塔楼看台图片=pygame.image.load(“C:/knuckles\u pictures/tarta\u stand.png”).convert\u alpha()
等级转台支架():
定义初始化(自、x、y、图片、图片标签):
self.xpos=x
self.ypos=y
self.picture=图片
super()。\uuuu init\uuuuu()
self.picture=pygame.transform.scale(self.picture,(200200))
self.rect=self.picture.get_rect()
self.rect.x=self.xpos
self.rect.y=self.ypos
self.tag=图片\标签
def牵引(自):
blit(self.picture,(self.xpos,self.ypos))
炮塔炮塔图片=pygame.image.load(“C:/knuckles\u pictures/tarta\u gun.png”).convert\u alpha()
类炮塔_炮():
定义初始化(自、x、y、图片、图片标签):
self.xpos=x
self.ypos=y
self.picture=图片
super()。\uuuu init\uuuuu()
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=图片\标签
自前角度=0
自转角=-90
自速度_x=0
自速度_y=0
self.faceting=“左”
def旋转(自身、角度):
如果角度!=自转角:
self.picture=pygame.transform.rotate(self.picture,角度)
“”“self.angle+=角度
self.previous_angle=self.angle”“”
def牵引(自):
如果self.angle==0:
屏幕blit(self.picture,(self.xpos+70,self.ypos-70))
elif self.angle==-90:
blit(self.picture,(self.xpos,self.ypos))
炮塔=炮塔炮(500370,炮塔炮图,“炮塔”)
转台架=转台架(500400,转台架图,“转台架”)
尽管如此:
[...]
如果操纵杆:
移动=3
轴x_二,轴y_二=(操纵杆。获取_轴(3),操纵杆。获取_轴(2))
如果abs(轴x轴2)>0.1:
转塔速度x=移动*轴x二
转塔速度y=移动*轴y二
炮塔速度y=圆形(int(炮塔速度y))
炮塔速度x=圆形(整数(炮塔速度x))
如果转塔角度==-90,转塔速度y==-3,转塔速度x=-1:
转塔。旋转(90)
如果转塔角度==0,转塔速度x==3,转塔速度y=-1:
转塔。旋转(-90)
塔楼更新()
塔楼图
实际结果是,当你向上推动操纵杆时,机枪指向下方。我的意思是:

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

在这种情况下,炮塔最终指向下方: [1] :

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

我所期望的是,当我向上移动操纵杆时,炮塔指向上方。 [2] :


有时,当操纵杆指向右侧时,枪不显示:

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

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

我不知道您的具体设置,但这可能只适用于上升/下降情况。如果你把棍子放在左边,枪可能仍然指向右边。如果发生这种情况,请将其更改为

self.picture=pygame.transform.rotate(self.picture,180角)

这不仅仅是翻转它,旋转的方向是相反的。增加角度实际上会减少旋转

同样,这可能不起作用。如果你把棍子向上移动,它可能指向右边。如果是这样,试着把180改成另一个数字,比如90或270。这将使旋转在一个方向或另一个方向上偏移90度

self.picture=pygame.transform.rotate(self.picture,90度)


self.picture=pygame.transform.rotate(self.picture,270角)
基本上,您需要反转图像的旋转方向。因此,要翻转它,请将其再旋转180度。 将
rotate
方法中的行更改为

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

我不知道您的具体设置,但这可能只适用于上升/下降情况。如果你把棍子放在左边,枪可能仍然指向右边。如果发生这种情况,请将其更改为

self.picture=pygame.transform.rotate(self.picture,180角)

这不仅仅是翻转它,旋转的方向是相反的。增加角度实际上会减少旋转

同样,这可能不起作用。如果你把棍子向上移动,它可能指向右边。如果是这样,试着把180改成另一个数字,比如90或270。这将使旋转在一个方向或另一个方向上偏移90度

self.picture=pygame.transform.rotate(self.picture,90度)


self.picture=pygame.transform.rotate(self.picture,270角)

它可以使if面朝上,但现在当操纵杆指向右侧时,枪消失了。我已经尝试过使用所有3种方法,但所有方法都会产生与我刚才检查的相同的效果,并且当操纵杆处于正确位置时