Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cd65e9c
Initial commit
nicolasartman Sep 23, 2014
4da07d3
Initial project commit.
Sep 23, 2014
3a00b6e
Include rubric
durant-udacity Oct 2, 2014
c6ec879
Removed double Resources.get() bug in Enemy class
Oct 6, 2014
dafbd81
Moved *.js files to js/ directory, moved *.css files to css/ directory
Oct 14, 2014
35fbaab
Added additional comments to js/resources.js
Nov 5, 2014
a5e589a
Additional comments added to engine.js
Nov 5, 2014
eb13d10
Removed extraneous patterns var
Nov 5, 2014
65bb192
Fix typos in comments in JavaScript files.
6stringbeliever Dec 19, 2014
7acb20c
Fixed some minor typos in the comments of the resources file.
cherylcourt Dec 19, 2014
edc2a70
Minor JavaScript syntax corrections
jmorenor Mar 16, 2015
6803116
Update engine.js
jmorenor Mar 16, 2015
87d4e88
Declare character encoding
bcuz Mar 25, 2015
93c1156
Fixed link to rubric and added link for getting started guide
siakaramalegos Apr 3, 2015
96c210e
Delete redundant spaces
yyforyongyu Aug 4, 2015
10d0c46
Update app.js
susansmith Aug 22, 2015
d23682b
Spelling: It's -> Its, developer's -> developers; Extra spaces; Extra…
rhynodesigns Oct 5, 2015
b74ff04
Spelling: developer's -> developers; Use 'properly' like the next com…
rhynodesigns Oct 5, 2015
7251524
Added embedded param to guide link
walesmd Dec 3, 2015
0d2bd86
update rubric link
hkasemir May 27, 2016
a356217
Update README.md
susansmith Jun 22, 2016
b63b1e4
Update README.md
susansmith Jun 22, 2016
06f63d8
Game is complete
jnware7 Feb 24, 2017
e65e94a
fixed controler bugs
jnware7 Feb 27, 2017
a5b33ba
Merge pull request #1 from jnware7/NewBranch
jnware7 Feb 27, 2017
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


# Core Object Oriented JavaScript

A day-by-day structured deep dive into object-oriented JavaScript. Read articles, watch videos, complete lessons and exercises, and implement a final project.
Expand Down Expand Up @@ -121,3 +123,10 @@ Do you know/understand:
[ninja]:https://github.com/GuildCrafts/core-object-oriented-javascript/raw/master/Books/Secrets%20of%20the%20JavaScript%20Ninja%20-%20John%20Resig%20and%20Bear%20Bibeault%20-%20December%202012.pdf
[cheat]:https://github.com/GuildCrafts/core-object-oriented-javascript/raw/master/Books/Objects-Cheat-Sheet.pdf
[oojs]:ftp://ftp.micronet-rostov.ru/linux-support/books/programming/JavaScript/[Packt]%20-%20Object-Oriented%20JavaScript%20-%20[Stefanov].pdf

frontend-nanodegree-arcade-game


Students should use this [rubric](https://review.udacity.com/#!/projects/2696458597/rubric) for self-checking their submission. Make sure the functions you write are **object-oriented** - either class functions (like Player and Enemy) or class prototype functions such as Enemy.prototype.checkCollisions, and that the keyword 'this' is used appropriately within your class and class prototype functions to refer to the object the function is called upon. Also be sure that the **readme.md** file is updated with your instructions on both how to 1. Run and 2. Play your arcade game.

For detailed instructions on how to get started, check out this [guide](https://docs.google.com/document/d/1v01aScPjSWCCWQLIpFqvg3-vXLH2e8_SZQKC8jNO0Dc/pub?embedded=true).
3 changes: 3 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
text-align: center;
}
Binary file added images/Gem Blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Gem Green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Gem Orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Rock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Selector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/char-boy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/char-cat-girl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/char-horn-girl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/char-pink-girl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/char-princess-girl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/enemy-bug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grass-block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/stone-block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/water-block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Effective JavaScript: Frogger</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="js/resources.js"></script>
<script src="js/app.js"></script>
<script src="js/engine.js"></script>
</body>
</html>
128 changes: 128 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Enemies our player must avoid


var Enemy = function() {
// Variables applied to each of our instances go here,
// we've provided one for you to get started

// The image/sprite for our enemies, this uses
// a helper we've provided to easily load images
this.sprite = 'images/enemy-bug.png';

this.xRange = [-150,600]
this.yRange = [60,140,220]
this.speed = (Math.floor(Math.random()*3)+1) * 83

this.reset()

};

Enemy.prototype.reset = function(){
var startPosition = this.xRange[0]
this.x = startPosition
this.y = this.getRandomY()

}

// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function(dt) {
// You should multiply any movement by the dt parameter
// which will ensure the game runs at the same speed for
// all computers.
this.x += dt * this.speed
var maxPosition = this.xRange[1]

if(this.x > maxPosition){
this.reset()
}
};

