2

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

5
  • There seems to be a bug in draw_bg() where it is scaling and blitting a temporary Surface instead of s directly. Commented Nov 7, 2023 at 15:32
  • Is there a way how to fix it? Commented Nov 7, 2023 at 15:36
  • 2
    There is no bug at all. A surface consists of pixels. When a surface is scaled down, some of the pixels are simply thrown away. You can try pygame.transform.smoothscale, but then you will get blurred lines. Commented Nov 7, 2023 at 16:01
  • @Rabbid76 Amazing, that seems to do the trick. Could you elaborate further tho? Why is it happening (that pixels are getting discarded), and what are the benefits of using smoothscale over normal scaling? Thanks in advance! Commented Nov 7, 2023 at 16:07
  • 2
    e.g. If you have a surface with 4x4 (16) pixel and you want scale it down to 2x2 (4) pixel, then you have to discard 12 pixel. Alternatively you can interpolate the colors of the pixels. Commented Nov 7, 2023 at 16:10

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.