Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i seem to have a conceptual misunderstanding of the surface and rect object in pygame.

I currently observe these objects this way:

Surface

  • Just the loaded image

rect

  • the 'hard' representation of the ingame object (sprite). Used for simplifying object moment and collision detection

sprite

  • rect and surface grouped together

What i want to do is rotate my sprite. The only available method i found for rotation is pygame.transform.rotate.

How do i rotate the rectangle, or even better, the whole sprite?

Below is the image of how i visualize this problem.

enter image description here

share|improve this question

2 Answers

up vote 4 down vote accepted

Conceptually you've got it, just think of the rectangle as a helper for you to deal with position and collision detection of your image. To implement it you could use:

  mySprite.image = pygame.transform.rotate(Surface, angle)

This will give you a rotated Surface (image), then you can use:

mySprite.rect = mySprite.image.get_rect()

To give you your new rectangle; this won't be a rotated rectangle, it will be a orthogonal one that is big enough to fit your image in, i.e. its sides will remain vertical and horizontal. This should serve most purposes, and be satisfactory for most collision detection, if it's not some people shrink the rectangle a bit, doing it pixel perfect is a lot more complexed and cpu hungry and often not worth it.

NB:keep a copy of your original image (with no rotation) and do all your rotations from that one, otherwise there is potential for your rectangle to keep getting bigger and bigger.

share|improve this answer
Excellent advice for always rotating the original image. I was getting some awful results without it. +1 as soon as i get the reputation to vote up. – Alan Nov 29 '12 at 15:20

Just noticed something which made me almost insane during this whole day of experimenting.

The rect of the image is not the rectangle of the sprite!

In case of this code:

class mySprite(pygame.sprite.Sprite):

    def __init__(self, xy):
        # initialize the pygame sprite part
        pygame.sprite.Sprite.__init__(self)
        # set image and rect
        self.image = pygame.image.load(os.path.join('images','gusjenica.jpg'))
        self.original_image = pygame.image.load(os.path.join('images','gusjenica.jpg'))
        self.rect = self.image.get_rect()

the self.rect is a rect for its on, not a reference of to the surface rect. That means that we have s situation like this.

enter image description here

This brings up another important thing i noticed.

The image.get_rect() location is relative to the Sprite rect!

The image explains it better. Gray are the locations of the sprite rect, red is the position of image.get_rect()

enter image description here

Please correct me if I'm wrong, this is just an empirical proof based on a single experience.

share|improve this answer
The get_rect() of the image (surface) only returns its dimensions, it's always located at (0,0), it's not located anywere. You should use sprite class to keep track of the image position. – pmoleri Nov 29 '12 at 19:32
so have a self.position attribute and reset it when you rotate the image: self.rect.center = self.position – ali_goes_oosh Nov 29 '12 at 20:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.