zoukankan      html  css  js  c++  java
  • 声控皮卡丘小游戏

    每天练习一点点~~

    参考文章
    百度云下载(密码rwlh)
    github地址

    不想看的直接看我贴代码吧.
    代码层次:

    1.pikachu类生成持续运动的pikachu

    import cocos
    import os
    
    
    class Pikachu(cocos.sprite.Sprite):
        # 实现一个皮卡丘类来生成皮卡实例
        def __init__(self):
            super(Pikachu, self).__init__('pikachu.png')
            # 是否可以跳跃
            self.jumpable = False
            # 行进速度
            self.speed = 0
            # 锚点
            self.image_anchor = 0, 0
            # 皮卡丘的位置
            self.position = 80, 280
            self.schedule(self.update)
    
        def jump(self, h):
            # 声控跳跃
            if self.jumpable:
                self.y += 1
                self.speed -= max(min(h, 10), 7)
                self.jumpable = False
    
        def land(self, y):
            # 着陆后静止
            if self.y > y - 25:
                self.jumpable = True
                self.speed = 0
                self.y = y
    
        def update(self, dt):
            # 更新(重力下降)
            self.speed += 10 * dt
            self.y -= self.speed
            if self.y < -85:
                self.reset()
    
        def reset(self):
            self.parent.reset()
            self.jumpable = False
            self.speed = 0
            self.position = 80, 280
    
    

    2.block类生成地图上随机出现的黑色柱子

    import os
    import cocos
    import random
    
    
    class Block(cocos.sprite.Sprite):
        # 生成地图上的随机块状物
        def __init__(self, position):
            super(Block, self).__init__('black.png')
    
            # 锚点
            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
                self.position = x + 50 + random.random() * 100, 0
    
    
    

    3.程序主逻辑入口函数

    import os
    import cocos
    import struct
    from cocos.sprite import Sprite
    from pyaudio import PyAudio, paInt16
    from classes.pikachu import Pikachu
    from classes.block import Block
    
    
    class VCGame(cocos.layer.ColorLayer):
        # 定义声音控制类Voice Control Class
        is_event_handler = True
    
        def __init__(self):
            super(VCGame, self).__init__(255, 255, 255, 255, 800, 600)
            # 初始化参数
            # frames_per_buffer
            self.numSamples = 1000
            # 声控条
            self.vbar = Sprite('black.png')
            self.vbar.position = 20, 450
            self.vbar.scale_y = 0.1
            self.vbar.image_anchor = 0, 0
            self.add(self.vbar)
    
            # 皮卡丘类
            self.pikachu = Pikachu()
            self.add(self.pikachu)
    
            # cocosnode精灵类
            self.floor = cocos.cocosnode.CocosNode()
            self.add(self.floor)
            position = 0, 100
    
            for i in range(120):
                b = Block(position)
                self.floor.add(b)
                position = b.x + b.width, b.height
    
            # 声音输入
            audio = PyAudio()
            SampleRate = int(audio.get_device_info_by_index(0)['defaultSampleRate'])
            self.stream = audio.open(format=paInt16,
                                     channels=1,
                                     rate=SampleRate,
                                     input=True,
                                     frames_per_buffer=self.numSamples)
            self.schedule(self.update)
    
            # 碰撞检测
    
        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.
                    if self.pikachu.y < b.height:
                        self.pikachu.land(b.height)
                        break
    
            # 定义游戏规则
    
        def update(self, dt):
            # 获取每帧的音量
            audio_data = self.stream.read(self.numSamples)
            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
            if k > 8000:
                self.pikachu.jump((k - 8000) / 1000.0)
            self.collide()
    
            # 重置
    
        def reset(self):
            self.floor.x = 0
    
    
    if __name__ == '__main__':
        cocos.director.director.init(caption="Pikachu~~~")
        cocos.director.director.run(cocos.scene.Scene(VCGame()))
    
    
  • 相关阅读:
    简记配置tesorflow-gpu环境(吐血了)
    个人总结
    浪潮之巅阅读笔记03
    第十六周的学习进度条
    第十五周的学习进度条
    第十四的周学习进度条
    课堂练习-购书问题
    第二阶段—个人工作总结10
    第二阶段—个人工作总结09
    第二阶段—个人工作总结08
  • 原文地址:https://www.cnblogs.com/xiujin/p/11479141.html
Copyright © 2011-2022 走看看