2013-11-23 85 views
0

最近开始一个新的pygame项目,小行星。我一直在努力让图像在光标方向上旋转,这让我感到非常辛苦。任何帮助表示赞赏,这里是旋转到目前为止的代码: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() 
+0

那么...你的代码有什么问题? – jazzpi

+0

请修正身份。它在Python中不是可选的。就目前而言,'while True'行后面有一个语法错误 – jsbueno

回答

0

documentation:“A面改造是移动或调整大小的像素操作所有这些功能需要一个表面上操作和返回一个带有结果的新Surface。“

事实上,您可以操作变换,但是会丢弃结果。 你可以只改变线

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

到:

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

为了看它的工作,但它不会是一个很好的: 你需要重新计算你的度从一次迭代到下一次迭代只有 度数的差异 - 但更糟糕的是,连续的光栅图像变换会使您的飞船非常快地退化为像素的无定形斑点。

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

所以,你的主循环之前,你这样做: space_ship_image = pygame.image.load(飞船).convert_alpha() 和环路内,前述:

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

BTW,光栅文物是他们是什么,你可能会想要在你的“spaceship.png”文件中使用更大的 版本的船只,并使用“rotozoom”而不是“rotate”, 在您旋转船舶时缩小以获得最终图像。