Advertisement
Guest User

Resizable Window

a guest
Mar 5th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Set initial window dimensions
  8. initial_width = 800
  9. initial_height = 600
  10.  
  11. # Create the window with RESIZABLE flag
  12. screen = pygame.display.set_mode(
  13.     (initial_width, initial_height),
  14.     pygame.RESIZABLE
  15. )
  16. pygame.display.set_caption("Resizable Window Example")
  17.  
  18. # Main loop
  19. running = True
  20. while running:
  21.     for event in pygame.event.get():
  22.         if event.type == pygame.QUIT:
  23.             running = False
  24.        
  25.         # Handle window resize event
  26.         elif event.type == pygame.VIDEORESIZE:
  27.             # Update screen surface with new dimensions
  28.             screen = pygame.display.set_mode(
  29.                 (event.w, event.h),
  30.                 pygame.RESIZABLE
  31.             )
  32.    
  33.     # Clear screen with white color
  34.     screen.fill((255, 255, 255))
  35.    
  36.     # Draw elements that adapt to window size
  37.     current_width, current_height = screen.get_size()
  38.    
  39.     # Draw a centered rectangle that's 80% of window size
  40.     rect_width = current_width * 0.8
  41.     rect_height = current_height * 0.8
  42.     rect = pygame.Rect(
  43.         (current_width - rect_width) // 2,
  44.         (current_height - rect_height) // 2,
  45.         rect_width,
  46.         rect_height
  47.     )
  48.     pygame.draw.rect(screen, (0, 0, 255), rect, 5)
  49.    
  50.     # Draw text that scales with window size
  51.     font_size = int(min(current_width, current_height) * 0.1)
  52.     font = pygame.font.Font(None, font_size)
  53.     text = font.render("Resize Window!", True, (255, 0, 0))
  54.     text_rect = text.get_rect(center=(current_width//2, current_height//4))
  55.     screen.blit(text, text_rect)
  56.    
  57.     # Update the display
  58.     pygame.display.flip()
  59.  
  60. # Clean up
  61. pygame.quit()
  62. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement