Skip to content

Commit fe078f3

Browse files
front-end game using object oriented javascript
0 parents  commit fe078f3

23 files changed

+459
-0
lines changed

README.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
TO RUN THE GAME
2+
To start the game open the game in your browser.
3+
BASIC CRUX
4+
The basic crux of the game is that the player should reach the water avoiding the enemies,if he does so he wins.
5+
STEPS TO PLAY THE GAME
6+
1. There are are 3 enenmy bugs moving at a random speed.
7+
2. There is a player who is at the starting point in the beginning.
8+
3. Now the player can move up ,down,left,right.
9+
4. The player has to reach the water dodging the enemy bugs.
10+
5. If the player hits the bug,the player has to restart from the starting position.
11+
6. If the player reaches the water his score is incremented by 1.
12+
7. Now a dialog box will appear asking whether you wish to continue?.If you press ok the player will again be on the starting line.
13+
8. If you press cancel,the game ends and an alert will be generated displaying your score.
14+
9.Press refresh to play again.
15+
16+
17+
18+

css/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
text-align: center;
3+
}

images/Gem Blue.png

19.7 KB
Loading

images/Gem Green.png

23.5 KB
Loading

images/Gem Orange.png

16.9 KB
Loading

images/Heart.png

6.27 KB
Loading

images/Key.png

7.28 KB
Loading

images/Rock.png

9.21 KB
Loading

images/Selector.png

21.9 KB
Loading

images/Star.png

13.4 KB
Loading

images/char-boy.png

7.61 KB
Loading

images/char-cat-girl.png

8.35 KB
Loading

images/char-horn-girl.png

8.52 KB
Loading

images/char-pink-girl.png

8.66 KB
Loading

images/char-princess-girl.png

9.51 KB
Loading

images/enemy-bug.png

12.3 KB
Loading

images/grass-block.png

8.39 KB
Loading

images/stone-block.png

5.94 KB
Loading

images/water-block.png

8.99 KB
Loading

index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Effective JavaScript: Frogger</title>
6+
<link rel="stylesheet" href="css/style.css">
7+
</head>
8+
<body>
9+
<div id="action">YOUR SCORE IS:0</div>
10+
<script src="js/resources.js"></script>
11+
<script src="js/app.js"></script>
12+
<script src="js/engine.js"></script>
13+
</body>
14+
</html>

