// create the array of star colors so we can use them later
var starArray = ["red", "blue", "green", "violet", "yellow"];
// create the target that the player will chase
var target = "red";
var score = 0;
//randomly choose a star color from the star array to be the target
chooseTarget();
//run the program as the accelerometer updates
onBoardEvent(accelerometer, "data", function() {
movePlayer();
loopStars();
checkStars();
});
// loops all the stars around the screen
function loopStars() {
for (var i = 0; i < starArray.length; i++) {
loopStar(starArray[i]);
}
}
// checks all the stars for collision with the player
function checkStars() {
for (var i = 0; i < starArray.length; i++) {
checkStar(starArray[i]);
}
}
// send the red star down the screen, then loop it back to the top
function loopStar(color) {
// move the star down the screen
setProperty(color, "y", getProperty(color, "y") + 1);
// if the star is off the bottom of the screen, move it to the top
if (getProperty(color, "y") > 450) {
setProperty(color, "y", -50);
}
}
// check the red star for whether the player has reached it
function checkStar(color) {
// if the player is touching the star, move the star
if (detectHit(color)) {
moveStar(color);
// if the star is the target star, add a point to the score and choose a new target
if (target == color) {
getOnePoint();
chooseTarget();
}
}
}
// move the red star to a random location on the screen
function moveStar(color) {
setProperty(color, "x", randomNumber(0,275));
setProperty(color, "y", randomNumber(0,200));
}
// choose a new target and change the LED color
function chooseTarget() {
// randomly choose one target color from the list
target = starArray[randomNumber(0, starArray.length - 1)];
// make all the color LEDs the target color
for (var i=0; i<colorLeds.length; i++) {
colorLeds[i].color(target);
}
}
//detect whether the player is hitting the star of the given color
function detectHit(color) {
return (Math.abs(getProperty("player","x") - getProperty(color, "x")) < 50) && (Math.abs(getProperty("player","y") - getProperty(color, "y")) < 50);
}
// add a point and update the score board
function getOnePoint() {
score = score + 1;
setProperty("score", "text", score);
}
// moves the player according to the accelerometer data
function movePlayer() {
setProperty("player", "x", accelerometer.getOrientation("roll") + getProperty("player", "x"));
setProperty("player", "y", accelerometer.getOrientation("pitch") + getProperty("player", "y"));
}