How to make resizeable window in pygame and make content adjustable according to window size
import pygame, sys
# Initialize Pygame
pygame.init()
# Set up the display
screen_width = 800
screen_height = 600
screen_flags = pygame.RESIZABLE
screen = pygame.display.set_mode((screen_width, screen_height), screen_flags)
# Load the image and scale it to the initial size of the window
image = pygame.image.load("image.png")
image = pygame.transform.scale(image, (screen_width, screen_height))
# Set up the game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.VIDEORESIZE:
# Resize the window and the image
screen_width, screen_height = event.size
screen = pygame.display.set_mode((screen_width, screen_height), pygame.RESIZABLE)
image = pygame.transform.scale(image, (screen_width, screen_height))
# Draw everything to the screen
screen.blit(image, (0, 0))
pygame.display.flip()
Comments
Post a Comment