All there but errors need fixing

Dependencies:   mbed

Overview:

Rookie Tetris is a jigsaw style game based on the classic Tetris.

A block will appear at the top of the screen, you must move it (your options for movement are left, right and down - you cannot move up the board). The block will stop when it if placed either on the floor of the board or on-top of another block.

Your goal is to fill a complete row of the board with the blocks; when you do so the row will delete and the pattern above it will drop down. The game is over when your pattern is tall enough to reach to the top of the board!

Controls:

Use the joystick to move your block! Your block cannot move out of the parameters of the board.

Pot 2 controls the contrast of the screen.

Revision:
6:39cbec524483
Parent:
5:53e021832adc
Child:
8:cebb2aca8e19
diff -r 53e021832adc -r 39cbec524483 TetrisGame/TetrisGame.cpp
--- a/TetrisGame/TetrisGame.cpp	Sun May 31 17:35:30 2020 +0000
+++ b/TetrisGame/TetrisGame.cpp	Sun May 31 20:41:41 2020 +0000
@@ -10,15 +10,16 @@
     
 }
 
-void TetrisGame::init(int number, int x, int y, int speed)
+void TetrisGame::init(int x, int y, int height, int width, int speed)
 {
     // initialise game parameters
-    _number = blockArray[blocknumber];
-    _x = x;
-    _y = y;
+    _height = height;
+    _width = width;
+    _x = WIDTH/2 - _width/2;
+    _y = HEIGHT;
     _speed = speed;
     
-    _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // puts tetromino in middle SHOULD THIS INCLUDE ARRAY????
+    _tetromino.init(WIDTH/2 - 2, HEIGHT, 4, 4, _speed); // puts tetromino in middle SHOULD THIS INCLUDE ARRAY????
     
 }
 
@@ -36,24 +37,24 @@
     // board
     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
     
-    _tetromino.draw(lcd, _number);
+    _tetromino.draw(lcd);
     
     
 }
 
-void TetrisGame::update(Gamepad &pad)
+void TetrisGame::update(Gamepad &pad, N5110 &lcd)
 {
     // check_score(pad);
     
     // _p1.update(_d,_mag);
     _tetromino.update(_d,_mag);
     
-    check_wall_collision(pad);
-    check_tetromino_collisions();
+    check_wall_collision(pad, lcd);
+    check_tetromino_collisions(pad, lcd);
 }
 
 
-void TetrisGame::check_wall_collision(Gamepad &pad)
+void TetrisGame::check_wall_collision(Gamepad &pad, N5110 &lcd)
 {
     Vector2D tetromino_pos = _tetromino.get_pos();
     Vector2D tetromino_velocity = _tetromino.get_velocity();
@@ -66,19 +67,17 @@
         
         pad.tone(750.0,0.1);
         
-        cancel_line(); // check if line full and cancel + drop down if it is 
-                
-        _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // drop new shame
+        cancel_line(lcd); // check if line full and cancel + drop down if it is 
         
-        blocknumber ++;
-        if (blocknumber > 4) {
-            blocknumber = 0;
-            }
+        lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK);
+                
+        _tetromino.init(WIDTH/2 - 2, HEIGHT, 4, 4, _speed); // drop new shame
+        
     }
     // check if hit roof
     else if (tetromino_pos.y <= 1) {
         
-        exit_game();
+        exit_game(lcd);
                 
     }
     
@@ -86,25 +85,63 @@
  // update tetromino parameters??   
 }
 
-void check_tetromino_collisions(Tetromino tetromino)
+void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd)
 {
     Vector2D tetromino_pos = _tetromino.get_pos();
     Vector2D tetromino_velocity = _tetromino.get_velocity();
 
     if (
-    (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x) = 1) || // not sure if it should be 4 or 5 ??? 
-    (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 1) = 1) || 
-    (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 2) = 1) ||
-    (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 3) = 1)
+    (lcd.getPixel(tetromino_pos.y + 4, tetromino_pos.x) == 1) || // not sure if it should be 4 or 5 ??? 
+    (lcd.getPixel(tetromino_pos.y + 4, tetromino_pos.x + 1) == 1) || 
+    (lcd.getPixel(tetromino_pos.y + 4, tetromino_pos.x + 2) == 1) ||
+    (lcd.getPixel(tetromino_pos.y + 4, tetromino_pos.x + 3) == 1)
     ) {
         tetromino_velocity.x = 0;
         
         pad.tone(1000.0,0.1);
-        cancel_line();
+        cancel_line(lcd);
         
-        blocknumber ++ 
-        if (blocknumber > 4) { blocknumber = 0; }
+        lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK);
             
-        _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // break to loop back or keep this ??
-        
+        _tetromino.init(WIDTH/2 - 2, HEIGHT, 4, 4, _speed); // break to loop back or keep this ??
+        }
 }
+
+void TetrisGame::cancel_line(N5110 &lcd) 
+{
+        int count;
+    for(int j=0; j<=46; j+=1) {
+    for(int i=0; i<=82; i++) {
+        if (lcd.getPixel(i,j)==0) {
+            count=0;
+            break;
+        } else if (lcd.getPixel(i,j)==1){
+            count ++;
+              }
+            }
+        if(count==83) {
+            count = 0;
+            lcd.drawLine(0,j,82,j,0); // clear line
+ //           score++;
+            for(int x=0; x<=82; x++) {
+                for(int y=j; y>=0; y--) {
+                    lcd.setPixel(x,y,false);
+                    lcd.setPixel(x,y+1);
+                    }
+                }
+            }
+        }
+    
+
+} 
+
+void TetrisGame::exit_game(N5110 &lcd) {
+     lcd.clear();
+     lcd.printString(" GAME OVER ",0, 3);
+     lcd.refresh();
+     wait(10);
+     lcd.clear();
+     lcd.printString(" press RESET ",0,6);
+     lcd.refresh();
+     exit(1.0);
+}