In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to open a window of PyGame". In daily operation, I believe many people have doubts about how to open a window of PyGame. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to open a window of PyGame". Next, please follow the editor to study!
Import and run pygame
You can create a python file such as mygame1.py. Then write at the beginning of the file
Import pygamepygame.init ()
All right, run it. No change? That's right, now there are several lines of output on the console.
Screen, Surface and Background
The largest window displayed when pygame is running is screen. For example, if you run LOL, it should be easy to determine which window is the largest. With pygame, we can create and control many rectangular areas in screen, which are called surface. We will use a surface as the background, and this surface is background. Next we create a background and add it to the screen.
Screen = pygame.display.set_mode ((640480)) # set the size of the window background = pygame.Surface (screen.get_size ()) # create a surface named background, the same size as screen background.fill ((255255255)) # fill background with white 255255255 is the white background of RGB = background.convert () # convert () to background, which can speed up the addition of background (blit) to screen later
Run your program. What? All you see is a black window flashing past? That's right again.
So far, we've only created background in memory, and we haven't added background to screen yet, so we can't see a white background.
Screen.blit (background, (0,0)) # adds background to screen. (0,0) means to place the upper left corner of background in the upper left corner of screen.
In many game frames, and of course pygame, the coordinate origin is at the vertex in the upper left corner of the window, and that's what it looks like.
Main loop and event handling
The main loop is an almost infinite loop, which stops only when you want to quit the game.
Mainloop = Truewhile mainloop: for event in pygame.event.get (): # this part is event handling if event.type = = pygame.QUIT: # if you press the fork in the upper right corner mainloop = False # exit the main loop elif event.type = = pygame.KEYDOWN: # if you press the keyboard if event Key = = pygame.K_ESCAPE: # and press the ESC key mainloop = False # exit the main loop # do some game-related operations here Pygame.display.flip () # refresh display is temporarily omitted here
Ok, run your game again ~ then you will get a white window. Next we will show something in the window.
Display frame rate
Frame rate is the number of frames per second that the game runs, or FPS for short. You must know FPS if you have played the game. The higher the frame rate, the smoother the game. We can get the frame rate like this.
Clock = pygame.time.Clock () # create clock object clock.get_fps () # get frame rate
Now let's add the logic that displays the frame rate to the main loop
Clock = pygame.time.Clock () # create clock object FPS = 30 # define the upper limit of the frame rate That is, the maximum frame rate mainloop = Truewhile mainloop: clock.tick (FPS) # sets the maximum frame rate for event in pygame.event.get (): # this part is the event handling if event.type = = pygame.QUIT: # if you press the fork in the upper right corner mainloop = False # exit the main loop elif event.type = = pygame.KEYDOWN: # if you press the keyboard if event.key = = pygame.K_ESCAPE: # and press the ESC key mainloop = False # exit the main loop # do some game-related operations here Text = "FPS: {0pur.2f}" .format (clock.get_fps ()) pygame.display.set_caption (text) pygame.display.flip () # refresh display is temporarily omitted here
Run your code ~ the frame rate can be displayed.
Exit pygame
At the end of the program, you need to exit pygame to release the requested computing resources.
Pygame.quit () complete code import pygamepygame.init () screen = pygame.display.set_mode ((640480)) # set the size of the window background = pygame.Surface (screen.get_size ()) # create a surface named background, the same size as screen background.fill ((255255255)) # fill background with white 255255255 is the white background of RGB = background.convert () # convert () to background, which can speed up the addition of screen.blit (background, (0,0)) clock = pygame.time.Clock () # create clock object FPS = 30 # define the upper limit of frame rate That is, the maximum frame rate mainloop = Truewhile mainloop: clock.tick (FPS) # sets the maximum frame rate for event in pygame.event.get (): # this part is the event handling if event.type = = pygame.QUIT: # if you press the fork in the upper right corner mainloop = False # exit the main loop elif event.type = = pygame.KEYDOWN: # if you press the keyboard if event.key = = pygame.K_ESCAPE: # and press the ESC key mainloop = False # exit the main loop # do some game-related operations here Text = "FPS: {0pygame.display.set_caption .2f}" .format (clock.get_fps ()) pygame.display.set_caption (text) pygame.display.flip () # refresh display pygame.quit () is omitted here, and the study on "how to open a window for PyGame" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.