js/app.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Enemies our player must avoid
2+
var Enemy = function(x, y) {
3+
// Variables applied to each of our instances go here,
4+
// we've provided one for you to get started
5+
this.x = x;
6+
this.y = y;
7+
// The image/sprite for our enemies, this uses
8+
// a helper we've provided to easily load images
9+
this.sprite = 'images/enemy-bug.png';
10+
};
11+
// Update the enemy's position, required method for game
12+
// Parameter: dt, a time delta between ticks
13+
Enemy.prototype.update = function(dt) {
14+
// You should multiply any movement by the dt parameter
15+
// which will ensure the game runs at the same speed for
16+
// all computers.
17+
/*As we need the speed of the bug to be random,Math.random()
18+
function is used,so as the speed does not become too slow,
19+
a minimum speed of 200 is being set.*/
20+
this.speed = Math.random() * 1000;
21+
if (this.speed < 500) {
22+
this.speed = 200;
23+
}
24+
console.log(this.speed);
25+
this.x = this.x + this.speed * dt;
26+
if (this.x > 500) {
27+
this.x = -100;
28+
}
29+
};
30+
// Draw the enemy on the screen, required method for game
31+
Enemy.prototype.render = function() {
32+
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
33+
};
34+
// Now write your own player class
35+
// This class requires an update(), render() and
36+
// a handleInput() method.
37+
var Player = function(x, y) {
38+
this.sprite = 'images/char-boy.png';
39+
this.x = x;
40+
this.y = y;
41+
/*This property is taken to keep check of the score*/
42+
this.score = 0;
43+
};
44+
Player.prototype.update = function(dt) {
45+
/*These conditions have been given to perform actions when the player reaches the water.*/
46+
if (this.y <= 0) {
47+
this.score++;
48+
var c = confirm("Your Score Is\'" + this.score + "\'Do You Wish To Continue?");
49+
if (c == true) {
50+
this.x = 200;
51+
this.y = 410;
52+
document.getElementById('action').innerHTML = "YOUR SCORE IS:" + this.score;
53+
} else {
54+
this.y = 10;
55+
var a = this.score;
56+
document.write("THANX FOR PLAYING THE GAME,YOUR SCORE IS \'" + this.score + "\'PRESS REFRESH TO PLAY AGAIN");
57+
}
58+
}
59+
/* These conditions have been put to reset the game when the player hits the enemy*/
60+
else if (this.x - allEnemies[0].x < 40 && this.x - allEnemies[0].x > 0 && this.y === allEnemies[0].y) {
61+
this.x = 200;
62+
this.y = 410;
63+
} else if (this.x - allEnemies[1].x < 40 && this.x - allEnemies[1].x > 0 && this.y === allEnemies[1].y) {
64+
this.x = 200;
65+
this.y = 410;
66+
} else if (this.x - allEnemies[2].x < 40 && this.x - allEnemies[2].x > 0 && this.y === allEnemies[2].y) {
67+
this.x = 200;
68+
this.y = 410;
69+
}
70+
this.x = this.x;
71+
this.y = this.y;
72+
};
73+
Player.prototype.render = function() {
74+
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
75+
}
76+
/* In this function we are just comparing the key code with
77+
the four possible values(up,down,left,right) and moving the
78+
player accordingly*/
79+
Player.prototype.handleInput = function(code) {
80+
var a = code;
81+
if (a === "up") {
82+
/*These condition is put so that player does not
83+
go out of canvas*/
84+
if (this.y <= 0) {
85+
this.y = 0;
86+
} else {
87+
this.x = this.x;
88+
this.y = this.y - 50;
89+
}
90+
} else if (a === "down") {
91+
if (this.y === 410) {
92+
this.y = 410;
93+
} else {
94+
this.x = this.x;
95+
this.y = this.y + 50;
96+
}
97+
} else if (a === "left") {
98+
if (this.x === 0) {
99+
this.x = 0;
100+
} else {
101+
this.x = this.x - 25;
102+
this.y = this.y;
103+
}
104+
} else if (a === "right") {
105+
if (this.x === 400) {
106+
this.x = 400;
107+
} else {
108+
this.x = this.x + 25;
109+
this.y = this.y;
110+
}
111+
}
112+
};
113+
// Now instantiate your objects.
114+
// Place all enemy objects in an array called allEnemies
115+
// Place the player object in a variable called player
116+
/* Here we are also sending the coordinates we want while instantiating the
117+
object.*/
118+
var allEnemies = [new Enemy(-100, 60), new Enemy(-100, 160), new Enemy(100, 160)];
119+
var player = new Player(200, 410);
120+
// This listens for key presses and sends the keys to your
121+
// Player.handleInput() method. You don't need to modify this.
122+
document.addEventListener('keyup', function(e) {
123+
var allowedKeys = {
124+
37: 'left',
125+
38: 'up',
126+
39: 'right',
127+
40: 'down'
128+
};
129+
player.handleInput(allowedKeys[e.keyCode]);
130+
});

