Skip to content

Commit 637014d

Browse files
authored
Merge pull request #207 from abhinav280601/feat-#201-breakoutgame
Added a new Breakout-Game
2 parents 455686f + 60ff623 commit 637014d

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

Games/breakout/breakout.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
body{
2+
font-family: 'Press Start 2P', cursive;
3+
background-color:rgb(8, 6, 37);
4+
text-align: center;
5+
color:rgb(155, 226, 41) ;
6+
}
7+
canvas{
8+
background-color: rgb(61, 91, 117);
9+
}
10+
h1{
11+
position: relative;
12+
top: 35px;
13+
right: 180px;
14+
font-size: 35px;
15+
color: rgb(196, 219, 159);
16+
}
17+
.btn{
18+
font-family: 'Press Start 2P', cursive;
19+
height: 50px;
20+
width: 150px;
21+
position: relative;
22+
top: 8px;
23+
border-radius: 10px;
24+
font-size: 20px;
25+
background-color: rgb(228, 138, 54);
26+
}
27+
#score{
28+
position: relative;
29+
font-size: 30px;
30+
bottom: 20px;
31+
left: 280px;
32+
}

Games/breakout/breakout.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
const canvas = document.getElementById("canvas");
2+
const ctx = canvas.getContext("2d");
3+
const canvasH = canvas.height;
4+
const canvasW = canvas.width;
5+
let scoreAudio= new Audio('score.mp3');
6+
let hitAudio= new Audio('hit.mp3');
7+
let paddlehit= new Audio('paddlehit.mp3');
8+
let rightPressed = false;
9+
let leftPressed = false;
10+
let x,
11+
y,
12+
dy,
13+
dx,
14+
interval,
15+
radius,
16+
paddleW,
17+
paddleX,
18+
paddleY,
19+
brickW,
20+
brickH,
21+
brickOffset;
22+
let bricks = [];
23+
let score = 0;
24+
let brickCount = 14;
25+
let brickRows = 9;
26+
setVariables();
27+
drawBall();
28+
drawPaddle();
29+
createBrickArray();
30+
drawBricks();
31+
drawScore();
32+
paddleNavigation();
33+
// startGame()
34+
35+
function drawScore() {
36+
ctx.beginPath();
37+
ctx.fillStyle = "#000";
38+
ctx.fill();
39+
ctx.fillText("Score: " + score, 9, 10);
40+
ctx.closePath();
41+
}
42+
function createBrickArray() {
43+
for (let j = 0; j < brickRows; j++) {
44+
bricks[j] = [];
45+
for (let i = 0; i < brickCount; i++) {
46+
bricks[j][i] = { x: 0, y: 0, isVisible: true };
47+
}
48+
}
49+
}
50+
51+
function drawBricks() {
52+
for (let j = 0; j < brickRows; j++) {
53+
for (let i = 0; i < brickCount; i++) {
54+
if (bricks[j][i].isVisible) {
55+
const brickX = 18 + i * (brickW + brickOffset);
56+
const brickY = (10 + brickOffset) * (j + 1);
57+
bricks[j][i].x = brickX;
58+
bricks[j][i].y = brickY;
59+
ctx.beginPath();
60+
ctx.rect(brickX, brickY, brickW, brickH);
61+
ctx.fillStyle = "green";
62+
ctx.fill();
63+
ctx.closePath();
64+
}
65+
}
66+
}
67+
}
68+
69+
function paddleNavigation() {
70+
document.addEventListener("keydown", handleKeyDown);
71+
document.addEventListener("keyup", handleKeyUp);
72+
73+
function handleKeyDown(e) {
74+
if (e.key === "ArrowRight") {
75+
rightPressed = true;
76+
}
77+
if (e.key === "ArrowLeft") {
78+
leftPressed = true;
79+
}
80+
}
81+
82+
function handleKeyUp(e) {
83+
if (e.key === "ArrowRight") {
84+
rightPressed = false;
85+
}
86+
if (e.key === "ArrowLeft") {
87+
leftPressed = false;
88+
}
89+
}
90+
}
91+
92+
function detectCollision() {
93+
if (x + dx >= canvasW || x + dx < 0) {
94+
dx = -dx;
95+
}
96+
97+
if (y + dy > canvasH - radius) {
98+
if (x + dx > paddleX && x + dx < paddleX + paddleW) {
99+
paddlehit.play();
100+
dy = -dy;
101+
dx = dx + (x + dx - paddleX) / 100;
102+
}
103+
}
104+
105+
if (y + dy < 0
106+
) {
107+
dy = -dy;
108+
}
109+
110+
for (let b = 0; b < bricks.length; b++) {
111+
for (let i = 0; i < bricks[b].length; i++) {
112+
const brick = bricks[b][i];
113+
if (brick.isVisible) {
114+
if (
115+
x > brick.x &&
116+
x < brick.x + brickW &&
117+
y > brick.y &&
118+
y < brick.y + brickH
119+
) {
120+
bricks[b][i].isVisible = false;
121+
score += 1;
122+
scoreAudio.play();
123+
document.getElementById("score").innerHTML="Score: "+score;
124+
dy = -dy;
125+
checkYouWon();
126+
}
127+
}
128+
}
129+
}
130+
}
131+
132+
function startGame() {
133+
if (!interval) {
134+
interval = setInterval(() => {
135+
if (rightPressed) {
136+
paddleX = paddleX + 6;
137+
}
138+
if (leftPressed) {
139+
paddleX = paddleX - 6;
140+
}
141+
detectCollision();
142+
x = x + dx;
143+
y = y + dy;
144+
checkGameover();
145+
ctx.clearRect(0, 0, canvasW, canvasH);
146+
drawPaddle();
147+
drawBall();
148+
drawBricks();
149+
drawScore();
150+
}, 20);
151+
}
152+
}
153+
154+
function checkGameover() {
155+
if (y === canvasH) {
156+
hitAudio.play();
157+
alert("game over");
158+
clearInterval(interval);
159+
interval = null;
160+
setVariables();
161+
}
162+
}
163+
164+
function checkYouWon() {
165+
if (score === 126) {
166+
alert("You Won !!");
167+
clearInterval(interval);
168+
interval = null;
169+
setVariables();
170+
}
171+
}
172+
173+
function setVariables() {
174+
x = canvasW / 2;
175+
y = canvasH - 40;
176+
dx = 5;
177+
dy = -5;
178+
radius = 15;
179+
paddleW = 100;
180+
paddleX = canvasW / 2 - paddleW / 2;
181+
paddleY = canvasH - 25;
182+
183+
brickW = 40;
184+
brickH = 10;
185+
brickOffset = 8;
186+
}
187+
188+
function drawBall() {
189+
ctx.beginPath();
190+
ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
191+
ctx.fillStyle = "rgb(145, 216, 31)";
192+
ctx.fill();
193+
ctx.closePath();
194+
}
195+
196+
function drawPaddle() {
197+
ctx.beginPath();
198+
ctx.rect(paddleX, paddleY, paddleW, 20);
199+
ctx.fillStyle = "rgb(58, 8, 45)";
200+
ctx.fill();
201+
ctx.closePath();
202+
}

Games/breakout/hit.mp3

17.7 KB
Binary file not shown.

Games/breakout/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
<link rel="stylesheet" href="breakout.css">
7+
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
8+
<title>Breakout Game</title>
9+
</head>
10+
<body>
11+
<section style="display: flex; justify-content: center">
12+
<div>
13+
<h1>BreakOut Game</h1>
14+
<div>
15+
<p id="score">Score</p>
16+
</div>
17+
18+
<canvas
19+
id="canvas"
20+
height="550"
21+
width="700"
22+
style="border: 1px solid black; border-bottom: 2px solid brown"
23+
>Heyyy canvas is not supported</canvas
24+
>
25+
<div >
26+
<button class="btn" onclick="startGame()">Start</button>
27+
</div>
28+
</div>
29+
</section>
30+
<script src="breakout.js"></script>
31+
</body>
32+
</html>

Games/breakout/paddlehit.mp3

5.73 KB
Binary file not shown.

Games/breakout/score.mp3

4.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)