头图

Disclaimer: It is not easy to be original, and may not be reproduced without permission

xa

0. Preface

Hello, friends, I am the cat lady who took you to write games together! Recently, Sister Cat is busy doing python the course of introductory programming. 57 episodes have been updated on Douyin. For those who are learning python programming from 0 , you can watch it on Douyin (Douyin personal homepage ) Look, you will never be disappointed! Well, let's continue playing the game today. Today, Sister Cat brings you a classic little game on Douyin. In the game, a child needs to catch mushrooms that fall from the sky, but only non-poisonous mushrooms can be caught. If you catch a poisonous mushroom, the game is lost! Although this game is very simple, the code of the whole game is only 200 多行 , but it contains 70% of the core knowledge points in python game programming , so as long as you understand all the codes of this game! You can start to develop your own python mini-games, for example, such mini-games as fighter jets, whack-a-mole, etc. You must be able to write them too!

Although this game is very simple, in order to let your friends really learn python game programming, Sister Cat will also divide it into several videos to teach you how to write this game step by step . In today's episode, let's learn first python the basic routine of game programming. After learning this routine, in the next episode, Sister Cat will implement a method using the arrow keys based on the code of the first episode. Controlled small square , the effect is like this.

Of course, this cube ends up being a little boy picking mushrooms! Without further ado, let's get to the point!

1. The basic routine of python game development

In this episode, Sister Cat will give you a brief introduction to pygame , pygame is python a framework for game development , using this framework we can use python language to quickly develop various games. So before writing the game code, you still need to understand the basic working principle of pygame .

2. Understand the basic framework of pygame

First, we create a new part1-game.py file, then open this file and start writing code! We will import pygame , locals , sys these modules for later use.

 import pygame
from pygame.locals import *
import sys

Next, we will complete the initialization of the game through the pygame init function of ---50523f1ebaec23851bee926138d7eb59---, because the game framework we use is pygame, and we need to initialize it before using it. Learn more about the details, just know that it is necessary, just like we have to wash our hands and take chopsticks before eating.

 pygame.init()

After the initialization work is completed, since our game is running in a window, we also need to create a window here . Let's first define the width and height of the window. Creating a window can be achieved through the pygame.display.set_mode function. In the brackets of the set_mode function, the width and height of the game window need to be passed in. . This function will get a return value, we use screen this variable to store this return value.

 WIDTH = 600
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))

Now run the program to see the effect, click the right mouse button, select Run + part1-game , after the program starts, you can see that the window flashes and disappears.

窗口消失

Why does the window disappear ? This is because the code successfully exits after running from top to bottom, and the real game needs to constantly refresh the window and display the content on the screen, so we also need to use while True this infinite loop , so the code doesn't exit. Inside the while True loop, use the pygame.display.update function to update the content displayed on the screen. Here we can see a relatively complete code.

 import pygame
from pygame.locals import *
import sys
pygame.init()
WIDTH = 600
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
while True:
    # 更新屏幕内容
    pygame.display.update()

Now run the program again to see the effect, right-click the mouse, select Run + part1-game , after the program runs, you can see that the window will not disappear (it has been displayed).

窗口显示

Next, we click the close button in the upper right corner of the window, and we can see that the program is now in an unresponsive state. This is because our program doesn't know how to handle the mouse click (close the menu) event , so in the while loop, we also need to handle the mouse click to close the menu event.

窗口未响应

In pygame, it is very simple to handle mouse or keyboard events, we need to use for loop, traverse pygame.event.get function, and get the mouse through pygame.event.get function Or a list of keyboard events. Then pass event.type to judge whether the event is equal to QUIT (a QUIT event will be generated when the window x is clicked). If event.type is equal to QUIT , the system exits.

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

Run the program to see the effect, click the right mouse button, select Run + part1-game , after the program is running, click the button in the upper right corner of the close window to exit the window.

窗口退出


猫姐_游戏编程
56 声望17 粉丝

😸猫姐,211硕士,定居成都的荆州人,5年IT生涯,教大家做益智小游戏,快乐学编程