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:
4:7ddd287a5d28
Parent:
3:522c6f850e91
Child:
5:53e021832adc
diff -r 522c6f850e91 -r 7ddd287a5d28 main.cpp
--- a/main.cpp	Sun May 24 11:18:33 2020 +0000
+++ b/main.cpp	Sun May 31 17:01:53 2020 +0000
@@ -16,93 +16,128 @@
 #include "N5110.h"
 #include "TetrisGame.h"
 
+// set defaults 
 #define TETROMINO_WIDTH 4
 #define TETROMINO_HEIGHT 4
+// #define TETROMINO_SIZE 4
+#define TETROMINO_SPEED 1
+#define HEIGHT 48
+#define WIDTH 84
+// STRUCTS //
 
 struct UserInput {
     Direction d;
     float mag;
 };
 
+// OBJECTS //
+
 N5110 lcd;
 Gamepad pad;
 TetrisGame tetris;
 
+// PROTOTYPES //
+
 void init();
 void update_game(UserInput input);
 void render();
 void welcome();
-
+void exit_game();
+void cancel_line();
+int score = 0;
 
 // int ScreenHeight = 84;
 // int ScreenWidth = 48;
-// string block[7] // store tetromino blocks as a w-string
 
-
-
-
-/* int Rotate(int px, int py, int r)
-{
-    int pi = 0;
-    switch (r % 4);
-    {
-        case 0: pi = py * 4 + px; // shape width = 4
-        break;
-        
-        case 1: pi = 12 + py - (px * 4); // rotates the shape by 90 degrees
-        break;
-        
-        case 2: pi = 15 - (py * 4) - px; // rotates the shape by 180 degrees
-        break; 
-        
-        case 3: pi = 3 - py + (px * 4); // rotates the shape by 270 degrees
-        break;
-        
-        }
-        return pi;
-    }
-*/
+// FUNCTIONS // 
 
 int main()
 {
-    int fps = 6;
+    int fps = 6; // 6 frames per second 
     
-    init();
-    welcome();
+    init(); // initialise and display welcome screen
+    welcome(); // wait for user to press start
+    
+    render(); // draw initial frame 
+    wait(1.0f/fps); // wait one frame period 
     
-    render();
-    wait(1.0f/fps);
-    
+// game loop
     while(1) {
-        tetris.read_input(pad);
+        tetris.read_input(pad); // read input 
         tetris.update(pad);
-        render();
-        wait(1.0f/fps);
-    }
+        //play_game(); // update game state 
+        render(); // render the display
+        wait(1.0f/fps); // wait one frame period 
+        break;
+        
+}
+}
+        
+void exit_game() {
+     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);
+}  
+
+    
+void cancel_line() {
+        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 init()
-{
-    lcd.init();
-    padinit();
+
+// initialise classes and libraries
+void init() {
+    lcd.init(); // initialise lcd 
+    pad.init(); // initialise Gamepad
     
-    tetris.init(TETROMINO_WIDTH, TETROMINO_HEIGHT);
+    // initialise the game with correct tetromino sizes etc
+    tetris.init(0, WIDTH/2 - 2, HEIGHT, 1);
     
 }
 
-void render()
-{
-    lcd.clear();
-    tetris.draw(lcd);
-    lcd.refresh();
+void render() { // draws each frame on the lcd 
+
+    lcd.clear(); // clear screen
+    tetris.draw(lcd); // re-draw 
+    lcd.refresh(); // refresh screen
 }
 
+// welcome screen display
 void welcome() {
     
     lcd.printString("      TETRIS      ",0,1);
     lcd.printString("    Press Start   ",0,4);
     lcd.refresh();
     
+    // flash LEDs untile start button pressed
     while (pad.start_pressed() == false) {
         lcd.setContrast(pad.read_pot1());
         pad.leds_on();
@@ -110,4 +145,27 @@
         pad.leds_off();
         wait(0.1);
     }
-}
\ No newline at end of file
+}
+
+void check_tetromino_collisions()
+{
+    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)
+    ) {
+        tetromino_velocity.x = 0;
+        
+        pad.tone(1000.0,0.1);
+        cancel_line();
+        
+        blocknumber ++ 
+        if (blocknumber > 4) { blocknumber = 0; }
+            
+        _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // break to loop back or keep this ??
+        
+}