-
Notifications
You must be signed in to change notification settings - Fork 50
/
survivor.html
222 lines (178 loc) · 6.09 KB
/
survivor.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html>
<head>
<title>Player Movement using onkeydown/onkeyup (Enhanced version)</title>
<style type="text/css" media="screen">
canvas { border: 4px solid; }
</style>
<!-- Ignore this script file, i'm not sure if this is needed. -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1150473-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!--Main script file starts here. You can find the base code for the game loop at http://nokarma.org -->
<script type="text/javascript" charset="utf-8">
var Key = {//*******************************MANAGE KEY PRESSES
_pressed: {},
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
isDown: function(keyCode) { return this._pressed[keyCode]; },
onKeydown: function(event) { this._pressed[event.keyCode] = true; },
onKeyup: function(event) { delete this._pressed[event.keyCode]; }
};
window.addEventListener('keyup', function(event) { Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) { Key.onKeydown(event); }, false);
//*******************************************MANAGE KEY PRESSES
var Game = {
fps: 60,
width: 640,
height: 480
};
Game._onEachFrame = (function() {//**********_onEachFrame
var requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
if (requestAnimationFrame) {
return function(cb) {
var _cb = function() { cb(); requestAnimationFrame(_cb); }
_cb();
};
} else {
return function(cb) {
setInterval(cb, 1000 / Game.fps);
}
}
})();//**************************************_onEachFrame
Game.start = function() {//******************START: Initialize Game
document.getElementById("startbutton").style.display = "none";//hide start button
document.getElementById("directions").style.display = "block";//display arrow directions
Game.canvas = document.createElement("canvas");
Game.canvas.width = Game.width;
Game.canvas.height = Game.height;
Game.context = Game.canvas.getContext("2d");
document.body.appendChild(Game.canvas);
Game.player = new Player();
Game.enemy = new Enemy();
Game._onEachFrame(Game.run);
};//*****************************************START: Initialize Game
Game.run = (function() {//*******************RUN: Main Game Loop
var loops = 0, skipTicks = 1000 / Game.fps,
maxFrameSkip = 10,
nextGameTick = (new Date).getTime(),
lastGameTick;
return function() {
loops = 0;
while ((new Date).getTime() > nextGameTick) {
Game.update();
Game.isGameOver();
nextGameTick += skipTicks;
loops++;
}
if (loops) Game.draw();
}
})();//**************************************RUN: Main Game Loop
//Check to see if enemy hit player. If true, game is over
Game.isGameOver = function(){
if((Game.player.x + Game.player.width > Game.enemy.x) && (Game.player.x < Game.enemy.x +Game.enemy.width) && (Game.player.y < Game.enemy.y + Game.enemy.height) && (Game.player.y + Game.player.height >Game.enemy.y))
Game.endGame();
}
//Player was hit. End game.
Game.endGame = function(){
document.getElementById("directions").innerHTML = "GAME OVER";
throw new Error("game over!"); //throw error ends script
}
//Restart: reload page.
Game.restart = function(){
}
/*The rest of the functions below are to update location and draw both player and enemy */
//define player
function Player() {
this.x = 300;
this.y = 300;
this.width = 30;
this.height = 30;
}
//define enemy
function Enemy(){
this.x = 100;
this.y = 100;
this.width = 32;
this.height = 32;
this.left = true;
this.down = true;
}
Game.update = function() {
Game.player.update();
Game.enemy.update();
};
//Player movement, depends on arrow keys pressed.
Player.prototype.update = function() {
if (Key.isDown(Key.UP)) this.moveUp();
if (Key.isDown(Key.LEFT)) this.moveLeft();
if (Key.isDown(Key.DOWN)) this.moveDown();
if (Key.isDown(Key.RIGHT)) this.moveRight();
};
Player.prototype.moveLeft = function() {
this.x -= 1;
};
Player.prototype.moveRight = function() {
this.x += 1;
};
Player.prototype.moveUp = function() {
this.y -= 1;
};
Player.prototype.moveDown = function() {
this.y += 1;
};
//Enemy movement, depends on position of game player
Enemy.prototype.update = function() {
if((Math.floor(Math.random() * 29))>27){
if(this.x < Game.player.x) this.left = false;
if(this.x >Game.player.x) this.left = true;
if(this.y>Game.player.y) this.down = false;
if(this.y<Game.player.y) this.down = true;
}
if(this.left) this.moveLeft();
if(this.down) this.moveDown();
if(!this.left) this.moveRight();
if(!this.down) this.moveUp();
};
Enemy.prototype.moveLeft = function() {
this.x -= 1;
};
Enemy.prototype.moveRight = function() {
this.x += 1;
};
Enemy.prototype.moveUp = function() {
this.y -= 1;
};
Enemy.prototype.moveDown = function() {
this.y += 1;
};
//Draw player and enemy
Game.draw = function() {
Game.context.clearRect(0, 0, Game.width, Game.height);
Game.player.draw(Game.context);
Game.enemy.draw(Game.context);
};
Player.prototype.draw = function(context) {
context.fillStyle="#000000";
context.fillRect(this.x, this.y, this.width, this.height);
};
Enemy.prototype.draw = function(context) {
context.fillStyle="#FF0000";
context.fillRect(this.x, this.y, this.width, this.height);
};
</script>
</head>
<body>
<button id="startbutton" onclick="Game.start()">Demo Game</button>
<p id="directions" style="margin-left: 280px;" hidden>Press arrow keys to move</p>
</body>
</html>