Skip to content

Commit 385034a

Browse files
committed
added 2DBrickBreaker game
1 parent dc5f967 commit 385034a

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Loading

2DBrickBreaker/Somil-Shukla/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 2D-break-breaker

2DBrickBreaker/Somil-Shukla/app.js

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//Create variables to reference and store canvas
2+
let canvas = document.getElementById('mycanvas');
3+
let ctx = canvas.getContext('2d');
4+
let ballRadius = 10;
5+
let x = canvas.width/2;
6+
let y = canvas.height - 30;
7+
let dx = 2;
8+
let dy = -2;
9+
//create the paddle
10+
let paddleHeight = 12;
11+
let paddleWidth = 72;
12+
//specify starting point of paddle
13+
let paddleX = (canvas.width-paddleWidth)/2;
14+
//holding variables for right and left arrows on keyboard
15+
let rightPressed=false;
16+
let leftPressed=false;
17+
//holding variables for bricks
18+
let brickRowCount = 4;
19+
let brickColumnCount = 7;
20+
let brickWidth = 72;
21+
let brickHeight = 24;
22+
let brickPadding = 12;
23+
let brickOffsetTop = 32;
24+
let brickOffsetLeft = 32;
25+
//Create variables to take score
26+
let score = 0;
27+
28+
//Creating arrays for the bricks
29+
let bricks = [];
30+
for (c =0; c<brickColumnCount; c++){
31+
bricks[c] = [];
32+
for(r=0; r<brickRowCount; r++){
33+
//set the x and y position of the bricks
34+
bricks[c][r] = { x: 0, y: 0, status: 1};
35+
}
36+
}
37+
38+
document.addEventListener('keydown', keyDownHandler, false);
39+
document.addEventListener('keyup', keyUpHandler, false);
40+
document.addEventListener("mousemove", mouseMoveHandler, false);
41+
42+
//Anchor paddle movement to mouse movement
43+
function mouseMoveHandler(e) {
44+
var relativeX = e.clientX - canvas.offsetLeft;
45+
if(relativeX > 0 && relativeX < canvas.width) {
46+
paddleX = relativeX - paddleWidth/2;
47+
}
48+
}
49+
50+
function keyDownHandler(e){
51+
if(e.keyCode === 39){
52+
rightPressed = true;
53+
}
54+
else if (e.keyCode === 37){
55+
leftPressed = true;
56+
}
57+
}
58+
function keyUpHandler(e){
59+
if(e.keyCode === 39){
60+
rightPressed = false;
61+
}
62+
else if (e.keyCode === 37){
63+
leftPressed = false;
64+
}
65+
}
66+
67+
function drawBall(){
68+
ctx.beginPath();
69+
ctx.arc(x,y,ballRadius,0,Math.PI*2); //centered at (x,y) position with radius r = ballRadius starting at 0 = startAngle, ending at Math.PI*2 = endAngle (in Radians)
70+
ctx.fillStyle = 'red';
71+
ctx.fill();
72+
ctx.closePath();
73+
}
74+
//Create a function to create the paddle
75+
function drawPaddle(){
76+
ctx.beginPath();
77+
ctx.rect(paddleX,canvas.height-paddleHeight,paddleWidth,paddleHeight); //centered at (x,y) position with radius r = ballRadius starting at 0 = startAngle, ending at Math.PI*2 = endAngle (in Radians)
78+
ctx.fillStyle = 'blue';
79+
ctx.fill();
80+
ctx.closePath();
81+
}
82+
//Create a function to draw the bricks
83+
function drawBricks(){
84+
for(c=0; c < brickColumnCount; c++){
85+
for(r=0; r < brickRowCount; r++){
86+
if(bricks[c][r].status === 1){
87+
let brickX = (c* (brickWidth + brickPadding)) + brickOffsetLeft;
88+
let brickY = (r* (brickHeight+brickPadding)) + brickOffsetTop;
89+
bricks[c][r].x=brickX;
90+
bricks[c][r].y=brickY;
91+
ctx.beginPath();
92+
ctx.rect(brickX, brickY, brickWidth, brickHeight);
93+
ctx.fillStyle = '#6600cc';
94+
ctx.fill();
95+
ctx.closePath();
96+
}
97+
}
98+
}
99+
}
100+
//Create function to keep track of score
101+
function drawScore(){
102+
ctx.font = '18px Arial';
103+
ctx.fillStyle = 'brown';
104+
ctx.fillText('score: '+ score, 8, 20); //position score at 8,20 on the x,y axis of the canvas
105+
}
106+
107+
//Collision dections for the bricks
108+
function collisionDetection(){
109+
for(c=0; c<brickColumnCount;c++){
110+
for(r=0; r<brickRowCount; r++){
111+
let b = bricks[c][r];
112+
if(b.status === 1){
113+
if(x > b.x && x < b.x + brickWidth && y > b.y && y < b.y + brickHeight){
114+
dy = -dy;
115+
b.status = 0;
116+
score++;
117+
if (score === brickRowCount*brickColumnCount){
118+
alert('Congratulations!! You\'ve won!');
119+
document.location.reload();
120+
}
121+
}
122+
}
123+
}
124+
}
125+
}
126+
function draw(){
127+
//clear each instance of the canvas so a new circle can be drawn
128+
ctx.clearRect(0,0,canvas.width, canvas.height);
129+
drawScore();
130+
drawBricks();
131+
drawBall();
132+
drawPaddle();
133+
collisionDetection();
134+
//Calculate collision detections
135+
//left and right walls
136+
if(x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
137+
dx = -dx;
138+
}
139+
//top walls
140+
if(y + dy < ballRadius){
141+
dy = -dy;
142+
}
143+
else if (y + dy > canvas.height-ballRadius){
144+
//detect paddle hits
145+
if(x > paddleX && x < paddleX + paddleWidth){
146+
dy=-dy;
147+
}
148+
//if no paddle hit, body of canvas is hit ==> game over
149+
else {
150+
alert('GAME OVER!!');
151+
document.location.reload();
152+
}
153+
}
154+
//bottom wall
155+
if (y + dy > canvas.height - ballRadius || y + dy < ballRadius){
156+
dy = -dy;
157+
}
158+
//Make paddle move
159+
if(rightPressed && paddleX <canvas.width-paddleWidth){
160+
paddleX += 7;
161+
}
162+
else if(leftPressed && paddleX > 0){
163+
paddleX -= 7;
164+
}
165+
//Making the ball move
166+
x +=dx; //update x movement every frame
167+
y +=dy; //update y movement every frame
168+
}
169+
170+
//Create an infinite loop that creates the ball
171+
//paints the ball on the canvas every 10 milliseconds.
172+
setInterval(draw, 10)
173+
174+
175+
//Notes
176+
//Using HTML Canvas
177+
//Understanding HTML Coordinates
178+
//Web APIs - https://developer.mozilla.org/en-US/docs/Web/API
179+
// Drawing shapes with Canvas: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial
180+
//MDN BrickerBreaker Tutorial https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript
181+
182+
183+
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<link rel="stylesheet" href="styles.css">
8+
<title>2D Brick Breaker</title>
9+
</head>
10+
<body>
11+
<canvas id="mycanvas" width="650" height="450">
12+
I'm sorry your browser version does not support canvas <!--Fallback Content-->
13+
</canvas>
14+
<script src="app.js"></script>
15+
</body>
16+
</html>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#mycanvas{
2+
background-color: #31f0f0;
3+
margin: 125px 0px 125px 295px;
4+
}
5+

0 commit comments

Comments
 (0)