r'ouster
r'ouster

Reputation: 63

Pygame Surface Scaling Issue

I have a problem in Pygame where, if I scale a Surface with a grid (which is displayed correctly in the first image) to a smaller size, I experience a strange bug where the lines are not drawn properly. As far as I understand, when I draw something on a dummy Surface with certain dimensions, I should be able to draw anything relative to these dimensions and when I scale it, everything inside should scale as well, but in my case, it's not working that way. Here is my code:

import pygame
WIDTH = 640
HEIGHT = 320
TILE_SIZE = 32

pygame.init()

# 16:9 R
screen = pygame.display.set_mode((192, 108), pygame.RESIZABLE)
s = pygame.Surface([WIDTH, HEIGHT])

pygame.display.set_caption("Grid")

def draw_grid():

    s.fill((255, 255, 255))
    for v in range((WIDTH // TILE_SIZE) + 1):
        # vertical
        pygame.draw.line(s, (0, 0, 0), (TILE_SIZE * v, 0), (TILE_SIZE * v, HEIGHT))
        # horizontal
    for h in range((HEIGHT // TILE_SIZE) + 1):
        pygame.draw.line(s, (0, 0, 0), (0, TILE_SIZE * h), (WIDTH, TILE_SIZE * h))

def draw_bg():
    temp_frame = pygame.transform.scale(s, (192, 108)) # bug
    screen.blit(temp_frame , (0, 0))

    # screen.blit(s, (0, 0)) would work properly


while True:
    draw_grid()
    draw_bg()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    pygame.display.flip()

enter image description here

enter image description here

enter image description here

I tried changing WIDTH and HEIGHT variables in draw_grid but that doesn't seem to be issue here

Upvotes: 2

Views: 272

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

A surface consists of pixels. When a surface is scaled down with pygame.transform.scale, some of the pixels are simply thrown away. pygame.transform.smoothscale uses a different algorithm and does not throw away pixels, but interpolates them:

temp_frame = pygame.transform.scale(s, (192, 108))

temp_frame = pygame.transform.smoothscale(s, (192, 108))

Upvotes: 1

Related Questions