Skip to content

Commit

Permalink
square freezing now need to apply to all shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
kubowania committed Jul 14, 2019
1 parent c4820b6 commit 18d3c23
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 55 deletions.
5 changes: 5 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ h2 {
/* background-color: #4BC39B; */
}

.block2 {
background-image: url(/Users/limit/development/Tetris/images/purple_block.png);
/* background-color: #4BC39B; */
}

.oTetromino {
background-color: green;
}
Expand Down
24 changes: 12 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Tetris!</title>
<script src="js/app.js" charset="utf-8"></script>
<script src="js/app2.js" charset="utf-8"></script>
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400&display=swap" rel="stylesheet">
</head>
Expand All @@ -15,7 +15,6 @@ <h1>to tetris</h1>
<main>
<div class="game">
<div class="grid">
<div class="block"></div>
<div></div>
<div></div>
<div></div>
Expand Down Expand Up @@ -215,16 +214,17 @@ <h1>to tetris</h1>
<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>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
<div class="block2"></div>
</div>
</div>
<section class="column-left">
Expand Down
107 changes: 64 additions & 43 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,34 @@ document.addEventListener('DOMContentLoaded', () => {
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('block')
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('block2')) {
currentIndex+=0
squares[currentIndex].classList.add('block2')
// freeze()
}
}

// function freeze() {
// if (squares[currentIndex + width].classList.contains('block2')) {
// return autoDrop()
// }
// }
setInterval(autoDrop, 200)

function gameLoop() {
const currentIndex = 0
squares[currentIndex].classList.add('block')
autoDrop()
}
setInterval(gameLoop,4000)

// startBtn.addEventListener('click', () => {
// setInterval(autoDrop, 1000)
// setInterval(autoDrop, 200)
// })


Expand All @@ -60,40 +74,47 @@ document.addEventListener('DOMContentLoaded', () => {

console.log(random)

//move the theTetrominoes

let currentPosition = 0

function moveDown() {



const current = theTetrominoes[random]

current.forEach( index => {
squares[currentPosition + index].classList.add('block')
})

current.forEach( index => {
squares[currentPosition + index].classList.remove('block')
})

currentPosition = currentPosition + width

current.forEach( index => {
squares[currentPosition + index + width].classList.add('block')
})



console.log(currentPosition)

}

// squares[currentPosition + index].classList.remove('block')
// currentPosition +=width
// squares[currentPosition + index].classList.add('block')

setInterval(moveDown, 500)

// move the Tetrominoes
// let currentPosition = 0
//
// function moveDown() {
//
// const current = theTetrominoes[random]
//
//
// current.forEach( index => {
// squares[currentPosition + index].classList.add('block')
// })
// console.log(currentPosition)
//
// current.forEach( index => {
// squares[currentPosition + index].classList.remove('block')
// })
// console.log(currentPosition)
//
// currentPosition = currentPosition += width
//
// current.forEach( index => {
// squares[currentPosition + index + width].classList.add('block')
// console.log(currentPosition)
// })
// }
//
//
// console.log(currentPosition)
//
//
// startBtn.addEventListener('click', () => {
// setInterval(moveDown, 200)
// })
//
// //stop the blocks
// if (squares[currentPosition + width].classList.contains('block')) {
// currentPosition+=0
// squares[currentPosition].classList.add('block')
// }
//
})
114 changes: 114 additions & 0 deletions js/app2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
document.addEventListener('DOMContentLoaded', () => {
const squares = document.querySelectorAll('.grid div')
const startBtn = document.querySelector('.button')
let currentIndex = 0
const width = 10
const height = 20

//The Tetrominoes
const lTetromino = [1,1,width+1,width*2+1]
const zTetromino = [width+1, width+2,width*2,width*2+1]
const tTetromino = [1,width,width+1,width+2]
const oTetromino = [0,1,width,width+1]
const iTetromino = [1,width+1,width*2+1]

const theTetrominoes = [lTetromino, zTetromino, tTetromino, oTetromino, iTetromino]

//Randomly Select Tetromino
const random = Math.ceil(Math.random()*theTetrominoes.length)-1
console.log(random)

//move the Tetromino moveDown
let currentPosition = 4

//draw the shape
function draw() {
const current = theTetrominoes[random]
current.forEach( index => {
squares[currentPosition + index].classList.add('block')
})
}

//undraw the shape
function undraw() {
const current = theTetrominoes[random]
current.forEach( index => {
squares[currentPosition + index].classList.remove('block')
})
}

//move down on loop
function moveDown() {
draw()
undraw()
currentPosition = currentPosition += width
draw()
}
moveDown()
setInterval(moveDown, 200)

//move left
function moveleft() {
draw()
undraw()
currentPosition = currentPosition +1
//how to I stop the whole shape if ANY of the shapes Classlist are too long for the width?
draw()
}
moveleft()

//move right
function moveright() {
draw()
undraw()
currentPosition = currentPosition -1
//how to I stop the whole shape if ANY of the shapes Classlist are too long for the width?
draw()
}
moveright()

//freeze the shape

//if ANY of the tetrominos currentindexes meet a div with block in it plus width, i want to envoke freeze function and change them to classList block2. this function will change ALL tetrominos classLists into block2. it will also stop them from moving.

function freeze() {
if squares[currentPosition + width].classList.contains('block2')) {
currentIndex+=0
squares[currentPosition].classList.add('block2')
current.forEach( index => {
squares[currentPosition + index].classList.add('block2')
})
}
}

freeze()


//Rotate the Tetromino
const lTetromino2 = [width,width+1,width+2,width*2+2]
const zTetromino2 = [0,width,width+1,width*2+1]
const tTetromino2 = [1,width+1,width+2,width*2+1]
const oTetromino2 = [0,1,width,width+1]
const iTetromino2 = [1,width+1,width+2]

const lTetromino3 = [width+1, width+2,width*2,width*2+1]
const zTetromino3 = [width+1,width+2,width*2,width*2+1]
const tTetromino3 = [width,width+1,width+2,width*2+1]
const oTetromino3 = [0,1,width,width+1]
const iTetromino3 = [1,width+1,width*2+1]

const lTetromino4 = [width,width*2,width*2+1,width*2+2]
const zTetromino4 = [0,width,width+1,width*2+1]
const tTetromino4 = [1,width,width+1,width*2+1]
const oTetromino4 = [0,1,width,width+1]
const iTetromino4 = [1,width+1,width+2]


//Stop the Tetromino at the sides


//Stop the Tetromino at the end



})
9 changes: 9 additions & 0 deletions js/app3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
document.addEventListener('DOMContentLoaded', () => {
const squares = document.querySelectorAll('.grid div')
const startBtn = document.querySelector('.button')
let currentIndex = 0
const width = 10
const height = 20
const empty = 'white'

})

0 comments on commit 18d3c23

Please sign in to comment.