In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to realize eight-note sauce Mini games in Python dry goods actual combat." In daily operation, I believe many people have doubts about how to realize eight-note sauce Mini games in Python dry goods actual combat. Xiao Bian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to realize eight-note sauce Mini games in Python dry goods actual combat." Next, please follow the small series to learn together!
directory
Lead?
Text?
? I. Preparations
1.1 Rules of the game?
1.2 Photo material?
? II. Environment
2.1 A little introduction?
2.2 Configuration environment?
? III. Formal knock code
3.1 Definition of black octave class: ?
3.2 Definition of block class: ?
3.3 The main game cycle:
? IV. Effect display
Lead?
This game requires the operator to control the characters by speaking, and at the same time, you have to master the intensity of the sound... Sneakily playing.jpg
All operations rely on shouting, all positions depend on tone, no matter how much you play, it is useless. Xiao Bian can only advise everyone not to play at night, probably misunderstood by neighbors ~
Text?
? 1.1 Rules of the game?
Different from the previous computer keyboard operation, but also different from the network mobile game touch screen operation, this game's operation method unexpectedly is sound.
The player can control the jumping of the note sauce by adjusting the size of the sound. Put simply. The louder the sound, the higher the notes jump.
If it makes only a faint sound, the note sauce will only move slowly. Isn't that super fun!
1.2 Photo material?
? Environment 2.1: A brief introduction. Cocos2d module:
Cocos2d is a framework for building 2D games, presentations and other graphical/interactive applications.
It works on Windows, OS X and Linux, and it can be used by applications written in Python.
Pyaudio module:
Pyaudio library, use this to record, play, generate wav files and so on. PyAudio provides PortAudio
Python language version of, this is a cross-platform audio I/O library, using PyAudio you can in Python program
Play and record audio in sequence. Python bindings for PoTaTudio, cross-platform audio I/O library. 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。
2.2 Configuration environment?
This article covers environments: Python 3, Pycharm, cocos2d modules;pyaudio modules; and some Python modules that come with Python.
Module installation:
pip install + module name or with Douban mirror source pip install -i https://pypi.douban.com/simple/ + module name
? 3. Formal knock code 3.1 Definition of small black eight note class:
Using the cocos2d module to define protagonist classes is a very easy task. You just need to inherit the sprites class and tell sprites
Class needs to perform. According to the eight-note rules of the game, we give Blackie the ability to jump, fall, and stop.
We made it a rule that Blackie couldn't jump while he was in the air. The specific codes are as follows:
import cocos '''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) #Update self.schedule(self.update) '''Voice-activated 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 '''Still after landing ''' def land(self, y): if self.y > y - 25: self.is_able_jump = True self.speed = 0 self.y = y '''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 #Location self.position = 80, 2803.2 Define block class: ?
initialization interface black to have a standing area, for the ground, at the beginning of the first time there must be a long point of the ground buffer,
Let the player try out his voice first, and then randomly generate jump blocks to let the player show his voice.
The specific codes are as follows:
import cocosimport 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, 03.3 Implement the main loop of the game: ? import cfgimport cocosimport structfrom modules import *from cocos.sprite import Spritefrom pyaudio import PyAudio, paInt16 '''Define voice game classes''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 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 #Voice 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 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()))? IV. Effect display
At this point, the study of "Python dry goods actual combat how to realize eight-note sauce Mini games" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.