Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions meteorite_fall.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ const PLAYER_WIDTH = 10;
const PLAYER_HEIGHT = 10;
const PLAYER_SPEED = 10;
const PLAYER_COLOR = "rgb(89, 179, 0)";
const METEORITE_COLOR = "rgb(0, 0, 0)";
const METEORITE_COLOR_NORMAL = "rgb(0, 0, 0)";
const METEORITE_COLOR_POINT = "rgb(255, 0, 0)";
const PLAYER_SPAWN_X = CANVAS_WIDTH / 2;
const PLAYER_SPAWN_Y = CANVAS_HEIGHT - PLAYER_WIDTH;
// METEORITE CONSTANTS
const METEORITE_FALL_SPEED = 5;
const METEORITE_WIDTH = 10;
const METEORITE_HEIGHT = 40;
const METEORITE_SPAWN_Y = 0;

// GAME VARIABLES
let meteorites = new Array();

let isGameOver = false;
let player = new Player(PLAYER_SPAWN_X, PLAYER_SPAWN_Y, PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_SPEED, PLAYER_COLOR);
let paused = false;
let score = 0;

function main() {
initializeMeteorites();
Expand All @@ -43,11 +44,15 @@ function loopGame() {

for (i = 0; i < meteorites.length; i++) {
if (checkCollision(meteorites[i], player)) {
isGameOver = true;
context.clearRect(0,0, CANVAS_WIDTH, CANVAS_HEIGHT);
resetMeteorites();
alert("Game Over!");
break;
if (meteorites[i].color == METEORITE_COLOR_POINT) {
score++;
} else {
isGameOver = true;
context.clearRect(0,0, CANVAS_WIDTH, CANVAS_HEIGHT);
resetMeteorites();
alert("Game Over!");
break;
}
}
if (meteorites[i].isFalling == true) {
meteorites[i].draw(context);
Expand All @@ -60,14 +65,21 @@ function loopGame() {
}

player.draw(context);

context.fillStyle = "black";
context.font = "12px sans-serif";
context.textAlign = "start";
context.textBaseline = "top";
context.fillText("score: " + score, 10, 10);
}

// fill meteorites array with all possible meteorites in the canvas width
function initializeMeteorites() {
let numberOfMeteorites = parseInt(CANVAS_WIDTH / METEORITE_WIDTH);

for (arrayIndex = 0, xPositionValue = 0; arrayIndex < numberOfMeteorites; arrayIndex++, xPositionValue += METEORITE_WIDTH) {
meteorites[arrayIndex] = new Meteorite(xPositionValue, METEORITE_SPAWN_Y, METEORITE_HEIGHT, METEORITE_WIDTH, METEORITE_COLOR, METEORITE_FALL_SPEED);
meteoriteColor = Math.random() > 0.5 ? METEORITE_COLOR_POINT : METEORITE_COLOR_NORMAL;
meteorites[arrayIndex] = new Meteorite(xPositionValue, METEORITE_SPAWN_Y, METEORITE_HEIGHT, METEORITE_WIDTH, meteoriteColor, METEORITE_FALL_SPEED);
}
}

Expand All @@ -90,9 +102,10 @@ document.addEventListener('keydown', (e) => {
if (e.key == " ") {
paused = !paused;
if (paused) {
context.fillStyle = "red";
context.fillStyle = "rgb(0, 0, 255)";
context.font = "bold 36px sans-serif";
context.textAlign = "center";
context.textBaseline = "alphabetic";
context.fillText("Paused", CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2);
}
}
Expand All @@ -104,4 +117,4 @@ document.addEventListener('keydown', (e) => {
});

// starting game
main();
main();