-
Notifications
You must be signed in to change notification settings - Fork 0
/
game1.html
240 lines (208 loc) · 4.41 KB
/
game1.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>
<title>Classic Bricks Game</title>
<style type="text/css">
#board
{
float:left;
outline:black solid thin;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
initialScreen();
$(document).keydown(onKeyDown);
$(document).keyup(onKeyUp);
});
</script>
</head>
<body>
<div id="container">
<canvas id="board" height="500" width="500">
Your browser does not support HTML5 canvas
</canvas>
</div>
</body>
<script type="text/javascript">
var canvas,context;
var ballx,bally;
var xunits=0;
var yunits=0;
var radians=0;
var speed;
var angle;
var moveRight,moveLeft;
const BARHEIGHT=10;
const BARWIDTH=120;
const RADIUS=15;
const BRICKROWS=5;
const BRICKCOLUMNS=10;
const BRICKHEIGHT=50;
const BRICKWIDTH=20;
const NUMBEROFBRICKS=50;
var barx,bary;
var gameOver=false;
var bricks=new Array();
function initialScreen()
{
canvas=document.getElementById("board");
context=canvas.getContext("2d");
//starting positions of the ball and the bar
ballx=canvas.width/2;
bally=canvas.height-30;
barx=canvas.width/2-60;
bary=canvas.height-10;
drawBricks();
drawGame();
speed = 5;
angle=35;
updateBall();
setInterval(drawGame, 33);
}
function drawGame()
{
if(!gameOver)
{
//clear the earlier drawing
context.clearRect(0, 0, canvas.width, canvas.height);
//draw the ball
ballx+=xunits;
bally+=yunits;
context.fillStyle="black";
context.beginPath();
context.arc(ballx,bally,RADIUS,0,Math.PI*2,true);
context.closePath();
context.fill();
//draw the bar everytime we draw the game board
drawBar();
//draw the bricks
reDrawBricks();
//ball bouncing off the walls
if (ballx < 0 || ballx>canvas.width ) //side walls
{
angle = 180 - angle;
updateBall();
}
else if(bally< 0)//top wall
{
angle = 360 - angle;
updateBall();
}
else if(ballTouchesBar())//ball bounce on touching the bar
{
angle=360-angle;
updateBall();
}
else if(bally>canvas.width)
{
drawGameOver();
}
}
}
function updateBall()
{
radians = angle * Math.PI/ 180;
xunits = Math.cos(radians) * speed;
yunits = Math.sin(radians) * speed;
}
function drawBar()
{
context.fillStyle="orange";
if(moveRight)
{
if(barx+BARWIDTH<canvas.width)
{
barx+=5;
}
}
else if(moveLeft)
{
if(barx>0)
{
barx-=5;
}
}
context.fillRect(barx,bary,BARWIDTH,BARHEIGHT);
}
function drawBricks()
{
context.fillStyle="#8E2323";
context.strokeStyle="gray";
var posx=0;
var posy=0;
for(var j=0;j<BRICKROWS;j++)
{
for(var k=0;k<BRICKCOLUMNS;k++)
{
context.fillRect(posx,posy,BRICKHEIGHT,BRICKWIDTH);
context.strokeRect(posx,posy,BRICKHEIGHT,BRICKWIDTH);
bricks.push({x:posx,y:posy,broken:false});
posx+=BRICKHEIGHT;
}
posy+=BRICKWIDTH;
posx=0;
}
}
//event handler for key press
function onKeyDown(event)
{
if(event.keyCode==39)
{
moveRight=true;
}
else if(event.keyCode==37)
{
moveLeft=true;
}
}
//event handler for key release
function onKeyUp(event)
{
if(event.keyCode==39)
{
moveRight=false;
}
else if(event.keyCode==37)
{
moveLeft=false;
}
}
//to check if the ball touches the bar
function ballTouchesBar()
{
if((ballx+RADIUS>barx-1)&&(ballx+RADIUS<barx+BARWIDTH+1))
{
if((bally+RADIUS>bary-1)&&(bally+RADIUS<bary+BARHEIGHT+1))
{
return true;
}
else return false;
}
else return false;
}
//to draw the bricks again
function reDrawBricks()
{
context.fillStyle="#8E2323";
context.strokeStyle="gray";
for(var i=0;i<bricks.length;i++)
{
if(!bricks[i].broken)
{
context.fillRect(bricks[i].x,bricks[i].y,BRICKHEIGHT,BRICKWIDTH);
context.strokeRect(bricks[i].x,bricks[i].y,BRICKHEIGHT,BRICKWIDTH);
}
}
}
//to display when the game is over
function drawGameOver()
{
gameOver=true;
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = "bold 12px sans-serif";
context.fillText("GAME OVER!!",10,canvas.height/2);
}
</script>
</html>