61 |
62 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - FULL GAME/sounds/brick_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - FULL GAME/sounds/brick_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - FULL GAME/sounds/life_lost.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - FULL GAME/sounds/life_lost.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - FULL GAME/sounds/paddle_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - FULL GAME/sounds/paddle_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - FULL GAME/sounds/wall.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - FULL GAME/sounds/wall.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - FULL GAME/sounds/win.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - FULL GAME/sounds/win.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/components.js:
--------------------------------------------------------------------------------
1 | /////// LOAD IMAGES ////////
2 |
3 |
4 |
5 | /////// END LOAD IMAGES ////////
6 |
7 | // ************************ //
8 |
9 | /////// LOAD SOUNDS ////////
10 |
11 |
12 |
13 | /////// END LOAD SOUNDS ////////
14 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/game.js:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////
2 | //* BreakOut JavaScript - Code Explained *//
3 | ////////////////////////////////////////////
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/SOUND_OFF.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/SOUND_OFF.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/SOUND_ON.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/SOUND_ON.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/bg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/bg.jpg
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/bg1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/bg1.jpg
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/level.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/level.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/life.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/life.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/score.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/img/score.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/brick_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/brick_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/life_lost.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/life_lost.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/paddle_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/paddle_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/wall.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/wall.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/win.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 1 - Starter Template files/sounds/win.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/components.js:
--------------------------------------------------------------------------------
1 | /////// LOAD IMAGES ////////
2 |
3 | // LOAD BG IMAGE
4 | const BG_IMG = new Image();
5 | BG_IMG.src = "img/bg.jpg";
6 |
7 | /////// END LOAD IMAGES ////////
8 |
9 | // ************************ //
10 |
11 | /////// LOAD SOUNDS ////////
12 |
13 |
14 |
15 | /////// END LOAD SOUNDS ////////
16 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/game.js:
--------------------------------------------------------------------------------
1 | // SELECT CANVAS ELEMENT
2 | const cvs = document.getElementById("breakout");
3 | const ctx = cvs.getContext("2d");
4 |
5 | // ADD BORDER TO CANVAS
6 | cvs.style.border = "1px solid #0ff";
7 |
8 | // MAKE LINE THIK WHEN DRAWING TO CANVAS
9 | ctx.lineWidth = 3;
10 |
11 | // GAME VARIABLES AND CONSTANTS
12 | const PADDLE_WIDTH = 100;
13 | const PADDLE_MARGIN_BOTTOM = 50;
14 | const PADDLE_HEIGHT = 20;
15 | const BALL_RADIUS = 8;
16 | let LIFE = 3; // PLAYER HAS 3 LIVES
17 | let leftArrow = false;
18 | let rightArrow = false;
19 |
20 | // CREATE THE PADDLE
21 | const paddle = {
22 | x : cvs.width/2 - PADDLE_WIDTH/2,
23 | y : cvs.height - PADDLE_MARGIN_BOTTOM - PADDLE_HEIGHT,
24 | width : PADDLE_WIDTH,
25 | height : PADDLE_HEIGHT,
26 | dx :5
27 | }
28 |
29 | // DRAW PADDLE
30 | function drawPaddle(){
31 | ctx.fillStyle = "#2e3548";
32 | ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
33 |
34 | ctx.strokeStyle = "#ffcd05";
35 | ctx.strokeRect(paddle.x, paddle.y, paddle.width, paddle.height);
36 | }
37 |
38 | // CONTROL THE PADDLE
39 | document.addEventListener("keydown", function(event){
40 | if(event.keyCode == 37){
41 | leftArrow = true;
42 | }else if(event.keyCode == 39){
43 | rightArrow = true;
44 | }
45 | });
46 | document.addEventListener("keyup", function(event){
47 | if(event.keyCode == 37){
48 | leftArrow = false;
49 | }else if(event.keyCode == 39){
50 | rightArrow = false;
51 | }
52 | });
53 |
54 | // MOVE PADDLE
55 | function movePaddle(){
56 | if(rightArrow && paddle.x + paddle.width < cvs.width){
57 | paddle.x += paddle.dx;
58 | }else if(leftArrow && paddle.x > 0){
59 | paddle.x -= paddle.dx;
60 | }
61 | }
62 |
63 | // CREATE THE BALL
64 | const ball = {
65 | x : cvs.width/2,
66 | y : paddle.y - BALL_RADIUS,
67 | radius : BALL_RADIUS,
68 | speed : 4,
69 | dx : 3 * (Math.random() * 2 - 1),
70 | dy : -3
71 | }
72 |
73 | // DRAW THE BALL
74 | function drawBall(){
75 | ctx.beginPath();
76 |
77 | ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI*2);
78 | ctx.fillStyle = "#ffcd05";
79 | ctx.fill();
80 |
81 | ctx.strokeStyle = "#2e3548";
82 | ctx.stroke();
83 |
84 | ctx.closePath();
85 | }
86 |
87 | // MOVE THE BALL
88 | function moveBall(){
89 | ball.x += ball.dx;
90 | ball.y += ball.dy;
91 | }
92 |
93 | // BALL AND WALL COLLISION DETECTION
94 | function ballWallCollision(){
95 | if(ball.x + ball.radius > cvs.width || ball.x - ball.radius < 0){
96 | ball.dx = - ball.dx;
97 | }
98 |
99 | if(ball.y - ball.radius < 0){
100 | ball.dy = -ball.dy;
101 | }
102 |
103 | if(ball.y + ball.radius > cvs.height){
104 | LIFE--; // LOSE LIFE
105 | resetBall();
106 | }
107 | }
108 |
109 | // RESET THE BALL
110 | function resetBall(){
111 | ball.x = cvs.width/2;
112 | ball.y = paddle.y - BALL_RADIUS;
113 | ball.dx = 3 * (Math.random() * 2 - 1);
114 | ball.dy = -3;
115 | }
116 |
117 | // BALL AND PADDLE COLLISION
118 | function ballPaddleCollision(){
119 | if(ball.x < paddle.x + paddle.width && ball.x > paddle.x && paddle.y < paddle.y + paddle.height && ball.y > paddle.y){
120 |
121 | // CHECK WHERE THE BALL HIT THE PADDLE
122 | let collidePoint = ball.x - (paddle.x + paddle.width/2);
123 |
124 | // NORMALIZE THE VALUES
125 | collidePoint = collidePoint / (paddle.width/2);
126 |
127 | // CALCULATE THE ANGLE OF THE BALL
128 | let angle = collidePoint * Math.PI/3;
129 |
130 |
131 | ball.dx = ball.speed * Math.sin(angle);
132 | ball.dy = - ball.speed * Math.cos(angle);
133 | }
134 | }
135 |
136 | // DRAW FUNCTION
137 | function draw(){
138 | drawPaddle();
139 |
140 | drawBall();
141 | }
142 |
143 | // UPDATE GAME FUNCTION
144 | function update(){
145 | movePaddle();
146 |
147 | moveBall();
148 |
149 | ballWallCollision();
150 |
151 | ballPaddleCollision();
152 | }
153 |
154 | // GAME LOOP
155 | function loop(){
156 | // CLEAR THE CANVAS
157 | ctx.drawImage(BG_IMG, 0, 0);
158 |
159 | draw();
160 |
161 | update();
162 |
163 | requestAnimationFrame(loop);
164 | }
165 | loop();
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/SOUND_OFF.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/SOUND_OFF.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/SOUND_ON.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/SOUND_ON.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/bg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/bg.jpg
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/bg1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/bg1.jpg
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/level.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/level.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/life.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/life.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/score.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/img/score.png
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 2D Breakout Game - JavScript
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/brick_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/brick_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/life_lost.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/life_lost.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/paddle_hit.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/paddle_hit.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/wall.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/wall.mp3
--------------------------------------------------------------------------------
/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/win.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeExplainedRepo/2D-Breakout-Game-JavaScript/d3903312529aea0e3b541b691dc3eaaad84b5bc4/2D Breakout Game JavaScript - PART 2 - Starter Template files/sounds/win.mp3
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 2D-Breakout-Game-JavaScript
2 |
3 | Code Explained Says Hi.
4 |
5 | Welcome to another tutorial, where we're going to create a game called "2D Breakout" using JavaScript and HTML 5, the tutorial is created especially for beginners in JavaScript. We will discuss the logic behind every line of code, And try to break down things as much as we can. and after that, we will open our text editors and start typing the code.
6 |
7 | In the 2D Breakout game, you need to break all the bricks using a bouncing ball, a ball that you need to prevent from leaving the game area using the paddle.
8 |
9 | This tutorial will be long, that's why I chose to split it into two parts.
10 |
11 | In the first part, we will be able, to draw the paddle and the ball, we will be able to control the paddle using the left and the right arrows on the keyboard, we will make the ball moves, and we will also implement the collision detection logic, so when the ball hits a wall it must change the direction.
12 |
13 | When the ball hit the paddle, the ball should go in a definite direction based on where the ball hit the paddle so the paddle won't act like a wall. which means you can determine where the ball goes when you hit it with the paddle.
14 |
15 | In the second part, we will implement the bricks in the game, add a collision detection function, when the ball hit a brick, the brick must disappear and then increment the player's score.
16 |
17 | The player has 3 lives, when he loses a life, we reset the ball position and give him a chance to continue playing, when he loses all the 3 lives, it's a game over, we show him a game over message, and a "play again" button to play over.
18 |
19 | To win the game, the player has to break all the bricks on each level, and he has to complete 7 levels, when he passes from a level to another, the number of bricks increases and also the speed of the ball.
20 |
21 | We will also implement some sounds in the game, and we will create a button for when you want to turn ON/OFF the sounds.
22 |
23 | ****************************************************************
24 | The video tutorial for the 1st part : https://www.youtube.com/watch?v=FyZ4_T0GZ1U
25 |
26 | The video tutorial for the 2nd part : Coming Soon
27 |
28 | *****************************************************************
29 |
30 | Support Us, by a LIKE, COMMENT, SUBSCRIBE. as I put a lot of time to create these tutorials, and make them accessible by anyone for free.
31 |
32 | *****************************************************************
33 |
34 | Here you can find other tutorials, that you might like to see:
35 |
36 | Ping Pong Game Using JavaScript
37 | https://youtu.be/nl0KXCa5pJk
38 |
39 | Create a Multiple Choice Quiz Using JavaScript
40 | https://youtu.be/49pYIMygIcU
41 |
42 | Tetris Game Using JavaScript
43 | https://youtu.be/HEsAr2Yt2do
44 |
45 | Snake Game Using JavaScript
46 | https://youtu.be/9TcU2C1AACw
47 |
48 | Flappy Bird Game Using JavaScript
49 | https://youtu.be/L07i4g-zhDA
50 |
51 |
--------------------------------------------------------------------------------