Pygame轮换麻烦

时间:2013-11-23 19:45:45

标签: python

最近开始了一个新的pygame项目,小行星。我一直在努力让图像在光标方向上旋转,我发现这很烦人。感谢任何帮助,这是迄今为止轮换的代码:

import sys, pygame, math, time;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
fire_beam = ('beam.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
f_beam = pygame.image.load(fire_beam).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
x, y = 357, 300 #position of space_ship, (line 38 btw, second from bottom)
while True:
screen.blit(bk, (0, 0))
for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    elif event.type == MOUSEBUTTONDOWN and event.button == 1:
        print("Left Button Pressed")
    elif event.type == MOUSEBUTTONDOWN and event.button == 3:
        print("Right Button Pressed")
    if event.type == MOUSEMOTION:
        clock.tick(60)
        x1, y1 = pygame.mouse.get_pos()
        x2, y2 = x, y
        dx, dy = x2 - x1, y2 - y1
        rads = math.atan2(dx, dy)
        degs = math.degrees(rads)
        pygame.transform.rotate(space_ship, (degs))
        print degs #Prints correct output..
        pygame.display.update() #the image flickers, but does not rotate
pos = pygame.mouse.get_pos()
screen.blit(mousec, (pos))
screen.blit(space_ship, (375, 300))
pygame.display.update()

1 个答案:

答案 0 :(得分:0)

来自documentation:“曲面变换是一种移动或调整像素大小的操作。所有这些函数都会使Surface运行并返回带有结果的新曲面。”

实际上,你操作变换,但扔掉了结果。 你可以改变一行

pygame.transform.rotate(space_ship, (degs))

为:

space_ship = pygame.transform.rotate(space_ship, (degs))

为了看到它正常工作,但这不是一件好事:  你需要重新计算你的学位才能只有差异 从一次迭代到下一次迭代的度数 - 但更糟糕的是,连续的栅格图片 变换会使你的宇宙飞船很快退化成无定形的像素斑点。

正确的做法是保留对原始spacehsip图片的引用,并始终将其旋转。

因此,在主循环之前,您执行以下操作:     space_ship_image = pygame.image.load(spaceship).convert_alpha() 在循环内部,前面提到:

space_ship = pygame.transform.rotate(space_ship_image, (degs))
BTW,光栅伪像就像它们一样,你可能会想要使用更大的光栅 您的“spaceship.png”文件中的船只版本,并使用“rotozoom”代替“旋转”, 旋转船只缩小以获得最终图像。