how would I go about making the rects be evenly placed throughout the grid while still being scalable. I want to cover the whole window with them and increase how many there are as you scale them down, my intention is to make it look like its zooming out.
import pygame
import sys
width, height = 750, 750
window = pygame.display.set_mode((width, height))
scale = 1
scroll_scale = .05
while True:
graph_size = 50 * scale
graph_count = width / graph_size
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
scale += scroll_scale
elif event.button == 5 and scale > 0:
scale -= scroll_scale
window.fill((255,255,255))
pygame.draw.rect(window, (0,255,0),(0, 0, graph_size, graph_size))
pygame.draw.rect(window, (0,255,0),(0 + graph_size, 0, graph_size, graph_size))
pygame.draw.rect(window, (0,255,0),(0 + graph_size * 2, 0, graph_size, graph_size))
pygame.display.update()
I know there must be a way to do this without drawing all of the rects separately but I just cant seem to find it
>Solution :
Use 2 nested loops and the built-in range
function:
import pygame
import sys
width, height = 750, 750
window = pygame.display.set_mode((width, height))
scale = 1
scroll_scale = .05
while True:
graph_size = 50 * scale
graph_count = width / graph_size
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
scale += scroll_scale
elif event.button == 5 and scale > 0:
scale -= scroll_scale
window.fill((255,255,255))
size = round(graph_size)
for x in range(0, width + size, size):
for y in range(0, height + size, size):
c = (0,255,0) if (((x + y) // size) % 2) == 0 else (127, 127, 127)
pygame.draw.rect(window, c, (x, y, size, size))
pygame.display.update()