forked from kubowania/Tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autoDrop works and the shape stops at the end
- Loading branch information
Showing
3 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
|
||
}) |