Simple tetris game to show usage of C12832 LCD.

Dependencies:   C12832_lcd mbed

Revision:
1:cdd5880742fc
Parent:
0:644f70b470b5
Child:
2:da22b028cf2e
diff -r 644f70b470b5 -r cdd5880742fc main.cpp
--- a/main.cpp	Mon Mar 17 19:47:49 2014 +0000
+++ b/main.cpp	Mon Mar 17 20:16:32 2014 +0000
@@ -9,10 +9,10 @@
 DigitalIn fire(p14);
 BusOut leds(LED1,LED2,LED3,LED4);
 
-#define KEY_LEFT    2
-#define KEY_RIGHT   1
-#define KEY_UP      4
-#define KEY_DOWN    8
+#define JOY_KEY_LEFT    1
+#define JOY_KEY_RIGHT   2
+#define JOY_KEY_UP      4
+#define JOY_KEY_DOWN    8
 
 #define BOUNCER_SIZE 2
 
@@ -20,55 +20,78 @@
 {
     // Welcome message
     lcd.cls();
+
+    bool KEY_LEFT  = false;
+    bool KEY_RIGHT = false;
+    bool KEY_UP    = false;
+    bool KEY_DOWN  = false;
  
+    int loop = 0;
     while(1) {
         int joy_key = joy.read();
         switch (joy_key) {
-            case KEY_LEFT:  { game.try_rotate();      break; }
-            case KEY_RIGHT: { game.try_move_right();  break; }
-            case KEY_UP:    { game.try_rotate();      break; }
-            case KEY_DOWN:  { game.move_brick_down(); break; }
+            case JOY_KEY_LEFT:  { KEY_LEFT = true;   break; }
+            case JOY_KEY_RIGHT: { KEY_RIGHT = true;  break; }
+            case JOY_KEY_UP:    { KEY_UP = true;     break; }
+            case JOY_KEY_DOWN:  { KEY_DOWN = true;   break; }
         }
         
-        // PRINT MATRIX
-        for (int x = 0; x < LCD_W; x++)
-        {
-            for (int y = 0; y < LCD_H; y++)
-            {               
-                // This should be replaces with just 'putpixel on LCD'
-                int draw_pix_x = x*BOUNCER_SIZE;
-                int draw_pix_y = y*BOUNCER_SIZE;
-
-                if (game.get_matrix_cell(x, y))
-                {
-                    lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
-                }
-                else
-                {
-                    lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 0);
+        if ((loop % 10) == 0)
+        {        
+            if (KEY_LEFT)  { game.try_move_left();   break; }
+            if (KEY_RIGHT) { game.try_move_right();  break; }
+            if (KEY_UP)    { game.try_rotate();      break; }
+            if (KEY_DOWN)  { game.move_brick_down(); break; }
+       
+            KEY_LEFT  = false;
+            KEY_RIGHT = false;
+            KEY_UP    = false;
+            KEY_DOWN  = false;
+        
+            // PRINT MATRIX
+            for (int x = 0; x < LCD_W; x++)
+            {
+                for (int y = 0; y < LCD_H; y++)
+                {               
+                    // This should be replaces with just 'putpixel on LCD'
+                    int draw_pix_x = x*BOUNCER_SIZE;
+                    int draw_pix_y = y*BOUNCER_SIZE;
+    
+                    if (game.get_matrix_cell(x, y))
+                    {
+                        //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
+                        lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 1);
+                    }
+                    else
+                    {
+                        //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 0);
+                        lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 0);
+                    }
                 }
             }
-        }
-   
-        // PRINT BRICK
-        for (int x = 0; x < BRICK_SIZE_X; x++)
-        {
-            for (int y = 0; y < BRICK_SIZE_Y; y++)
+       
+            // PRINT BRICK
+            for (int x = 0; x < BRICK_SIZE_X; x++)
             {
-                if (game.get_brick_cell(x, y))
+                for (int y = 0; y < BRICK_SIZE_Y; y++)
                 {
-                    std::pair<int, int> brick_pos = game.get_brick_pos();
-
-                    int draw_pix_x = (brick_pos.first + x) * BOUNCER_SIZE;
-                    int draw_pix_y = (brick_pos.second + y) * BOUNCER_SIZE;
-                    lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
+                    if (game.get_brick_cell(x, y))
+                    {
+                        std::pair<int, int> brick_pos = game.get_brick_pos();
+    
+                        int draw_pix_x = (brick_pos.first + x) * BOUNCER_SIZE;
+                        int draw_pix_y = (brick_pos.second + y) * BOUNCER_SIZE;
+                        //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
+                        lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 1);
+                    }
                 }
             }
+       
+            
+            lcd.copy_to_lcd();
+            game.move_brick_down();
         }
-   
-        
-        lcd.copy_to_lcd();
-        wait(1.0);
-        game.move_brick_down();
+        wait(0.01);
+        loop++;
     }
 }