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: 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
What's the Difference Between Subprocess Popen and Call (How to Use Them)
Find Index of Last Occurrence of a Substring in a String
Shell Script: Execute a Python Program from Within a Shell Script
Print to the Same Line and Not a New Line
How to Know/Change Current Directory in Python Shell
Simple Way to Query Connected Usb Devices Info in Python
Nan Loss When Training Regression Network
Loading JSONl File as JSON Objects
Numpy Array Initialization (Fill with Identical Values)
Pyspark Dataframes - Way to Enumerate Without Converting to Pandas
Most Efficient Way to Reverse a Numpy Array
How to Use the Optional Type Hint
How to Use Numpy.Correlate to Do Autocorrelation
How to Use Asyncio with Existing Blocking Library
Pycharm Error: 'No Module' When Trying to Import Own Module (Python Script)
Using Configparser to Read a File Without Section Name
How to Check for Python Version in a Program That Uses New Language Features