xxxxxxxxxx
92
var serial_values = [0];
var serial = new Serial();
var pos = {
x: 0,
y: 0,
};
var pos_goal = {
x: 0,
y: 0,
};
var mode = 0; // 0: ゲーム中, 1:ゴール
var music;
function preload(){
music = loadSound('MusicBox_01.mp3');
}
function setup() {
createCanvas(400, 400);
pos_goal.x = random(20, width - 20);
pos_goal.y = random(20, height - 20);
music.play();
}
function draw() {
background(150);
rectMode(CENTER);
if (mode == 0) {
// ゴールを描画
fill(0);
ellipse(pos_goal.x, pos_goal.y, 20, 20);
// 画面の中心座標を計算
var centerX = width / 2;
var centerY = height / 2;
pos.x = pos.x - a.y / 10.0;
pos.y = pos.y - a.x / 10.0;
if (pos.x > width) pos.x = width;
if (pos.x <= 0) pos.x = 0;
if (pos.y > height) pos.y = height;
if (pos.y <= 0) pos.y = 0;
// 円を描く
fill(255);
ellipse(pos.x, pos.y, 20, 20);
// ゴールと円の距離を計算する
let d = dist(pos.x, pos.y, pos_goal.x, pos_goal.y);
if (d < 5) {
// ゴール!!
console.log("ゴール!!");
mode = 1;
}
}
else if( mode == 1){
background(0);
fill(255);
textSize(48);
textAlign(CENTER, CENTER);
text('Goal',width/2, height/2);
}
}
var flg = "none";
var a = { x: 0, y: 0, z: 0 };
function gotSerialValues(values) {
serial_values = values;
if (serial_values.length > 0) {
for (let sv of serial_values) {
if (sv == 211) {
// ax
flg = "ax";
} else if (sv == 212) {
// ay
flg = "ay";
} else if (sv == 213) {
// az
flg = "az";
} else {
// ax or ay or azのデータ
if (flg == "ax") a.x = sv - 100;
if (flg == "ay") a.y = sv - 100;
if (flg == "az") a.z = sv - 100;
flg = "none";
}
}
}
}