Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do I cover a window with scalable rects in pygame

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading