简体   繁体   English

改变旋转方向的Pygame

[英]Changing direction of rotation Pygame

How would you change the direction of a rotating image/rect in Pygame? 您如何在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? 有没有办法确保旋转方向的改变?


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. 也许每5秒更改一次旋转图像的旋转角度,或者如果能够在击中X轴或Y轴时改变旋转方向,则可能会改变旋转方向。 I've added some code below. 我在下面添加了一些代码。

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. 似乎只要我指定速度并具有location子句,就可以使用rect.move_ip轻松切换运动方向,它可以完成我想要的操作。 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)

You can check the official documentation 您可以查看官方文档

http://www.pygame.org/docs/ref/transform.html Here is the example code:- 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)

This code will keep on rotating the image after every 2 seconds by 90 degrees in clockwise direction. 该代码将每2秒钟在顺时针方向旋转90度后继续旋转图像。 Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM