Pygame .rotate 使表面生长而不是旋转

发布于 2025-01-17 14:02:11 字数 1113 浏览 0 评论 0原文

我试图让程序生成图形,并且我试图让正方形旋转只是为了以后的某些事情,但即使我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2025-01-24 14:02:11

您必须使用每像素 alpha 格式创建一个表面(请参阅如何在 Pygame 中制作具有透明背景的图像?):

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

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

另请参阅如何使用 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()

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))

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

See also 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文