Is there a way to turn a rectangle into a circle in pygame?

Viewed 89

This is my code atm. It outputs a square right on top of a circle. I want to make the square sort of "turn into" the circle.

import pygame

GREEN = (0,255,0)


pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))

quitVar = True

while quitVar == True:
    screen.fill(WHITE)

    pygame.draw.rect(screen, GREEN, (100,100,200,200))
    pygame.draw.circle(screen, GREEN, (200,200),100)


    
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            quitVar = False

    pygame.display.update()

pygame.quit()

1 Answers

You can draw a rectangle with round corners in Pygame (see Setting a pygame surface to have rounded corners). Animate the corner radius from 0 to the radius of the circle:

import pygame

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
clock = pygame.time.Clock()

radius = 0
step = 1

quitVar = True
while quitVar == True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quitVar = False

    screen.fill(WHITE)
    pygame.draw.rect(screen, GREEN, (100, 100, 200, 200), border_radius = radius)
    pygame.display.update()

    radius += step
    if radius <= 0 or radius >= 100:
        step *= -1

pygame.quit()
Related