I am still having problems with rotation (see my previous question) so to solve them i took the problem out of the context of my game, to see if i could get it to rotate without the confusion of everything else i was trying to do. The problem I have now is that the image rotates, but it also flies off screen at the same time. I do not have any move commands, it is only supposed to rotate in place. why is this happening?
here is my code:
import pygame
import sys
pygame.init()
size = width , height = 400 , 400
screen = pygame.display.set_mode(size)
car = pygame.image.load("Car3.png").convert()
carRect = car.get_rect()
carRect = carRect.move(200 , 200)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
car = pygame.transform.rotozoom(car ,30 , 1)
screen.blit(car , carRect)
pygame.display.flip()
pygame.quit()
sys.exit()
note: the reason i am using rotozoom is that i read that problems can arise from the image automatically resizing when it is rotated.
What are you planning to achieve by using these two lines here?
carRect = car.get_rect()
carRect = carRect.move(200 , 200)
Why do you call the Rect.move function here?
Your problem is probably, that the blit is done with the top left corner. I don't know the measurements of your image, but could it be that it's "rotating" out of the window because the top left corner shifts during the rotation process?
It's a bit late here, otherwise I'd look up the documentaion myself and give you a more elaborate answer. But till then: When rotating images, I'd go by the center of the image for placing it, and not by its top left corner. Can be done either way, I find it easier using the center.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.