聲控皮卡丘小遊戲

2022-08-15 16:33:18 字數 3917 閱讀 3837

參考文章

github位址

不想看的直接看我貼**吧.

**層次:

# 實現乙個皮卡丘類來生成皮卡例項

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

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

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.width:

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()))

皮卡丘的夢想

problem description 一天,乙隻住在 501 的皮卡丘決定發奮學習,成為像 leiq 一樣的巨巨,於是他向鎮上的賢者金桔請教如何才能進化成乙隻雷丘。金桔告訴他需要進化石才能進化,並給了他乙個地圖,地圖上有 n 個小鎮,並且標註了每個小鎮上可收集的進化石。但是皮卡丘拿到地圖就矇圈了,...

css畫皮卡丘

不知道有沒有機會做你的皮卡丘,舉高高,要抱抱 這才是猛男該看的東西 doge gitee 預覽 搜尋 pikachu 1 border radius 可以實現彎曲的鬍鬚,並且使用偽元素覆蓋顏色。在畫皮卡丘的過程中,最難的應該就是彎曲的鬍鬚的那一部分了。mouth up lip.left mouth ...

皮卡丘的夢想2

皮卡丘的夢想2 time limit 1000ms memory limit 65536kb submit statistic discuss problem description 一天,乙隻住在 501 實驗室的皮卡丘決定發奮學習,成為像 leiq 一樣的巨巨,於是他向鎮上的賢者金桔請教如何才能...