My ELEC2645 joystick project Tetris Game NAME: JIANWEI CHEN SID: 200879849
Dependencies: N5110 SDFileSystem mbed
main.cpp@0:12a1972fa0d0, 2016-04-17 (annotated)
- Committer:
- cjw851102
- Date:
- Sun Apr 17 12:13:34 2016 +0000
- Revision:
- 0:12a1972fa0d0
- Child:
- 1:2a758565f691
test 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cjw851102 | 0:12a1972fa0d0 | 1 | #include "mbed.h" |
cjw851102 | 0:12a1972fa0d0 | 2 | #include "Patterns.h" |
cjw851102 | 0:12a1972fa0d0 | 3 | #include "N5110.h" |
cjw851102 | 0:12a1972fa0d0 | 4 | |
cjw851102 | 0:12a1972fa0d0 | 5 | //VCC, SCE, RST, D/C, MOSI,SCLK, LED |
cjw851102 | 0:12a1972fa0d0 | 6 | N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); |
cjw851102 | 0:12a1972fa0d0 | 7 | Patterns patterns; |
cjw851102 | 0:12a1972fa0d0 | 8 | void drawPattern(int type,int rotation,int x,int y,int fill); // draw pattern at (x,y), fill = 0 white, fill = 1 black |
cjw851102 | 0:12a1972fa0d0 | 9 | |
cjw851102 | 0:12a1972fa0d0 | 10 | void left_collisionDetect(); |
cjw851102 | 0:12a1972fa0d0 | 11 | int left_xMin = 0; |
cjw851102 | 0:12a1972fa0d0 | 12 | int left_collision_flag = 0; |
cjw851102 | 0:12a1972fa0d0 | 13 | |
cjw851102 | 0:12a1972fa0d0 | 14 | void right_collisionDetect(); |
cjw851102 | 0:12a1972fa0d0 | 15 | int right_xMax = 29; |
cjw851102 | 0:12a1972fa0d0 | 16 | int right_collision_flag = 0; |
cjw851102 | 0:12a1972fa0d0 | 17 | |
cjw851102 | 0:12a1972fa0d0 | 18 | |
cjw851102 | 0:12a1972fa0d0 | 19 | void bottom_collisionDetect(); |
cjw851102 | 0:12a1972fa0d0 | 20 | int bottom_yMax = 47; |
cjw851102 | 0:12a1972fa0d0 | 21 | int bottom_collision_flag = 0; |
cjw851102 | 0:12a1972fa0d0 | 22 | |
cjw851102 | 0:12a1972fa0d0 | 23 | struct Position { |
cjw851102 | 0:12a1972fa0d0 | 24 | float x; |
cjw851102 | 0:12a1972fa0d0 | 25 | float y; |
cjw851102 | 0:12a1972fa0d0 | 26 | int type; |
cjw851102 | 0:12a1972fa0d0 | 27 | int rotation; |
cjw851102 | 0:12a1972fa0d0 | 28 | }; |
cjw851102 | 0:12a1972fa0d0 | 29 | typedef struct Position position; |
cjw851102 | 0:12a1972fa0d0 | 30 | |
cjw851102 | 0:12a1972fa0d0 | 31 | position pos_current; |
cjw851102 | 0:12a1972fa0d0 | 32 | |
cjw851102 | 0:12a1972fa0d0 | 33 | void init_game(); |
cjw851102 | 0:12a1972fa0d0 | 34 | |
cjw851102 | 0:12a1972fa0d0 | 35 | int pattern_buffer[6][6]; |
cjw851102 | 0:12a1972fa0d0 | 36 | void get_pattern(int type, int rotatin); |
cjw851102 | 0:12a1972fa0d0 | 37 | |
cjw851102 | 0:12a1972fa0d0 | 38 | void scan(); |
cjw851102 | 0:12a1972fa0d0 | 39 | int buffer[84][48]; |
cjw851102 | 0:12a1972fa0d0 | 40 | |
cjw851102 | 0:12a1972fa0d0 | 41 | int main() |
cjw851102 | 0:12a1972fa0d0 | 42 | { |
cjw851102 | 0:12a1972fa0d0 | 43 | wait(2.0); // short delay for power to settle |
cjw851102 | 0:12a1972fa0d0 | 44 | lcd.init(); |
cjw851102 | 0:12a1972fa0d0 | 45 | lcd.normalMode(); // normal colour mode |
cjw851102 | 0:12a1972fa0d0 | 46 | lcd.setBrightness(1.0); // put LED backlight on 50% |
cjw851102 | 0:12a1972fa0d0 | 47 | /* |
cjw851102 | 0:12a1972fa0d0 | 48 | init_game(); |
cjw851102 | 0:12a1972fa0d0 | 49 | lcd.clear(); |
cjw851102 | 0:12a1972fa0d0 | 50 | |
cjw851102 | 0:12a1972fa0d0 | 51 | lcd.drawLine(0,16,25,16,1); |
cjw851102 | 0:12a1972fa0d0 | 52 | drawPattern(pos_current.type,pos_current.rotation,pos_current.x,pos_current.y,1); |
cjw851102 | 0:12a1972fa0d0 | 53 | right_collisionDetect(); |
cjw851102 | 0:12a1972fa0d0 | 54 | if (right_collision_flag == 1) { |
cjw851102 | 0:12a1972fa0d0 | 55 | lcd.printString("1",0,0); |
cjw851102 | 0:12a1972fa0d0 | 56 | } |
cjw851102 | 0:12a1972fa0d0 | 57 | lcd.drawLine(35,0,35,47,2); |
cjw851102 | 0:12a1972fa0d0 | 58 | lcd.printString("Level:",42,0); |
cjw851102 | 0:12a1972fa0d0 | 59 | lcd.printString("easy",42,1); |
cjw851102 | 0:12a1972fa0d0 | 60 | lcd.printString("Score:",42,2); |
cjw851102 | 0:12a1972fa0d0 | 61 | lcd.printString("100",42,3); |
cjw851102 | 0:12a1972fa0d0 | 62 | lcd.printString("Next:",42,4); |
cjw851102 | 0:12a1972fa0d0 | 63 | wait(0.5); |
cjw851102 | 0:12a1972fa0d0 | 64 | lcd.refresh();*/ |
cjw851102 | 0:12a1972fa0d0 | 65 | /* |
cjw851102 | 0:12a1972fa0d0 | 66 | lcd.clear(); |
cjw851102 | 0:12a1972fa0d0 | 67 | |
cjw851102 | 0:12a1972fa0d0 | 68 | drawPattern(0,0,0,0,1); |
cjw851102 | 0:12a1972fa0d0 | 69 | drawPattern(1,0,10,0,1); |
cjw851102 | 0:12a1972fa0d0 | 70 | drawPattern(2,0,20,0,1); |
cjw851102 | 0:12a1972fa0d0 | 71 | drawPattern(3,0,30,0,1); |
cjw851102 | 0:12a1972fa0d0 | 72 | drawPattern(4,0,40,0,1); |
cjw851102 | 0:12a1972fa0d0 | 73 | drawPattern(5,0,50,0,1); |
cjw851102 | 0:12a1972fa0d0 | 74 | drawPattern(6,0,60,0,1); |
cjw851102 | 0:12a1972fa0d0 | 75 | lcd.refresh(); |
cjw851102 | 0:12a1972fa0d0 | 76 | */ |
cjw851102 | 0:12a1972fa0d0 | 77 | int i =0; |
cjw851102 | 0:12a1972fa0d0 | 78 | while (1) { |
cjw851102 | 0:12a1972fa0d0 | 79 | |
cjw851102 | 0:12a1972fa0d0 | 80 | |
cjw851102 | 0:12a1972fa0d0 | 81 | drawPattern(0,i,0,0,1); |
cjw851102 | 0:12a1972fa0d0 | 82 | drawPattern(1,i,10,0,1); |
cjw851102 | 0:12a1972fa0d0 | 83 | drawPattern(2,i,20,0,1); |
cjw851102 | 0:12a1972fa0d0 | 84 | drawPattern(3,i,30,0,1); |
cjw851102 | 0:12a1972fa0d0 | 85 | drawPattern(4,i,40,0,1); |
cjw851102 | 0:12a1972fa0d0 | 86 | drawPattern(5,i,50,0,1); |
cjw851102 | 0:12a1972fa0d0 | 87 | drawPattern(6,i,60,0,1); |
cjw851102 | 0:12a1972fa0d0 | 88 | lcd.refresh(); |
cjw851102 | 0:12a1972fa0d0 | 89 | i++; |
cjw851102 | 0:12a1972fa0d0 | 90 | if (i==4) { |
cjw851102 | 0:12a1972fa0d0 | 91 | i=0; |
cjw851102 | 0:12a1972fa0d0 | 92 | } |
cjw851102 | 0:12a1972fa0d0 | 93 | wait(0.5); |
cjw851102 | 0:12a1972fa0d0 | 94 | lcd.clear(); |
cjw851102 | 0:12a1972fa0d0 | 95 | } |
cjw851102 | 0:12a1972fa0d0 | 96 | |
cjw851102 | 0:12a1972fa0d0 | 97 | } |
cjw851102 | 0:12a1972fa0d0 | 98 | |
cjw851102 | 0:12a1972fa0d0 | 99 | void init_game() |
cjw851102 | 0:12a1972fa0d0 | 100 | { |
cjw851102 | 0:12a1972fa0d0 | 101 | pos_current.x = 10; |
cjw851102 | 0:12a1972fa0d0 | 102 | pos_current.y = 10; |
cjw851102 | 0:12a1972fa0d0 | 103 | pos_current.type = 6; |
cjw851102 | 0:12a1972fa0d0 | 104 | pos_current.rotation = 2; |
cjw851102 | 0:12a1972fa0d0 | 105 | } |
cjw851102 | 0:12a1972fa0d0 | 106 | |
cjw851102 | 0:12a1972fa0d0 | 107 | void get_pattern(int type, int rotation) |
cjw851102 | 0:12a1972fa0d0 | 108 | { |
cjw851102 | 0:12a1972fa0d0 | 109 | for(int i=0; i<=5; i++) { |
cjw851102 | 0:12a1972fa0d0 | 110 | for(int j=0; j<=5; j++) { |
cjw851102 | 0:12a1972fa0d0 | 111 | pattern_buffer[i][j] = patterns.getPatterns(type,rotation,j,i); // return pattern[type][rotation][y][x]; |
cjw851102 | 0:12a1972fa0d0 | 112 | } |
cjw851102 | 0:12a1972fa0d0 | 113 | } |
cjw851102 | 0:12a1972fa0d0 | 114 | } |
cjw851102 | 0:12a1972fa0d0 | 115 | |
cjw851102 | 0:12a1972fa0d0 | 116 | void scan() |
cjw851102 | 0:12a1972fa0d0 | 117 | { |
cjw851102 | 0:12a1972fa0d0 | 118 | for (int i=0; i<=83; i++) { |
cjw851102 | 0:12a1972fa0d0 | 119 | for (int j=0; j<=47; j++) { |
cjw851102 | 0:12a1972fa0d0 | 120 | if(lcd.getPixel(i,j)) { |
cjw851102 | 0:12a1972fa0d0 | 121 | buffer[i][j] = 1; |
cjw851102 | 0:12a1972fa0d0 | 122 | } else { |
cjw851102 | 0:12a1972fa0d0 | 123 | buffer[i][j] = 0; |
cjw851102 | 0:12a1972fa0d0 | 124 | } |
cjw851102 | 0:12a1972fa0d0 | 125 | } |
cjw851102 | 0:12a1972fa0d0 | 126 | } |
cjw851102 | 0:12a1972fa0d0 | 127 | } |
cjw851102 | 0:12a1972fa0d0 | 128 | |
cjw851102 | 0:12a1972fa0d0 | 129 | |
cjw851102 | 0:12a1972fa0d0 | 130 | void drawPattern(int type,int rotation,int x,int y,int fill) |
cjw851102 | 0:12a1972fa0d0 | 131 | { |
cjw851102 | 0:12a1972fa0d0 | 132 | get_pattern(type,rotation); |
cjw851102 | 0:12a1972fa0d0 | 133 | for(int i=x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square |
cjw851102 | 0:12a1972fa0d0 | 134 | for(int j=y; j <= y+5; j++) { |
cjw851102 | 0:12a1972fa0d0 | 135 | if(pattern_buffer[i-x][j-y]==1) { |
cjw851102 | 0:12a1972fa0d0 | 136 | if (fill==0) { |
cjw851102 | 0:12a1972fa0d0 | 137 | lcd.clearPixel(i,j); |
cjw851102 | 0:12a1972fa0d0 | 138 | } else if (fill==1) { |
cjw851102 | 0:12a1972fa0d0 | 139 | lcd.setPixel(i,j); |
cjw851102 | 0:12a1972fa0d0 | 140 | } |
cjw851102 | 0:12a1972fa0d0 | 141 | } |
cjw851102 | 0:12a1972fa0d0 | 142 | } |
cjw851102 | 0:12a1972fa0d0 | 143 | } |
cjw851102 | 0:12a1972fa0d0 | 144 | } |
cjw851102 | 0:12a1972fa0d0 | 145 | |
cjw851102 | 0:12a1972fa0d0 | 146 | void left_collisionDetect() |
cjw851102 | 0:12a1972fa0d0 | 147 | { |
cjw851102 | 0:12a1972fa0d0 | 148 | scan(); |
cjw851102 | 0:12a1972fa0d0 | 149 | get_pattern(pos_current.type,pos_current.rotation); |
cjw851102 | 0:12a1972fa0d0 | 150 | int left_boundary[6][6]; |
cjw851102 | 0:12a1972fa0d0 | 151 | // get the left boundary pattern |
cjw851102 | 0:12a1972fa0d0 | 152 | for (int j=0; j<=6; j++) { // |
cjw851102 | 0:12a1972fa0d0 | 153 | for (int i=0; i<=6; i++) { |
cjw851102 | 0:12a1972fa0d0 | 154 | if (pattern_buffer[i][j]==1) { |
cjw851102 | 0:12a1972fa0d0 | 155 | left_boundary[i][j]=1; |
cjw851102 | 0:12a1972fa0d0 | 156 | for(int k=i+1; k<=6; k++) { |
cjw851102 | 0:12a1972fa0d0 | 157 | left_boundary[k][j] = 0; |
cjw851102 | 0:12a1972fa0d0 | 158 | } |
cjw851102 | 0:12a1972fa0d0 | 159 | } else { |
cjw851102 | 0:12a1972fa0d0 | 160 | left_boundary[i][j]=0; |
cjw851102 | 0:12a1972fa0d0 | 161 | } |
cjw851102 | 0:12a1972fa0d0 | 162 | } |
cjw851102 | 0:12a1972fa0d0 | 163 | } |
cjw851102 | 0:12a1972fa0d0 | 164 | |
cjw851102 | 0:12a1972fa0d0 | 165 | //check left collision |
cjw851102 | 0:12a1972fa0d0 | 166 | int x = pos_current.x; |
cjw851102 | 0:12a1972fa0d0 | 167 | int y = pos_current.y; |
cjw851102 | 0:12a1972fa0d0 | 168 | for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square |
cjw851102 | 0:12a1972fa0d0 | 169 | for(int j=y; j <= y+5; j++) { |
cjw851102 | 0:12a1972fa0d0 | 170 | if(left_boundary[i-x][j-y]==1) { |
cjw851102 | 0:12a1972fa0d0 | 171 | if(i == 0) { |
cjw851102 | 0:12a1972fa0d0 | 172 | left_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 173 | left_xMin = 0; |
cjw851102 | 0:12a1972fa0d0 | 174 | } else if (buffer[i-1][j] == 1) { |
cjw851102 | 0:12a1972fa0d0 | 175 | left_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 176 | if (i > left_xMin) { |
cjw851102 | 0:12a1972fa0d0 | 177 | left_xMin = i; |
cjw851102 | 0:12a1972fa0d0 | 178 | } |
cjw851102 | 0:12a1972fa0d0 | 179 | } |
cjw851102 | 0:12a1972fa0d0 | 180 | } |
cjw851102 | 0:12a1972fa0d0 | 181 | } |
cjw851102 | 0:12a1972fa0d0 | 182 | |
cjw851102 | 0:12a1972fa0d0 | 183 | } |
cjw851102 | 0:12a1972fa0d0 | 184 | } |
cjw851102 | 0:12a1972fa0d0 | 185 | |
cjw851102 | 0:12a1972fa0d0 | 186 | void right_collisionDetect() |
cjw851102 | 0:12a1972fa0d0 | 187 | { |
cjw851102 | 0:12a1972fa0d0 | 188 | scan(); |
cjw851102 | 0:12a1972fa0d0 | 189 | get_pattern(pos_current.type,pos_current.rotation); |
cjw851102 | 0:12a1972fa0d0 | 190 | int right_boundary[6][6]; |
cjw851102 | 0:12a1972fa0d0 | 191 | // get the left boundary pattern |
cjw851102 | 0:12a1972fa0d0 | 192 | for (int j=0; j<=6; j++) { |
cjw851102 | 0:12a1972fa0d0 | 193 | for (int i=6; i>=0; i--) { |
cjw851102 | 0:12a1972fa0d0 | 194 | if (pattern_buffer[i][j]==1) { |
cjw851102 | 0:12a1972fa0d0 | 195 | right_boundary[i][j]=1; |
cjw851102 | 0:12a1972fa0d0 | 196 | for(int k=i-1; k>=0; k--) { |
cjw851102 | 0:12a1972fa0d0 | 197 | right_boundary[k][j] = 0; |
cjw851102 | 0:12a1972fa0d0 | 198 | } |
cjw851102 | 0:12a1972fa0d0 | 199 | } else { |
cjw851102 | 0:12a1972fa0d0 | 200 | right_boundary[i][j]=0; |
cjw851102 | 0:12a1972fa0d0 | 201 | } |
cjw851102 | 0:12a1972fa0d0 | 202 | } |
cjw851102 | 0:12a1972fa0d0 | 203 | } |
cjw851102 | 0:12a1972fa0d0 | 204 | |
cjw851102 | 0:12a1972fa0d0 | 205 | //check left collision |
cjw851102 | 0:12a1972fa0d0 | 206 | int x = pos_current.x; |
cjw851102 | 0:12a1972fa0d0 | 207 | int y = pos_current.y; |
cjw851102 | 0:12a1972fa0d0 | 208 | for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square |
cjw851102 | 0:12a1972fa0d0 | 209 | for(int j=y; j <= y+5; j++) { |
cjw851102 | 0:12a1972fa0d0 | 210 | if(right_boundary[i-x][j-y]==1) { |
cjw851102 | 0:12a1972fa0d0 | 211 | if(i == 29) { |
cjw851102 | 0:12a1972fa0d0 | 212 | right_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 213 | right_xMax = 29; |
cjw851102 | 0:12a1972fa0d0 | 214 | } else if (buffer[i+1][j] == 1) { |
cjw851102 | 0:12a1972fa0d0 | 215 | right_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 216 | if(i< right_xMax) { |
cjw851102 | 0:12a1972fa0d0 | 217 | right_xMax = i; |
cjw851102 | 0:12a1972fa0d0 | 218 | } |
cjw851102 | 0:12a1972fa0d0 | 219 | } |
cjw851102 | 0:12a1972fa0d0 | 220 | } |
cjw851102 | 0:12a1972fa0d0 | 221 | } |
cjw851102 | 0:12a1972fa0d0 | 222 | |
cjw851102 | 0:12a1972fa0d0 | 223 | } |
cjw851102 | 0:12a1972fa0d0 | 224 | } |
cjw851102 | 0:12a1972fa0d0 | 225 | |
cjw851102 | 0:12a1972fa0d0 | 226 | void bottom_collisionDetect() |
cjw851102 | 0:12a1972fa0d0 | 227 | { |
cjw851102 | 0:12a1972fa0d0 | 228 | scan(); |
cjw851102 | 0:12a1972fa0d0 | 229 | get_pattern(pos_current.type,pos_current.rotation); |
cjw851102 | 0:12a1972fa0d0 | 230 | int bot_boundary[6][6]; |
cjw851102 | 0:12a1972fa0d0 | 231 | // get the left boundary pattern |
cjw851102 | 0:12a1972fa0d0 | 232 | for (int i=0; i<=6; i++) { |
cjw851102 | 0:12a1972fa0d0 | 233 | for (int j=6; j>=0; j--) { |
cjw851102 | 0:12a1972fa0d0 | 234 | if (pattern_buffer[i][j]==1) { |
cjw851102 | 0:12a1972fa0d0 | 235 | bot_boundary[i][j]=1; |
cjw851102 | 0:12a1972fa0d0 | 236 | for(int k=i-1; k>=0; k--) { |
cjw851102 | 0:12a1972fa0d0 | 237 | bot_boundary[i][k] = 0; |
cjw851102 | 0:12a1972fa0d0 | 238 | } |
cjw851102 | 0:12a1972fa0d0 | 239 | } else { |
cjw851102 | 0:12a1972fa0d0 | 240 | bot_boundary[i][j]=0; |
cjw851102 | 0:12a1972fa0d0 | 241 | } |
cjw851102 | 0:12a1972fa0d0 | 242 | } |
cjw851102 | 0:12a1972fa0d0 | 243 | } |
cjw851102 | 0:12a1972fa0d0 | 244 | |
cjw851102 | 0:12a1972fa0d0 | 245 | //check left collision |
cjw851102 | 0:12a1972fa0d0 | 246 | int x = pos_current.x; |
cjw851102 | 0:12a1972fa0d0 | 247 | int y = pos_current.y; |
cjw851102 | 0:12a1972fa0d0 | 248 | for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square |
cjw851102 | 0:12a1972fa0d0 | 249 | for(int j=y; j <= y+5; j++) { |
cjw851102 | 0:12a1972fa0d0 | 250 | if(bot_boundary[i-x][j-y]==1) { |
cjw851102 | 0:12a1972fa0d0 | 251 | if(j == 47) { |
cjw851102 | 0:12a1972fa0d0 | 252 | bottom_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 253 | bottom_yMax = 47; |
cjw851102 | 0:12a1972fa0d0 | 254 | } else if (buffer[i][j+1] == 1) { |
cjw851102 | 0:12a1972fa0d0 | 255 | right_collision_flag = 1; |
cjw851102 | 0:12a1972fa0d0 | 256 | if(j< bottom_yMax) { |
cjw851102 | 0:12a1972fa0d0 | 257 | bottom_yMax = j; |
cjw851102 | 0:12a1972fa0d0 | 258 | } |
cjw851102 | 0:12a1972fa0d0 | 259 | } |
cjw851102 | 0:12a1972fa0d0 | 260 | } |
cjw851102 | 0:12a1972fa0d0 | 261 | } |
cjw851102 | 0:12a1972fa0d0 | 262 | |
cjw851102 | 0:12a1972fa0d0 | 263 | } |
cjw851102 | 0:12a1972fa0d0 | 264 | } |