Skip to content

Commit 83f7c5d

Browse files
authored
Add files via upload
1 parent 2538c65 commit 83f7c5d

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

breakout/index.js

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
const grid = document.querySelector('.grid')
2+
const scoreDisplay = document.querySelector('#score')
3+
const blockWidth = 100
4+
const blockHeight = 20
5+
const ballDiameter = 20
6+
const boardWidth = 560
7+
const boardHeight = 300
8+
let xDirection = -2
9+
let yDirection = 2
10+
11+
const userStart = [230, 10]
12+
let currentPosition = userStart
13+
14+
const ballStart = [270, 40]
15+
let ballCurrentPosition = ballStart
16+
17+
let timerId
18+
let score = 0
19+
20+
//my block
21+
class Block {
22+
constructor(xAxis, yAxis) {
23+
this.bottomLeft = [xAxis, yAxis]
24+
this.bottomRight = [xAxis + blockWidth, yAxis]
25+
this.topRight = [xAxis + blockWidth, yAxis + blockHeight]
26+
this.topLeft = [xAxis, yAxis + blockHeight]
27+
}
28+
}
29+
30+
//all my blocks
31+
const blocks = [
32+
new Block(10, 270),
33+
new Block(120, 270),
34+
new Block(230, 270),
35+
new Block(340, 270),
36+
new Block(450, 270),
37+
new Block(10, 240),
38+
new Block(120, 240),
39+
new Block(230, 240),
40+
new Block(340, 240),
41+
new Block(450, 240),
42+
new Block(10, 210),
43+
new Block(120, 210),
44+
new Block(230, 210),
45+
new Block(340, 210),
46+
new Block(450, 210),
47+
]
48+
49+
//draw my blocks
50+
function addBlocks() {
51+
for (let i = 0; i < blocks.length; i++) {
52+
const block = document.createElement('div')
53+
block.classList.add('block')
54+
block.style.left = blocks[i].bottomLeft[0] + 'px'
55+
block.style.bottom = blocks[i].bottomLeft[1] + 'px'
56+
grid.appendChild(block)
57+
console.log(blocks[i].bottomLeft)
58+
}
59+
}
60+
addBlocks()
61+
62+
//add user
63+
const user = document.createElement('div')
64+
user.classList.add('user')
65+
grid.appendChild(user)
66+
drawUser()
67+
68+
//add ball
69+
const ball = document.createElement('div')
70+
ball.classList.add('ball')
71+
grid.appendChild(ball)
72+
drawBall()
73+
74+
//move user
75+
function moveUser(e) {
76+
switch (e.key) {
77+
case 'ArrowLeft':
78+
if (currentPosition[0] > 0) {
79+
currentPosition[0] -= 10
80+
console.log(currentPosition[0] > 0)
81+
drawUser()
82+
}
83+
break
84+
case 'ArrowRight':
85+
if (currentPosition[0] < (boardWidth - blockWidth)) {
86+
currentPosition[0] += 10
87+
console.log(currentPosition[0])
88+
drawUser()
89+
}
90+
break
91+
}
92+
}
93+
document.addEventListener('keydown', moveUser)
94+
95+
//draw User
96+
function drawUser() {
97+
user.style.left = currentPosition[0] + 'px'
98+
user.style.bottom = currentPosition[1] + 'px'
99+
}
100+
101+
//draw Ball
102+
function drawBall() {
103+
ball.style.left = ballCurrentPosition[0] + 'px'
104+
ball.style.bottom = ballCurrentPosition[1] + 'px'
105+
}
106+
107+
//move ball
108+
function moveBall() {
109+
ballCurrentPosition[0] += xDirection
110+
ballCurrentPosition[1] += yDirection
111+
drawBall()
112+
checkForCollisions()
113+
}
114+
timerId = setInterval(moveBall, 30)
115+
116+
//check for collisions
117+
function checkForCollisions() {
118+
//check for block collision
119+
for (let i = 0; i < blocks.length; i++){
120+
if
121+
(
122+
(ballCurrentPosition[0] > blocks[i].bottomLeft[0] && ballCurrentPosition[0] < blocks[i].bottomRight[0]) &&
123+
((ballCurrentPosition[1] + ballDiameter) > blocks[i].bottomLeft[1] && ballCurrentPosition[1] < blocks[i].topLeft[1])
124+
)
125+
{
126+
const allBlocks = Array.from(document.querySelectorAll('.block'))
127+
allBlocks[i].classList.remove('block')
128+
blocks.splice(i,1)
129+
changeDirection()
130+
score++
131+
scoreDisplay.innerHTML = score
132+
if (blocks.length == 0) {
133+
scoreDisplay.innerHTML = 'You Win!'
134+
clearInterval(timerId)
135+
document.removeEventListener('keydown', moveUser)
136+
}
137+
}
138+
}
139+
// check for wall hits
140+
if (ballCurrentPosition[0] >= (boardWidth - ballDiameter) || ballCurrentPosition[0] <= 0 || ballCurrentPosition[1] >= (boardHeight - ballDiameter))
141+
{
142+
changeDirection()
143+
}
144+
145+
//check for user collision
146+
if
147+
(
148+
(ballCurrentPosition[0] > currentPosition[0] && ballCurrentPosition[0] < currentPosition[0] + blockWidth) &&
149+
(ballCurrentPosition[1] > currentPosition[1] && ballCurrentPosition[1] < currentPosition[1] + blockHeight )
150+
)
151+
{
152+
changeDirection()
153+
}
154+
155+
//game over
156+
if (ballCurrentPosition[1] <= 0) {
157+
clearInterval(timerId)
158+
scoreDisplay.innerHTML = 'You lose!'
159+
document.removeEventListener('keydown', moveUser)
160+
}
161+
}
162+
163+
164+
function changeDirection() {
165+
if (xDirection === 2 && yDirection === 2) {
166+
yDirection = -2
167+
return
168+
}
169+
if (xDirection === 2 && yDirection === -2) {
170+
xDirection = -2
171+
return
172+
}
173+
if (xDirection === -2 && yDirection === -2) {
174+
yDirection = 2
175+
return
176+
}
177+
if (xDirection === -2 && yDirection === 2) {
178+
xDirection = 2
179+
return
180+
}
181+
}

breakout/styles.css

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.grid {
2+
position: absolute;
3+
width: 560px;
4+
height: 300px;
5+
border: solid 1px black;
6+
margin-top: 100px;
7+
}
8+
9+
.user {
10+
position: absolute;
11+
width: 100px;
12+
height: 20px;
13+
background-color: blueviolet;
14+
}
15+
16+
.block {
17+
position: absolute;
18+
width: 100px;
19+
height: 20px;
20+
background-color: blue;
21+
}
22+
23+
.ball {
24+
position: absolute;
25+
width: 20px;
26+
height: 20px;
27+
border-radius: 10px;
28+
background-color: red;
29+
}

0 commit comments

Comments
 (0)