js/engine.js

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/* Engine.js
2+
* This file provides the game loop functionality (update entities and render),
3+
* draws the initial game board on the screen, and then calls the update and
4+
* render methods on your player and enemy objects (defined in your app.js).
5+
*
6+
* A game engine works by drawing the entire game screen over and over, kind of
7+
* like a flipbook you may have created as a kid. When your player moves across
8+
* the screen, it may look like just that image/character is moving or being
9+
* drawn but that is not the case. What's really happening is the entire "scene"
10+
* is being drawn over and over, presenting the illusion of animation.
11+
*
12+
* This engine is available globally via the Engine variable and it also makes
13+
* the canvas' context (ctx) object globally available to make writing app.js
14+
* a little simpler to work with.
15+
*/
16+
17+
var Engine = (function(global) {
18+
/* Predefine the variables we'll be using within this scope,
19+
* create the canvas element, grab the 2D context for that canvas
20+
* set the canvas elements height/width and add it to the DOM.
21+
*/
22+
var doc = global.document,
23+
win = global.window,
24+
canvas = doc.createElement('canvas'),
25+
ctx = canvas.getContext('2d'),
26+
lastTime;
27+
28+
canvas.width = 505;
29+
canvas.height = 606;
30+
doc.body.appendChild(canvas);
31+
32+
/* This function serves as the kickoff point for the game loop itself
33+
* and handles properly calling the update and render methods.
34+
*/
35+
function main() {
36+
/* Get our time delta information which is required if your game
37+
* requires smooth animation. Because everyone's computer processes
38+
* instructions at different speeds we need a constant value that
39+
* would be the same for everyone (regardless of how fast their
40+
* computer is) - hurray time!
41+
*/
42+
var now = Date.now(),
43+
dt = (now - lastTime) / 1000.0;
44+
45+
/* Call our update/render functions, pass along the time delta to
46+
* our update function since it may be used for smooth animation.
47+
*/
48+
update(dt);
49+
render();
50+
51+
/* Set our lastTime variable which is used to determine the time delta
52+
* for the next time this function is called.
53+
*/
54+
lastTime = now;
55+
56+
/* Use the browser's requestAnimationFrame function to call this
57+
* function again as soon as the browser is able to draw another frame.
58+
*/
59+
win.requestAnimationFrame(main);
60+
}
61+
62+
/* This function does some initial setup that should only occur once,
63+
* particularly setting the lastTime variable that is required for the
64+
* game loop.
65+
*/
66+
function init() {
67+
reset();
68+
lastTime = Date.now();
69+
main();
70+
}
71+
72+
/* This function is called by main (our game loop) and itself calls all
73+
* of the functions which may need to update entity's data. Based on how
74+
* you implement your collision detection (when two entities occupy the
75+
* same space, for instance when your character should die), you may find
76+
* the need to add an additional function call here. For now, we've left
77+
* it commented out - you may or may not want to implement this
78+
* functionality this way (you could just implement collision detection
79+
* on the entities themselves within your app.js file).
80+
*/
81+
function update(dt) {
82+
updateEntities(dt);
83+
// checkCollisions();
84+
}
85+
86+
/* This is called by the update function and loops through all of the
87+
* objects within your allEnemies array as defined in app.js and calls
88+
* their update() methods. It will then call the update function for your
89+
* player object. These update methods should focus purely on updating
90+
* the data/properties related to the object. Do your drawing in your
91+
* render methods.
92+
*/
93+
function updateEntities(dt) {
94+
allEnemies.forEach(function(enemy) {
95+
enemy.update(dt);
96+
});
97+
player.update();
98+
}
99+
100+
/* This function initially draws the "game level", it will then call
101+
* the renderEntities function. Remember, this function is called every
102+
* game tick (or loop of the game engine) because that's how games work -
103+
* they are flipbooks creating the illusion of animation but in reality
104+
* they are just drawing the entire screen over and over.
105+
*/
106+
function render() {
107+
/* This array holds the relative URL to the image used
108+
* for that particular row of the game level.
109+
*/
110+
var rowImages = [
111+
'images/water-block.png', // Top row is water
112+
'images/stone-block.png', // Row 1 of 3 of stone
113+
'images/stone-block.png', // Row 2 of 3 of stone
114+
'images/stone-block.png', // Row 3 of 3 of stone
115+
'images/grass-block.png', // Row 1 of 2 of grass
116+
'images/grass-block.png' // Row 2 of 2 of grass
117+
],
118+
numRows = 6,
119+
numCols = 5,
120+
row, col;
121+
122+
/* Loop through the number of rows and columns we've defined above
123+
* and, using the rowImages array, draw the correct image for that
124+
* portion of the "grid"
125+
*/
126+
for (row = 0; row < numRows; row++) {
127+
for (col = 0; col < numCols; col++) {
128+
/* The drawImage function of the canvas' context element
129+
* requires 3 parameters: the image to draw, the x coordinate
130+
* to start drawing and the y coordinate to start drawing.
131+
* We're using our Resources helpers to refer to our images
132+
* so that we get the benefits of caching these images, since
133+
* we're using them over and over.
134+
*/
135+
ctx.drawImage(Resources.get(rowImages[row]), col * 101, row * 83);
136+
}
137+
}
138+
139+
renderEntities();
140+
}
141+
142+
/* This function is called by the render function and is called on each game
143+
* tick. Its purpose is to then call the render functions you have defined
144+
* on your enemy and player entities within app.js
145+
*/
146+
function renderEntities() {
147+
/* Loop through all of the objects within the allEnemies array and call
148+
* the render function you have defined.
149+
*/
150+
allEnemies.forEach(function(enemy) {
151+
enemy.render();
152+
});
153+
154+
player.render();
155+
}
156+
157+
/* This function does nothing but it could have been a good place to
158+
* handle game reset states - maybe a new game menu or a game over screen
159+
* those sorts of things. It's only called once by the init() method.
160+
*/
161+
function reset() {
162+
// noop
163+
}
164+
165+
/* Go ahead and load all of the images we know we're going to need to
166+
* draw our game level. Then set init as the callback method, so that when
167+
* all of these images are properly loaded our game will start.
168+
*/
169+
Resources.load([
170+
'images/stone-block.png',
171+
'images/water-block.png',
172+
'images/grass-block.png',
173+
'images/enemy-bug.png',
174+
'images/char-boy.png'
175+
]);
176+
Resources.onReady(init);
177+
178+
/* Assign the canvas' context object to the global variable (the window
179+
* object when run in a browser) so that developers can use it more easily
180+
* from within their app.js files.
181+
*/
182+
global.ctx = ctx;
183+
})(this);

0 commit comments

Comments
 (0)