Changing direction of rotation Pygame

Posted by czl on Stack Overflow See other posts from Stack Overflow or by czl
Published on 2013-10-23T03:22:36Z Indexed on 2013/10/23 3:53 UTC
Read the original article Hit count: 131

Filed under:
|
|
|

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?


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.

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about graphics