-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbricks.html
165 lines (146 loc) · 3.85 KB
/
bricks.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
<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="application/x-javascript">
var WIDTH = 300;
var HEIGHT = 300;
var x = WIDTH/2;
var y = HEIGHT-10;
var paddlex = WIDTH/2;
var paddledx = WIDTH/75;
var paddlewidth = WIDTH/4;
var dx, dy = 0;
var intervalID = 0;
var canvas, ctx;
var rightDown = false;
var leftDown = false;
var mouser = "deleteme";
var windowMinX = 0;
var windowMaxX = 0;
var NROWS = 5;
var NCOLS = 5;
var BRICKWIDTH = (WIDTH/NCOLS) - 1;
var BRICKHEIGHT = 15;
var PADDING = 1;
var bricks;
function init() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
windowMinX = $("#canvas").offset().left;
windowMaxX = windowMinX + $("#canvas").width();
initbricks();
draw();
}
function initbricks() {
bricks = new Array(NROWS);
for (i=0; i < NROWS; i++) {
bricks[i] = new Array(NCOLS);
for (j=0; j < NCOLS; j++) {
bricks[i][j] = 1;
}
}
}
function startdraw() {
x = WIDTH/2;
y = HEIGHT-10;
dx = parseFloat(document.getElementById("dx").value);
dy = parseFloat(document.getElementById("dy").value);
clearInterval(intervalID);
intervalID = setInterval(draw, 20);
initbricks();
}
function abort() {
clearInterval(intervalID);
}
function draw() {
ctx.clearRect(0,0,300,300);
//draw ball
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(x,y,10,0,Math.PI*2,true);
ctx.fill();
px = paddlex - paddlewidth / 2;
ctx.fillRect(px, HEIGHT-10, paddlewidth, HEIGHT);
//draw bricks
for (i=0; i < NROWS; i++) {
for (j=0; j < NCOLS; j++) {
if (bricks[i][j] == 1) {
ctx.fillRect((j * (BRICKWIDTH + PADDING)) + PADDING,
(i * (BRICKHEIGHT + PADDING)) + PADDING,
BRICKWIDTH, BRICKHEIGHT);
}
}
}
//update x and y
x += dx;
y += dy;
//have we hit a brick?
rowheight = BRICKHEIGHT + PADDING;
colwidth = BRICKWIDTH + PADDING;
row = Math.floor(y/rowheight)
col = Math.floor(x/colwidth)
if (y < NROWS * rowheight && row >= 0 && col >= 0 && bricks[row][col] == 1) {
dy = -dy;
bricks[row][col] = 0;
}
//have we hit a wall and/or the paddle?
if (x > WIDTH || x < 0) {
x -= 2*dx;
dx = -dx;
}
if ((y > HEIGHT && x > px && x < px + paddlewidth) || y < 0) {
y -= 2*dy;
dy = -dy;
}
else if (y > HEIGHT) {
//TODO: you lose!
clearInterval(intervalID);
}
if (leftDown)
paddlex -= paddledx;
else if (rightDown)
paddlex += paddledx;
}
function doKeyDown(evt) {
//right is 39 left is 37
if (evt.keyCode == 39) {
rightDown = true;
}
else if (evt.keyCode == 37) {
leftDown = true;
}
}
function doKeyUp(evt) {
if (evt.keyCode == 39) {
rightDown = false;
}
else if (evt.keyCode == 37) {
leftDown = false;
}
}
function mousemove(evt) {
if (evt.pageX > windowMinX && evt.pageX < windowMaxX) {
paddlex = evt.pageX - windowMinX;
}
}
function touchmove(evt) {
if (evt.touches.length==1) {
paddlex = evt.touches[0].pageX - windowMinX;
}
}
window.addEventListener('keydown',doKeyDown,false);
window.addEventListener('keyup',doKeyUp,false);
window.addEventListener('mousemove',mousemove,false);
window.addEventListener('touchmove',touchmove,false);
</script>
</head>
<body onload="init()">
<canvas id="canvas" width="300" height="300" style="border:1px solid;"></canvas>
<br>
<form>
dx: <input type="text" id="dx" size="4" value="1"/>
dy: <input type="text" id="dy" size="4" value="-4"/>
<input type="submit" onclick="startdraw(); return false;" value="start">
<input type="submit" onclick="abort(); return false;" value="clear">
</body>
</html>