Introduction: Obstacle Avoidance Game With Distance Sensor
Obstacle avoidance game like Flappy Bird. Move your hand to avoid the collision. It is easy to make and fun to play!
Step 1: Things Used in This Project
Step 2: Setting Up Obniz
Step 3:
Connect obniz and the distance sensor as below.
- io0 : Signal(yellow)
- io1 : GND(black)
- io2 : VCC(red)
Step 4:
We use HTML5 canvas.
<canvas id="field" width="300" height="300"/>
let canvas = document.getElementById('field');
let ctx = canvas.getContext('2d');
Set value of distance sensor to var "inputHeight", and use it anytime.
let inputHeight = 0;
let obniz = new Obniz("OBNIZ_ID_HERE"); obniz.onconnect = async function () { let sensor = obniz.wired("GP2Y0A21YK0F", {vcc: 2, gnd: 1, signal: 0}); sensor.start(function (height) { inputHeight = height; }) };
{vcc: 2, gnd: 1, signal: 0} should be changed if you connect devices different from Step 2.
This value is used as each frame input.
let input = (300 - inputHeight);
input = Math.min(Math.max(0, input), canvas.height); dot.push(input);
Step 5: Program
Please get the program from here