Skip to content

Commit 10a9f27

Browse files
committed
generate playfield from game object
1 parent 3ed0ba7 commit 10a9f27

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h1>TETRIS</h1>
1313
<button class= "play-button">play</button>
1414
</header>
1515
<main>
16-
<section class="player1">
16+
<section class="player0">
1717
<div class="info">
1818
<p>Score:&nbsp;<span class="score-span">000</span></p>
1919
<ul class="controls">

scripts/tetris.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function testJestConnection() {
4141

4242
// DOM Elements
4343

44-
const playMatrixView = document.querySelector('.play-matrix')
45-
const playerScoreView = document.querySelector('.info .score-span')
44+
const playMatrixView = document.querySelector('.player0 .play-matrix')
45+
const playerScoreView = document.querySelector('.player0 .info .score-span')
4646
const globalPlayButton = document.querySelector('.play-button')
4747
const pageMain = document.querySelector('main')
4848
const playerCoreHTML = '<div class="info"><p>Score:&nbsp;<span class="score-span">000</span></p><ul class="controls"><p>Controls:</p></ul></div><div class="play-decorator"><div class="play-matrix"></div></div>'
@@ -242,13 +242,16 @@ const playerInputScheme = {
242242

243243
// **************************************************************************
244244
// Build play window
245-
246245
class TetrisGame {
247246
constructor(playerNumber = 1, displayParent = pageMain){
248247
this.playerNumber = playerNumber
249248
this.playerName = 'player' + playerNumber
250249
this.displayParent = displayParent
250+
this.playerSection = {}
251+
this.playMatrixView = {}
251252
this.coreHTML = playerCoreHTML
253+
this.playMatrix = []
254+
this.landedShape = []
252255

253256
this.initPlayspace()
254257
}
@@ -260,7 +263,11 @@ class TetrisGame {
260263

261264
this.displayParent.appendChild(newPlayerSection)
262265

263-
this.playerSection = newPlayerSection
266+
this.playMatrixView = newPlayerSection.querySelector('.play-matrix')
267+
268+
const buildReturn = buildNewPlayMatrix(playMatrixHeight + maxShapeSize, playMatrixWidth, this.playMatrixView)
269+
this.playMatrix = buildReturn[0]
270+
this.landedShape = buildReturn[1]
264271
}
265272
}
266273

@@ -271,7 +278,7 @@ players.push(new TetrisGame)
271278

272279
const tetrominoSpawnYX = [tetrominoSpawnXY[1],tetrominoSpawnXY[0]]
273280

274-
function buildNewPlayMatrix(height, width){
281+
function buildNewPlayMatrix(height, width, playMatrixView){
275282
playMatrixView.innerHTML = ''
276283
playMatrix = new Array
277284
landedShape = new LandedShape
@@ -288,10 +295,10 @@ function buildNewPlayMatrix(height, width){
288295
playMatrix[y].push(playCell)
289296
}
290297
}
291-
return (playMatrix, landedShape)
298+
return [playMatrix, landedShape]
292299
}
293300

294-
buildNewPlayMatrix(playMatrixHeight + maxShapeSize, playMatrixWidth) //todo: refactor to use return
301+
buildNewPlayMatrix(playMatrixHeight + maxShapeSize, playMatrixWidth, playMatrixView) //todo: refactor to use return
295302

296303
// inject control legend
297304
for (const controlKey in playerInputScheme) { //todo: refactor to for-of

0 commit comments

Comments
 (0)