<>pygame How to realize full screen mode and adjustable window size

stay pygame Can be set by pygame.display.set_mode(size=(0, 0), flags=0, depth=0)
Set the window parameters , This function returns a Surface object .

In the four parameters ,size Represents the size of the window ,flags Parameters can control the window mode ,depth The parameter represents the number of bits of the color

flags parameter :

* pygame.FULLSCREEN Create a full screen window
* pygame.DOUBLEBUF use HWSURFACE or OPENGL It is suggested that this sign be added to the design
* pygame.HWSURFACE Using hardware acceleration , Only in FULLSCREEN Valid when
* pygame.OPENGL Create a opengl Windows for
* pygame.RESIZABLE Window variable size
* pygame.NOFRAME The window has no borders and control bars
Want to achieve press F11 Full screen mode , And the window size is adjustable , You can refer to the following code
import pygame, sys from pygame.locals import * def main(): global screen,
WINDOWWIDTH, WINDOWHEIGHT SIZE = WINDOWWIDTH, WINDOWHEIGHT = 1000, 800 fps =
pygame.time.Clock() isfullscreen = False pygame.init() screen = pygame.display.
set_mode(SIZE, RESIZABLE) screen.fill((255,255,255)) # Background color white while True:
isfullscreen= Resize(isfullscreen) for event in pygame.event.get(): if event.
type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key ==
K_ESCAPE: pygame.quit() sys.exit() pygame.display.update() screen.fill((255,255
,255)) fps.tick(30) def Resize(isfullscreen): # This function must first determine whether the window size changes , When judging whether the screen is full or not #
otherwise , After full screen ,pygame Will determine that the full screen operation is also an operation to change the size of the form , So a larger window will be displayed, but not in full screen mode for event in pygame.
event.get(VIDEORESIZE): size = WINDOWWIDTH, WINDOWHEIGHT = event.size[0], event.
size[1] screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), RESIZABLE)
for event in pygame.event.get(KEYDOWN): if event.key == K_F11: if not
isfullscreen: isfullscreen = True SIZE = WINDOWWIDTH, WINDOWHEIGHT = pygame.
display.list_modes()[0] screen = pygame.display.set_mode(SIZE, FULLSCREEN) else:
isfullscreen= False SIZE = WINDOWWIDTH, WINDOWHEIGHT = 1000, 800 screen =
pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), RESIZABLE) pygame.event.
post(event) return isfullscreen if __name__ == '__main__': main()
<> Some unsolved problems :

*
In code isfullscreen Is used to determine whether the window is full screen , I wanted to use it at first global Set as global variable , But after trying, I found that everything would be wrong , It's not clear what went wrong .

*
At the beginning, I want to put the exit code of the game interface into a function , But in that case, you'll find that F11 Then the screen flashes back and forth between full screen and original mode , And then it flashed back , But if you add one for
event in pygame.event.get() Get the event , There would be no such phenomenon . Guess it's because event The event is not completely read . Is there a big man pointing out the mistake , The code is as follows
def main(): global screen, isfullscreen, WINDOWWIDTH, WINDOWHEIGHT #
take isfullscreen Also set to global variables SIZE = WINDOWWIDTH, WINDOWHEIGHT = 1000, 800 fps = pygame.
time.Clock() isfullscreen = False pygame.init() screen = pygame.display.set_mode
(SIZE, RESIZABLE) screen.fill((255,255,255)) # Background color white while True: checkQuit()
Resize() pygame.event.get() # Add event acquisition function pygame.display.update() screen.fill((255,
255,255)) fps.tick(30) # Exit function def terminate(): pygame.quit() sys.exit() def
checkQuit(): for event in pygame.event.get(QUIT): terminate() for event in
pygame.event.get(KEYDOWN): if event.key == K_ESCAPE: terminate() pygame.event.
post(event) def Resize(): global isfullscreen, WINDOWWIDTH, WINDOWHEIGHT, screen
# add to global statement for event in pygame.event.get(VIDEORESIZE): size = WINDOWWIDTH,
WINDOWHEIGHT= event.size[0], event.size[1] screen = pygame.display.set_mode((
WINDOWWIDTH, WINDOWHEIGHT), RESIZABLE) for event in pygame.event.get(KEYDOWN):
if event.key == K_F11: if not isfullscreen: isfullscreen = True SIZE =
WINDOWWIDTH, WINDOWHEIGHT = pygame.display.list_modes()[0] screen = pygame.
display.set_mode(SIZE, FULLSCREEN) else: isfullscreen = False SIZE = WINDOWWIDTH
, WINDOWHEIGHT = 1000, 800 screen = pygame.display.set_mode((WINDOWWIDTH,
WINDOWHEIGHT), RESIZABLE) pygame.event.post(event) if __name__ == '__main__':
main()
<> to update

After a period of time after the search , Solved the first problem . When you find that you need to assign values to global variables , A statement is required , Use in functions global statement isfullscreen
Equivariant , In this way, the value of the global variable will be changed during the assignment , So the above code has been modified

On question 2 , Found that as long as the Resize() Add a line after the function event Function of event , Or delete Resize() In function post()
function , There will be no flash screen exit , Prove that the above guess is correct , You can use the post() Function returns the KEYDOWN event

Technology
©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple ) It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Browser kernel ( understand )HashMap Explain in detail Some East 14 Pay change 16 salary , Sincerity or routine ?html Writing about cherry trees , Writing about cherry trees