This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enemies our player must avoid | |
var Enemy = function(x, y, speed) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
// The sprite continuously crops and displays the image. | |
this.sprite = 'images/enemy-bug.png'; | |
}; | |
var Enemy_Rock = function(x, y, speed) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enemy_Rock.prototype.update = function(dt) { | |
this.x += this.speed * dt; | |
if (this.x >= 505) { | |
this.x = 0; | |
} | |
checkCollision(this); | |
}; | |
Enemy_Rock.prototype.render = function() { | |
ctx.drawImage(Resources.get(this.sprite), this.x, this.y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definition Player | |
var Player = function(x, y, speed) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
this.sprite = 'images/char-boy.png'; | |
}; | |
Player.prototype.update = function() {} | |
Player.prototype.render = function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// KeyPress Settings | |
Player.prototype.handleInput = function(keyPress) { | |
if (keyPress == 'left') { | |
player.x -= player.speed; | |
} | |
if (keyPress == 'up') { | |
player.y -= player.speed - 20; | |
} | |
if (keyPress == 'right') { | |
player.x += player.speed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Display Score | |
var displayScoreLevel = function(aScore, aLevel) { | |
var canvas = document.getElementsByTagName('canvas'); | |
var firstCanvasTag = canvas[0]; | |
scoreLevelDiv.innerHTML = 'Score : ' + aScore + ' / ' + 'Level : ' + aLevel; | |
document.body.insertBefore(scoreLevelDiv, firstCanvasTag[0]); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initial setting | |
var allEnemies = []; | |
var player = new Player(202.5, 383, 100); | |
var score = 0; | |
var gameLevel = 1; | |
var scoreLevelDiv = document.createElement('div'); | |
var enemy = new Enemy(0, Math.random() * 184 + 50, Math.random() * 256); | |
var enemy_rock = new Enemy_Rock(0, Math.random() * 184 + 50, Math.random() * 256); | |
allEnemies.push(enemy, enemy_rock); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This listens for key presses and sends the keys to your | |
// Player.handleInput() method. You don't need to modify this. | |
document.addEventListener('keyup', function(e) { | |
var allowedKeys = { | |
37: 'left', | |
38: 'up', | |
39: 'right', | |
40: 'down' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enemies our player must avoid | |
var Enemy = function(x, y, speed) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
// The sprite continuously crops and displays the image. | |
this.sprite = 'images/enemy-bug.png'; | |
}; | |
var Enemy_Rock = function(x, y, speed) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enemy_Rock.prototype.update = function(dt) { | |
this.x += this.speed * dt; | |
if (this.x >= 505) { | |
this.x = 0; | |
} | |
checkCollision(this); | |
checkWin(this); | |
}; | |
OlderNewer