Question

I'm trying to do a relatively simple animation that requires rotating an ellipse on a rotating background. I've had to resort to a bit of trickery to get pygame.transform.rotate to play nice with the surfaces I'm trying to rotate. Namely, I've made this function that recenters the new rect obtained from pygame's rotation function:

def recenter(orig_rect, rotated_surf):
    oldcenter = orig_rect.center

    rotRect = rotated_surf.get_rect()
    rotRect.center = oldcenter

    screen.blit(rotated_surf, rotRect)

The functionning is fairly self-explanatory. I realize that the original (unrotated) surface remains blitted onto the display, but this turns out not to really be a problem. Don't worry about that.

The above function is used by the rotating functions (background & ellipse) as such:

# width, height, screen are globals
def rotate_background(surf, speed, t0):
    new_angle = ((pygame.time.get_ticks() - t0) * (speed / 1000)) % 360

    w, h = surf.get_size()
    blittedRect = screen.blit(surf, (width / 2 - w / 2, height / 2 - h / 2))
    recenter(blittedRect, pygame.transform.rotate(surf, new_angle))

    return surf


def rotate_ellipse(surf, coords, speed, t0):
    new_angle = ((pygame.time.get_ticks() - t0) * (speed / 1000)) % 360
    if new_angle > 90:
        new_angle = 90  # limit rotation

    w, h = surf.get_size()
    x, y = coords
    blittedRect = screen.blit(surf, (width / 2 - w / 2 + x, height / 2 - h / 2 + y))
    recenter(blittedRect, pygame.transform.rotate(surf, new_angle))

These rotation functions are called one for each frame.

Here's a simplified version of my main game loop. Note that this is not the actual production code, but serves as an illustration of how the above elements (which are taken directly from the code base) tie together:

ellipse = create_ellipse()  # returns a surf
background = create_background()  # returns a surf

while True:
    rotate_background()
    rotate_ellipse()
    pygame.display.flip()

A word about the background and ellipse variables above. Both variables contain a pygame.Surface instance on which things have been drawn. I won't go into details about background since that is working fine. In create_ellipse, an ellipse was drawn onto the ellipse surface by means of pygame.draw.ellipse, in yellow. Prior to that, ellipse.fill(GREEN) was called. The colorkey for ellipse was also set to GREEN to make sure that the entire surface is transparent except where the yellow ellipse was drawn.

The Problem: I'm not seeing the ellipse

I commented out ellipse.set_colorkey to make sure that the ellipse was properly blitted. It is. I see a green rectagle appear that changes in dimension as the ellipse is rotated. This led me to infer that there is, indeed, an ellipse being drawn since it can be rotated.

What gives? I can provide the full code if it can be useful. The whole thing is approximately 200 lines, but I hope my explanation above is enough for you guys. I figured we should start locally and work outwards =)

Thanks very much in advance!

Was it helpful?

Solution 2

Okay so the problem stems from my own dire stupidity.

I mistook the rect argument for pygame.draw.ellipse as being center_x_coordinate, center_y_coordinate, width, height.

I changed my calls to pygame.draw.ellipse and everything is now working beautifully.

OTHER TIPS

while True:
    rotate_background()
    rotate_ellipse
    pygame.display.flip()

rotate_elipse() Is not being called, and has no arguments. If it's a typo, use a print to check if elipse is blitting to where you expect. Maybe the math isn't right?

You can simplify things, like width/2 is equivalent to rect.centerx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top