
Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen
Dependencies: N5110 PowerControl mbed
Revision 31:5b4a4d225ab4, committed 2015-05-09
- Comitter:
- ThomasBGill
- Date:
- Sat May 09 17:37:40 2015 +0000
- Parent:
- 30:4a03611a3d99
- Child:
- 32:99ca304085e6
- Commit message:
- Analogue stick added + interfaced with PCB
Changed in this revision
Game.cpp | Show annotated file Show diff for this revision Revisions of this file |
Game.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Game.cpp Sat May 09 11:46:32 2015 +0000 +++ b/Game.cpp Sat May 09 17:37:40 2015 +0000 @@ -16,11 +16,46 @@ { StartFlag = 1; } +/* void DirPressed() { DirFlag = 1; } +*/ +//Joystick functions +// read default positions of the joystick to calibrate later readings +void calibrateJoystick() +{ + //button.mode(PullDown); + // 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 { + joystick.direction = Unknown; + } +} //High score system @@ -171,18 +206,14 @@ Sleep(); } - if (DirFlag) { - - DirFlag = 0; - - if(Up) { - if(menu > 0) { - menu--; - } - } else if(Down) { - if(menu < 3) { - menu++; - } + if (joystick.direction == Up) { + if(menu > 0) { + menu--; + } + } + if(joystick.direction == Down) { + if(menu < 3) { + menu++; } } if (ActFlag) { @@ -217,9 +248,8 @@ } lcd.refresh(); - if (DirFlag) { - - DirFlag = 0; + + if (joystick.direction != Centre) { bright = !bright; } @@ -262,15 +292,14 @@ //RevealMap(); //Uncomment for debugging to view map - while (1) { +srand(Noise * 1000000); - srand(Noise * 1000000); +PlayerCamera(); + + while (1) { ActFlag = 0; StartFlag = 0; - DirFlag = 0; - - PlayerCamera(); if (map[px][py] == CHEST) { Chest(); @@ -290,9 +319,7 @@ Sleep(); - if (DirFlag) { - - DirFlag = 0; + if (joystick.direction != Centre) { PlayerMove(); @@ -300,6 +327,9 @@ if (ph < PH_MAX) { ph++; } + + PlayerCamera(); + } if (rand() % 50 == 0) { @@ -425,7 +455,6 @@ while (1) { ActFlag = 0; StartFlag = 0; - DirFlag = 0; lcd.clear(); @@ -449,8 +478,7 @@ lcd.refresh(); Sleep(); - if (DirFlag) { - DirFlag = 0; + if (joystick.direction != Centre) { menu = !menu; } if (ActFlag) { @@ -647,11 +675,11 @@ Sleep(); } - if (DirFlag) { - DirFlag = 0; - if ((Down || Right) && menu < 3) { + if (joystick.direction != Centre) { + + if ((joystick.direction == Down || joystick.direction == Right) && menu < 3) { menu++; - } else if ((Up || Left) && menu > 0) + } else if ((joystick.direction == Up || joystick.direction == Left) && menu > 0) menu--; } if (ActFlag) { @@ -681,7 +709,6 @@ } StartFlag = 0; ActFlag = 0; - DirFlag = 0; } void DrawMap() @@ -725,10 +752,6 @@ if (ActFlag) { ActFlag = 0; } - if (DirFlag) { - DirFlag = 0; - break; - } if (StartFlag) { StartFlag = 0; break; @@ -758,10 +781,6 @@ if (ActFlag) { ActFlag = 0; } - if (DirFlag) { - DirFlag = 0; - break; - } if (StartFlag) { StartFlag = 0; break; @@ -794,8 +813,6 @@ int r = rand() % 10; - DirFlag = 0; - lcd.clear(); lcd.printString("Do you want to", 0, 1); lcd.printString("take the", 0, 2); @@ -827,8 +844,7 @@ } else { StartFlag = 0; - DirFlag = 0; - + lcd.clear(); lcd.printString("You leave the", 0, 1); lcd.printString(ItemList[r].ItemName, 0, 2); @@ -971,7 +987,6 @@ Sleep(); } } else { //Leave the potion - DirFlag = 0; StartFlag = 0; lcd.clear(); lcd.printString("You walk away", 0, 0); @@ -1046,16 +1061,21 @@ int main() { + calibrateJoystick(); // get centred values of joystick + pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second + //Power Saving PHY_PowerDown(); int result = semihost_powerdown(); - Up.rise(&DirPressed); - Down.rise(&DirPressed); - Right.rise(&DirPressed); - Left.rise(&DirPressed); - Start.rise(&StartPressed); - Act.rise(&ActPressed); + Start.mode(PullDown); + Act.mode(PullDown); + + Start.fall(&StartPressed); + Act.fall(&ActPressed); + + Start.mode(PullDown); + Act.mode(PullDown); //Generate random seed srand(Noise * 1000000);
--- a/Game.h Sat May 09 11:46:32 2015 +0000 +++ b/Game.h Sat May 09 17:37:40 2015 +0000 @@ -7,14 +7,51 @@ #define PH_MAX 20 // vcc sce rst dc mosi clk led -N5110 lcd(p5, p6, p7, p8, p11, p13, p21); -InterruptIn Act(p22); -InterruptIn Start(p23); -InterruptIn Up(p30); -InterruptIn Down(p27); -InterruptIn Left(p24); -InterruptIn Right(p29); +N5110 lcd(p7, p8, p9, p10, p11, p13, p26); +InterruptIn Act(p27); +InterruptIn Start(p28); +//InterruptIn Up(p25); +//InterruptIn Down(p24); +//InterruptIn Left(p22); +//InterruptIn Right(p21); AnalogIn Noise(p19); +AnalogIn xPot(p15); +AnalogIn yPot(p16); + +//Joystick stuff +// timer to regularly read the joystick +Ticker pollJoystick; + +// change this to alter tolerance of joystick direction +#define DIRECTION_TOLERANCE 0.05 + +// create enumerated type (0,1,2,3 etc. for direction) +// could be extended for diagonals etc. +enum DirectionName { + Up, + Down, + Left, + Right, + Centre, + Unknown +}; + +// struct for Joystick +typedef struct JoyStick Joystick; +struct JoyStick { + float x; // current x value + float x0; // 'centred' x value + float y; // current y value + float 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; + +// function prototypes +void calibrateJoystick(); +void updateJoystick(); #define USR_POWERDOWN (0x104) @@ -79,7 +116,7 @@ //Variables int ActFlag = 0; int StartFlag = 0; -int DirFlag = 0; +//int DirFlag = 0; //Space type player is on int pSpace; @@ -109,7 +146,7 @@ void ActPressed(); void StartPressed(); -void DirPressed(); +//void DirPressed(); void writeDataToFile(); void readDataFromFile();