Skip to content

Commit 563cf52

Browse files
committed
update
1 parent c4c99a9 commit 563cf52

File tree

1 file changed

+34
-93
lines changed

1 file changed

+34
-93
lines changed

test.js

+34-93
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ class Controller {
2222
this.bindupdateGrid = this.bindupdateGrid.bind(this);
2323
this.modele.bindupdateGrid(this.bindupdateGrid);
2424

25-
this.bindifCollisionStop = this.bindifCollisionStop.bind(this);
26-
this.view.bindifCollisionStop(this.bindifCollisionStop);
27-
2825
}
2926

3027
bindgetEmptyGrid(grid) {
@@ -43,9 +40,7 @@ class Controller {
4340
this.view.updateGrid();
4441
}
4542

46-
bindifCollisionStop(grid) {
47-
this.modele.ifCollisonStop(grid);
48-
}
43+
4944
}
5045

5146
export let ctx;
@@ -82,11 +77,6 @@ class TetrisView {
8277
this.drop = callback; // On veut pouvoir actualiser la View (depuis le Controller) quand nous récupérons les données.
8378
}
8479

85-
bindifCollisionStop (callback) {
86-
// Définition d'une nouvelle propriété pouvant être utilisée à partir d'une instance de Model.
87-
this.ifCollisonStop = callback; // On veut pouvoir actualiser la View (depuis le Controller) quand nous récupérons les données.
88-
}
89-
9080
updateGrid() {
9181
// On efface la grille
9282
ctx.fillStyle = ctx.background;
@@ -115,7 +105,35 @@ class TetrisView {
115105
for (let i = 0; i < 20; i++) {
116106
for (let j = 0; j < 10; j++) {
117107
if (grid[i][j] !== 0) {
118-
ctx.fillStyle = 'blue';
108+
switch (grid[i][j]) {
109+
case 1:
110+
ctx.fillStyle = 'blue';
111+
break;
112+
113+
case 2:
114+
ctx.fillStyle = 'green';
115+
break;
116+
117+
case 3:
118+
119+
ctx.fillStyle = 'red';
120+
break;
121+
122+
case 4:
123+
124+
ctx.fillStyle = 'purple';
125+
break;
126+
127+
case 5:
128+
ctx.fillStyle = 'grey';
129+
break;
130+
case 6:
131+
ctx.fillStyle = 'yellow';
132+
break;
133+
case 7:
134+
ctx.fillStyle = 'pink';
135+
break;
136+
}
119137
ctx.fillRect(j * 35, i * 32, 35, 32);
120138
}
121139
}
@@ -161,6 +179,7 @@ class TetrisView {
161179
start() {
162180
this.getRandomPiece(grid);
163181
console.log(grid);
182+
this.drop();
164183
}
165184

166185
//Pause the game
@@ -180,8 +199,6 @@ class TetrisModel {
180199
this.level = 1;
181200
this.lines = 0;
182201
this.gameOver = false;
183-
this.currentPiece;
184-
this.nextPiece;
185202
}
186203

187204
// Binding.
@@ -198,92 +215,16 @@ class TetrisModel {
198215
//Focntion qui se lance au démarrage du jeu
199216
start() {
200217
this.getEmptyGrid();
201-
}
202218

203-
//Fonction qui arrete la piece si checkCollision renvoie true
204-
ifCollisonStop() {
205-
if (this.checkCollision(this.currentPiece)) {
206-
this.currentPiece.y--;
207-
this.currentPiece.insertPiece();
208-
this.currentPiece = this.nextPiece;
209-
this.nextPiece = new Piece(pieces[Math.floor(Math.random() * pieces.length)], grid);
210-
this.updateGrid();
211-
this.checkLines();
212-
if (this.checkCollision(this.currentPiece)) {
213-
this.gameOver = true;
214-
this.pause();
215-
}
216-
} else {
217-
this.currentPiece.y++;
218-
}
219-
this.updateGrid();
220219
}
221220

222-
//Fonction qui vérifie si une ligne est remplie
223-
checkLines() {
224-
for (let i = 0; i < 20; i++) {
225-
let line = true;
226-
for (let j = 0; j < 10; j++) {
227-
if (grid[i][j] === 0) {
228-
line = false;
229-
}
230-
}
231-
if (line) {
232-
this.lines++;
233-
this.score += 100;
234-
this.level = Math.floor(this.lines / 10) + 1;
235-
for (let k = i; k > 0; k--) {
236-
for (let j = 0; j < 10; j++) {
237-
grid[k][j] = grid[k - 1][j];
238-
}
239-
}
240-
for (let j = 0; j < 10; j++) {
241-
grid[0][j] = 0;
242-
}
243-
}
244-
}
245-
}
246-
247-
//Ecrit la fonction gameover
248-
gameOver() {
249-
if (this.gameOver) {
250-
alert('Game Over');
251-
}
252-
}
253-
254-
//Fonction qui regarde si la pièce ne sort pas de la grille et si elle ne touche pas une autre pièce
255-
checkCollision(piece) {
256-
for (let i = 0; i < piece.length; i++) {
257-
for (let j = 0; j < piece[i].length; j++) {
258-
if (piece[i][j] !== 0) {
259-
if (i + piece.y >= 20 || j + piece.x < 0 || j + piece.x >= 10 || grid[i + piece.y][j + piece.x] !== 0) {
260-
return true;
261-
}
262-
}
263-
}
264-
}
265-
return false;
266-
}
267-
268-
//ecrit la fonction getrandompiece pour qu'elle ajoute une couleuer à la piece
269-
getRandomPiece(grid) {
270-
const pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z'];
271-
this.currentPiece = new Piece(pieces[Math.floor(Math.random() * pieces.length)], grid);
272-
this.nextPiece = new Piece(pieces[Math.floor(Math.random() * pieces.length)], grid);
273-
this.currentPiece.insertPiece();
274-
this.updateGrid();
275-
this.drop();
276-
}
277-
278-
//EN ATTENDANT DE TEST LA NVELLE FONCTION
279-
/*
280221
getRandomPiece(grid) {
281222
const pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z'];
282223
const new_piece = new Piece(pieces[Math.floor(Math.random() * pieces.length)], grid);
283224
new_piece.insertPiece();
284225
this.updateGrid();
285226
}
286-
*/
227+
287228
moveDown() {
288229
for (let i = 19; i >= 0; i--) {
289230
for (let j = 0; j < 10; j++) {
@@ -301,10 +242,10 @@ class TetrisModel {
301242
}
302243

303244
drop() {
245+
const pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z'];
246+
const new_piece = new Piece(pieces[Math.floor(Math.random() * pieces.length)], grid);
304247
this.intervalId = setInterval(() => {
305248
this.moveDown();
306-
this.checkCollision(this.currentPiece);
307-
console.log(grid);
308249
}, 1000);
309250
}
310251

0 commit comments

Comments
 (0)