I'm having a great difficulty to create a single sprite rotation using pygame.
I'm following this code (https://stackoverflow.com/a/47688650/5074998) to obtain a center rotation of a sprite. And here is my current code:
import pygame
FPS: int = 100
W = 600
H = 400
MAGENTA = (255, 0, 255)
BLACK = (0, 0, 0)
pygame.init()
screen = pygame.display.set_mode([W, H])
pygame.display.set_caption("Cars")
clock = pygame.time.Clock()
class sp():
def __init__(self):
self.image = pygame.Surface((122, 70), pygame.SRCALPHA)
pygame.draw.polygon(self.image, pygame.Color('dodgerblue1'),
((1, 0), (120, 35), (1, 70)))
self.orig_image = self.image
self.rect = self.image.get_rect(center=[W / 2, H / 2])
self.angle = 0
def update(self):
self.angle += 2
self.rotate()
screen.blit(self.image, [100, 100])
def rotate(self):
self.image = pygame.transform.rotozoom(self.orig_image, self.angle, 1)
self.rect = self.image.get_rect(center=self.rect.center)
sp1 = sp()
out = pause = False
while not out:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
out = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pause = not pause
if not pause:
screen.fill(BLACK)
sp1.update()
pygame.display.flip()
But I can't understand why I get this:
Could anyone help me?