pygame creates a simple window

Posted by CodeJunkie88 on Tue, 08 Oct 2019 12:34:20 +0200

Before that, we had a basic understanding of the common modules of Pygame, and some methods and functions of common modules. Now we can simply use pyGame to create a window that can change color. Let's see:

  • First we need to import modules:
import pygame, sys
  • Then you need to initialize pygame, and I define a pygame_test() method, so if you need to execute a program, call this method yo:
import pygame, sys
def pygame_test():
    # Initialization
    pygame.init()
  • After initialization, you can start to create windows. I created a 500x500 window. When the program executes, we can see that the creation has been created, but the window will exit immediately after it appears:
import pygame, sys

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

    # Setting the title of the window
    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). Events can be listened for through continuous loops, which can be used if you want to exit the program sys.exit().
import pygame, sys

def pygame_test():
    # Initialization
    pygame.init()
    # Create a window
    screen = pygame.display.set_mode((500, 500))
    # Setting the title of the window
    pygame.display.set_caption('This is a window title.')

    # Listen for events through continuous loops
    while True:
        # get(): Gets the return value of the event
        for event in pygame.event.get():
            # Determine whether an event is an exit event or an exit event
            if event.type == pygame.QUIT:
                # Exit the pygame window first, then exit the program
                pygame.quit()
                sys.exit()
pygame_test()
  • That way, when you execute the code, a window will pop up that will run and close properly. If we want to add 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 changes to pink.
import pygame, sys

def pygame_test():
    # Initialization
    pygame.init()
    # Create a window
    screen = pygame.display.set_mode((500, 500))
    # Setting the title of the window
    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(): Gets the return value of the event
        for event in pygame.event.get():
            # Determine whether an event is an exit event or an exit event
            if event.type == pygame.QUIT:
                # Exit the pygame window first, then exit the program
                pygame.quit()
                sys.exit()
            # Click the mouse window to turn 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()

For pygame's modular approach, you can see: https://www.jianshu.com/p/a439afd5140d

Topics: Programming Windows