-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbreakout_6.js
131 lines (122 loc) · 3.43 KB
/
breakout_6.js
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
// JAVASCRIPT-6: BRICK COLLISIONS
// © 2018, wbamberg
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius = 12;
var x = ballRadius;
var y = ballRadius;
var dx = 2;
var dy = -2;
var paddleHeight = 100;
var paddleWidth = 12;
var paddleY = (canvas.height-paddleHeight)/2;
var downPressed = false;
var upPressed = false;
var brickRowCount = 5;
var brickColumnCount = 4;
var brickWidth = 25;
var brickHeight = 60;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 400;
var bricks = [];
for(var c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(var r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, visible: 1 };
}
}
function keyDownHandler(e) {
if(e.key == "Down" || e.key == "ArrowDown") {
downPressed = true;
}
else if(e.key == "Up" || e.key == "ArrowUp") {
upPressed = true;
}
}
function keyUpHandler(e) {
if(e.key == "Down" || e.key == "ArrowDown") {
downPressed = false;
}
else if(e.key == "Up" || e.key == "ArrowUp") {
upPressed = false;
}
}
function collisionDetection() {
for(var c=0; c<brickColumnCount; c++) {
for(var r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.visible == 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.visible = 0;
}
}
}
}
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, 2 * Math.PI);
ctx.fillStyle = "#FF0000";
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(0, paddleY, paddleWidth, paddleHeight);
ctx.fillStyle = "#333";
ctx.fill();
ctx.closePath();
}
function drawBricks() {
for(var c=0; c<brickColumnCount; c++) {
for(var r=0; r<brickRowCount; r++) {
if(bricks[c][r].visible == 1) {
var brickX = (c*(brickWidth+brickPadding))+brickOffsetLeft;
var brickY = (r*(brickHeight+brickPadding))+brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
drawBricks();
collisionDetection();
if(x + dx > canvas.width-ballRadius) {
dx = -dx;
}
else if (x + dx < ballRadius) {
if (y > paddleY && y < paddleY + paddleHeight) {
dx = -dx;
}
else {
alert("GAME OVER");
document.location.reload();
clearInterval(interval);
}
}
if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
dy = -dy;
}
if(downPressed && paddleY < canvas.height-paddleHeight) {
paddleY += 4;
}
else if(upPressed && paddleY > 4) {
paddleY -= 4;
}
x += dx;
y += dy;
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
var interval = setInterval(draw, 10);