Skip to content

Instantly share code, notes, and snippets.

@writingdeveloper
Created November 20, 2017 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save writingdeveloper/85a2c76a510add28e84c1600d4e70aa7 to your computer and use it in GitHub Desktop.
Save writingdeveloper/85a2c76a510add28e84c1600d4e70aa7 to your computer and use it in GitHub Desktop.
// Check Collision
var checkCollision = function(anEnemy) {
// check for collision between enemy and player
if (
player.y + 131 >= anEnemy.y + 90 &&
player.x + 25 <= anEnemy.x + 88 &&
player.y + 73 <= anEnemy.y + 135 &&
player.x + 76 >= anEnemy.x + 11) {
console.log('collided');
// Position player after Collision
player.x = 202.5;
player.y = 383;
}
// check for player reaching top of canvas and winning the game
if (player.y + 63 <= 0) {
// if player wins, add 1 to the score and level
player.x = 202.5;
player.y = 383;
console.log('you made it!');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 505, 350);
// pass score as an argument to the increaseDifficulty function
score += 1;
gameLevel += 1;
console.log('current score: ' + score + ', current level: ' + gameLevel);
increaseDifficulty(score);
}
// check if player runs into left, bottom, or right canvas walls
// prevent player from moving beyond canvas wall boundaries
if (player.y > 383) {
player.y = 383;
}
if (player.x > 402.5) {
player.x = 402.5;
}
if (player.x < 2.5) {
player.x = 2.5;
}
};
// Increase number of enemies on screen based on player's score
var increaseDifficulty = function(numEnemies) {
// remove all previous enemies on canvas
allEnemies.length = 0;
// load new set of enemies
for (var i = 0; i <= numEnemies; i++) {
var enemy = new Enemy(0, Math.random() * 260 + 50, Math.random() * 256);
var enemy_rock = new Enemy_Rock(0, Math.random() * 260 + 50, Math.random() * 256);
allEnemies.push(enemy, enemy_rock);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment