forked from HTML5-Games/html5-games.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pebblerush.html
339 lines (339 loc) · 10.5 KB
/
pebblerush.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!doctype html>
<html>
<head>
+<meta name="description" content="[add your bin description]" />
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>Pebble Rush</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ <script>
+
+ $(function() {
+
+ });
+ </script>
+
+<style id="jsbin-css">
+
+</style>
+</head>
+ <body>
+ <style type="text/css">
+ canvas {
+ border:1px solid black;
+ }
+ </style>
+ <h1>Pebble Rush</h1>
+ <canvas id="canvas" width="500" height="500">
+ Your browser does not support the HTML5 Canvas feature. This game uses new HTML5 features and will only work on the latest versions of Firefox, Safari or Chrome (and maybe Internet Explorer 9).
+ </canvas>
+ <script>
+var canvas = $('#canvas')[0];
+ // different browsers support different contexts. All support 2d
+ var context = canvas.getContext('2d');
+
+
+ var paddleX = 200;
+ var paddleY = 400;
+
+ var paddleWidth = 50;
+ var paddleHeight = 15;
+
+ var paddleDeltaX = 0;
+ var paddleDeltaY = 0;
+
+ function drawPaddle(){
+ context.fillStyle="rgb(100,100,100)";
+ context.fill();
+
+ context.fillRect(paddleX,paddleY,paddleWidth,paddleHeight);
+ }
+
+ var ballX = 10;
+ var ballY = 10;
+ var ballRadius = 20;
+ var ballX1=20;
+ var ballY1=10;
+ var ballX2=40;
+ var ballY2=10;
+ var ballX3=60;
+ var ballY3=10;
+ var ballX4=60;
+ var ballY4=10;
+ var ballX5=80;
+ var ballY5=10;
+ var score=0;
+
+
+ function drawball_1(){
+ context.beginPath();
+ context.arc(ballX,ballY,ballRadius,0,Math.PI*2,true);
+ context.fillStyle="rgb(100,100,100)";
+ context.fill();
+ }
+
+
+ function drawBall(){
+ // Context.beginPath when you draw primitive shapes
+ context.beginPath();
+
+ // Draw arc at center ballX, ballY with radius ballRadius,
+ // From 0 to 2xPI radians (full circle)
+
+
+
+ context.arc(ballX1,ballY1,ballRadius,0,Math.PI*2,true);
+ context.arc(ballX2,ballY2,ballRadius,0,Math.PI*2,true);
+ context.arc(ballX3,ballY3,ballRadius,0,Math.PI*2,true);
+ context.arc(ballX4,ballY4,ballRadius,0,Math.PI*2,true);
+ context.arc(ballX5,ballY5,ballRadius,0,Math.PI*2,true);
+ // Fill up the path that you just drew
+ context.fillStyle="rgb(100,00,100)";
+ context.fill();
+ context.fill();
+ }
+
+
+
+
+ function drawplatform(){
+
+ context.fillStyle="rgb(00,0,0)";
+ context.fill();
+ context.fillRect(0,415,500,65);
+
+
+ }
+ function displayscoreboard(){
+ context.fillStyle="rgb(79,123,145)";
+
+context.font = "20px Times New Roman";
+
+ context.clearRect(0,canvas.height-30,canvas.width,30);
+ if(score>=30 && score<=35)
+ {
+ context.fillText("Carry on dude!!!!",10,canvas.height-5);
+ }
+ else if(score >=60 && score <=65)
+ {
+ context.fillText("Way to go!!!!",10,canvas.height-5);
+ }
+ else if(score >=100 && score <=110)
+ {
+ context.fillText("On Fire!!!!",10,canvas.height-5);
+ }
+ else if(score >=150 && score <=160)
+ {
+ context.fillText("Exhausted praising You!!!",10,canvas.height-5);
+ }
+ else if(score <=-10 && score >=-15)
+ {
+ context.fillText("Watch Out!!!!",10,canvas.height-5);
+ }
+ else if(score <=-30 && score >=-35 )
+ {
+ context.fillText("Game ends at -40",10,canvas.height-5);
+ }
+ else{
+ context.fillText('Score:'+score,10,canvas.height-5);
+ }
+ }
+ function animate () {
+ context.clearRect(0,0,canvas.width,canvas.height);
+
+ moveBall();
+ movePaddle();
+
+
+ drawPaddle();
+
+ drawBall();
+ drawball_1();
+ drawplatform();
+ displayscoreboard();
+ endGame();
+
+ //displayScoreBoard();
+ }
+
+
+ var ballDeltaY=8;
+
+ function moveBall(){
+ // Move the ball
+ ballY = ballY + ballDeltaY;
+ ballY1 = ballY1 + ballDeltaY;
+ ballY2 = ballY2 + ballDeltaY;
+ ballY3 = ballY3 + ballDeltaY;
+ ballY4 = ballY4 + ballDeltaY;
+ ballY5 = ballY5 + ballDeltaY;
+ var a=Math.floor((Math.random()*500)+1);
+ var b=Math.floor((Math.random()*500)+1);
+ var c=Math.floor((Math.random()*500)+1);
+ var d=Math.floor((Math.random()*500)+1);
+ var e=Math.floor((Math.random()*500)+1);
+
+ while((a != b)&&(a !=c)&&(a !=d)&&(a != e))
+ {
+ a=Math.floor((Math.random()*500)+1);
+ }
+
+ if(ballY <= 500 )
+ {
+ ballY=ballY;
+ }
+ else
+ {
+ ballX=a;
+ ballY=0;
+ }
+ if(ballY1 <= 500 )
+ {
+ ballY1=ballY1;
+ }
+ else
+ {
+ ballX1=a;
+ ballY1=0;
+ }
+ if(ballY2 <= 500 )
+ {
+ ballY2=ballY2;
+ }
+ else
+ {
+ ballX2=a;
+ ballY2=0;
+ }
+ if(ballY3 <= 500 )
+ {
+ ballY3=ballY3;
+ }
+ else
+ {
+ ballX3=a;
+ ballY3=0;
+ }
+
+ if(ballY4 <= 500 )
+ {
+ ballY4=ballY4;
+ }
+ else
+ {
+ ballX4=a;
+ ballY4=0;
+ }
+
+ if ((ballY + ballDeltaY + ballRadius >= paddleY) && (ballX>=paddleX && ballX <= paddleX+100)){
+ // and it is positioned between the two ends of the paddle (is on top)
+
+ ballY = 0;
+ ballX=Math.floor((Math.random()*500+1));
+ score++;
+
+}
+
+ if ((ballY1 + ballDeltaY + ballRadius >= paddleY) && (ballX1>=paddleX && ballX1 <= paddleX+100)){
+ // and it is positioned between the two ends of the paddle (is on top)
+ ballY1 = 0;
+ ballX1=Math.floor((Math.random()*500+1));
+ score--;
+
+}
+
+ if ((ballY2 + ballDeltaY + ballRadius >= paddleY) && (ballX2>=paddleX && ballX2 <= paddleX+100)){
+ // and it is positioned between the two ends of the paddle (is on top)
+
+ ballY2 = 0;
+ ballX2=Math.floor((Math.random()*500+1));
+ score--;
+
+}
+
+ if ((ballY3 + ballDeltaY + ballRadius >= paddleY) && (ballX3>=paddleX && ballX3 <= paddleX+100)){
+ // and it is positioned between the two ends of the paddle (is on top)
+
+ ballY3 = 0;
+ ballX3=Math.floor((Math.random()*500+1));
+ score--;
+
+}
+
+ if ((ballY4 + ballDeltaY + ballRadius >= paddleY) && (ballX4>=paddleX && ballX4 <= paddleX+100)){
+ // and it is positioned between the two ends of the paddle (is on top)
+
+ ballY4 = 0;
+ ballX4=Math.floor((Math.random()*500+1));
+
+
+}
+
+
+
+
+
+ }
+
+ var paddleDeltaX;
+ var paddleSpeedX = 10;
+
+ function movePaddle(){
+ if (paddleMove == 'LEFT'){
+ paddleDeltaX = -paddleSpeedX;
+ } else if (paddleMove == 'RIGHT'){
+ paddleDeltaX = paddleSpeedX;
+ } else {
+ paddleDeltaX = 0;
+ }
+ // If paddle reaches the ends, then don't let it move
+ if (paddleX + paddleDeltaX < 0 || paddleX + paddleDeltaX +paddleWidth >canvas.width){
+ paddleDeltaX = 0;
+ }
+ paddleX = paddleX + paddleDeltaX;
+ }
+
+ var gameLoop;
+ var paddleMove;
+
+ function startGame(){
+
+ paddleMove = 'NONE';
+ paddleDeltaX = 0;
+
+ // call the animate() function every 200ms until clearInterval(gameLoop) is called
+
+
+ // Start Tracking Keystokes
+ $(document).keydown(function(evt) {
+ if (evt.keyCode == 39) {
+ paddleMove = 'RIGHT';
+ } else if (evt.keyCode == 37){
+ paddleMove = 'LEFT';
+ }
+ });
+
+ $(document).keyup(function(evt) {
+ if (evt.keyCode == 39) {
+ paddleMove = 'NONE';
+ } else if (evt.keyCode == 37){
+ paddleMove = 'NONE';
+ }
+ });
+
+ gameLoop = setInterval(animate,20);
+ }
+
+ function endGame(){
+ if(score <= -40 )
+ {
+ clearInterval(gameLoop);
+ context.clearRect(0,0,canvas.width,canvas.height);
+ context.fillText('The End!!!!',canvas.width/2,canvas.height/2);
+ }
+
+ }
+
+ startGame();
+</script>
+</body>
</html>