Python 在Pygame中旋转图像而不失真
我一直试图在pygame中使用Python3.6使图像旋转,但是,当我这样做时,图像要么扭曲为无法识别的图像,要么旋转时会到处颠簸 只需使用Python 在Pygame中旋转图像而不失真,python,image,rotation,pygame,Python,Image,Rotation,Pygame,我一直试图在pygame中使用Python3.6使图像旋转,但是,当我这样做时,图像要么扭曲为无法识别的图像,要么旋转时会到处颠簸 只需使用pygame.transform.rotate(图像、角度)即可将扭曲的图像弄得一团糟。 使用类似于: pygame.draw.rect(gameDisplay,self.color,[self.x,self.y,self.width,self.height])使图像到处起伏 我在这个网站和其他网站上看到了很多问题,到目前为止,没有一个是完美的。 到目前为止
pygame.transform.rotate(图像、角度)
即可将扭曲的图像弄得一团糟。
使用类似于:
pygame.draw.rect(gameDisplay,self.color,[self.x,self.y,self.width,self.height])
使图像到处起伏
我在这个网站和其他网站上看到了很多问题,到目前为止,没有一个是完美的。
到目前为止,对这里感兴趣的任何人都可以链接到我的代码。
我的图像是64x64。
提前谢谢 根据文档():
有些变换被认为是破坏性的。这意味着每次执行它们时都会丢失像素数据。常见的例子是调整大小和旋转因此,重新变换原始曲面比多次变换图像要好。
每次调用transform.rotate
时,都需要对原始图像执行此操作,而不是对先前旋转的图像执行此操作。例如,如果希望图像每帧旋转10度:
image = pygame.image.load("myimage.png").convert()
image_clean = image.copy()
rot = 0
然后在游戏循环中(或对象的更新
):
根据文件():
有些变换被认为是破坏性的。这意味着每次执行它们时都会丢失像素数据。常见的例子是调整大小和旋转因此,重新变换原始曲面比多次变换图像要好。
每次调用transform.rotate
时,都需要对原始图像执行此操作,而不是对先前旋转的图像执行此操作。例如,如果希望图像每帧旋转10度:
image = pygame.image.load("myimage.png").convert()
image_clean = image.copy()
rot = 0
然后在游戏循环中(或对象的更新
):
这里有一个完整的例子。不要修改原始图像,在while循环中使用
pygame.transform.rotate
或rotozoom
获得新的旋转曲面并将其指定给其他名称。使用矩形保持中心
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color('darkslategray')
# Here I just create an image with per-pixel alpha and draw
# some shapes on it so that we can better see the rotation effects.
ORIG_IMAGE = pg.Surface((240, 180), pg.SRCALPHA)
pg.draw.rect(ORIG_IMAGE, pg.Color('aquamarine3'), (80, 0, 80, 180))
pg.draw.rect(ORIG_IMAGE, pg.Color('gray16'), (60, 0, 120, 40))
pg.draw.circle(ORIG_IMAGE, pg.Color('gray16'), (120, 180), 50)
def main():
clock = pg.time.Clock()
# The rect where we'll blit the image.
rect = ORIG_IMAGE.get_rect(center=(300, 220))
angle = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
# Increment the angle, then rotate the image.
angle += 2
# image = pg.transform.rotate(ORIG_IMAGE, angle) # rotate often looks ugly.
image = pg.transform.rotozoom(ORIG_IMAGE, angle, 1) # rotozoom is smoother.
# The center of the new rect is the center of the old rect.
rect = image.get_rect(center=rect.center)
screen.fill(BG_COLOR)
screen.blit(image, rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
sys.exit()
这里有一个完整的例子。不要修改原始图像,在while循环中使用
pygame.transform.rotate
或rotozoom
获得新的旋转曲面并将其指定给其他名称。使用矩形保持中心
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color('darkslategray')
# Here I just create an image with per-pixel alpha and draw
# some shapes on it so that we can better see the rotation effects.
ORIG_IMAGE = pg.Surface((240, 180), pg.SRCALPHA)
pg.draw.rect(ORIG_IMAGE, pg.Color('aquamarine3'), (80, 0, 80, 180))
pg.draw.rect(ORIG_IMAGE, pg.Color('gray16'), (60, 0, 120, 40))
pg.draw.circle(ORIG_IMAGE, pg.Color('gray16'), (120, 180), 50)
def main():
clock = pg.time.Clock()
# The rect where we'll blit the image.
rect = ORIG_IMAGE.get_rect(center=(300, 220))
angle = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
# Increment the angle, then rotate the image.
angle += 2
# image = pg.transform.rotate(ORIG_IMAGE, angle) # rotate often looks ugly.
image = pg.transform.rotozoom(ORIG_IMAGE, angle, 1) # rotozoom is smoother.
# The center of the new rect is the center of the old rect.
rect = image.get_rect(center=rect.center)
screen.fill(BG_COLOR)
screen.blit(image, rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
sys.exit()
创建一个。我猜最多大概有10行代码。创建一个。我猜最多大概有10行代码。