xxxxxxxxxx
114
/* 説明
最初にプレビュー画面をクリックしてペアリング!
操作方法
↑:前進
↓:後退
←:左旋回
→:右旋回
a:toio移動開始
b:音楽再生
c:底部ライト色変更
*/
const s = 30; //移動スピード
const d = 50; //移動継続時間
let i = 0;
const colorArray = ["white","red","green","orange","blue","yellow"]
const gCubes = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
textSize(64);
stroke("black");
text("Click Here!",20,200);
}
function mouseClicked() {
P5tCube.connectNewP5tCube().then((cube) => {
gCubes.push(cube);
cube.turnLightOn("white");
});
}
function sleep(millis){
const t1 = Date.now();
while(Date.now()-t1 < millis){
console.log(Date.now()-t1);
}
}
// == メソッド・関数説明 ==
//.move()メソッド
// 第一引数:左車輪の速度 (-115〜-8,0,8〜115)
// 第二引数:右車輪の速度 (-115〜-8,0,8〜115)
// 第三引数:継続時間、単位10msec (100を設定すると1秒) (0〜255)(0は永遠に継続)
// sleep()関数
// 引数:単位msec
// ======= runToio()関数内のみを変更する =======
function runToio(){
gCubes[0]?.move(40, 40, 140);
sleep(3000);
gCubes[0]?.move(0, 40, 40);
sleep(3000);
gCubes[0]?.move(40, 40, 50);
sleep(3000);
gCubes[0]?.move(40, 0, 40);
sleep(3000);
gCubes[0]?.move(-40, -40, 100);
sleep(3000);
}
// ======= runToio()関数内のみを変更する =======
function keyPressed() {
console.log(keyCode);
switch (keyCode) {
case UP_ARROW:
gCubes[0]?.move(s, s, d);
break;
case RIGHT_ARROW:
gCubes[0]?.move(s, 0, d);
break;
case LEFT_ARROW:
gCubes[0]?.move(0, s, d);
break;
case DOWN_ARROW:
gCubes[0]?.move(-s, -s, d);
break;
case 65: // a
runToio();
break;
case 66: // b
gCubes[0]?.playMelody([
{ note: 0x3c, duration: 0x1e },
{ note: 0x3e, duration: 0x1e },
{ note: 0x40, duration: 0x1e },
{ note: 0x41, duration: 0x1e },
{ note: 0x43, duration: 0x1e },
{ note: 0x45, duration: 0x1e },
{ note: 0x47, duration: 0x1e },
{ note: 0x48, duration: 0x1e },
]);
break;
case 67: // c
i = i % 6;
gCubes[0]?.turnLightOn(colorArray[i]);
i++;
break;
default:
break;
}
}