Introduction 🎼
Hi, big babies, it's the weekend again. Did you have a holiday today?
Weekends are always idle, idle. I've been thinking about playing some games to give you something new. Isn't that right?
Play games, find games, find materials and write code. Have you heard of this game?
Voice controlled 2D action game, its name is "don't stop! Eighth note sauce" ♪!
Today, let's give you an imitation eight tone Rune game!
This game requires the operator to make a sound to control the characters, but also to master the strength of the sound... Secretly play. jpg
The operation depends on roaring, and the walking position depends on tone. No matter how much Sao operation is useless, Xiaobian can only advise you not to play at night. Maybe you will be misunderstood by your neighbors~
text 🎼
🎵 1, In preparation
1.1 rules of the game 🎶
Different from the previous computer keyboard operation and the touch screen operation of online mobile games, the operation method of this game is actually sound.
Players can control the jump of note sauce by adjusting the size of the sound. In short. The louder the sound, the higher the notes jump.
If only a faint sound is made, the note sauce will only move slowly. Isn't it super interesting!
1.2 material pictures 🎶
🎵 2, Environment
1.1 INTRODUCTION 🎶
Cocos2d module:
Cocos2d is a framework for building 2D games, presentations, and other graphical / interactive applications. It can work on Windows, OSX and Linux, and it can be used by applications written in python.
Pyaudio module:
PyAudio library, which can be used for recording, playing, generating wav files, etc. PyAudio provides PortAudio Python language version, which is a cross platform Audio I/O library. You can use PyAudio in Python Play and record audio in sequence. Provide Python binding and cross platform Audio I/O library for PoTaTudio. With PyAudio, you can To easily use Python to play and record audio on various platforms, such as GNU/Linux, Microsoft Windows and Apple Mac OS X/MACOS.
one point two Configuration environment 🎶
This paper deals with the environment: Python 3, pychar and cocos2d modules; pyaudio module; And some Python built-in modules.
Module installation:
pip install +Module name or image source with watercress pip install -i https://Pypi. Doublan. COM / simple / + module name
🎵 3, Formal knock code
3.1 definition of small black eight tone symbols: 🎶
Using the cocos2d module to define the protagonist class is a very easy thing. You just need to inherit the sprites class and tell sprites
Class can do whatever it needs to do. According to the rules of the game, we give Xiaohei the ability to jump, fall and rest, the same as
We stipulated that Xiao Hei could not jump when he was in the air. The specific codes are as follows:
import cocos '''Small black class''' class Pikachu(cocos.sprite.Sprite): def __init__(self, imagepath, **kwargs): super(Pikachu, self).__init__(imagepath) # Anchor point self.image_anchor = 0, 0 # Initial reset self.reset(False) # to update self.schedule(self.update) '''Voice controlled jump''' def jump(self, h): if self.is_able_jump: self.y += 1 self.speed -= max(min(h, 10), 7) self.is_able_jump = False '''Standstill after landing''' def land(self, y): if self.y > y - 25: self.is_able_jump = True self.speed = 0 self.y = y '''to update(Gravity drop)''' def update(self, dt): self.speed += 10 * dt self.y -= self.speed if self.y < -85: self.reset() '''Reset''' def reset(self, flag=True): if flag: self.parent.reset() # Can I jump self.is_able_jump = False # speed self.speed = 0 # position self.position = 80, 280
three point two Define the block class: 🎶
The initialization interface Xiaohei should have a standing place. For the ground, there must be a long flat buffer at the beginning,
Let players try their voice first, and then randomly generate jump blocks to let players show their voice.
The specific codes are as follows:
import cocos import random '''Ground block''' class Block(cocos.sprite.Sprite): def __init__(self, imagepath, position, **kwargs): super(Block, self).__init__(imagepath) self.image_anchor = 0, 0 x, y = position if x == 0: self.scale_x = 4.5 self.scale_y = 1 else: self.scale_x = 0.5 + random.random() * 1.5 self.scale_y = min(max(y - 50 + random.random() * 100, 50), 300) / 100.0 self.position = x + 50 + random.random() * 100, 0
three point three Realize the main cycle of the game: 🎶
import cfg import cocos import struct from modules import * from cocos.sprite import Sprite from pyaudio import PyAudio, paInt16 '''Define voice control game class''' class VCGame(cocos.layer.ColorLayer): def __init__(self): super(VCGame, self).__init__(255, 255, 255, 255, 800, 600) # frames_per_buffer self.num_samples = 1000 # Voice control strip self.vbar = Sprite(cfg.BLOCK_IMAGE_PATH) self.vbar.position = 20, 450 self.vbar.scale_y = 0.1 self.vbar.image_anchor = 0, 0 self.add(self.vbar) # Pikachu self.pikachu = Pikachu(cfg.PIKACHU_IMAGE_PATH) self.add(self.pikachu) # ground self.floor = cocos.cocosnode.CocosNode() self.add(self.floor) position = 0, 100 for i in range(120): b = Block(cfg.BLOCK_IMAGE_PATH, position) self.floor.add(b) position = b.x + b.width, b.height # Sound input audio = PyAudio() self.stream = audio.open(format=paInt16, channels=1, rate=int(audio.get_device_info_by_index(0)['defaultSampleRate']), input=True, frames_per_buffer=self.num_samples) # Screen update self.schedule(self.update) '''collision detection ''' def collide(self): diffx = self.pikachu.x - self.floor.x for b in self.floor.get_children(): if (b.x <= diffx + self.pikachu.width * 0.8) and (diffx + self.pikachu.width * 0.2 <= b.x + b.width): if self.pikachu.y < b.height: self.pikachu.land(b.height) break '''Define the rules of the game''' def update(self, dt): # Gets the volume per frame audio_data = self.stream.read(self.num_samples) k = max(struct.unpack('1000h', audio_data)) self.vbar.scale_x = k / 10000.0 if k > 3000: self.floor.x -= min((k / 20.0), 150) * dt # Pikachu jump if k > 8000: self.pikachu.jump((k - 8000) / 1000.0) # collision detection self.collide() '''Reset''' def reset(self): self.floor.x = 0 '''run''' if __name__ == '__main__': cocos.director.director.init(caption="xiaohei Go Go Go ") cocos.director.director.run(cocos.scene.Scene(VCGame()))
🎵 4, Effect display
summary 🎼
- Today's Muzi also tried this eighth note sauce. Because she was really afraid that her colleagues would hit me, she went home and recorded it.
- I can't pass the customs and I can't help it. I can't control this game. After playing a few games, my voice will start smoking~
- This reminds the curious babies not to play this voice control game in the dormitory at night. Otherwise, you may be beaten!!
Well, it's over here. If you want to play, you can play secretly at home. Remember to keep your voice down~ 🤫
🎶 Complete free source code collection:
Your support is my biggest motivation!! Remember the third company ~mua Welcome to read previous articles~
🍓 New answers——
If you encounter problems in learning Python, you will answer them when you have time! You need to add it yourself!
The WX number is as follows: apython68
🎉 Recommendation of previous game articles:
👑 Article summary——