Pygame .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]}
I'm trying to make a program generate graphs and I'm trying to make the square rotate just for something later on, but the squares are growing rather than rotating even though I'm using 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()
And the data for anyone who wants to try it:
{"x":[100,200,300], "y":[100,200,300]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用每像素 alpha 格式创建一个表面(请参阅如何在 Pygame 中制作具有透明背景的图像?):
surf = pygame.Surface((50,50))
另请参阅如何使用 PyGame 围绕图像中心旋转图像?
You have to create a Surface with a per pixel alpha format (see How can I make an Image with a transparent Backround in Pygame?):
surf = pygame.Surface((50,50))
See also How do I rotate an image around its center using PyGame?