xxxxxxxxxx
86
let state = false;
let dirPick = '';
let startTime;
function setup() {
createCanvas(400, 400);
//draw start/end buttons that change state
let buttonStart = createButton("START");
buttonStart.mousePressed(startGame);
let buttonEnd = createButton("END");
buttonEnd.mousePressed(endGame);
textSize(floor(width/20, 0));
textAlign(CENTER);
//initialize starting time
startTime = frameCount;
//startTime = second(); //ver.using second()
}
function draw() {
//draw screen only if game is running (state == true)
background(220);
if (state == true) {
drawScreen();
}
}
function startGame() {
state = true;
}
function endGame() {
state = false;
}
//draws screen of arrows + seconds left
function drawScreen() {
//definition of possible directions to face
let dir = ["left", "right", "up", "down"];
let currentTime = frameCount;
//let currentTime = second(); //ver. using second()
//console.log(startTime, currentTime);
//pick new direction every 3 seconds
if(currentTime - startTime >= 180) //frameRate default: 60 frames per second
//if(currentTime - startTime >= 3) //ver. using second()
{
dirPick = random(dir);
//console.log(dirPick);
startTime = currentTime;
}
//count down from 3 seconds
text(3-floor(frameCount/60, 0)%3, width/2, height/5);
//draw arrows
if (dirPick == "left") {
//left arrow
line(width / 3, height / 2, (width * 2) / 3, height / 2);
line(width / 3, height / 2, (width * 4) / 9, (height * 3) / 8);
line(width / 3, height / 2, (width * 4) / 9, (height * 5) / 8);
} else if (dirPick == "right") {
//right arrow
line(width / 3, height / 2, (width * 2) / 3, height / 2);
line((width * 2) / 3, height / 2, (width * 5) / 9, (height * 3) / 8);
line((width * 2) / 3, height / 2, (width * 5) / 9, (height * 5) / 8);
} else if (dirPick == "up") {
//up arrow
line(width / 2, height / 3, width / 2, (height * 2) / 3);
line(width / 2, height / 3, (width * 3) / 8, (height * 4) / 9);
line(width / 2, height / 3, (width * 5) / 8, (height * 4) / 9);
} else if (dirPick == "down") {
//down arrow
line(width / 2, height / 3, width / 2, (height * 2) / 3);
line(width / 2, (height * 2) / 3, (width * 3) / 8, (height * 5) / 9);
line(width / 2, (height * 2) / 3, (width * 5) / 8, (height * 5) / 9);
}
else {
background(220);
}
}