Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,483 Programmers for FREE! Ask your question and get quick answers from experts. There are 957 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Image rotation/pygame.transform.rotate

 
Reply to this topicStart new topic

Image rotation/pygame.transform.rotate, Pygame help?

Lowrie
post 1 May, 2008 - 02:26 AM
Post #1


New D.I.C Head

*
Joined: 14 Jul, 2005
Posts: 10

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;
cool.gif 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;
CODE
class Turret:
    def __init__(self, x, surface, angle):
        self.x = x
        self.y = 100
        self.image = pygame.image.load("Sprites/turret.bmp")#Load player turret spriye
        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 wink2.gif 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
User is offlineProfile CardPM

Go to the top of the page


Lowrie
post 1 May, 2008 - 11:59 PM
Post #2


New D.I.C Head

*
Joined: 14 Jul, 2005
Posts: 10

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:
CODE
class Turret:
    def __init__(self, x):
        self.x = x
        self.y = 550
        self.image = pygame.image.load("Sprites/turret.bmp")#Load player turret spriye
        self.imageblack = pygame.image.load("Sprites/turret.bmp")
        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
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 2 May, 2008 - 11:50 AM
Post #3


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,429



Thanked 10 times

Dream Kudos: 325
My Contributions


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

This post has been edited by Tom9729: 2 May, 2008 - 11:50 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/11/08 06:11AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month