AKG
AKG

Reputation: 2459

Pygame: Rescale pixel size

With pygame, I created a 20x20 pixel window and added a 2x2 pixel rectangle. When I run the program, the window size is super small and I can barely see the rectangle. How can I increase the window size whilst keeping the number of pixels constant, i.e. increase the pixel size? I am aware of this similar question, but there a somewhat more complicated case is discussed.

import pygame
screen_width, screen_height = 20, 20
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width, screen_height))
run = True
while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    win.fill(black)
    pygame.draw.rect(win, white, (x, y, rect_width, rect_height))
    pygame.display.update()
pygame.quit()

Upvotes: 7

Views: 3330

Answers (1)

sloth
sloth

Reputation: 101042

Don't draw directly to the screen, but to another Surface.

Then scale that new Surface to the size of the screen and blit it onto the real screen surface.

Here's an example:

import pygame
screen_width, screen_height = 20, 20

scaling_factor = 6

x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width*scaling_factor, screen_height*scaling_factor))

screen = pygame.Surface((screen_width, screen_height))

run = True
while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    screen.fill(black)
    pygame.draw.rect(screen, white, (x, y, rect_width, rect_height))

    win.blit(pygame.transform.scale(screen, win.get_rect().size), (0, 0))
    pygame.display.update()
pygame.quit()

Upvotes: 6

Related Questions