My ELEC2645 project. Nikollao Sulollari. 200804685
Dependencies: N5110 SoftPWM mbed
Fork of Treasure_Hunt by
main.cpp
- Committer:
- Nikollao
- Date:
- 2016-03-28
- Revision:
- 6:c11bb31a06b3
- Parent:
- 5:ffa498d7071f
- Child:
- 7:f31a4b4beb91
File content as of revision 6:c11bb31a06b3:
//Fadia /** @file main.cpp @brief Game implementation */ #include "main.h" #include "N5110.h" #include "stdlib.h" N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); Serial pc(USBTX,USBRX); #define DIRECTION_TOLERANCE 0.05 Ticker ticker; Ticker game_ticker; Timeout timeout; enum DirectionName { UP, DOWN, LEFT, RIGHT, CENTRE, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; typedef struct JoyStick Joystick; struct JoyStick { double x; /// current x value double x0; /// 'centred' x value double y; /// current y value double y0; /// 'centred' y value int button; /// button state (assume pull-down used, so 1 = pressed, 0 = unpressed) DirectionName direction; // current direction }; /// create struct variable Joystick joystick; int main() { lcd.init(); init_K64F(); init_serial(); button.rise(&button_isr); ///assign rise with ISR init_game(); ///initialize game calibrateJoystick(); ///calibrate joystick ticker.attach(&timer_isr,0.1); ///attach ticker with ISR every 0.1 sec reset = level; ///set reset = level to check later if level has increased while (1) { if (g_timer_flag) { g_timer_flag = 0; ///reset flag lcd.clear(); guidance(); hero(); enemies(); obstacles(); if (heroY < -45) { ///if hero has reached the top of the screen heroY = 0; ///hero goes back to the bottom of the screen level++; ///level increases by 1 } if (reset < level) { ///if level has increased reset = level; ///update reset rectX = rand() % 84; ///set the position of rect enemy rectY = 0; circleX = 0; ///set position of circle enemy circleY = rand() % 47; } pc.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button); pc.printf("heroY = %d , heroX = %d , n = %d \n",heroY, heroX, n); } checkOverlap(); updateJoystick(); //callibrateJoystick(); lcd.refresh(); sleep(); // break; } } void init_serial() { // set to highest baud - ensure terminal software matches pc.baud(115200); } void init_K64F() { // on-board LEDs are active-low, so set pin high to turn them off. r_led = 1; g_led = 1; b_led = 1; // since the on-board switches have external pull-ups, we should disable the internal pull-down // resistors that are enabled by default using InterruptIn sw2.mode(PullNone); sw3.mode(PullNone); } void timer_isr () { g_timer_flag = 1; } void game_timer_isr() { g_game_timer_flag = 1; } void sw2_isr() { g_sw2_flag = 1; } void sw3_isr() { g_sw3_flag = 1; } void button_isr() { g_button_flag =1; } void timeout_isr() { if (button) { pc.printf("button is pressed! \n"); } } void enemyRect() { ///generate rect shape enemy lcd.drawRect(rectX,rectY,5,4,1); rectX = rectX + rand()%4 - 2; rectY++; ///enemy moving towards hero } void hero() { ///draw hero heroX = heroX + 5*xPot; /// control hero's x direction heroY = heroY - 5*yPot; /// control hero's y direction ///set x-axis boundaries so hero does not go out of screen if (heroX > 35) { heroX = 35; } if (heroX < 5) { heroX = 5; } lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1); lcd.drawLine(40+heroX, 43+heroY,48+heroX, 47+heroY,1); lcd.drawLine(44+heroX, 45+heroY,44+heroX, 41+heroY,1); lcd.drawCircle(44+heroX, 39+heroY,2,0); } void enemyCircle() { ///generate circle shape enemy lcd.drawCircle(circleX,circleY,4,1); circleY = circleY + rand() %4 - 2; circleX++; ///enemy moving towards hero } void init_game() { ///initialise game //button.mode(PullNone); srand(time(NULL)); /// generate random numbers rectY = 0; /// init rectX, rectY rectX = rand() %40 + 20; circleY = rand() %20 + 10; /// init circleX, circleY circleX = 0; if ( play == 0) { ///if its first play print welcome message, otherwise don't lcd.setBrightness(0.5); // put LED backlight on 50% timeout.attach(&timeout_isr,2); lcd.printString("Welcome to",11,1); lcd.printString("Treasure Hunt!",1,3); lcd.refresh(); sleep(); lcd.clear(); } game_ticker.attach(game_timer_isr,0.2); while (1) { if (g_game_timer_flag) { g_game_timer_flag = 0; updateJoystick(); lcd.clear(); menu(); ///get Joystick's value to select option in menu //option = menu(); if (option == 0) { /// select Start Game lcd.printString("Start Game <",0,0); lcd.printString("Settings",0,2); lcd.printString("Exit",0,4); } else if (option == 1) { /// select Settings lcd.printString("Start Game",0,0); lcd.printString("Settings <",0,2); lcd.printString("Exit",0,4); } else { /// select Exit lcd.printString("Start Game",0,0); lcd.printString("Settings",0,2); lcd.printString("Exit <",0,4); } } if (g_button_flag) { g_button_flag = 0; if (option == 0) { game_ticker.detach(); break; } else if (option == 1) { //settings_menu(); pc.printf("Modify Settings!"); } else { lcd.turnOff(); deepsleep(); } } sleep(); } } void guidance() { /// show arrow to act as guidance towards the treasure if (level < 7) { ///check level of difficulty lcd.drawLine(42,0,42,4,1); lcd.drawLine(42,0,40,2,1); lcd.drawLine(42,0,44,2,1); } else if (level == 7) { lcd.printString("F",42,0); /// print the treasure icon } else if (level == 8) { ticker.detach(); lcd.clear(); lcd.printString("Well done!",0,0); lcd.refresh(); timeout.attach(&timeout_isr,2); sleep(); lcd.clear(); lcd.printString("Play again <",0,0); lcd.refresh(); sleep(); if (g_button_flag) { g_button_flag = 0; level = 0; play++; lcd.clear(); main(); } } } void obstacles() { /// place obstacles in the screen /// as level difficulty increases, more obstacles are added if (level == 1) { lcd.drawRect(10,15,2,2,1); lcd.drawRect(74,15,2,2,1); } else if (level == 2) { lcd.drawRect(10,15,2,2,1); lcd.drawRect(74,15,2,2,1); lcd.drawRect(10,28,2,2,1); lcd.drawRect(74,28,2,2,1); } else if (level == 3) { lcd.drawRect(10,15,2,2,1); lcd.drawRect(74,15,2,2,1); lcd.drawRect(10,28,2,2,1); lcd.drawRect(74,28,2,2,1); lcd.drawRect(30,15,2,2,1); lcd.drawRect(54,15,2,2,1); lcd.drawRect(30,28,2,2,1); lcd.drawRect(54,28,2,2,1); } else if (level == 4) { lcd.drawRect(10 + rand() %4 - 2,15 + rand() &4 -2,2,2,1); lcd.drawRect(74 + rand() %4 - 2,15 + rand() %4 - 2,2,2,1); lcd.drawRect(10 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1); lcd.drawRect(74 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1); lcd.drawRect(30 + rand() %4 - 2,15,2,2,1); lcd.drawRect(54,15 + rand() %4 - 2,2,2,1); lcd.drawRect(30,28 + rand() %4 - 2,2,2,1); lcd.drawRect(54 + rand() %4 - 2,28,2,2,1); } else if ( level == 5) { lcd.drawLine(50,35,65,35,1); lcd.drawLine(38,30,28,30,1); lcd.drawLine(60,25,70,25,1); lcd.drawLine(28,25,18,25,1); lcd.drawLine(70,20,80,20,1); lcd.drawLine(18,20,8,20,1); } else if (level == 6) { } else if (level == 7) { /* lcd.drawLine(50,15,30,15,1); lcd.drawLine(30,15,30,35,1); lcd.drawLine(30,25,45,25,1); */ } } void enemies() { /// generate enemies in the screen depending on the level difficulty if (level == 0) { enemyRect(); //enemyCircle(); } else if (level == 1) { enemyRect(); //enemyCircle(); } else if (level == 2) { } else if (level == 3) { } enemyCircle(); } void calibrateJoystick() { // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } void updateJoystick() { // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state joystick.button = button; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = CENTRE; } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = UP_LEFT; } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = UP_RIGHT; } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = DOWN_LEFT; } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = DOWN_RIGHT; } } int menu() { if (joystick.y <= 0.33) { /// if Joystick moves up option = 0; /// the pointer moves upper in the menu display } else if (joystick.y <= 0.66) { /// if joystick moves down option = 1; /// else pointer moves down } else { option = 2; } return option; } int intersection(int i, int j) { /// check for overlap between enemies and hero n=0; if (lcd.getPixel(i-1,j-1)!=0) //pixel to the top-left n++; // increase n by 1 if (lcd.getPixel(i-1,j)!=0) //pixel to the left n++; // increase n by 1 if (lcd.getPixel(i-1,j+1)!=0) //pixel to the bottom-left n++; // increase n by 1 if (lcd.getPixel(i,j-1)!=0) // pixel to the top n++; // increase n by 1 if (lcd.getPixel(i,j+1)!=0) //pixel to the bottom n++; // increase n by 1 if (lcd.getPixel(i+1,j-1)!=0) //pixel to the top-right n++; // increase n by 1 if (lcd.getPixel(i+1,j)!=0) // pixel to the right n++; // increase n by 1 if (lcd.getPixel(i+1,j+1)!=0) //pixel to the bottom right n++; // increase n by 1 return n; } void checkOverlap() { for (int i=40+heroX; i<50+heroX; i++) { for (int j=35+heroY; j<48+heroY; j++) { lcd.setPixel(i,j); } } } void error() { /// display error message while (1) { lcd.printString("Error!",0,0); r_led = 0; wait(0.2); r_led = 1; wait(0.2); } }