In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to create a simple window with pygame". In daily operation, I believe many people have doubts about how to create a simple window with pygame. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to create a simple window with pygame". Next, please follow the editor to study!
First, we need to import the module:
Import pygame, sys
Then you need to initialize pygame, and I define a pygame_test () method, so if you need to execute the program, call this method:
Import pygame, sysdef pygame_test (): # initialize pygame.init ()
After initialization, you can start to create a window. I created a 500x500 window, and we can see that the creation has been created by executing the program, but the window will exit as soon as it appears:
Import pygame, sysdef pygame_test (): # initialize pygame.init () # create a window screen = pygame.display.set_mode ((500,500)) # set the window title pygame.display.set_caption ('this is a window title') pygame_test ()
So we can add a while loop to it and set the value to True (that is, an endless loop). You can listen for events through constant loops, or use sys.exit () if you want to exit the program.
Import pygame, sysdef pygame_test (): # initialize pygame.init () # create a window screen = pygame.display.set_mode # set the window title pygame.display.set_caption ('this is a window title') # listen for the event while True: # get (): get the return value of the event for event in pygame.event.get (): # determine whether the event is an exit event Yes, exit if event.type = = pygame.QUIT: # exit the pygame window first, and then exit the program pygame.quit () sys.exit () pygame_test ()
In this way, when the code is executed, a window will pop up that will work properly and close. We need to use the fill () method if we want to add a background color to the window. For example, we add a blue background color to the window, and then add a mouse click event. When the mouse is clicked, the background color changes to pink.
Import pygame, sysdef pygame_test (): # initialize pygame.init () # create a window screen = pygame.display.set_mode ((500,500)) # set window title pygame.display.set_caption ('this is a window title') # listen for events while True: # fill the screen with light blue screen.fill ((135,206) # get (): get the return value of the event for event in pygame.event.get (): # determine whether the event is an exit event If yes, exit if event.type = = pygame.QUIT: # exit the pygame window first Then exit the program pygame.quit () sys.exit () # Click the mouse window color to pink if event.type = = pygame.MOUSEBUTTONDOWN: screen.fill ((255,192,203)) # Update the entire Surface object to be displayed to the screen pygame.display.flip () pygame_test () to this The study on "how to create a simple window with pygame" is over. I hope I can solve your 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.