改变旋转方向的Pygame [英] Changing direction of rotation Pygame
问题描述
您如何在Pygame中更改旋转图像/矩形的方向?应用正和负度值都可以,但是似乎只能在整个窗口中旋转一个方向.有没有办法确保旋转方向的改变?
How would you change the direction of a rotating image/rect in Pygame? Applying positive and negative degree values works but it seems to only be able to rotate one direction throughout my window. Is there a way to ensure a change in direction of rotation?
也许每5秒钟改变旋转图像的旋转一次,或者在击中X轴或Y轴时是否能够改变旋转方向.我在下面添加了一些代码.
Perhaps change up rotation of a spinning image every 5 seconds, or if able to change the direction of the spin when hitting a X or Y axis. I've added some code below.
只要我指定速度并具有location子句,似乎可以通过 rect.move_ip
轻松切换运动方向,它可以满足我的要求.不幸的是,旋转不是那样的.在这里,我将添加角度以确保其旋转,但是无论我尝试什么,我都无法抵消旋转.
It seems like switching movement directions is easy with rect.move_ip
as long as I specify a speed and have location clause, it does what I want. Unfortunately rotation is't like that. Here I'l adding angles to make sure it spins, but no matter what I try, I'm unable to negate the rotation.
def rotate_image(self): #rotate image
orig_rect = self.image.get_rect()
rot_image = pygame.transform.rotate(self.image, self.angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
def render(self):
self.screen.fill(self.bg_color)
self.rect.move_ip(0,5) #Y axis movement at 5 px per frame
self.angle += 5 #add 5 anglewhen the rect has not hit one of the window
self.angle %= 360
if self.rect.left < 0 or self.rect.right > self.width:
self.speed[0] = -self.speed[0]
self.angle = -self.angle #tried to invert the angle
self.angle -= 5 #trying to negate the angle rotation
self.angle %= 360
self.screen.blit(self.rotate_image(),self.rect)
pygame.display.flip()
我真的很想知道如何反转图像的旋转.您可以提供自己的示例.
I would really like to know how to invert rotation of a image. You may provide your own examples.
推荐答案
据我所知,只有一种使用
As far as I know, there is only one way of rotating the image using
pygame.transform.rotate(surface, angle)
您可以查看官方文档
http://www.pygame.org/docs/ref/transform.html 这是示例代码:-
screen = pygame.display.set_mode((640,480))
surf = pygame.image.load("/home/image.jpeg").convert()
while True:
newsurf = pygame.transform.rotate(surf, -90)
screen.blit(newsurf, (100,100))
pygame.display.flip()
time.sleep(2)
此代码将在每2秒钟后继续沿顺时针方向旋转图像90度.希望对您有所帮助.
This code will keep on rotating the image after every 2 seconds by 90 degrees in clockwise direction. Hope this helps..
这篇关于改变旋转方向的Pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!