Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- # Initialize Pygame
- pygame.init()
- # Set initial window dimensions
- initial_width = 800
- initial_height = 600
- # Create the window with RESIZABLE flag
- screen = pygame.display.set_mode(
- (initial_width, initial_height),
- pygame.RESIZABLE
- )
- pygame.display.set_caption("Resizable Window Example")
- # Main loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Handle window resize event
- elif event.type == pygame.VIDEORESIZE:
- # Update screen surface with new dimensions
- screen = pygame.display.set_mode(
- (event.w, event.h),
- pygame.RESIZABLE
- )
- # Clear screen with white color
- screen.fill((255, 255, 255))
- # Draw elements that adapt to window size
- current_width, current_height = screen.get_size()
- # Draw a centered rectangle that's 80% of window size
- rect_width = current_width * 0.8
- rect_height = current_height * 0.8
- rect = pygame.Rect(
- (current_width - rect_width) // 2,
- (current_height - rect_height) // 2,
- rect_width,
- rect_height
- )
- pygame.draw.rect(screen, (0, 0, 255), rect, 5)
- # Draw text that scales with window size
- font_size = int(min(current_width, current_height) * 0.1)
- font = pygame.font.Font(None, font_size)
- text = font.render("Resize Window!", True, (255, 0, 0))
- text_rect = text.get_rect(center=(current_width//2, current_height//4))
- screen.blit(text, text_rect)
- # Update the display
- pygame.display.flip()
- # Clean up
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement