1

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]}

1 Answers1

0

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174