Enemy.prototype.getRandomY = function (){
return this.yRange[Math.floor(Math.random()*this.yRange.length)]
}

// Draw the enemy on the screen, required method for game
Enemy.prototype.render = function() {
console.log(this);
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};

Enemy.prototype.getRandomSpeed = function(){
return this.speed
}

// Now write your own player class
// This class requires an update(), render() and
// a handleInput() method.
var Player = function() {
this.sprite = 'images/char-boy.png'

this.yRange = [-2,402]
this.xRange = [-20,380]

this.reset()
}

Player.prototype.reset = function(){
this.x = 200
this.y = 380
}

Player.prototype.checkCollisions = function(){
if(this.y == -20){
alert('YOU WIN!!')
this.reset()
}else if(this.y >= 60 && this.y <= 220) {
var self = this
allEnemies.forEach(function(enemy){
if(enemy.y == self.y){
if(enemy.x >= player.x -30 && enemy.x <= player.x + 30){
self.reset()
}
}
})
}
}

Player.prototype.update = function(dt){
this.checkCollisions()
}

Player.prototype.render = function() {
console.log(this);
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};


Player.prototype.handleInput = function(allowedKeys){
if(allowedKeys == 'left'){
this.x -=(this.x - 100 < this.xRange[0]) ? 0 : 100
}else if(allowedKeys == 'right'){
this.x +=(this.x + 100 > this.xRange[1]) ? 0 : 100
}else if(allowedKeys == 'up'){
this.y -=(this.x - 80 < this.xRange[0]) ? 0 : 80
}else if(allowedKeys == 'down'){
this.y +=(this.x + 80 > this.xRange[1]) ? 0 : 80
}
}
// Now instantiate your objects.
// Place all enemy objects in an array called allEnemies
// Place the player object in a variable called player
var allEnemies = [new Enemy(), new Enemy(), new Enemy()]

var player = new Player()


// 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'
};

player.handleInput(allowedKeys[e.keyCode]);
});
183 changes: 183 additions & 0 deletions js/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/* Engine.js
* This file provides the game loop functionality (update entities and render),
* draws the initial game board on the screen, and then calls the update and
* render methods on your player and enemy objects (defined in your app.js).
*
* A game engine works by drawing the entire game screen over and over, kind of
* like a flipbook you may have created as a kid. When your player moves across
* the screen, it may look like just that image/character is moving or being
* drawn but that is not the case. What's really happening is the entire "scene"
* is being drawn over and over, presenting the illusion of animation.
*
* This engine is available globally via the Engine variable and it also makes
* the canvas' context (ctx) object globally available to make writing app.js
* a little simpler to work with.
*/

