Scale Everything on Pygame Display Surface

Scale Everything On Pygame Display Surface

I'm wondering if there's a way I can globally scale everything up in my game without either having to scale each sprite up individually [...]"

No there is no way. You have to scale each coordinate, each size and each surface individually. PyGame is made for images (Surfaces) and shapes in pixel units. Anyway up scaling an image will result in either a fuzzy, blurred or jagged (Minecraft) appearance.

Is there a way I could make a separate surface and just put that on top of the base window surface, and just scale that?

Yes of course.

Create a Surface to draw on (win). Use pygame.transform.scale() or pygame.transform.smoothscale() to scale it to the size of the window and blit it to the actual display Surface (display_win):

display_win = pygame.display.set_mode((WINDOW_WIDTH*2, WINDOW_HEIGHT*2))
win = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT))

while running:
# [...]

if not paused:
win.fill(COL_BG)
all_sprites.update()
all_sprites.draw(win)

# [...]

scaled_win = pygame.transform.smoothscale(win, display_win.get_size())
# or scaled_win = pygame.transform.scale(win, display_win.get_size())
display_win.blit(scaled_win, (0, 0))
pygame.display.flip()

Minimal example: Sample Image repl.it/@Rabbid76/PyGame-UpScaleDisplay

Pygame. How do I resize a surface and keep all objects within proportionate to the new window size?

Don't draw on the screen directly, but on another surface. Then scale that other surface to size of the screen and blit it on the screen.

Here's a simple example:

import pygame
from pygame.locals import *

def main():
pygame.init()
screen = pygame.display.set_mode((200, 200),HWSURFACE|DOUBLEBUF|RESIZABLE)
fake_screen = screen.copy()
pic = pygame.surface.Surface((50, 50))
pic.fill((255, 100, 200))

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.display.quit()
elif event.type == VIDEORESIZE:
screen = pygame.display.set_mode(event.size, HWSURFACE|DOUBLEBUF|RESIZABLE)

fake_screen.fill('black')
fake_screen.blit(pic, (100, 100))
screen.blit(pygame.transform.scale(fake_screen, screen.get_rect().size), (0, 0))
pygame.display.flip()

main()

Pygame - Is it possible to create everything in one surface and scale it down to display surface?

The circle is bigger than the big surface. The pos argument is the center of the circle and the radius is larger than half of both the width and the height. Try to draw something smaller.

Also, manipulating such a huge surface will result in very poor performance.

surface.fill doesn't work in with a pygame.Surface variable in pygame

It is not sufficient to only scale the game Surface when the window size changes You have to scale the game _Surface in every frame.

The scene is drawn in each frame on a 1200 x 700 Surface. You must scale this Surface to the size of the window in each frame. More precisely you have to scale and blit the game surface in every frame in which the scene changes.

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.VIDEORESIZE:
screen_width = screen.get_height() * screen_width / screen_height
screen_height = screen.get_height()
screen = pygame.display.get_surface()

game.fill('black')
pygame.draw.rect(game,'red',(tile_size,tile_size,tile_size,tile_size))

scaled_game = pygame.transform.scale(game,(screen_width,screen_height))
screen.blit(scaled_game, (0,0))

pygame.display.update()
clock.tick(60

Pygame surface resize

The size of text_surf and background doesn't magically change. You need to create a new surface with a new size..

The pygame.RESIZABLE flag has no effect on pygame.Surface (or it has an effect you do not expect).

pygame.Surface((screen_width,screen_height),pygame.RESIZABLE)

pygame.RESIZABLE is only intended for use with pygame.display.set_mode. The valid flags for flag argument of the pygame.Surface constructor are pygame.HWSURFACE and pygame.SRCALPHA.

Create a new Surfaces with the new size when the pygame.VIDEORESIZE event occurs:

while run==True:
# [...]

for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
if event.type == pygame.VIDEORESIZE:
surface = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
screen_width = event.w
screen_height = event.h
text_surf = pygame.Surface((screen_width,screen_height))
background = pygame.Surface((screen_width,screen_height))

# [...]


Related Topics



Leave a reply



Submit