Pygame .rotate 使表面变大而不是旋转

Pygame .rotate is making a surface grow rather than rotate

我正在尝试让程序生成图表,并且我正在尝试让正方形旋转只是为了稍后的事情,但是正方形正在增长而不是旋转,即使我正在使用 pygame.transform.rotate ().

import pygame, json
from pygame.locals import *

with open("data.json") as f:
    data = json.load(f)


running = True
pygame.init()
clock = pygame.time.Clock()
angle = 0
screen = pygame.display.set_mode(size=(800,600))
while running:
    screen.fill((0,0,0))
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    for i in range(0,len(data["x"])):
        surf = pygame.Surface((50,50))
        surf.fill((255,0,0))
        surfrect = surf.get_rect()
        surfrect.x = (data["x"][i])
        surfrect.y = (data["y"][i])
        surf = pygame.transform.rotate(surf,angle)
        screen.blit(surf,(surfrect))
    pygame.display.flip()
    angle += 1
    print(angle)
    if angle == 90:
        angle = 0 

pygame.quit()

以及任何想尝试的人的数据:

{"x":[100,200,300], "y":[100,200,300]}

您必须使用每像素 alpha 格式创建一个 Surface(参见 ):

surf = pygame.Surface((50,50))

surf = pygame.Surface((50,50), pygame.SRCALPHA)

另见 How do I rotate an image around its center using PyGame?

import pygame, json
from pygame.locals import *

with open("data.json") as f:
    data = json.load(f)

running = True
pygame.init()
clock = pygame.time.Clock()
angle = 0
screen = pygame.display.set_mode(size=(800,600))

original_surf = pygame.Surface((50,50), pygame.SRCALPHA)
original_surf.fill((255,0,0))

while running:
    screen.fill((0,0,0))
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    for i in range(0,len(data["x"])):
        surf = pygame.transform.rotate(original_surf, angle)
        surfrect = surf.get_rect()
        surfrect.centerx = (data["x"][i])
        surfrect.centery = (data["y"][i])
        screen.blit(surf,(surfrect))
    pygame.display.flip()
    angle += 1
    print(angle)
    if angle == 90:
        angle = 0 

pygame.quit()