Simple tetris game to show usage of C12832 LCD.

Dependencies:   C12832_lcd mbed

Files at this revision

API Documentation at this revision

Comitter:
PrzemekWirkus
Date:
Tue Mar 18 10:03:03 2014 +0000
Parent:
1:cdd5880742fc
Child:
3:dbd1976e4add
Commit message:
Changing game loop

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 17 20:16:32 2014 +0000
+++ b/main.cpp	Tue Mar 18 10:03:03 2014 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include <time.h>
 #include "C12832_lcd.h"
 #include "mtrix.h"
 
@@ -16,82 +17,68 @@
 
 #define BOUNCER_SIZE 2
 
+bool KEYS[4] = {false, false, false, false};
+
 int main()
 {
     // Welcome message
     lcd.cls();
+ 
+    while(1) {
+        clock_t t1, t2;
+        t1 = clock();
 
-    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 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; }
+            case JOY_KEY_LEFT:  { game.try_move_left();   break; }
+            case JOY_KEY_RIGHT: { game.try_move_right();  break; }
+            case JOY_KEY_UP:    { game.try_rotate();     break; }
+            case JOY_KEY_DOWN:  { game.move_brick_down();   break; }
         }
-        
-        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 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++)
+        }
+   
+        // PRINT BRICK
+        for (int x = 0; x < BRICK_SIZE_X; x++)
+        {
+            for (int y = 0; y < BRICK_SIZE_Y; y++)
             {
-                for (int y = 0; y < BRICK_SIZE_Y; y++)
+                if (game.get_brick_cell(x, y))
                 {
-                    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);
-                    }
+                    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();
         }
-        wait(0.01);
-        loop++;
+
+        lcd.copy_to_lcd();
+        game.move_brick_down();
+        t2 = clock();
+        float diff = (((float)t2 - (float)t1) / CLOCKS_PER_SEC ); 
+        wait(diff);
     }
 }