pygame -- create a simple window

Keywords: Python Programming

  • First, we need to import the module:

import pygame, sys

  • Then you need to initialize pygame. I have defined a pygame UU test() method, so if you need to execute a program, you need to call this method.
If you are still confused in the world of programming,
I don't know my future plan.
Interested in python,
Here is my QQ group of learning exchange circle: 895 797 751.
It's all about learning python.

import pygame, sys
def pygame_test():
    Initialization
    pygame.init()
  • After initialization, you can start to create a window. I have created a 500x500 window. When executing the program, we can see that the creation has been created, but the window will exit as soon as it appears:
import pygame, sys

def pygame_test():
    # Initialization
    pygame.init()
    # Create a window
    screen = pygame.display.set_mode((500, 500))

    # Set 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, a dead loop). You can listen for events by looping through them, and you can use sys.exit() if you want to exit the program.
import pygame, sys

def pygame_test():
    # Initialization
    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 through continuous loops
    while True:
        # get(): get the return value of the event
        for event in pygame.event.get():
            # Judge whether the event is an exit event. If yes, exit.
            if event.type == pygame.QUIT:
                # Exit the pygame window before exiting the program
                pygame.quit()
                sys.exit()
pygame_test()

  • In this way, when the code is executed, a window will pop up that can run normally and be closed. If we want to add a background color to the window, we need to use the fill() method. For example, we add a blue background color to the window, and then add a mouse click event. When we click the mouse, the background color will change to pink.
import pygame, sys

def pygame_test():
    # Initialization
    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 through continuous loops
    while True:
        # Fill the screen with light blue
        screen.fill((135, 206, 250))

        # get(): get the return value of the event
        for event in pygame.event.get():
            # Judge whether the event is an exit event. If yes, exit.
            if event.type == pygame.QUIT:
                # Exit the pygame window before exiting 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()

Posted by saradrungta on Wed, 23 Oct 2019 13:32:13 -0700