My ELEC2645 joystick project Tetris Game NAME: JIANWEI CHEN SID: 200879849
Dependencies: N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 0:12a1972fa0d0
- Child:
- 1:2a758565f691
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Apr 17 12:13:34 2016 +0000 @@ -0,0 +1,264 @@ +#include "mbed.h" +#include "Patterns.h" +#include "N5110.h" + +//VCC, SCE, RST, D/C, MOSI,SCLK, LED +N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); +Patterns patterns; +void drawPattern(int type,int rotation,int x,int y,int fill); // draw pattern at (x,y), fill = 0 white, fill = 1 black + +void left_collisionDetect(); +int left_xMin = 0; +int left_collision_flag = 0; + +void right_collisionDetect(); +int right_xMax = 29; +int right_collision_flag = 0; + + +void bottom_collisionDetect(); +int bottom_yMax = 47; +int bottom_collision_flag = 0; + +struct Position { + float x; + float y; + int type; + int rotation; +}; +typedef struct Position position; + +position pos_current; + +void init_game(); + +int pattern_buffer[6][6]; +void get_pattern(int type, int rotatin); + +void scan(); +int buffer[84][48]; + +int main() +{ + wait(2.0); // short delay for power to settle + lcd.init(); + lcd.normalMode(); // normal colour mode + lcd.setBrightness(1.0); // put LED backlight on 50% + /* + init_game(); + lcd.clear(); + + lcd.drawLine(0,16,25,16,1); + drawPattern(pos_current.type,pos_current.rotation,pos_current.x,pos_current.y,1); + right_collisionDetect(); + if (right_collision_flag == 1) { + lcd.printString("1",0,0); + } + lcd.drawLine(35,0,35,47,2); + lcd.printString("Level:",42,0); + lcd.printString("easy",42,1); + lcd.printString("Score:",42,2); + lcd.printString("100",42,3); + lcd.printString("Next:",42,4); + wait(0.5); + lcd.refresh();*/ + /* + lcd.clear(); + + drawPattern(0,0,0,0,1); + drawPattern(1,0,10,0,1); + drawPattern(2,0,20,0,1); + drawPattern(3,0,30,0,1); + drawPattern(4,0,40,0,1); + drawPattern(5,0,50,0,1); + drawPattern(6,0,60,0,1); + lcd.refresh(); + */ + int i =0; + while (1) { + + + drawPattern(0,i,0,0,1); + drawPattern(1,i,10,0,1); + drawPattern(2,i,20,0,1); + drawPattern(3,i,30,0,1); + drawPattern(4,i,40,0,1); + drawPattern(5,i,50,0,1); + drawPattern(6,i,60,0,1); + lcd.refresh(); + i++; + if (i==4) { + i=0; + } + wait(0.5); + lcd.clear(); + } + +} + +void init_game() +{ + pos_current.x = 10; + pos_current.y = 10; + pos_current.type = 6; + pos_current.rotation = 2; +} + +void get_pattern(int type, int rotation) +{ + for(int i=0; i<=5; i++) { + for(int j=0; j<=5; j++) { + pattern_buffer[i][j] = patterns.getPatterns(type,rotation,j,i); // return pattern[type][rotation][y][x]; + } + } +} + +void scan() +{ + for (int i=0; i<=83; i++) { + for (int j=0; j<=47; j++) { + if(lcd.getPixel(i,j)) { + buffer[i][j] = 1; + } else { + buffer[i][j] = 0; + } + } + } +} + + +void drawPattern(int type,int rotation,int x,int y,int fill) +{ + get_pattern(type,rotation); + for(int i=x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square + for(int j=y; j <= y+5; j++) { + if(pattern_buffer[i-x][j-y]==1) { + if (fill==0) { + lcd.clearPixel(i,j); + } else if (fill==1) { + lcd.setPixel(i,j); + } + } + } + } +} + +void left_collisionDetect() +{ + scan(); + get_pattern(pos_current.type,pos_current.rotation); + int left_boundary[6][6]; + // get the left boundary pattern + for (int j=0; j<=6; j++) { // + for (int i=0; i<=6; i++) { + if (pattern_buffer[i][j]==1) { + left_boundary[i][j]=1; + for(int k=i+1; k<=6; k++) { + left_boundary[k][j] = 0; + } + } else { + left_boundary[i][j]=0; + } + } + } + + //check left collision + int x = pos_current.x; + int y = pos_current.y; + for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square + for(int j=y; j <= y+5; j++) { + if(left_boundary[i-x][j-y]==1) { + if(i == 0) { + left_collision_flag = 1; + left_xMin = 0; + } else if (buffer[i-1][j] == 1) { + left_collision_flag = 1; + if (i > left_xMin) { + left_xMin = i; + } + } + } + } + + } +} + +void right_collisionDetect() +{ + scan(); + get_pattern(pos_current.type,pos_current.rotation); + int right_boundary[6][6]; + // get the left boundary pattern + for (int j=0; j<=6; j++) { + for (int i=6; i>=0; i--) { + if (pattern_buffer[i][j]==1) { + right_boundary[i][j]=1; + for(int k=i-1; k>=0; k--) { + right_boundary[k][j] = 0; + } + } else { + right_boundary[i][j]=0; + } + } + } + + //check left collision + int x = pos_current.x; + int y = pos_current.y; + for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square + for(int j=y; j <= y+5; j++) { + if(right_boundary[i-x][j-y]==1) { + if(i == 29) { + right_collision_flag = 1; + right_xMax = 29; + } else if (buffer[i+1][j] == 1) { + right_collision_flag = 1; + if(i< right_xMax) { + right_xMax = i; + } + } + } + } + + } +} + +void bottom_collisionDetect() +{ + scan(); + get_pattern(pos_current.type,pos_current.rotation); + int bot_boundary[6][6]; + // get the left boundary pattern + for (int i=0; i<=6; i++) { + for (int j=6; j>=0; j--) { + if (pattern_buffer[i][j]==1) { + bot_boundary[i][j]=1; + for(int k=i-1; k>=0; k--) { + bot_boundary[i][k] = 0; + } + } else { + bot_boundary[i][j]=0; + } + } + } + + //check left collision + int x = pos_current.x; + int y = pos_current.y; + for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square + for(int j=y; j <= y+5; j++) { + if(bot_boundary[i-x][j-y]==1) { + if(j == 47) { + bottom_collision_flag = 1; + bottom_yMax = 47; + } else if (buffer[i][j+1] == 1) { + right_collision_flag = 1; + if(j< bottom_yMax) { + bottom_yMax = j; + } + } + } + } + + } +} \ No newline at end of file