Image rotation/pygame.transform.rotate

Page 1 of 1

2 Replies - 14814 Views - Last Post: 02 May 2008 - 11:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50694&amp;s=4fef8db77efb011e5ad139803aa2eb1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Lowrie

Reputation: 2
• Posts: 15
• Joined: 14-July 05

Image rotation/pygame.transform.rotate

Posted 01 May 2008 - 02:26 AM

I am building a tank game for an assignment at school. I have got projectile motion happening perfectly and power and launch angle can be adjusted. The game is a simple tank game which uses projectile motion to fire a projectile at the opposing tank on the other side of the map. At the present i have not yet got backgrounds working.

The main problem i have come across at the moment is getting the turret of the tank to rotate to the launch angle. The way i see it there are two ways to do this:
a) use pygame to rotate the image depending upon the angle or;
draw 180 sprites for the angle rotation.

I have found a method that i believe should work to rotate the image, however i cant seem to get it to work in my pygame. The method is pygame.transform.rotate. Currently when i add the pygame.transform.rotate method the turret does not draw on the screen. Very frustrating...

My Turret class is as shown;
```class Turret:
def __init__(self, x, surface, angle):
self.x = x
self.y = 100
image = self.image
image.set_colorkey(image.get_at((0,0)))		 #Removes white background from sprite
self.rotate = pygame.transform.rotate
self.image = self.rotate(surface, angle)

def draw (self):
screen.blit(self.image, (self.x, self.y))

def turretlocationx(self):
return self.x+60

def turretlocationy(self):
return self.y

```

If anyone knows how to work the pygame.transform.rotate method help would be much appreciated. I have seen the Line by Line Chimp example on the pygame website. This tutorial didnt help me the slightest Any help will be much appreciated. If you know of a program that can draw 180 different angle sprites without me having to draw them manually that would also be appreciated.

Thanks for the potential help

Lowrie

Is This A Good Question/Topic? 0

#5 Lowrie

Reputation: 2
• Posts: 15
• Joined: 14-July 05

Re: Image rotation/pygame.transform.rotate

Posted 01 May 2008 - 11:59 PM

Due to the lack of responses i guess my little problem was far too difficult for the vast knowledge of the Dream in code forums. I worked it out myself today and thought I'd share to increase your databases, and maybe teach the people here a thing or two. The reason it didnt work was because i believe python it likes to have it's own function for the rotation. So by adding a turning function into the class it works perfectly.
Here is the new class:
```class Turret:
def __init__(self, x):
self.x = x
self.y = 550
image = self.image
self.baseImage = self.image
self.dir = 0
image.set_colorkey(image.get_at((0,0)))		 #Removes white background from sprite

def turn(self, amount):
self.dir += amount
self.image = pygame.transform.rotate(self.baseImage, self.dir)
self.rect = self.image.get_rect()

def draw (self):
screen.blit(self.image, (self.x, self.y))

def drawblack (self):
screen.blit(self.imageblack, (self.x, self.y))

def turretlocationx(self):
return self.x+60

def turretlocationy(self):
return self.y

```

Feel free to use my turret class for anything you may potentially want, unless it involves nuclear weapons. Then you must email me to ask permission, i will probably grant it anyway.

Cheers Lowrie

#6 Tom9729

• Segmentation fault

Reputation: 181
• Posts: 2,642
• Joined: 30-December 07

Re: Image rotation/pygame.transform.rotate

Posted 02 May 2008 - 11:50 AM

Sometimes you need to wait longer than a day for a reply.

This post has been edited by Tom9729: 02 May 2008 - 11:50 AM