To create a game window in Python, we can use the Pygame library which provides a set of Python modules that allow you to create graphics and games easily. Here's an example code snippet that initializes Pygame and creates a game window:
main.py533 chars31 lines
In this code, first we import the Pygame library and initialize it using the pygame.init() function. Then, we create a game window by setting its size using a tuple size and calling the pygame.display.set_mode() function, which returns a Surface object that represents the game window.
We also set the window title using the pygame.display.set_caption() function.
The main game loop runs until the done variable is set to True. The loop processes any events that occur using a for loop and the pygame.event.get() function. In this example, we only check for the QUIT event, which occurs when the user clicks the close button of the window.
The game logic and drawing code can be added to the appropriate sections within the main loop. In this example, we simply fill the screen with white using the fill() method of the Surface object.
Finally, we update the screen using the pygame.display.flip() function, which draws the contents of the screen to the window.
At the end of the program, we quit Pygame using the pygame.quit() function.
gistlibby LogSnag