/
index.html
executable file
·278 lines (232 loc) · 7.97 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
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
<!--
Assignment 1 for CMSC838F.
Parts of the code were derived from:
1. http://www.html5rocks.com/en/tutorials/device/orientation/
2. http://www.html5canvastutorials.com/tutorials/html5-canvas-arcs/
-->
<html>
<head>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta http-equiv="cleartype" content="on">
<style type="text/css">
body {
margin:auto;
padding:0;
width:100%;
height:100%;
}
#canvas {
border: 1px solid #333;
background-color: #D3F0DF;
margin: auto;
float: left;
}
</style>
</head>
<body onmousedown="return false;" onLoad="init()">
<canvas id="canvas" width="350" height="490"></canvas>
<div id="debug"></div>
<div id="debug2"></div>
<script type="text/javascript">
var radius = 10;
var canvas = document.getElementById('canvas');
var debug = document.getElementById('debug');
var debug2 = document.getElementById('debug2');
var context = canvas.getContext("2d");
var startX = canvas.width / 2;
var startY = canvas.height - radius -5;
var centerX = canvas.width / 2;
var centerY = canvas.height - radius -5;
var boundaryX = canvas.width;
var boundaryY = canvas.height;
var blockSize = 20;
var livesLeft = 4;
tries = 0;
var score = 0;
var newX=0, newY=0;
var moveX = 0, moveY = 0;
var blockerArray = new Array();
blockerArray = [
{"x":0, "y":50, "dir":1},
{"x":(blockSize * 4), "y":50, "dir":1},
{"x":(boundaryX - blockSize), "y":100, "dir":-1},
{"x":(boundaryX - (blockSize * 5)), "y":100, "dir":-1},
{"x":0, "y":150, "dir":1},
{"x":(blockSize * 4), "y":150, "dir":1},
{"x":(boundaryX - blockSize), "y":200, "dir":-1},
{"x":(boundaryX - (blockSize * 5)), "y":200, "dir":-1},
{"x":0, "y":250, "dir":1},
{"x":(blockSize * 4), "y":250, "dir":1},
{"x":(boundaryX - blockSize), "y":300, "dir":-1},
{"x":(boundaryX - (blockSize * 5)), "y":300, "dir":-1},
{"x":0, "y":350, "dir":1},
{"x":(blockSize * 4), "y":350, "dir":1}];
function init() {
drawCircle(startX, startY);
drawBlockers(blockerArray);
debug.innerHTML = "Score: " + score +
"<br /><br />" +
"Move the ball to the top of the screen." +
"<br />" +
"Beware the edges and the blocks! Don't crash into them." +
"<br /><br />" +
"Lives left: " + livesLeft;
// This device orientation comes from the following link
// http://www.html5rocks.com/en/tutorials/device/orientation/
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', function(eventData) {
var LR = eventData.gamma;
var FB = eventData.beta;
var DIR = eventData.alpha;
deviceOrientationHandler(LR, FB, DIR);
}, false);
}
else if (window.OrientationEvent) {
window.addEventListener('MozOrientation', mozDevOrientHandler, false);
}
setInterval(function(){moveBlockers(blockerArray)}, 100);
}
function deviceOrientationHandler(LR, FB, DIR) {
// Set x and y move amounts
moveX = Math.round(LR); // - moveX;
moveY = Math.round(FB); // - moveY;
// run moveBall() to update ball center x and y
moveBall(moveX,moveY);
}
function moveBall(moveX,moveY){
// Calculate the ball move
moveX = moveX / 20;
moveY = moveY / 20;
// Make new center points
newX = centerX + moveX;
newY = centerY + moveY;
// Check if ball hit boundary --> start over
if(newX - radius < 0) crash();
if(newX + radius > boundaryX) crash();
if(newY - radius < 0) {
tries = 0;
newGame();
}
if(newY + radius > boundaryY) newY = boundaryY - radius;
//check boundaries for each blocker
canMoveBall(newX, newY);
redrawCanvas();
}
function crash(){
livesLeft--;
if (livesLeft < 1) {
livesLeft = 4;
tries++
score = score - 20;
if (score < 0) { score = 0; }
debug.innerHTML = "Score: " + score +
"<br /><br />" +
"Woops! You died. Game OVER." +
"<br /><br />" +
"...Starting over." +
"<br /><br />" +
"Lives Left: " + livesLeft;
debug2.innerHTML = "You've failed at this game " +
"<br />" + tries + " times in a row.";
}
else {
score = score - 10;
if (score < 0) { score = 0; }
debug.innerHTML = "Score: " + score +
"<br /><br />" +
"Woops! You died." +
"<br /><br />" +
"Lives left: " + livesLeft;
}
// Check if ball hit a side or top --> reset and run main()
newX = startX;
newY = startY;
//context.beginPath();
redrawCanvas();
}
function newGame() {
newX = startX;
newY = startY;
livesLeft = 4;
tries = 0;
score = score + 30
debug.innerHTML = "You've won!" +
"<br /><br />" +
"Have some points" +
"<br /><br />" +
"Score: " + score +
"<br /><br />" +
"Lives left " + livesLeft;
debug2.innerHTML = "You've failed at this game " +
"<br />" + tries + " times in a row.";
}
function canMoveBall(x, y) {
// Run through all blockers and check collisions
for (var j = 0; j < blockerArray.length; j++) {
// Check blocker y pos against ball y pos
if ((y - radius <= (blockerArray[j].y + blockSize)) && (y - radius >= blockerArray[j].y)) {
// Check blocker y pos against ball x pos
if (((x - radius <= (blockerArray[j].x + blockSize)) && (x - radius >= blockerArray[j].x)) ||
((x + radius <= (blockerArray[j].x + blockSize)) && (x + radius >= blockerArray[j].x))) {
crash();
}
}
// Check blocker y pos against ball y pos
if ((y + radius <= (blockerArray[j].y + blockSize)) && (y + radius >= blockerArray[j].y)) {
// Check blocker y pos against ball x pos
if (((x - radius <= (blockerArray[j].x + blockSize)) && (x - radius >= blockerArray[j].x)) ||
((x + radius <= (blockerArray[j].x + blockSize)) && (x + radius >= blockerArray[j].x))) {
crash();
}
}
}
}
function canMoveBlocker(i) {
if (blockerArray[i].dir == -1 && (blockerArray[i].x - blockSize / 4) < 0) {
blockerArray[i].dir = 1;
}
if (blockerArray[i].dir == 1 && (blockerArray[i].x + blockSize + (blockSize / 4)) > boundaryX) {
blockerArray[i].dir = -1;
}
}
function redrawCanvas(){
context.clearRect ( 0 , 0 , boundaryX , boundaryY );
drawCircle(newX, newY);
drawBlockers(blockerArray);
}
function drawCircle(x, y){
context.beginPath();
centerX = x;
centerY = y;
context.fillStyle = "#8ED6FF";
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fill();
context.lineWidth = 1;
context.strokeStyle = "#0080c9";
context.stroke();
}z
function moveBlockers(blockerArray) {
for (var i = 0; i < blockerArray.length; i++) {
// Check Collisions to see if blockers pushed the ball (blocker boundaries are less than the circlex+radius)
canMoveBlocker(i);
// Update each blocker position
blockerArray[i].x = blockerArray[i].x + ((blockSize / 4) * blockerArray[i].dir);
}
}
function drawBlockers(blockerArray) {
for (var i = 0; i < blockerArray.length; i++) {
context.beginPath();
context.fillStyle = "#880000";
context.fillRect(blockerArray[i].x, blockerArray[i].y, blockSize, blockSize);
context.fill();
context.lineWidth = 1;
context.strokeStyle = "#990000";
context.stroke();
}
}
</script>
</body>
</html>