How to I set minimum and maximum width and height of the screen of my Python Pygame window?
Question:
I am stuck on how do I set do I block the user to go less than the set screen width and height of my Pygame and also block the user to go beyond the max width and height of the display screen in Pygame
Answers:
I don’t know exactly what are your project’s variables etc. but this is just an easy example. In the place of x you put integer from range 0 to quantity of provided resolutions ofc 🙂
resolutions = [(1280, 768), (1600, 900), (1920, 1080)]
picked_resolution = x
window = pygame.display.set_mode(size=resolution[picked_resolution])
Here is an easy example that changes the screen size when its minimum/maximum bounds are broken:
#imports
import pygame
from pygame.locals import *
pygame.init()
#bounds definition
STARTWIDTH, STARTHEIGHT = 200, 200
MAXWIDTH, MAXHEIGHT = 200, 200
MINWIDTH, MINHEIGHT = 200, 200
#variables
screen = pygame.display.set_mode((STARTWIDTH, STARTHEIGHT), RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == VIDEORESIZE:
width = min(MAXWIDTH, max(MINWIDTH, event.w))
height = min(MAXHEIGHT, max(MINHEIGHT, event.h))
if (width, height) != event.size:
screen = pygame.display.set_mode((width, height), RESIZABLE)
screen.fill((255,255,255))
pygame.display.update()
pygame.quit()
The VIDEORESIZE
event is, as its name says, always released when the screen size is changed. So, when it is released, we want to check whether the user remained inside the bounds and, if not, reset its size.
It has three attributes: w
, h
and size
. The w
and h
attributes contain the windows height and width respectivily, after resizing. The size
attribute is a tuple of (width, height)
.
When this event is raised, we do of course want to check whether the user remained in our bounds. This we do using the min
and max
functions. We use max(MINWIDTH, event.w)
to determine whether the height is not under the max width, because if the width is smaller then MINWIDTH
, MINWIDTH
will be returned. Otherwise, the width itself will be returned. Then we do something similar with that result and the max
function to determine whether the window is not to big. Then we repeat the whole proces for the height.
Then we check whether the resulting new size is not the same as the old size. If it is, it does mean that the min/max bounds were broken and we need to adapt the size using pygame.display.set_mode
. If not, this means that we can continue, as the new size remains within its bounds.
I am stuck on how do I set do I block the user to go less than the set screen width and height of my Pygame and also block the user to go beyond the max width and height of the display screen in Pygame
I don’t know exactly what are your project’s variables etc. but this is just an easy example. In the place of x you put integer from range 0 to quantity of provided resolutions ofc 🙂
resolutions = [(1280, 768), (1600, 900), (1920, 1080)]
picked_resolution = x
window = pygame.display.set_mode(size=resolution[picked_resolution])
Here is an easy example that changes the screen size when its minimum/maximum bounds are broken:
#imports
import pygame
from pygame.locals import *
pygame.init()
#bounds definition
STARTWIDTH, STARTHEIGHT = 200, 200
MAXWIDTH, MAXHEIGHT = 200, 200
MINWIDTH, MINHEIGHT = 200, 200
#variables
screen = pygame.display.set_mode((STARTWIDTH, STARTHEIGHT), RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == VIDEORESIZE:
width = min(MAXWIDTH, max(MINWIDTH, event.w))
height = min(MAXHEIGHT, max(MINHEIGHT, event.h))
if (width, height) != event.size:
screen = pygame.display.set_mode((width, height), RESIZABLE)
screen.fill((255,255,255))
pygame.display.update()
pygame.quit()
The VIDEORESIZE
event is, as its name says, always released when the screen size is changed. So, when it is released, we want to check whether the user remained inside the bounds and, if not, reset its size.
It has three attributes: w
, h
and size
. The w
and h
attributes contain the windows height and width respectivily, after resizing. The size
attribute is a tuple of (width, height)
.
When this event is raised, we do of course want to check whether the user remained in our bounds. This we do using the min
and max
functions. We use max(MINWIDTH, event.w)
to determine whether the height is not under the max width, because if the width is smaller then MINWIDTH
, MINWIDTH
will be returned. Otherwise, the width itself will be returned. Then we do something similar with that result and the max
function to determine whether the window is not to big. Then we repeat the whole proces for the height.
Then we check whether the resulting new size is not the same as the old size. If it is, it does mean that the min/max bounds were broken and we need to adapt the size using pygame.display.set_mode
. If not, this means that we can continue, as the new size remains within its bounds.