I’m making a game in pygame and for one of the levels in my game, the window would shrink constantly, forcing the player to move fast before they couldn’t see the screen anymore but I don’t know how to do that exactly.
Here’s basically what I tried to make the window shrink:
import pygame
import sys
size_change = 0.8
X = 1600
Y = 900
background_color = (255, 255, 255)
running = True
clock = pygame.time.Clock()
while running:
DISPLAY = pygame.display.set_mode([X * size_change, Y * size_change])
DISPLAY.fill(background_color)
size_change -= 0.0005
pygame.display.flip()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
The DISPLAY would keep updating the window to change the size of the X and Y and it does work. However, the problem is that the window gets a weird flickering effect while it’s shrinking and it sometimes breaks the window size entirely. Is there a different way?
DiamondCat64 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
As per my comment, I’d suggest not resizing the entire display. I don’t think it’s able to buffer properly. I’d also see about a termination condition.
#! /usr/bin/env python
import pygame
import sys
size_change = 0.8
X = 1600
Y = 900
background_color = (255, 255, 255)
running = True
clock = pygame.time.Clock()
surface = pygame.Surface((X, Y)) # Create a surface with the initial size
DISPLAY = pygame.display.set_mode((X, Y)) # Set the display size
while running:
# Resize the surface
new_width = int(X * size_change)
new_height = int(Y * size_change)
if (new_width < 1) or (new_height < 1):
print('I just lost the game')
running = False
pygame.quit()
sys.exit()
resized_surface = pygame.transform.scale(surface, (new_width, new_height))
DISPLAY.fill(background_color)
surface_x = (X - new_width) // 2
surface_y = (Y - new_height) // 2
#DISPLAY.blit(resized_surface, (0, 0)) # Blit the resized surface onto the display
DISPLAY.blit(resized_surface, (surface_x, surface_y)) # Blit the resized surface onto the centered position on the display
size_change -= 0.0005 # srsly this looks wild
pygame.display.flip()
clock.tick(60) # fps
for event in pygame.event.get(): # graceful termination
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
__author__ : str = "AI Assistant"
__maintainer__: str = "@lmaddox"
To resize the display you can just call the pygame.display.set_mode() to resize it to a smaller size although this may be buggy, another way to do so is to blit the graphics onto a second surface then bliting it onto the window,
This method will be less buggy.
window2 = pygame.Surface((windowWidth, WindowHeight))
...
window2.blit(graphic)
...
window.blit(window2, (0, 0))
...
window2 = pygame.transform.scale(window2, required size)