My ELEC2645 project. Nikollao Sulollari. 200804685
Dependencies: N5110 SoftPWM mbed
Fork of Treasure_Hunt by
Diff: main.cpp
- Revision:
- 7:f31a4b4beb91
- Parent:
- 6:c11bb31a06b3
- Child:
- 8:606a488fa720
--- a/main.cpp Mon Mar 28 12:10:24 2016 +0000 +++ b/main.cpp Mon Apr 04 12:20:42 2016 +0000 @@ -5,56 +5,22 @@ */ #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 + button.fall(&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 + buzzer = 0.5; + buzzer.period(1/1000); + //buzzer.write(0.5); while (1) { @@ -72,6 +38,9 @@ level++; ///level increases by 1 } + if (heroY >= 0) { + heroY = 0; + } if (reset < level) { ///if level has increased reset = level; ///update reset @@ -88,7 +57,6 @@ //callibrateJoystick(); lcd.refresh(); sleep(); - // break; } } @@ -109,6 +77,7 @@ // resistors that are enabled by default using InterruptIn sw2.mode(PullNone); sw3.mode(PullNone); + button.mode(PullUp); } void timer_isr () @@ -142,320 +111,4 @@ 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); - } -} +} \ No newline at end of file