Problem:
I'm trying to draw a rotating image in Pygame but I only want the top half of the image to be displayed. I have managed to rotate the image so that it is centered correctly but I am unable to crop it to only display the top half.
I think that this is due to the rect being rotated and when I use surface.subsurface()
to crop it to a certain rect, the cropped part has a "bouncing" effect as the rect is getting larger and smaller depending on the angle of rotation
Code:
def updateTime(screen, hudtimecircle, time):
center = (200, 200) # The center of where the rotated image will be drawn
amount = time # just a number 1-360
newcircle = pygame.transform.rotate(hudtimecircle, amount) # The rotated version of the un-rotated "hudtimecircle" image
newrect = newcircle.get_rect() # The rect of the rotated image
newrect.center = center # Setting the middle of the rotated rect to the point I want to be the center
crop = (0, 0, 120, 60) # An area that I want to be left when I "Crop", This is the part that needs fixing
cropped = newcircle.subsurface(crop) # The cropped part of the rotated image, I want/need this to only give the top half of the image.
screen.blit(cropped, newrect.topleft) # Drawing the rotated image with it's middle point where the variable "center" says.
I have tried cropping it relative to the rotated rect like so:
crop = (newrect.topleft[0], newrect.topleft[1], newrect.midright[0], newrect.midright[1])
However this doesn't work and returns ValueError: subsurface rectangle outside surface area
as each or some of the points are outside the area of the image.
Other Info:
The image hudtimecircle
is 120px*120px and is an image of a circle, with a transparent background, I only want to draw the top 60px of the circle, after it has been rotated.
amount
and time
are just numbers from 1 to 360, I have this line as I plan to do stuff with amount later.
I can't just blit another image over the top of the bottom half of the image to "crop" it as there are many different things underneath where this image needs to be placed.
I have looked at:
https://www.reddit.com/r/learnpython/comments/3xeu4c/image_cropping_with_pygame/
http://blog.tankorsmash.com/?p=128
pygame rotation around center point
And a few other questions that weren't too relevant.
Just to clarify, my issue isn't to rotate the image, or keep the rotated image centered. The issue is that I need to only display the top half of the rotated, centered image.
If there is anymore information that is needed, I will happily provide it.