Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can python use sound to control Pikachu's continuous progress in the game?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "python how to use sound to control Pikachu's continuous progress game". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope that this article "how to use sound to control Pikachu's continuous progress game" can help you solve the problem.

Development tools

Python version: 3.6.4

Related modules:

Cocos2d module

Pyaudio module

And some modules that come with Python.

Environment building

Install Python and add it to the environment variable, and pip installs the relevant modules you need.

Introduction of principle

I believe many people are no stranger to the game of eight notes, and its core game is to use sound to control the continuous progress of a small black ball with braided hair, which probably looks like this:

Today we're going to make a low-end version of Taut, which uses sound to control Pikachu's progress.

The game mainly uses cocos2d module and pyaudio module, the former is used to build the game framework, and the latter is used to obtain the sound of the microphone.

Step1 defines the Pikachu class:

Since the protagonist of the game is Pikachu, we have to define a separate class to reflect the protagonist, right?

It is very easy to use the cocos2d module to define the protagonist class. You just need to inherit the sprites class and tell the sprites class what to do.

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

Step2 defines the block class:

Obviously, the protagonist needs to have a place to stand. There is an abyss everywhere. I'm afraid 10,000 Pikachu can't save the people of the earth.

For the ground, at the beginning, you have to have a long flat cushion, let the player try his voice first, and then randomly generate a jump block to let the player show his singing voice. The specific code is as follows:

Step3 implements the main cycle of the game:

With the above groundwork, we can begin to realize our Mini Game.

At this point we need to inherit the ColorLayer class to implement the main loop of the game. First, we define a voice control bar class to "secretly observe" our voice. The code is as follows:

Then there is the instantiation of the pickup mound class and the block class, where the block class will be placed in the container to facilitate overall processing. The code is as follows:

Ah, don't forget that only by inputting sound can our Pikachu keep moving. The code is as follows:

Finally, we need to tell the rules of the game that the length of the voice control bar and the action of Pikachu are controlled according to the volume of the sound received by the microphone. Here, we achieve the forward effect of Pikachu by moving the whole block block backward.

Of course, we also need to add the necessary collision detection procedures, otherwise Pikachu will fall into the swamp ground as soon as he takes the stage. And we stipulate that the game is reset when Pikachu falls into the gully.

The code is as follows:

This is the end of the content about "how python can use sound to control Pikachu to keep moving forward". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report