Rotating a Sprite


In Chapter 11, you learned how to move graphics objects around the screen, but livewires lets you rotate them as well. You can rotate any graphics objects, including sprites, through two rotation methods. One method lets you rotate a graphics object by a certain number of degrees, while the other method lets you rotate the graphics object to an exact orientation.

Introducing the Rotate Sprite Program

The Rotate Sprite program is an extension of the Read Key program. So, in addition to moving the ship, the user can rotate it. If the user presses the Right Arrow key, the ship rotates clockwise. If the user presses the Left Arrow key, the ship rotates counterclockwise. If the user presses the 1 key, the ship rotates to 0 degrees. If the user presses the 2 key, the ship rotates to 90 degrees. If the user presses the 3 key, the ship rotates to 180 degrees. If the user presses the 4 key, the ship rotates to 270 degrees. Figure 12.4 shows off the program.

click to expand
Figure 12.4: The ship can rotate clockwise, rotate counterclockwise, or jump to a predetermined orientation.

Rotating a Sprite by a Number of Degrees

By adding the following code to the end of Ship's moved() method, I allow the user to rotate the ship:

         # rotate the ship based on key presses         if self.screen.is_pressed(games.K_RIGHT):             self.rotate_by(1)         if self.screen.is_pressed(games.K_LEFT):             self.rotate_by(-1) 

I first check if the Right Arrow key is pressed. If it is, I invoke the Ship object's rotate_by() method, which rotates the sprite by the number of degrees passed to the method. In this case, I pass 1, so the sprite rotates by 1 degree clockwise. Next, I check if the Left Arrow key is pressed. If it is, I rotate the sprite by -1 degree, rotating the sprite 1 degree counterclockwise. You can rotate a sprite by any number of degrees you like.

Rotating a Sprite to a Specific Orientation

You can also rotate a sprite directly to a certain orientation by invoking the sprite's rotate_to() method. All you have to do is pass a number of degrees, and the sprite will rotate to that orientation. I add the following lines to illustrate the method:

         if self.screen.is_pressed(games.K_1):             self.rotate_to(0)         if self.screen.is_pressed(games.K_2):             self.rotate_to(90)         if self.screen.is_pressed(games.K_3):             self.rotate_to(180)         if self.screen.is_pressed(games.K_4):             self.rotate_to(270) 

So now, when the user presses the 1 key, the sprite rotates to 0 degrees (its starting orientation). When the user presses the 2 key, the sprite rotates to 90 degrees. When the user presses the 3 key, the sprite rotates to 180 degrees. And finally, when the user presses the 4 key, the sprite rotates to 270 degrees.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net