-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.html
314 lines (273 loc) · 8.8 KB
/
robot.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<html>
<head>
<title>Jolly Good Sport</title>
<script src="js/jQuery.js" type="text/javascript"></script>
<script src="js/jquery_ui.js" type="text/javascript"></script>
<script type="text/javascript">
var moveup = false;
var movedown = false;
var curTop = 0;
var TOP = -125;
var BOTTOM = 500;
var MOVE = 6;
var ARROWDEFAULTTOP = 133;
var arrowFiredAlready = false;
var characterDivWidth = 300;
var monsterSize = 75;
var monsterMove = 3;
var monsterMoveFast = 6;
var monstCount = 0;
var enemyAddInterval = 5000;
var arrowWidth = 187;
var monsterBuffer = 8;
var score = 0;
var enemiesLetThrough = 0;
var arrowsfired = 0;
var startedGettingHarder = false;
var arrowFireInterval = 300;
var audioOn = false;
var gameCountSeconds = 0;
var gameCountMinutes = 10;
var ended = false;
var timeOuts= new Array();
function getHarder()
{
monsterMove++;
monsterMoveFast += 2;
MOVE++;
if(arrowFireInterval > 50)
arrowFireInterval -= 50
timeOuts['getharder'] = setTimeout('getHarder();',15000);
}
function fasterEnemy()
{
// decrease interval
if(enemyAddInterval > 750){
enemyAddInterval -= 250;
// reset timer
timeOuts['fasterenemy'] = setTimeout('fasterEnemy();',10000);
}
if(enemyAddInterval == 750 && !startedGettingHarder) {timeOuts['getharder'] = setTimeout('getHarder();',10000);startedGettingHarder=true;}
}
function addAnEnemy()
{
// get a random top positioning for element
var randomPosition = Math.floor((Math.random()*(700 - monsterSize))+1);
var startingLeft = $('#EnemyDiv').width();
monstCount++;
// every third monster moves faster
if(monstCount % 3 == 0)
$('#EnemyDiv').append('<img src="images/monster.jpeg" class="monster monsterfast" />');
else
$('#EnemyDiv').append('<img src="images/monster.jpeg" class="monster" />');
var newEnemy = $('#EnemyDiv > img:last-child');
$(newEnemy).css('top',randomPosition);
curTop = randomPosition-100;
//$('body > div:first-child').animate({top: curTop-100}, 200);
setTimeout(function(){fire()}, 200);
$(newEnemy).css('left',startingLeft - monsterSize);
// restart timer
timeOuts['addanenemy'] = setTimeout('addAnEnemy();',enemyAddInterval);
}
function didWeHitSomething()
{
// check if any arrows on screen are on any enemies on screen
// loop through arrows on screen
$('.arrow').each(function(){
var arrowTip = $(this).position().left + arrowWidth*(2.5);
var arrowTop = $(this).position().top;
var curArrow = $(this);
var arrowMadeAHit = false;
// check each enemy on screen to see if he matches this arrow
$('.monster').each(function(){
if(!arrowMadeAHit)
{
var monstLeft = $(this).position().left + characterDivWidth;
var monstTop = $(this).position().top - monsterBuffer;
// check if arrow is within horizontal of monster
if(arrowTip > monstLeft && arrowTip < (monstLeft + (monsterSize + monsterBuffer)))
{
// check within veritcal
if(arrowTop > monstTop && arrowTop < (monstTop + (monsterSize + monsterBuffer)))
{
// its a hit! Remove them, set boolean
score++;
arrowMadeAHit = true;
$(this).remove();
$(curArrow).remove();
$('#EnemyDiv').append('<img class="explode" src="images/explode.gif" style="top:'+monstTop+'px;left:'+(monstLeft-characterDivWidth)+'px;position:absolute" />');
$('.explode').animate({height: '125',width: '125'},750,function(){$(this).remove();});
$('#scores > label:first-child').html('Your Score: ' + score);
if(audioOn)
{
document.getElementById('laugh').load();
document.getElementById('laugh').play();
}
$('#accuracy').html('Accuracy: ' + Math.floor((score/arrowsfired)*100) + '%');
}
}
}
});
});
// start over
timeOuts['didwehitsomething'] = setTimeout('didWeHitSomething();',50);
}
function fireTimer()
{
// check for moving
if(moveup)
{
if(curTop > (TOP + MOVE))
curTop = curTop - MOVE;
else
curTop = TOP;
}
if(movedown)
{
if(curTop < (BOTTOM - MOVE))
curTop = curTop + MOVE;
else
curTop = BOTTOM;
}
// Move player to new position
$('body > div:first-child > div').css('top',curTop);
// Move monsters left
$('.monster').each(function(){
var curLeft = $(this).position().left;
// check if it has hit the end, remove it
if(curLeft < -250)
{
$(this).remove();
enemiesLetThrough++;
$('#scores > label:first-child + label').html("Monsters Let Through: " + enemiesLetThrough);
}
else
{
if($(this).hasClass('monsterfast'))
$(this).css('left',curLeft - monsterMoveFast);
else
$(this).css('left',curLeft - monsterMove);
}
});
// reset timer
timeOuts['firetimer'] = setTimeout('fireTimer();',17);
}
function fire()
{
if(!arrowFiredAlready)
{
// set arrow bool, to stop arrows for a second
arrowFiredAlready = true;
// insert new arrow in canvas where arrow currently is
$('body').append('<img src="images/arrow.png" class="arrow"/>');
var newArrow = $('body > img:last-child');
var arrowPosition = $('body > div:first-child > div').position();
$(newArrow).css('top',ARROWDEFAULTTOP + arrowPosition.top);
// animate to the right, get body size to know how far
// remove the arrow when it hits the wall
var bodyWidth = $('body').width();
$(newArrow).animate({left:(bodyWidth-187)+'px'},2500,'linear',function(){
$(this).remove();
$('#accuracy').html('Accuracy: ' + Math.floor((score/arrowsfired)*100) + '%');
});
// prevent arrow fires for a second
timeOuts['preventarrows'] = setTimeout(function(){arrowFiredAlready=false;},arrowFireInterval);
// update arrows fired
arrowsfired++;
}
}
function toggleAudio()
{
// Change button text
if(audioOn)
$('#audioButton').html('Audio Off');
else
$('#audioButton').html('Audio On');
audioOn = !audioOn;
}
function endGame()
{
if(!ended){
for(key in timeOuts ){
clearTimeout(timeOuts[key]);
}
ended = true;
$('#EnemyDiv img').remove();
$('#time').html('Time Remaining: 0:00');
$('body > div:first-child > div').css('top',0);
}
}
function gameTimer()
{
gameCountSeconds--;
timeOuts['gametimer'] = setTimeout('gameTimer();',1000);
var gameSeconds = "";
if(gameCountSeconds < 10 && gameCountSeconds >= 0)
gameSeconds = "0"+gameCountSeconds;
else
gameSeconds = gameCountSeconds;
if(gameCountSeconds < 0){
gameCountSeconds = 59;
gameSeconds = gameCountSeconds;
gameCountMinutes--;
if(gameCountMinutes < 0){
endGame();
return;
}
}
$('#time').html('Time Remaining: ' + gameCountMinutes + ':' + gameSeconds);
}
$(function(){
fireTimer();
timeOuts['addanemeny'] = setTimeout('addAnEnemy();',5000);
timeOuts['fasterenemy'] = setTimeout('fasterEnemy();',20000);
timeOuts['didwehitsomething'] = setTimeout('didWeHitSomething();',5000);
timeOuts['gametimer'] = setTimeout('gameTimer();',1000);
});
$(document).keydown(function(e){
if(e.which == 32) {fire();}
if(e.which == 38) {moveup = true;}
if(e.which == 40) {movedown = true;}
});
$(document).keyup(function(e){
if(e.which == 38) {moveup = false;}
if(e.which == 40) {movedown = false;}
});
</script>
<style type="text/css">
body {width:100%;margin:0;padding:0;}
body > div:first-child {width:300px;float:left;position:relative;height:700px;}
body > div:first-child > div {width:inherit;height:350px;position:relative;}
body > div:first-child > div > img:first-child {height:325px;position:absolute;}
body > div:first-child > div > img + img {width:187px;position:absolute;-webkit-transform: rotate(180deg);-moz-transform: rotate(180deg);top: 133px;left: 56px;}
body > div + div {height:700px;position:relative;}
body > img {position:absolute;width:187px;-webkit-transform: rotate(180deg);-moz-transform: rotate(180deg);left: 56px;}
.monster {width:75px;height:75px;position:absolute;}
.explode {width:50px;height:50px;}
#scores {width:100%;height:100px;}
#scores label {padding:30px;font-size:20px;}
</style>
</head>
<body>
<div>
<div>
<img src="images/archer.jpg"></img>
<img src="images/arrow.png"></img>
</div>
</div>
<div id="EnemyDiv">
</div>
<div id="scores">
<label>Your Score: 0</label>
<label>Monsters Let Through: 0</label>
<label id="accuracy">Accuracy: 100%</label>
<label id="time">Time Remaining: 5:00</label>
<button id="audioButton" onClick="toggleAudio();">Audio Off</button>
<audio id="laugh" preload="none">
<source src="audio/laugh.aac" type="audio/aac" />
<source src="audio/laugh.mp3" type="audio/mpeg" />
<source src="audio/laugh.ogg" type="audio/ogg" />
</audio>
</div>
</body>
</html>