The little overlord of StarCraft and the little bee (1)--window interface design

Table of contents

 

foreword

1. Install the pygame library

 1. Introduction to pygame library

 2. Install the pygame library in the windows system

2. Build the game framework

1. Create a game window 

 2. Change the window color

 Summarize


foreword

Everyone should have read or heard of the python book "Boa Constrictor". There is a case on it called "Alien Invasion". Next, we will follow the steps in the book to see if the game function can be realized, and then upgrade.

1. Install the pygame library

 1. Introduction to pygame library

The Pygame library is a Python library for writing 2D games. It provides many functions for processing images, sounds, etc., enabling developers to create their own games quickly and easily. Pygame performs well in terms of cross-platform compatibility and can be used under multiple operating systems.

At the heart of Pygame is the Surface object, which represents a drawable area. Pygame can create game elements by drawing graphics and text on Surface. Pygame also provides common functions such as event processing, collision detection, and clock control, so that developers can easily implement game logic.

In addition to the basic functions, Pygame also has a large number of extension modules available, such as Pygame.mixer (for playing audio), Pygame.font (for processing fonts), etc. Developers can selectively introduce these modules according to their own needs.

In short, Pygame provides developers with a large number of tools and function libraries that make developing games easier and more enjoyable.

 2. Install the pygame library in the windows system

 Friends who have had a simple foundation know that installing the library is the easiest, just enter the code

pip install pygame

 We enter the command on the command line, as shown in the figure

96a4b19b0c2440f5818ef6c27f070929.jpeg

 This will start the installation, and the words "Success" will indicate that the installation is successful.

d233f05677014711a7a710915995d6c9.jpeg

2. Build the game framework

1. Create a game window 

Let's follow the steps in the book, first create an empty window and try to see if it can succeed 

In order to avoid confusion in subsequent calls, the python file we created is also named alien_invasion.py, alien means alien, and invasion means attack.

Put the code first, then explain in detail later

import sys
import pygame

def run_game():
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    pygame.display.set_caption("Alien Invasion")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        pygame.display.flip()

run_game()

Let's introduce them one by one:

 ❶sys module is a module in the Python standard library, which provides functions related to the Python interpreter and runtime environment. Using the sys library, you can obtain information about the Python interpreter, control program exit, standard input and output, and error streams, and operate module search path etc. So the purpose of importing the sys module here is to control the exit of the program

 ❷pygame.init() is to initialize the game object. This operation is necessary, and the specified action must be done. This function will initialize the Pygame library and provide it with the necessary resources and support. After initialization, the functions in the Pygame library can be used normally.

 ❸display module is a module used to manage screens and windows in Pygame. It contains functions for initializing the pygame display module, setting screen resolution, creating game windows, updating window contents, handling events, and more. It can be well understood that pygame.display.set_mode((800,600)) is to set the window size, and pygame.display.set_caption("Alien Invasion") is to set the name of the window

 ❹The loop statement is to monitor the operation of the mouse and keyboard all the time. We will talk about it in detail when there is keyboard and mouse input. This code is easy to understand. If the game is judged to exit, then close the program. The last statement of the loop, pygame.display.flip(), I personally understand is to refresh, constantly refresh the screen, and keep the dynamic effect.

 Let's run it and see how it works.

 0d4cc0ef301c4bad9222ac562e6a45a4.jpeg

 The effect is very good, as we thought, the next step is to change the color of the window.

 2. Change the window color

We know that many software, including Python, use RGB values ​​to control colors. RGB is the abbreviation of the three color components of red, green, and blue. The value of each component can take an integer value between 0 and 255, so RGB can represent 256 256 256 = 16777216 different colors. Some of the commonly used colors and their corresponding RGB values ​​are as follows:

  • Red: (255, 0, 0)
  • Yellow: (255, 255, 0)
  • Green: (0, 255, 0)
  • Cyan: (0, 255, 255)
  • Blue: (0, 0, 255)
  • Purple: (255, 0, 255)
  • White: (255, 255, 255)
  • Black: (0, 0, 0)

We only need to add one line of code to define a color for the window before drawing the screen each time, that is, before the refresh I mentioned above. Let's try the cyan background.

import sys
import pygame

def run_game():
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    pygame.display.set_caption("Alien Invasion")
  
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill((0,255,255))
        pygame.display.flip()

run_game()

 After adding the code screen.fill((0,255,255)), the effect will appear immediately, see the picture

 4ef78c5c34804f00b70e33e979a5402e.jpeg

 Summarize

 Everything is difficult at the beginning, set up the screen of the game today, and we will add all kinds of bugs later.

 

 

 

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132330061