I am trying to allow resizing for this app, I put the RESIZABLE flag, but when I try to resize, it messes up! Try my code.
It is a grid program, when the window resizes I want the grid to also resize/shrink.
import pygame,math from pygame.locals import * # Define some colors black = ( 0, 0, 0) white = ( 255, 255, 255) green = ( 0, 255, 0) red = ( 255, 0, 0) # This sets the width and height of each grid location width=50 height=20 size=[500,500] # This sets the margin between each cell margin=1 # Initialize pygame pygame.init() # Set the height and width of the screen screen=pygame.display.set_mode(size,RESIZABLE) # Set title of screen pygame.display.set_caption("My Game") #Loop until the user clicks the close button. done=False # Used to manage how fast the screen updates clock=pygame.time.Clock() # -------- Main Program Loop ----------- while done==False: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done=True # Flag that we are done so we exit this loop if event.type == pygame.MOUSEBUTTONDOWN: height+=10 # Set the screen background screen.fill(black) # Draw the grid for row in range(int(math.ceil(size[1]/height))+1): for column in range(int(math.ceil(size[0]/width))+1): color = white pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height]) # Limit to 20 frames per second clock.tick(20) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Be IDLE friendly. If you forget this line, the program will 'hang' # on exit. pygame.quit ()
Please tell me whats wrong, thanks.
You can get the windows size by first getting the display surface. pygame. display. get_surface(), then calling get_width() / get_height() on the surface.
The window does not respond (freeze), because you do not handle the events. You have to handle the events by either pygame. event. pump() or pygame.
The answer for this problem (allow the Pygame window and the surface inside it to resize) is simply to recreate the resizable window with an updated size, when the user changes its dimensions (done on pygame.VIDEORESIZE
events).
>>> import pygame >>> help(pygame.display.set_mode) Help on built-in function set_mode in module pygame.display: set_mode(...) set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface Initialize a window or screen for display >>>
This removes all previous content on the window surface, so below
there's a process to continue with the current window content.
Some example code:
import pygame, sys pygame.init() # Create the window, saving it to a variable. surface = pygame.display.set_mode((350, 250), pygame.RESIZABLE) pygame.display.set_caption("Example resizable window") while True: surface.fill((255,255,255)) # Draw a red rectangle that resizes with the window. pygame.draw.rect(surface, (200,0,0), (surface.get_width()/3, surface.get_height()/3, surface.get_width()/3, surface.get_height()/3)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if event.type == pygame.VIDEORESIZE: # There's some code to add back window content here. surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
How to continue with the current window content:
Here's some steps to add back the previous window content:
del
) to not use extra memory.Some example code for the above steps (replaces pygame.VIDEORESIZE
event if
statement):
if event.type == pygame.VIDEORESIZE: old_surface_saved = surface surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE) # On the next line, if only part of the window # needs to be copied, there's some other options. surface.blit(old_surface_saved, (0,0)) del old_surface_saved
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With