How do I maximize the display screen in PyGame?

2020-02-28 04:39发布

I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back? Thanks

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()

标签: python pygame
5条回答
Bombasti
2楼-- · 2020-02-28 04:44

This will let you toggle from maximize to the initial size

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()
查看更多
Explosion°爆炸
3楼-- · 2020-02-28 04:50

The method you are looking for is pygame.display.toggle_fullscreen

Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.

In your case this would look like

DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)

(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)

查看更多
【Aperson】
4楼-- · 2020-02-28 04:52
import pygame, os

os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()

pygame.init()

info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h

These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:

window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-28 05:03

You'd have to add the full screen parameter onto the display declaration like this:

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()
查看更多
Juvenile、少年°
6楼-- · 2020-02-28 05:05

To become fullscreen at native resolution, do

DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()

You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

查看更多
登录 后发表回答