Skip to content

Commit

Permalink
autoDrop works and the shape stops at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
kubowania committed Jul 12, 2019
1 parent cab7e7a commit 9fd6298
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 4 deletions.
22 changes: 21 additions & 1 deletion css/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
body {
div {
background-color: red;
height: 20px;
width: 20px;
}

.block {
background-color: blue;
}

.takenBlock {
background-color: blue;
}

.grid {
display: flex;
flex-wrap: wrap;
width: 200px;
}

.end {
background-color: white;
}
218 changes: 216 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,225 @@
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Tetris</title>
<title>Tetris!</title>
<script src="js/app.js" charset="utf-8"></script>
<link rel="stylesheet" href="css/style.css">
</head>
<body>

<button type="button" name="button" id="start">start</button>
<div class="game">
<div class="grid">
<div class="block"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
<div class="end"></div>
</div>
</div>
</body>
</html>
50 changes: 49 additions & 1 deletion js/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
console.log('loaded')
document.addEventListener('DOMContentLoaded', () => {
const squares = document.querySelectorAll('.grid div')
const startBtn = document.getElementById('start')
let currentIndex = 0
const width = 10
const height = 20

//Make the square move left, right and down on the board
function moveSquare(e) {

squares[currentIndex].classList.remove('block')

switch(e.keyCode) {
case 37: //left
if(currentIndex % width !== 0) currentIndex -= 1
break
case 39: //right
if(currentIndex % width < width - 1) currentIndex += 1
break
case 40: //down with a time loop of 1000
if(currentIndex + width < width * height)currentIndex += width
break
}

squares[currentIndex].classList.add('block')
}

document.addEventListener('keyup', moveSquare)

//loop the shape to continue going down
function autoDrop() {
squares[currentIndex].classList.remove('block')

if(currentIndex + width < width * height) {
currentIndex += width
squares[currentIndex].classList.add('block')
} else if (squares[currentIndex + width].classList.contains('end')) {
currentIndex+=0
squares[currentIndex].classList.add('takenBlock')
}

}

startBtn.addEventListener('click', () => {
setInterval(autoDrop, 100)
})


})

0 comments on commit 9fd6298

Please sign in to comment.