var canvas = document.getElementById('flappy_square_stage5_challenge4');
var context = canvas.getContext('2d');
var interval;
var distance = 0;
var gravity = 1;
var boundary = {
minX: 25,
minY: 25,
width: 425,
height: 275
};
var square = {
x: 25,
y: 75,
size: 15,
yVelocity: 0,
jump: -10
};
var wall = {
width: 50,
minHole: 70,
maxHole: 120,
positions: []
};
function drawBoundary() {
context.strokeRect(0, 0, boundary.width, boundary.height);
}
function drawSquare() {
context.save();
context.fillStyle = 'red';
context.fillRect(square.x, square.y, square.size, square.size);
context.restore();
}
function drawWall(wallInfo) {
context.save();
context.fillStyle = 'green';
context.translate(wallInfo.x - distance, 0);
context.fillRect(
0,
0,
wall.width,
wallInfo.topHeight
);
context.fillRect(
0,
boundary.height - wallInfo.bottomHeight,
wall.width,
wallInfo.bottomHeight
);
context.restore();
}
function random(min, max) {
return (min + Math.floor(Math.random() * (max - min)));
}
function drawWalls() {
var wallX = square.x;
if (wall.positions.length > 0) {
wallX = wall.positions[wall.positions.length - 1].x;
}
var end = boundary.width + distance + wall.width;
while (wallX < end) {
wallX += random(100, 200);
var topHeight = random(25, 150);
var bottomHeight = random(25, 150);
if (boundary.height - topHeight - bottomHeight < wall.minHole) {
bottomHeight = boundary.height - topHeight - wall.minHole;
}
if (boundary.height - topHeight - bottomHeight > wall.maxHole) {
bottomHeight = boundary.height - topHeight - wall.maxHole;
}
wall.positions.push({
x: wallX,
topHeight: topHeight,
bottomHeight: bottomHeight
});
}
for (var i=0; i < wall.positions.length; ++i) {
drawWall(wall.positions[i]);
}
}
function flap() {
square.yVelocity = square.jump;
}
function adjustPosition() {
distance += 2;
square.yVelocity += gravity;
square.y += square.yVelocity;
}
function clearBoundary() {
var maxX = boundary.minX + boundary.width;
var maxY = boundary.minY + boundary.height;
context.clearRect(0, 0, canvas.width, boundary.minY);
context.clearRect(maxX, 0, canvas.width - maxX, canvas.height);
context.clearRect(0, maxY, canvas.width, canvas.height - maxY);
context.clearRect(0, 0, boundary.minX, canvas.height);
}
function checkBoundary() {
if (square.y >= boundary.height) {
endGame();
}
}
function checkWalls() {
var left = square.x;
var right = square.x + square.size;
var top = square.y;
var bottom = square.y + square.size;
for (var i=0; i < wall.positions.length; ++i) {
var wallInfo = wall.positions[i];
var wallLeft = wallInfo.x - distance;
var wallRight = wallLeft + wall.width;
if (wallLeft > right) continue;
if (wallRight < left) continue;
var topWallBottom = wallInfo.topHeight;
var bottomWallTop = boundary.height - wallInfo.bottomHeight;
if (top < topWallBottom || bottom > bottomWallTop) {
endGame();
}
}
}
function drawTitle() {
context.save();
context.font = "15px serif";
context.textAlign = 'left';
context.textBaseline = 'bottom';
var x = boundary.minX;
var y = boundary.minY;
context.fillText('Flappy Square', x, y);
context.restore();
}
function drawScore() {
context.save();
context.font = "15px serif";
context.textAlign = 'right';
context.textBaseline = 'bottom';
var x = boundary.minX + boundary.width;
var y = boundary.minY;
context.fillText('Score: ' + Math.floor(distance / 20), x, y);
context.restore();
}
function endGame() {
context.font = "20px serif";
context.textAlign = 'center';
var xCenter = boundary.width / 2;
var yCenter = boundary.height / 2;
context.fillText('Game Over', xCenter, yCenter);
pauseAnimation();
}
function programSteps() {
context.clearRect(0, 0, canvas.width, canvas.height);
adjustPosition();
context.save();
context.translate(boundary.minX, boundary.minY);
drawBoundary();
drawSquare();
drawWalls();
checkWalls();
checkBoundary();
context.restore();
clearBoundary();
drawTitle();
drawScore();
}
function runProgram() {
interval = setInterval(programSteps, 50);
}
canvas.addEventListener('click', flap);
// The following code is provided for you.
// It creates an eventListener that listens
// for the canvas to come into "focus", which
// happens when you click on it.
// This allows us to stop and start each individual
// animation on this whole page separately.
function startAnimation() {
runProgram();
}
function pauseAnimation() {
clearInterval(interval);
}
canvas.addEventListener('focus', startAnimation);
canvas.addEventListener('blur', pauseAnimation);
canvas.focus();