-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
240 lines (169 loc) · 4.25 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="mturk.js"></script>
<style type="text/css">
body {
background-color: white;
}
canvas {
border: 1px solid black;
}
</style>
<title>Falling Block Game</title>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
//canvas variables
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// game variables
var startingScore = 0;
var continueAnimating = false;
var score;
var earned;
// block variables
var blockWidth = 50;
var blockHeight = 50;
var blockSpeed = 50;
var block = {
x: 0,
y: canvas.height - blockHeight,
width: blockWidth,
height: blockHeight,
blockSpeed: blockSpeed
}
// rock variables
var rockWidth = 50;
var rockHeight = 50;
var totalRocks = 2;
var rocks = [];
function init() {
totalRocks = Math.floor(Math.random() * (4))
for (var i = 0; i < totalRocks; i++) {
addRock();
}
}
init()
function checkWin() {
if (earned == 200) {
continueAnimating = False;
score = score / 70.0
alert("Completed with an earning of " + score / 70.0 * .1);
}
}
function addRock() {
var rock = {
width: rockWidth,
height: rockHeight
}
resetRock(rock);
rocks.push(rock);
}
// move the rock to a random position near the top-of-canvas
// assign the rock a random speed
function resetRock(rock) {
rock.x = Math.floor(Math.random() * (7)) * 50;
rock.y = 0;
rock.speed = 1
}
//left and right keypush event handlers
document.onkeydown = function(event) {
if (event.keyCode == 39) {
block.x += block.blockSpeed;
if (block.x >= 250) {
block.x = 250;
}
} else if (event.keyCode == 37) {
block.x -= block.blockSpeed;
if (block.x <= 0) {
block.x = 0;
}
}
}
function animate() {
earned = (score / 70.0) * .1
// request another animation frame
if (score % 250 < 1) {
init();
}
if (continueAnimating) {
requestAnimationFrame(animate);
}
// for each rock
// (1) check for collisions
// (2) advance the rock
// (3) if the rock falls below the canvas, reset that rock
score += 1
checkWin()
for (var i = 0; i < rocks.length; i++) {
var rock = rocks[i];
// test for rock-block collision
if (isColliding(rock, block)) {
continueAnimating = false;
alert("Completed with an earning of " + score / 70 * .1);
}
// advance the rocks
rock.y += 1;
// if the rock is below the canvas,
if (rock.y > canvas.height) {
resetRock(rock);
}
}
// redraw everything
drawAll();
}
function isColliding(a, b) {
return !(
b.x >= a.x + a.width || b.x + b.width <= a.x || b.y >= a.y + a.height || b.y + b.height <= a.y);
}
function drawAll() {
// clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// draw the background
// (optionally drawImage an image)
ctx.fillStyle = "ivory";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// draw the block
ctx.fillStyle = "skyblue";
ctx.fillRect(block.x, block.y, block.width, block.height);
ctx.strokeStyle = "lightgray";
ctx.strokeRect(block.x, block.y, block.width, block.height);
// draw all rocks
for (var i = 0; i < rocks.length; i++) {
var rock = rocks[i];
// optionally, drawImage(rocksImg,rock.x,rock.y)
ctx.fillStyle = "gray";
ctx.fillRect(rock.x, rock.y, rock.width, rock.height);
}
// draw the score
ctx.font = "14px Times New Roman";
ctx.fillStyle = "black";
ctx.fillText("Cents Earned: " + earned, 10, 15);
}
// button to start the game
$("#start").click(function() {
score = startingScore
block.x = 0;
earned = earned
for (var i = 0; i < rocks.length; i++) {
resetRock(rocks[i]);
}
if (!continueAnimating) {
continueAnimating = true;
animate();
};
});
});//]]>
</script>
</head>
<body>
<button id="start">Start Game</button>
<br>
<h4>Use left-right arrows. Avoid falling blocks, Game automatically ends when you earn 2 dollars.</h4>
<canvas id="canvas" width=300 height=600></canvas>
</body>
</html>