Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- }
- };
Add Comment
Please, Sign In to add comment