Coding With Fun
Home Docker Django Node.js Articles FAQ

python makes mini-games (II)


Jun 01, 2021 Article blog



This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn

Lead

T_T suddenly found out that N had done a series a long time ago???

Make small games with Python???

Well, since you've done it, there's a head and a tail

In this issue we will make a small game similar to the eight notes.

Let's start happily


Related documents

Baidu web download link: https://pan.baidu.com/s/1JKS3_bTToinfyGREcdqduw

Password: ufaw


References

cocos2d documentation:

http://python.cocos2d.org/doc/


Develop tools

Python version: 3.6.4

Related modules:

cocos2d module;

pyaudio module;

and some Python's own modules.

Environment construction

Install Python and add it to the environment variable, and pip installs the relevant modules that are required.


Introduction to the principle

I believe that many people are no stranger to the eight-note game, its core game is to use sound to control a small black ball with braids forward, probably long like this:

 python makes mini-games (II)1

Today we're going to make a low-match version of the T_T, which features the use of sound to control Pikachu's progress.

The game uses the cocos2d module, which is used to build the game frame, and the pyaudio module, which is used to get the sound of the microphone.

Step1 defines the Pikachu class:

Since the main character of the game is Pikachu, we have to define a class to reflect the main character?

Using the cocos2d module to define the main character class is a very easy task. All you have to do is inherit the sprint class and tell the sprint class what to do.

According to the eight-note rules of the game, we give Pikachu the ability to jump, drop and stand still, while we stipulate that Pikachu cannot jump in the air. The code is as follows:

 python makes mini-games (II)2

Step2 defines the block class:

Obviously, the main character needs to have a place to stand T_T, everywhere the abyss is afraid that 10,000 Pikachu can not save the Earthlings T_T.

For the ground, at first there must be a long flat buffer, let the player first try their voice, and then randomly generate jump blocks, let the player show their voice. The code is as follows:

 python makes mini-games (II)3

Step3 enables the game's main loop:

With the above paving, we can start to implement our mini-game.

At this point we need to inherit the ColorLayer class to implement the main loop of the game. First, we define the voice bar class to "see" our voice in the dark, as follows:

 python makes mini-games (II)4

Then there are the instantiation pikachu and block classes, where the block class is placed in a container for easy overall processing. The code is as follows:

 python makes mini-games (II)5

Ah, don't forget that only input sounds can control our Pikachu movements, the code is as follows:

 python makes mini-games (II)6

Finally, we need to tell the program the rules of the game, which is to control the length of the voice bar and the movement of Pikachu based on the volume of the sound received by the microphone. Here, we achieve Pikachu's forward effect by moving the block back as a whole.

Of course, we also need to add the necessary collision detection procedures, otherwise Pikachu will be stuck in the swamp-like ground as soon as he appears. ch.

The code is as follows:

 python makes mini-games (II)7

OK, it's done!!!


The effect of the game

Run the Game2.py file in the cmd window.

The effect is as follows:


more

For a tutorial on the cocos module, it is recommended to read:

http://python.cocos2d.org/doc/

Code as of 2018-06-14 test is correct.

Interested partners can improve and optimize on this basis, such as adding sound effects and score statistics, start and end interface, etc.

In short, the room for improvement is still relatively large