Bare Boned Tetris
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
main.cpp@0:75c1f1878803, 2014-10-22 (annotated)
- Committer:
- nbrown47
- Date:
- Wed Oct 22 00:06:53 2014 +0000
- Revision:
- 0:75c1f1878803
Bare Boned Tetris
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nbrown47 | 0:75c1f1878803 | 1 | #include "mbed.h" |
nbrown47 | 0:75c1f1878803 | 2 | #include "rtos.h" |
nbrown47 | 0:75c1f1878803 | 3 | #include "uLCD_4DGL.h" |
nbrown47 | 0:75c1f1878803 | 4 | |
nbrown47 | 0:75c1f1878803 | 5 | //Objects |
nbrown47 | 0:75c1f1878803 | 6 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
nbrown47 | 0:75c1f1878803 | 7 | DigitalIn pb_L(p21); //left pb |
nbrown47 | 0:75c1f1878803 | 8 | DigitalIn pb_R(p29); //right pb |
nbrown47 | 0:75c1f1878803 | 9 | DigitalIn pb_D(p25); //down pb |
nbrown47 | 0:75c1f1878803 | 10 | |
nbrown47 | 0:75c1f1878803 | 11 | |
nbrown47 | 0:75c1f1878803 | 12 | |
nbrown47 | 0:75c1f1878803 | 13 | /////////////////////////////////////////////////////////////////////SHAPES//////////////////////////////////////////////////// |
nbrown47 | 0:75c1f1878803 | 14 | // Square Shape |
nbrown47 | 0:75c1f1878803 | 15 | struct Rect{ |
nbrown47 | 0:75c1f1878803 | 16 | int x1,y1,x2,y2; // rectangle positions |
nbrown47 | 0:75c1f1878803 | 17 | float fx1 , fy1, fx2, fy2, vx, vy; // change in positions and rate of change |
nbrown47 | 0:75c1f1878803 | 18 | }; |
nbrown47 | 0:75c1f1878803 | 19 | |
nbrown47 | 0:75c1f1878803 | 20 | int mat[18][10]={}; |
nbrown47 | 0:75c1f1878803 | 21 | int y,x; |
nbrown47 | 0:75c1f1878803 | 22 | int col; |
nbrown47 | 0:75c1f1878803 | 23 | void arrayclear(){ |
nbrown47 | 0:75c1f1878803 | 24 | for(x=0;x<10;x++) |
nbrown47 | 0:75c1f1878803 | 25 | { |
nbrown47 | 0:75c1f1878803 | 26 | for (y=0;y<18;y++) |
nbrown47 | 0:75c1f1878803 | 27 | mat[x][y]=0; |
nbrown47 | 0:75c1f1878803 | 28 | } |
nbrown47 | 0:75c1f1878803 | 29 | } |
nbrown47 | 0:75c1f1878803 | 30 | // L Shape = 2 piece rectangle |
nbrown47 | 0:75c1f1878803 | 31 | struct L{ |
nbrown47 | 0:75c1f1878803 | 32 | int x1, y1, x2, y2; //first piece of L |
nbrown47 | 0:75c1f1878803 | 33 | int x3, y3, x4, y4; //second piece of L |
nbrown47 | 0:75c1f1878803 | 34 | float fx1, fy1, fx2, fy2, fx3, fy3, fx4, fy4; |
nbrown47 | 0:75c1f1878803 | 35 | float vx, vy; |
nbrown47 | 0:75c1f1878803 | 36 | }; |
nbrown47 | 0:75c1f1878803 | 37 | |
nbrown47 | 0:75c1f1878803 | 38 | // Z Shape = 3 piece rectangle |
nbrown47 | 0:75c1f1878803 | 39 | struct Z{ |
nbrown47 | 0:75c1f1878803 | 40 | int x1, y1, x2, y2; //first piece of Z |
nbrown47 | 0:75c1f1878803 | 41 | int x3, y3, x4, y4; //second piece of Z |
nbrown47 | 0:75c1f1878803 | 42 | int x5, y5, x6, y6; //third piece of Z |
nbrown47 | 0:75c1f1878803 | 43 | float fx1, fy1, fx2, fy2, fx3, fy3, fx4, fy4, fx5, fy5, fx6, fy6; |
nbrown47 | 0:75c1f1878803 | 44 | float vx, vy; |
nbrown47 | 0:75c1f1878803 | 45 | }; |
nbrown47 | 0:75c1f1878803 | 46 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
nbrown47 | 0:75c1f1878803 | 47 | |
nbrown47 | 0:75c1f1878803 | 48 | //void drawShapes(int shape); |
nbrown47 | 0:75c1f1878803 | 49 | //void test(); |
nbrown47 | 0:75c1f1878803 | 50 | void matrixread() |
nbrown47 | 0:75c1f1878803 | 51 | { |
nbrown47 | 0:75c1f1878803 | 52 | int i,j; |
nbrown47 | 0:75c1f1878803 | 53 | for(i=0;i<10;i++) |
nbrown47 | 0:75c1f1878803 | 54 | { |
nbrown47 | 0:75c1f1878803 | 55 | for (j=0;j<18;j++) |
nbrown47 | 0:75c1f1878803 | 56 | if (mat[j][i]==1) |
nbrown47 | 0:75c1f1878803 | 57 | { |
nbrown47 | 0:75c1f1878803 | 58 | uLCD.filled_rectangle(i*7, j*7, i*7+6, j*7+6, BLUE); |
nbrown47 | 0:75c1f1878803 | 59 | } |
nbrown47 | 0:75c1f1878803 | 60 | else if (mat[j][i]==2) |
nbrown47 | 0:75c1f1878803 | 61 | { |
nbrown47 | 0:75c1f1878803 | 62 | uLCD.filled_rectangle(i*7, j*7, i*7+6, j*7+6, BLACK); |
nbrown47 | 0:75c1f1878803 | 63 | } |
nbrown47 | 0:75c1f1878803 | 64 | } |
nbrown47 | 0:75c1f1878803 | 65 | } |
nbrown47 | 0:75c1f1878803 | 66 | void bottomrowdelete() |
nbrown47 | 0:75c1f1878803 | 67 | { |
nbrown47 | 0:75c1f1878803 | 68 | int count1,hot,coding; |
nbrown47 | 0:75c1f1878803 | 69 | for(count1=0;count1<10;count1++) |
nbrown47 | 0:75c1f1878803 | 70 | { |
nbrown47 | 0:75c1f1878803 | 71 | mat[17][count1]=2; |
nbrown47 | 0:75c1f1878803 | 72 | } |
nbrown47 | 0:75c1f1878803 | 73 | int f,u; |
nbrown47 | 0:75c1f1878803 | 74 | |
nbrown47 | 0:75c1f1878803 | 75 | matrixread(); |
nbrown47 | 0:75c1f1878803 | 76 | for(f=0;f<10;f++) |
nbrown47 | 0:75c1f1878803 | 77 | { |
nbrown47 | 0:75c1f1878803 | 78 | for (u=0;u<17;u++) |
nbrown47 | 0:75c1f1878803 | 79 | { |
nbrown47 | 0:75c1f1878803 | 80 | if(mat[u][f]==1) |
nbrown47 | 0:75c1f1878803 | 81 | { |
nbrown47 | 0:75c1f1878803 | 82 | mat[u][f]=2; |
nbrown47 | 0:75c1f1878803 | 83 | mat[u+1][f]=1; |
nbrown47 | 0:75c1f1878803 | 84 | } |
nbrown47 | 0:75c1f1878803 | 85 | } |
nbrown47 | 0:75c1f1878803 | 86 | } |
nbrown47 | 0:75c1f1878803 | 87 | } |
nbrown47 | 0:75c1f1878803 | 88 | void checkbottom() |
nbrown47 | 0:75c1f1878803 | 89 | { |
nbrown47 | 0:75c1f1878803 | 90 | int count; |
nbrown47 | 0:75c1f1878803 | 91 | int check; |
nbrown47 | 0:75c1f1878803 | 92 | check=1; |
nbrown47 | 0:75c1f1878803 | 93 | for(count=0;count<10;count++) |
nbrown47 | 0:75c1f1878803 | 94 | { |
nbrown47 | 0:75c1f1878803 | 95 | if(mat[17][count]!=1) |
nbrown47 | 0:75c1f1878803 | 96 | check=0; |
nbrown47 | 0:75c1f1878803 | 97 | } |
nbrown47 | 0:75c1f1878803 | 98 | if(check==1) |
nbrown47 | 0:75c1f1878803 | 99 | bottomrowdelete(); |
nbrown47 | 0:75c1f1878803 | 100 | } |
nbrown47 | 0:75c1f1878803 | 101 | void start() |
nbrown47 | 0:75c1f1878803 | 102 | { |
nbrown47 | 0:75c1f1878803 | 103 | col=5; |
nbrown47 | 0:75c1f1878803 | 104 | mat[0][col]=1 ; |
nbrown47 | 0:75c1f1878803 | 105 | checkbottom(); |
nbrown47 | 0:75c1f1878803 | 106 | } |
nbrown47 | 0:75c1f1878803 | 107 | |
nbrown47 | 0:75c1f1878803 | 108 | |
nbrown47 | 0:75c1f1878803 | 109 | void updatePos(Rect s) |
nbrown47 | 0:75c1f1878803 | 110 | { |
nbrown47 | 0:75c1f1878803 | 111 | //update square position |
nbrown47 | 0:75c1f1878803 | 112 | s.fx1 += s.vx; |
nbrown47 | 0:75c1f1878803 | 113 | s.fx2 += s.vx; |
nbrown47 | 0:75c1f1878803 | 114 | s.fy1 += s.vy; |
nbrown47 | 0:75c1f1878803 | 115 | s.fy2 += s.vy; |
nbrown47 | 0:75c1f1878803 | 116 | s.x1 = (int)s.fx1; |
nbrown47 | 0:75c1f1878803 | 117 | s.x2 = (int)s.fx2; |
nbrown47 | 0:75c1f1878803 | 118 | s.y1 = (int)s.fy1; |
nbrown47 | 0:75c1f1878803 | 119 | s.y2 = (int)s.fy2; |
nbrown47 | 0:75c1f1878803 | 120 | } |
nbrown47 | 0:75c1f1878803 | 121 | |
nbrown47 | 0:75c1f1878803 | 122 | Rect s = { 60, 10, 75, 25, 60.0, 10.0, 75.0, 25.0, 0.0, 0.05}; |
nbrown47 | 0:75c1f1878803 | 123 | |
nbrown47 | 0:75c1f1878803 | 124 | int main() |
nbrown47 | 0:75c1f1878803 | 125 | { |
nbrown47 | 0:75c1f1878803 | 126 | |
nbrown47 | 0:75c1f1878803 | 127 | arrayclear(); |
nbrown47 | 0:75c1f1878803 | 128 | |
nbrown47 | 0:75c1f1878803 | 129 | //set inputs to pullup |
nbrown47 | 0:75c1f1878803 | 130 | pb_L.mode(PullUp); |
nbrown47 | 0:75c1f1878803 | 131 | pb_R.mode(PullUp); |
nbrown47 | 0:75c1f1878803 | 132 | pb_D.mode(PullUp); |
nbrown47 | 0:75c1f1878803 | 133 | |
nbrown47 | 0:75c1f1878803 | 134 | //set up backgrd |
nbrown47 | 0:75c1f1878803 | 135 | uLCD.baudrate(3000000); |
nbrown47 | 0:75c1f1878803 | 136 | uLCD.background_color(BLACK); |
nbrown47 | 0:75c1f1878803 | 137 | uLCD.cls(); |
nbrown47 | 0:75c1f1878803 | 138 | int qi,nick,quack,stop; |
nbrown47 | 0:75c1f1878803 | 139 | qi=0; |
nbrown47 | 0:75c1f1878803 | 140 | while(1) |
nbrown47 | 0:75c1f1878803 | 141 | { |
nbrown47 | 0:75c1f1878803 | 142 | if(stop==1) |
nbrown47 | 0:75c1f1878803 | 143 | { |
nbrown47 | 0:75c1f1878803 | 144 | qi=0; |
nbrown47 | 0:75c1f1878803 | 145 | stop=0; |
nbrown47 | 0:75c1f1878803 | 146 | start(); |
nbrown47 | 0:75c1f1878803 | 147 | } |
nbrown47 | 0:75c1f1878803 | 148 | if(!pb_R && pb_L ) |
nbrown47 | 0:75c1f1878803 | 149 | { |
nbrown47 | 0:75c1f1878803 | 150 | mat[qi][col]=2; |
nbrown47 | 0:75c1f1878803 | 151 | col--; |
nbrown47 | 0:75c1f1878803 | 152 | mat[qi][col]=1; |
nbrown47 | 0:75c1f1878803 | 153 | } |
nbrown47 | 0:75c1f1878803 | 154 | else if(!pb_L && pb_R) |
nbrown47 | 0:75c1f1878803 | 155 | { |
nbrown47 | 0:75c1f1878803 | 156 | mat[qi][col]=2; |
nbrown47 | 0:75c1f1878803 | 157 | col++; |
nbrown47 | 0:75c1f1878803 | 158 | mat[qi][col]=1; |
nbrown47 | 0:75c1f1878803 | 159 | } |
nbrown47 | 0:75c1f1878803 | 160 | else |
nbrown47 | 0:75c1f1878803 | 161 | { |
nbrown47 | 0:75c1f1878803 | 162 | mat[qi][col]=2; |
nbrown47 | 0:75c1f1878803 | 163 | qi++; |
nbrown47 | 0:75c1f1878803 | 164 | mat[qi][col]=1; |
nbrown47 | 0:75c1f1878803 | 165 | } |
nbrown47 | 0:75c1f1878803 | 166 | if(qi==17||mat[qi+1][col]==1) stop=1; |
nbrown47 | 0:75c1f1878803 | 167 | matrixread(); |
nbrown47 | 0:75c1f1878803 | 168 | } |
nbrown47 | 0:75c1f1878803 | 169 | |
nbrown47 | 0:75c1f1878803 | 170 | } |
nbrown47 | 0:75c1f1878803 | 171 |