python3 uses pygame to initialize a window

import sys
import pygame
def run_game():
    pygame.init()
    screen = pygame.display.set_mode((1000,599))
    pygame.display.set_caption("my first screen")
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        pygame.display.flip()
run_game()

The steps for python to make a simple animation window are as follows:

Step 1: Import two files, sys, pygame, where sys is used to end the game and pygame is used to generate game code

The second part: define a function, this function is to define the main function of the game, in this function

              First, call pygame to initialize the background settings of pygame to make pygame work normally, that is, execute pygame.display.init() or pygame.init()

              Second, set the size of the entire window of the game and assign it to a window variable (specify a value yourself, such as screen), screen = pygame.display.set_mode(width,height)

              Once again, set the title of the game window, call pygame.display.set_caption, code example: pygame.display.set_caption("caption of screen")

              Later, a loop is established. This loop operation will automatically execute the window generated by pygame.display.set_mode above, as follows, while True:

              Then, a for loop is established in the while loop to detect events from hardware devices such as keyboards and mice. For example, the code: for event in pygame.event.get()

                             pygame.event.get() — get events from the queue

              Later, if the event event == pygame.QUIT, the window is closed, pygame, QUIT represents the window closed event, as shown below:

                Then, call pygame.display.flip() to dynamically refresh the screen to create animation effects. pygame.display.flip() is inside the while loop and outside the for loop. Each time the while loop is executed, a new screen is drawn and the old screen is wiped so that only the new screen is visible. When we move the position of the game element, pygame.display.flip() will continuously update the screen to show the new position of the screen, and hide the element in the original position to create a smooth moving effect.        

               Finally, execute this function. If the function name is run_game, then execute run_game()

Comrades who are interested in writing their own games, come on! ! !

 

 

  

Guess you like

Origin blog.csdn.net/digitalkee/article/details/112851410