不要停,八分音符酱

不要停,八分音符酱

最近微博特别火的一个小游戏,看视频都看的笑尿。

然后在一个开源社区看到了有个大神仅仅用了100行代码就实现了这款小游戏,然后我就拿来学习学习。

python代码

1,game.py

#coding: utf8
import cocos
from cocos.sprite import Sprite
from pyaudio import PyAudio, paInt16
import struct
from ppx import PPX
from block import Block

class VoiceGame(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)

        self.logo = cocos.sprite.Sprite('123.jpg')
        self.logo.position = 550, 400
        self.add(self.logo, 99999)

        # init voice
        self.NUM_SAMPLES = 1000  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX()
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        for i in range(100):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=self.NUM_SAMPLES)

        self.schedule(self.update)

    def on_mouse_press(self, x, y, buttons, modifiers):
        pass

    def collide(self):
        px = self.ppx.x - self.floor.x
        for b in self.floor.get_children():
            if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                if self.ppx.y < b.height:
                    self.ppx.land(b.height)
                    break

    def update(self, dt):
        # 读入NUM_SAMPLES个取样
        string_audio_data = self.stream.read(self.NUM_SAMPLES)
        k = max(struct.unpack('1000h', string_audio_data))
        # print k
        self.voicebar.scale_x = k / 10000.0
        if k > 3000:
            self.floor.x -= min((k / 20.0), 150) * dt
        if k > 8000:
            self.ppx.jump((k - 8000) / 1000.0)
        self.collide()

    def reset(self):
        self.floor.x = 0


cocos.director.director.init(caption="Let's Go! PiPiXia!")
cocos.director.director.run(cocos.scene.Scene(VoiceGame()))

2,ppx.py

# -*- coding: utf-8 -*-
import math
import random
import cocos

class PPX(cocos.sprite.Sprite):
    def __init__(self):
        super(PPX, self).__init__('ppx.png')
        self.can_jump = False
        self.speed = 0
        self.image_anchor = 0, 0
        self.position = 100, 300
        self.schedule(self.update)

    def jump(self, h):
        if self.can_jump:
            self.y += 1
            self.speed -= max(min(h, 10), 7)
            self.can_jump = False

    def land(self, y):
        if self.y > y - 30:
            self.can_jump = True
            self.speed = 0
            self.y = y

    def update(self, dt):
        self.speed += 10 * dt
        self.y -= self.speed
        if self.y < -80:
            self.reset()

    def reset(self):
        self.parent.reset()
        self.can_jump = False
        self.speed = 0
        self.position = 100, 300

3,black.py

# -*- coding: utf-8 -*-
import math
import random
import cocos

class Block(cocos.sprite.Sprite):
    def __init__(self, pos):
        super(Block, self).__init__('black.png')
        self.image_anchor = 0, 0
        x, y = pos
        if x == 0:
            self.scale_x = 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

其实实现思路也很简单,随机生成的障碍物(black),无线循环。black实际上是在向左边移动。

运动的可以换成自己的图片,哈哈哈,尖叫吧。

附上原作者的代码链接。

http://git.oschina.net/crossin/learn-python/tree/master/voicegame

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值