Python Pygame-旋转图像赢得';不要停止伸展

Python Pygame-旋转图像赢得';不要停止伸展,python,pygame,Python,Pygame,Hi all当我运行以下代码时,图像“Car1.jpg”会快速放大,离开屏幕,然后因错误而崩溃: 回溯(最近一次呼叫最后一次): 文件“C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py”,第72行,在 Car1.更新(deltaTime) 文件“C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py”,第45行,在更新中 self.image=pygame.transform.rotate(se

Hi all当我运行以下代码时,图像“Car1.jpg”会快速放大,离开屏幕,然后因错误而崩溃:

回溯(最近一次呼叫最后一次): 文件“C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py”,第72行,在 Car1.更新(deltaTime) 文件“C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py”,第45行,在更新中 self.image=pygame.transform.rotate(self.image,self.direction) pygame.error:宽度或高度太大

#运行游戏所需的导入。
输入数学
导入操作系统
导入pygame
从pygame.locals导入*
随机输入
导入系统
导入时间
#这将设置显示、帧速率和标题。
pygame.init()
screen=pygame.display.set_模式((1280720))
clock=pygame.time.clock()
pygame.display.set_标题(“Indy 500重拍”)
类车(pygame.sprite.sprite):
#使用游戏中的10辆车的默认统计信息初始化它们。
def _初始(自身、图像、位置、最大速度、加速度、操纵):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=os.path.join(“图形”,图像)
self.image=pygame.image.load(self.image)
self.position=位置
自速度=自方向=0
self.MaxSpeed=MaxSpeed
自加速=加速
自我处理
self.k_left=self.k_right=self.k_down=self.k_up=0
def更新(自我,deltaTime):
自我定向=0
self.speed+=(self.k_向上+self.k_向下)
如果self.speed>self.MaxSpeed:
self.speed=self.MaxSpeed
如果self.speed<-self.MaxSpeed:
self.speed=self.MaxSpeed
self.direction+=(self.k_right+self.k_left)
打印(自我指导)
dx,dy=自身位置
弧度=自身方向*(math.pi/180)
dx+=-self.speed*(数学正弦(弧度))
dy+=-self.speed*(数学cos(弧度))
打印(dx)
打印(dy)
self.position=((dx,dy))
self.image=pygame.transform.rotate(self.image,self.direction)
self.rect=self.image.get_rect()
screen.blit(self.image,self.rect)
self.rect.center=self.position
rect=screen.get_rect()
Car1=Car(“Car1.jpg”,rect.center,2,10,10)
#这将管理动画以确保屏幕将刷新。
尽管如此:
#本节将获取帧速率并将其打印到shell中。
pygame.event.get()
deltaTime=时钟。滴答声(60)
对于pygame.event.get()中的事件:
如果不是hasattr(事件,“键”):继续
down=event.type==KEYDOWN
如果event.key==K_RIGHT:
Car1.k_right=向下*-3
elif event.key==K_LEFT:
Car1.k_左=向下*3
elif event.key==K_UP:
Car1.k_向上=向下*2
elif event.key==K_DOWN:
Car1.k_向上=向下*-2
elif event.key==K_转义:
系统出口(0)
屏幕填充((0,0170))
Car1.更新(deltaTime)
pygame.display.flip()
有没有办法解决这个问题?打印dx、dy和self.direction时,只有self.direction更改为3或-3。是否每次旋转都会将图像放大过大?如何解决这个问题

谢谢,
亚当

你不应该不断地旋转。这有点像复印件。您应该保留图像的一个未转换的母版副本,然后从同一母版创建所有不同的旋转

无论何时调用图像上的旋转,返回的都是更大的图像。这是必要的,因为所有图像都是矩形的-如果将方形图像旋转45度,避免切掉任何角的唯一方法是使生成的图像比原始图像宽40%,高40%。您还会发现,旋转后的副本会失去一些质量,因为除非旋转是90度的倍数,否则方形像素无法完美对齐。因此,如果你继续旋转上一次旋转的结果,你会得到一个越来越大的图像(边缘有越来越多的空白填充)和一个越来越扭曲的图像,直到最终它太大而无法处理,你的程序崩溃或者你的计算机慢到爬行

我建议这样做:

def __init__(self, image_filename, ...):
    ...
    self.original_image = pygame.image.load(image_filename)
    ...

def render(self, screen):
    ...
    rotated_image = pygame.transform.rotate(self.original_image, self.angle)
    screen.blit(rotated_image, rotated_image.get_rect(center=(self.x, self.y)))
    ...

def turn(self, clockwise_angle):
    '''
    Rotate clockwise by the given angle in degrees.
    (Or indeed counter-clockwise if the angle is negative.)
    '''
    # Note: self.angle is positive for counter-clockwise
    # rotation, to match pygame.transform.rotate.
    self.angle -= clockwise_angle

你不应该一直旋转。这有点像复印件。您应该保留图像的一个未转换的母版副本,然后从同一母版创建所有不同的旋转

无论何时调用图像上的旋转,返回的都是更大的图像。这是必要的,因为所有图像都是矩形的-如果将方形图像旋转45度,避免切掉任何角的唯一方法是使生成的图像比原始图像宽40%,高40%。您还会发现,旋转后的副本会失去一些质量,因为除非旋转是90度的倍数,否则方形像素无法完美对齐。因此,如果你继续旋转上一次旋转的结果,你会得到一个越来越大的图像(边缘有越来越多的空白填充)和一个越来越扭曲的图像,直到最终它太大而无法处理,你的程序崩溃或者你的计算机慢到爬行

我建议这样做:

def __init__(self, image_filename, ...):
    ...
    self.original_image = pygame.image.load(image_filename)
    ...

def render(self, screen):
    ...
    rotated_image = pygame.transform.rotate(self.original_image, self.angle)
    screen.blit(rotated_image, rotated_image.get_rect(center=(self.x, self.y)))
    ...

def turn(self, clockwise_angle):
    '''
    Rotate clockwise by the given angle in degrees.
    (Or indeed counter-clockwise if the angle is negative.)
    '''
    # Note: self.angle is positive for counter-clockwise
    # rotation, to match pygame.transform.rotate.
    self.angle -= clockwise_angle

谢谢你的回答!那么,您建议如何模拟汽车转弯?它会是这样的:image=“car1.jpg”rotation=0。假设第一次让它旋转10度,然后第二次旋转20度。它将旋转原来的10,回到0,然后到20?我已经编辑了一个建议,你可以采取的一般方法。非常感谢!我明天就要试一试了!非常新的Pygame和OOP。只用于制作程序程序和基于文本的冒险。谢谢你的回答!那么,您建议如何模拟汽车转弯?它是否会像这样:image=“car1.jpg