Simple tetris game to show usage of C12832 LCD.

Dependencies:   C12832_lcd mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <time.h>
00003 #include "C12832_lcd.h"
00004 #include "mtrix.h"
00005 
00006 MBEDtrisTheGame<char> game;
00007  
00008 C12832_LCD lcd;
00009 BusIn joy(p15,p12,p13,p16);
00010 DigitalIn fire(p14);
00011 BusOut leds(LED1,LED2,LED3,LED4);
00012 
00013 #define JOY_KEY_LEFT    1
00014 #define JOY_KEY_RIGHT   2
00015 #define JOY_KEY_UP      4
00016 #define JOY_KEY_DOWN    8
00017 
00018 #define BOUNCER_SIZE    4
00019 
00020 int main()
00021 {
00022     // Welcome message
00023     lcd.cls();
00024     
00025     std::pair<int, int> brick_prev_pos = game.get_brick_pos();
00026     clock_t t1, t2;
00027     
00028     while(1) {
00029         t1 = clock();
00030 
00031         int joy_key = joy.read();
00032         switch (joy_key) {
00033             case JOY_KEY_LEFT:  { game.try_move_left();   break; }
00034             case JOY_KEY_RIGHT: { game.try_move_right();  break; }
00035             case JOY_KEY_UP:    { game.try_rotate();      break; }
00036             case JOY_KEY_DOWN:  { game.move_brick_down(); break; }
00037         }
00038  
00039         // Erase prev brick from screen
00040         for (int x = 0; x < BRICK_SIZE_X; x++)
00041         {
00042             for (int y = 0; y < BRICK_SIZE_Y; y++)
00043             {
00044                 int color = game.get_brick_cell(x, y);
00045                 {
00046                     int draw_pix_x = (brick_prev_pos.first + x) * BOUNCER_SIZE;
00047                     int draw_pix_y = (brick_prev_pos.second + y) * BOUNCER_SIZE;
00048                     lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 0);
00049                 }
00050             }
00051         }
00052     
00053         // PRINT MATRIX
00054         for (int x = 0; x < LCD_W; x++)
00055         {
00056             for (int y = 0; y < LCD_H; y++)
00057             {         
00058     
00059                 // This should be replaces with just 'putpixel on LCD'
00060                 int draw_pix_x = x*BOUNCER_SIZE;
00061                 int draw_pix_y = y*BOUNCER_SIZE;
00062 
00063                 if (game.get_matrix_cell(x, y))
00064                 {
00065                     //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
00066                     lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 1);
00067                 }
00068                 else
00069                 {
00070                     //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 0);
00071                     // lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, 0);
00072                 }
00073             }
00074         }
00075 
00076         // PRINT BRICK
00077         for (int x = 0; x < BRICK_SIZE_X; x++)
00078         {
00079             for (int y = 0; y < BRICK_SIZE_Y; y++)
00080             {
00081                 int color = game.get_brick_cell(x, y);
00082                 if (color)
00083                 {
00084                     std::pair<int, int> brick_pos = game.get_brick_pos();
00085 
00086                     int draw_pix_x = (brick_pos.first + x) * BOUNCER_SIZE;
00087                     int draw_pix_y = (brick_pos.second + y) * BOUNCER_SIZE;
00088                     //lcd.rect(draw_pix_x, draw_pix_y, draw_pix_x+BOUNCER_SIZE-1, draw_pix_y+BOUNCER_SIZE-1, 1);
00089                     lcd.rect(draw_pix_y, draw_pix_x, draw_pix_y+BOUNCER_SIZE-1, draw_pix_x+BOUNCER_SIZE-1, color);
00090                 }
00091             }
00092         }
00093 
00094         lcd.copy_to_lcd();
00095         
00096         brick_prev_pos = game.get_brick_pos();
00097         
00098         game.move_brick_down();
00099         t2 = clock();
00100         float diff = (((float)t2 - (float)t1) / CLOCKS_PER_SEC ); 
00101         wait(0.2);
00102     }
00103 }