var Engine = (function(global) {
/* Predefine the variables we'll be using within this scope,
* create the canvas element, grab the 2D context for that canvas
* set the canvas elements height/width and add it to the DOM.
*/
var doc = global.document,
win = global.window,
canvas = doc.createElement('canvas'),
ctx = canvas.getContext('2d'),
lastTime;

canvas.width = 505;
canvas.height = 606;
doc.body.appendChild(canvas);

/* This function serves as the kickoff point for the game loop itself
* and handles properly calling the update and render methods.
*/
function main() {
/* Get our time delta information which is required if your game
* requires smooth animation. Because everyone's computer processes
* instructions at different speeds we need a constant value that
* would be the same for everyone (regardless of how fast their
* computer is) - hurray time!
*/
var now = Date.now(),
dt = (now - lastTime) / 1000.0;

/* Call our update/render functions, pass along the time delta to
* our update function since it may be used for smooth animation.
*/
update(dt);
render();

/* Set our lastTime variable which is used to determine the time delta
* for the next time this function is called.
*/
lastTime = now;

/* Use the browser's requestAnimationFrame function to call this
* function again as soon as the browser is able to draw another frame.
*/
win.requestAnimationFrame(main);
}

/* This function does some initial setup that should only occur once,
* particularly setting the lastTime variable that is required for the
* game loop.
*/
function init() {
reset();
lastTime = Date.now();
main();
}

/* This function is called by main (our game loop) and itself calls all
* of the functions which may need to update entity's data. Based on how
* you implement your collision detection (when two entities occupy the
* same space, for instance when your character should die), you may find
* the need to add an additional function call here. For now, we've left
* it commented out - you may or may not want to implement this
* functionality this way (you could just implement collision detection
* on the entities themselves within your app.js file).
*/
function update(dt) {
updateEntities(dt);
// checkCollisions();
}

/* This is called by the update function and loops through all of the
* objects within your allEnemies array as defined in app.js and calls
* their update() methods. It will then call the update function for your
* player object. These update methods should focus purely on updating
* the data/properties related to the object. Do your drawing in your
* render methods.
*/
function updateEntities(dt) {
allEnemies.forEach(function(enemy) {
enemy.update(dt);
});
player.update();
}

/* This function initially draws the "game level", it will then call
* the renderEntities function. Remember, this function is called every
* game tick (or loop of the game engine) because that's how games work -
* they are flipbooks creating the illusion of animation but in reality
* they are just drawing the entire screen over and over.
*/
function render() {
/* This array holds the relative URL to the image used
* for that particular row of the game level.
*/
var rowImages = [
'images/water-block.png', // Top row is water
'images/stone-block.png', // Row 1 of 3 of stone
'images/stone-block.png', // Row 2 of 3 of stone
'images/stone-block.png', // Row 3 of 3 of stone
'images/grass-block.png', // Row 1 of 2 of grass
'images/grass-block.png' // Row 2 of 2 of grass
],
numRows = 6,
numCols = 5,
row, col;

/* Loop through the number of rows and columns we've defined above
* and, using the rowImages array, draw the correct image for that
* portion of the "grid"
*/
for (row = 0; row < numRows; row++) {
for (col = 0; col < numCols; col++) {
/* The drawImage function of the canvas' context element
* requires 3 parameters: the image to draw, the x coordinate
* to start drawing and the y coordinate to start drawing.
* We're using our Resources helpers to refer to our images
* so that we get the benefits of caching these images, since
* we're using them over and over.
*/
ctx.drawImage(Resources.get(rowImages[row]), col * 101, row * 83);
}
}

renderEntities();
}

/* This function is called by the render function and is called on each game
* tick. Its purpose is to then call the render functions you have defined
* on your enemy and player entities within app.js
*/
function renderEntities() {
/* Loop through all of the objects within the allEnemies array and call
* the render function you have defined.
*/
allEnemies.forEach(function(enemy) {
enemy.render();
});

player.render();
}

/* This function does nothing but it could have been a good place to
* handle game reset states - maybe a new game menu or a game over screen
* those sorts of things. It's only called once by the init() method.
*/
function reset() {
// noop
}

/* Go ahead and load all of the images we know we're going to need to
* draw our game level. Then set init as the callback method, so that when
* all of these images are properly loaded our game will start.
*/
Resources.load([
'images/stone-block.png',
'images/water-block.png',
'images/grass-block.png',
'images/enemy-bug.png',
'images/char-boy.png'
]);
Resources.onReady(init);

/* Assign the canvas' context object to the global variable (the window
* object when run in a browser) so that developers can use it more easily
* from within their app.js files.
*/
global.ctx = ctx;
})(this);
Loading