@@ -22,9 +22,6 @@ class Controller {
22
22
this . bindupdateGrid = this . bindupdateGrid . bind ( this ) ;
23
23
this . modele . bindupdateGrid ( this . bindupdateGrid ) ;
24
24
25
- this . bindifCollisionStop = this . bindifCollisionStop . bind ( this ) ;
26
- this . view . bindifCollisionStop ( this . bindifCollisionStop ) ;
27
-
28
25
}
29
26
30
27
bindgetEmptyGrid ( grid ) {
@@ -43,9 +40,7 @@ class Controller {
43
40
this . view . updateGrid ( ) ;
44
41
}
45
42
46
- bindifCollisionStop ( grid ) {
47
- this . modele . ifCollisonStop ( grid ) ;
48
- }
43
+
49
44
}
50
45
51
46
export let ctx ;
@@ -82,11 +77,6 @@ class TetrisView {
82
77
this . drop = callback ; // On veut pouvoir actualiser la View (depuis le Controller) quand nous récupérons les données.
83
78
}
84
79
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
-
90
80
updateGrid ( ) {
91
81
// On efface la grille
92
82
ctx . fillStyle = ctx . background ;
@@ -115,7 +105,35 @@ class TetrisView {
115
105
for ( let i = 0 ; i < 20 ; i ++ ) {
116
106
for ( let j = 0 ; j < 10 ; j ++ ) {
117
107
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
+ }
119
137
ctx . fillRect ( j * 35 , i * 32 , 35 , 32 ) ;
120
138
}
121
139
}
@@ -161,6 +179,7 @@ class TetrisView {
161
179
start ( ) {
162
180
this . getRandomPiece ( grid ) ;
163
181
console . log ( grid ) ;
182
+ this . drop ( ) ;
164
183
}
165
184
166
185
//Pause the game
@@ -180,8 +199,6 @@ class TetrisModel {
180
199
this . level = 1 ;
181
200
this . lines = 0 ;
182
201
this . gameOver = false ;
183
- this . currentPiece ;
184
- this . nextPiece ;
185
202
}
186
203
187
204
// Binding.
@@ -198,92 +215,16 @@ class TetrisModel {
198
215
//Focntion qui se lance au démarrage du jeu
199
216
start ( ) {
200
217
this . getEmptyGrid ( ) ;
201
- }
202
218
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 ( ) ;
220
219
}
221
220
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
- /*
280
221
getRandomPiece ( grid ) {
281
222
const pieces = [ 'I' , 'J' , 'L' , 'O' , 'S' , 'T' , 'Z' ] ;
282
223
const new_piece = new Piece ( pieces [ Math . floor ( Math . random ( ) * pieces . length ) ] , grid ) ;
283
224
new_piece . insertPiece ( ) ;
284
225
this . updateGrid ( ) ;
285
226
}
286
- */
227
+
287
228
moveDown ( ) {
288
229
for ( let i = 19 ; i >= 0 ; i -- ) {
289
230
for ( let j = 0 ; j < 10 ; j ++ ) {
@@ -301,10 +242,10 @@ class TetrisModel {
301
242
}
302
243
303
244
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 ) ;
304
247
this . intervalId = setInterval ( ( ) => {
305
248
this . moveDown ( ) ;
306
- this . checkCollision ( this . currentPiece ) ;
307
- console . log ( grid ) ;
308
249
} , 1000 ) ;
309
250
}
310
251
0 commit comments