
George Sykes ELEC2645 project
Dependencies: mbed
GHOST HUNTER
In a world of ghostly horrors there is much money to be made in underground ghost fighting rings. You've managed to get hold of a Ghostbuster, a special piece of equipment that allows you to catch, train and fight ghosts.
Instructions
Below you will find the instructions for the game. Please note that due to COVID-19 a large part of the game (fighting ghosts) could not be added as it would have required access to a second gamepad which i could not acquire.
Welcome screen
When first started you will be presented with a welcome screen
- Pot 1 to adjust the contrast on the screen
- Press A to continue.
Main menu
You have three options, catch ghosts (add ghosts to your inventory), inventory (sell ghosts) or settings(adjust the games settings).
- Press X and B to move the selection up and down respectively
- Press A to enter the selected submenu
Catch Ghost
Will now be presented with two challenges. In the first you need to find a ghost, in the second you catch it. Theses stages will start automatically.
Find ghost
Rotate the gamepad on its roll and pitch axis until all the LED's turn on. The ones on the left indicate roll and the right pitch.
- Rotate the gamepad on it roll and pitch to light up the LED's
Catch ghost
Return the gamepad to a comfortable position and use the joystick to move the crosshairs onto the ghost sprite. When ready press the A button to catch the ghost. You will be told what kind of ghost you have captured and it will be added to your inventory.
- Press A to catch the ghost
- Move the joystick to move the crosshairs
Inventory
The inventory allows you to view your ghosts and sell them.
- Use Pot 1 to scroll through the ghosts
- Pot 2 to scroll up and down the details of the individual ghosts
- Press X to prepare to sell a ghost and press again to confirm, if you don't press again the sale screen will disappear after 5 seconds
- Press Start to return to the main menu
Settings
This menu allows you to adjust some of the settings of the game.
- Press X to go up one option
- Press B to go down one option
- Press A to enter the selected submenu
- Press Start to return to the main menu
Contrast
Set the contrast of the LCD screen, the contrast will adjust on this screen so you can see the effect (contrast is bounded between 0.4 and 0.6).
- Pot 1 to increase or decrease the contrast
- Press A to set the contrast
Button Delay
Set the minimum time between button presses; if this is too low the game will detect two button presses when there was only one, too high and the buttons will seem unresponsive. So as to ensure these issues do not occur while changing the setting button X temporarily operates on the new delay but none of the others will until A is pressed.
- Pot 1 to increase or decrease the delay
- Press X to test the new delay, this will toggle the small circle to be filled in or unfilled
- Press A to save the setting
Diff: main.cpp
- Revision:
- 3:9d811414d35e
- Parent:
- 2:eaf245af2aae
- Child:
- 4:2e8d7c6d2953
--- a/main.cpp Fri Mar 06 19:27:12 2020 +0000 +++ b/main.cpp Mon May 11 11:58:31 2020 +0000 @@ -3,7 +3,6 @@ School of Electronic & Electrical Engineering University of Leeds 2019/20 - Name: George Sykes Username: el18gs Student ID Number: 201235346 @@ -17,12 +16,28 @@ #include "SDFileSystem.h" #include "FX0S8700CQ.h" #include "Ghost.h" +#include "Inventory.h" #include <vector> #include <string> // Declare the string vector type typedef std::vector<std::string> stringvec; +struct inven_state { + // UID, next{up,down}, type, attack, defense, level, xp, value, hp_max, hp + int uid; + int next[2]; + std::string type; + std::string name; + int attack; + int defense; + int level; + int xp; + int value; + int hp_max; + int hp; +}; + // objects Gamepad pad; N5110 lcd; @@ -32,10 +47,11 @@ //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS // Declare all the inputs -AnalogIn contrast(PTB2); InterruptIn buttonX(PTC5); InterruptIn buttonA(PTC7); InterruptIn buttonB(PTC9); +InterruptIn buttonY(PTC0); +InterruptIn buttonStart(PTC8); // Declare output // RED, YELLOW, GREEN @@ -46,26 +62,42 @@ void welcome(); int game_menu(); void catch_ghosts(); -void inventory(); +void display_inventory(Inventory inventory); +std::vector<inven_state> gen_ghost_fsm(Inventory inventory); void settings(); int** import_sprite(std::string file); void int_to_bin_digit(unsigned int in, int count, int* out); void listdir(void); -float cursor_transform(int OldMax, - int OldMin, - int NewMax, - int NewMin, +bool hasEnding (std::string const &fullString, std::string const &ending); +float cursor_transform(float OldMax, + float OldMin, + float NewMax, + float NewMin, float OldValue); +void adjustContrast(); +void buttonDelay(); +bool ghostHit( int xGhost, int yGhost, int xJoy, int yJoy); + + +// Settings variables +volatile int g_button_sesnsitivity = 250; +volatile bool g_buttonTesting = false; +volatile int g_buttonSensitivityTest = 250; + // ISR functions void buttonX_isr(); void buttonA_isr(); void buttonB_isr(); +void buttonY_isr(); +void buttonStart_isr(); // ISR flags volatile int g_buttonX_flag = 0; volatile int g_buttonA_flag = 0; volatile int g_buttonB_flag = 0; +volatile int g_buttonY_flag = 0; +volatile int g_buttonStart_flag = 0; // struct for state struct State { @@ -86,14 +118,20 @@ buttonB.mode(PullUp); // turn on internal pull-up resistor buttonB.fall(&buttonB_isr); + buttonY.mode(PullUp); // turn on internal pull-up resistor + buttonY.fall(&buttonY_isr); + + buttonStart.mode(PullUp); // turn on internal pull-up resistor + buttonStart.fall(&buttonStart_isr); + /* Initialise the game * Create an object to hold the players ghosts in * Print the welcome message on the screen and wait for the player */ + Inventory inventory; + lcd.init(); // Initialise the screen - lcd.clear(); // Clear all pixels - lcd.setContrast(contrast.read()); lcd.backLightOn(); welcome(); @@ -103,9 +141,10 @@ switch (choice) { case 0: catch_ghosts(); + inventory.regen(); break; case 1: - inventory(); + display_inventory(inventory); break; case 2: settings(); @@ -117,27 +156,24 @@ void welcome() { - printf("entered welcome screen\n"); + //printf("entered welcome screen\n"); int** welcome = import_sprite("/sd/assets/welcome.sprite"); - + lcd.clear(); + for(int i = 0; i < 48; i++) { + for(int j = 0; j < 84; j++) { + lcd.setPixel(j,i, welcome[i][j]); + } + } while(!g_buttonA_flag) { - lcd.clear(); - lcd.refresh(); - for(int i = 0; i < 48; i++) { - for(int j = 0; j < 84; j++) { - lcd.setPixel(j,i, welcome[i][j]); - } - } - // contrast should be bounded between 4 and 6 // contrast = 0.4 + contrast.read()/5 - lcd.setContrast(0.4 + ((double) contrast.read()/5)); + lcd.setContrast(0.4 + ((double) pad.read_pot1()/5)); lcd.refresh(); wait_ms(10); } g_buttonA_flag = 0; - wait_ms(25); + //wait_ms(25); } @@ -157,9 +193,6 @@ g_buttonX_flag = 0; g_buttonA_flag = 0; g_buttonB_flag = 0; - lcd.clear(); - lcd.refresh(); - lcd.setContrast(0.4 + ((double) contrast.read()/5)); while(1) { lcd.clear(); @@ -199,9 +232,7 @@ void catch_ghosts() { - - printf("catching ghosts\n"); - + float pitch = rand() % 46; if(rand() & 1) { pitch = pitch * -1; @@ -257,7 +288,7 @@ } Vector2D ideal = {rand() % 85,rand() % 49}; - + int** ghost = import_sprite("/sd/assets/ghost.sprite"); while(1) { @@ -276,19 +307,17 @@ for(int i = ideal.x - 8; i < ideal.x + 8; i++) { // Iterate Columns: x int row = 0; - for(int j = ideal.y - 8; j < ideal.y + 9; j++) { // Iterate Rows: y + for(int j = ideal.y - 9; j < ideal.y + 9; j++) { // Iterate Rows: y lcd.setPixel(i,j, ghost[row][col]); row++; } col++; } - if((double)actual.x/ideal.x > 0.75 && (double)actual.x/ideal.x < 1.25) { - if((double)actual.y/ideal.y < 0.75 && (double)actual.y/ideal.y < 1.25) { - lcd.printString("Press A!",0,0); - if(g_buttonA_flag) { - break; - } + if(ghostHit(ideal.x, ideal.y, actual.x, actual.y)) { + lcd.printString("Press A!",0,0); + if(g_buttonA_flag) { + break; } } @@ -314,16 +343,16 @@ ideal.x = ideal.x + x_move; ideal.y = ideal.y + y_move; - if(ideal.x > 83) { - ideal.x = 83; - } else if(ideal.x < 0) { - ideal.x = 0; + if(ideal.x > 70) { + ideal.x = 70; + } else if(ideal.x < 13) { + ideal.x = 13; } - if(ideal.y > 42) { - ideal.y = 42; - } else if(ideal.y < 0) { - ideal.y = 0; + if(ideal.y > 30) { + ideal.y = 30; + } else if(ideal.y < 12) { + ideal.y = 12; } @@ -332,53 +361,431 @@ printf("Ghost caught\n"); - Ghost caught_ghost(rand()%100, "/ghosts/"); + Ghost caught_ghost(rand()%100, rand()%20, "/ghosts/"); caught_ghost.save(); lcd.clear(); lcd.printString("You caught:", 0, 0); lcd.printString(caught_ghost.get_type_string().c_str(), 0, 1); + lcd.printString("Press A to", 0, 2); + lcd.printString("continue", 0, 3); lcd.refresh(); - wait(2); + //wait(2); g_buttonA_flag = 0; while(!g_buttonA_flag) { - wait_ms(10); } g_buttonA_flag = 0; } -float cursor_transform(int OldMax, - int OldMin, - int NewMax, - int NewMin, +bool ghostHit( int xGhost, int yGhost, int xJoy, int yJoy) +{ + int xDifference = abs(xGhost - xJoy); + int yDifference = abs(yGhost - yJoy); + + if(xDifference < 10 && yDifference <10){ + return true; + } else { + return false; + } +} + +void settings() +{ + // settings that can be edited: + // * contrast + // * button delay + + // menu from FSM + State fsm[2] = { + // line next{up, down} + {2,{1,1}}, // State: 0 + {4,{0,0}}, // State: 1 + }; + + + int current_state = 0; + + printf("entered game menu\n"); + g_buttonX_flag = 0; + g_buttonA_flag = 0; + g_buttonB_flag = 0; + + while(1) { + lcd.clear(); + lcd.printString("Settings",0,0); + lcd.printString("Contrast",0,2); + lcd.printString("Button delay",0,4); + + if(g_buttonA_flag) { + g_buttonA_flag = 0; + switch (fsm[current_state].output) { + case 2: + adjustContrast(); + break; + case 4: + buttonDelay(); + break; + } + } else if (g_buttonStart_flag) { + g_buttonStart_flag = 0; + return; + } + + if(g_buttonX_flag) { + current_state = fsm[current_state].nextState[0]; + g_buttonX_flag = 0; + g_buttonB_flag = 0; + } else if(g_buttonB_flag) { + current_state = fsm[current_state].nextState[1]; + g_buttonX_flag = 0; + g_buttonB_flag = 0; + } + + switch (fsm[current_state].output) { + case 2: + lcd.drawCircle(79, 20, 3, FILL_BLACK); + break; + case 4: + lcd.drawCircle(79, 35, 3, FILL_BLACK); + break; + } + + lcd.refresh(); + } +} + +void adjustContrast() +{ + while(1) { + lcd.clear(); + // Print title + + lcd.printString("Contrast", 0, 0); + lcd.printString("Press A to set", 0, 3); + + float conSet = 0.4 + ((double) pad.read_pot1()/5); + float bar = cursor_transform(0.6,0.4,64,0,conSet); + + printf("conSet: %f\n", conSet); + printf("bar: %f\n", bar); + + // example of how to draw rectangles + // origin x,y,width,height,type + lcd.drawRect(10,10,74,10,FILL_TRANSPARENT); // transparent, just outline + lcd.drawRect(10,10,bar,10,FILL_BLACK); // filled black rectangle + lcd.setContrast(conSet); + + if(g_buttonA_flag) { + g_buttonA_flag = 0; + return; + } + + lcd.refresh(); + } +} + +void buttonDelay() +{ + g_buttonTesting = true; + + lcd.clear(); + + bool circle = false; + + while(1) { + lcd.clear(); + lcd.printString("Button Delay", 0, 0); + lcd.printString("Press X to test", 0, 4); + lcd.printString("Press A to set", 0, 5); + + g_buttonSensitivityTest = cursor_transform(1,0,2,0,pad.read_pot2()) * 250; + int bar = cursor_transform(500,0,64,0,g_buttonSensitivityTest); + + // example of how to draw rectangles + // origin x,y,width,height,type + lcd.drawRect(10,10,74,10,FILL_TRANSPARENT); // transparent, just outline + lcd.drawRect(10,10,bar,10,FILL_BLACK); // filled black rectangle + + if(circle) { + lcd.drawCircle(42, 25, 3, FILL_BLACK); + } else { + lcd.drawCircle(42, 25, 3, FILL_TRANSPARENT); + } + + if(g_buttonX_flag) { + g_buttonX_flag = 0; + circle = !circle; + } else if(g_buttonA_flag) { + g_buttonA_flag = 0; + g_button_sesnsitivity = g_buttonSensitivityTest; + g_buttonTesting = false; + return; + } else if (g_buttonStart_flag) { + g_buttonStart_flag = 0; + g_buttonTesting = false; + return; + } + + lcd.refresh(); + + } +} + + +float cursor_transform(float OldMax, + float OldMin, + float NewMax, + float NewMin, float OldValue) { float NewValue = -1; - int OldRange = (OldMax - OldMin); + float OldRange = (OldMax - OldMin); if (OldRange == 0) { NewValue = NewMin; } else { - int NewRange = (NewMax - NewMin); + float NewRange = (NewMax - NewMin); NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin; } return NewValue; } -void inventory() +void display_inventory(Inventory inventory) { printf("viewing inventory\n"); - lcd.printString("Inventory:", 0, 0); + int which_ghost_state = 0; + int which_view_state = 0; + int new_which_view_state = 0; + + std::vector<inven_state> ghost_fsm; + + bool regen_inven = true; + bool scroll_ghost = false; + bool update = true; + + while(1) { + if(regen_inven) { + inventory.regen(); + ghost_fsm = gen_ghost_fsm(inventory); + regen_inven = false; + which_ghost_state = 0; + which_view_state = 0; + } + + if(update) { + update = false; + + if(scroll_ghost) { + wait_ms(750); + scroll_ghost = false; + } + + + lcd.clear(); + + lcd.printString("Inventory", 0, 0); + char buffer [64]; + + if(ghost_fsm.size() == 0) { + lcd.printString("No ghosts", 0, 1); + lcd.printString("found, exiting", 0, 2); + lcd.refresh(); + wait(1); + return; + } else { + + if(which_view_state <= 0) { + sprintf(buffer, "%s", ghost_fsm[which_ghost_state].type.c_str()); + lcd.printString(buffer, 0, 1 - which_view_state); + } + + if(which_view_state <= 1) { + sprintf(buffer, "Name: %s", ghost_fsm[which_ghost_state].name.c_str()); + lcd.printString(buffer, 0, 2 - which_view_state); + } + + if(which_view_state <= 2) { + sprintf(buffer, "Attack: %d", ghost_fsm[which_ghost_state].attack); + lcd.printString(buffer, 0, 3 - which_view_state); + } + + if(which_view_state <= 3) { + sprintf(buffer, "Defense: %d", ghost_fsm[which_ghost_state].defense); + lcd.printString(buffer, 0, 4 - which_view_state); + } + + if(which_view_state <= 4) { + sprintf(buffer, "Level: %d", ghost_fsm[which_ghost_state].level); + lcd.printString(buffer, 0, 5 - which_view_state); + } + + if(which_view_state <= 5) { + sprintf(buffer, "Value: %d", ghost_fsm[which_ghost_state].value); + lcd.printString(buffer, 0, 6 - which_view_state); + } + + if(which_view_state <= 6) { + sprintf(buffer, "HP Max: %d", ghost_fsm[which_ghost_state].hp_max); + lcd.printString(buffer, 0, 7 - which_view_state); + } + + if(which_view_state <= 7) { + sprintf(buffer, "HP: %d", ghost_fsm[which_ghost_state].hp); + lcd.printString(buffer, 0, 7 - which_view_state); + } + + lcd.refresh(); + } + + } + + if(pad.read_pot2() <= (double) 0.33) { + new_which_view_state = 0; + } else if(pad.read_pot2() <= (double) 0.66) { + new_which_view_state = 1; + } else { + new_which_view_state = 2; + } + + if(new_which_view_state != which_view_state) { + update = true; + which_view_state = new_which_view_state; + } + + if(pad.read_pot1() < (double) 0.33) { + which_ghost_state = ghost_fsm[which_ghost_state].next[1]; + scroll_ghost = true; + } else if(pad.read_pot1() > (double) 0.66) { + which_ghost_state = ghost_fsm[which_ghost_state].next[0]; + scroll_ghost = true; + } + + if(scroll_ghost) { + update = true; + } + + if(g_buttonX_flag) { + printf("X button pressed\n"); + update = true; + g_buttonX_flag = 0; + lcd.drawRect(0,16,84,16, FILL_WHITE); + lcd.printString("Press again",10,2); + lcd.printString("to sell",20,3); + + //wait_ms(50); + + Timer t; + t.start(); + + g_buttonX_flag = 0; + bool sell = false; + + while(t.read() <= 5) { + int time = t.read(); + bool changed = false; + if(time == 0) { + lcd.printString("5",60,3); + changed = true; + } else if (time == 1) { + lcd.printString("4",60,3); + changed = true; + } else if (time == 2) { + lcd.printString("3",60,3); + changed = true; + } else if (time == 3) { + lcd.printString("2",60,3); + changed = true; + } else if (time == 4) { + lcd.printString("1",60,3); + changed = true; + } else if (time == 5) { + lcd.printString("0",60,3); + changed = true; + } + + if(g_buttonX_flag) { + printf("button X pressed\n"); + g_buttonX_flag = 0; + //wait_ms(50); + sell = true; + } + + if(changed) { + lcd.refresh(); + } + + if(sell) { + printf("Exiting to sell\n"); + break; + } + + } + + if(sell) { + printf("Running sell function\n"); + inventory.sell_ghost_by_uid(ghost_fsm[which_ghost_state].uid); + regen_inven = true; + update = true; + //wait_ms(500); + } + + } else if(g_buttonStart_flag) { + g_buttonStart_flag = 0; + return; + } + + } } -void settings() +std::vector<inven_state> gen_ghost_fsm(Inventory inventory) { - printf("adjusting settings\n"); + std::vector<inven_state> ghost_fsm; + + std::vector<int> uids = inventory.list_ghost_uids(); + + sort(uids.begin(), uids.end()); + + for(int i = 0; i < uids.size(); i++) { + Ghost ghost_temp = inventory.get_ghost_by_uid(uids[i]); + inven_state temp; + temp.uid = ghost_temp.get_uid(); + temp.name = ghost_temp.get_name(); + temp.type = ghost_temp.get_type_string(); + temp.attack = ghost_temp.get_attack(); + temp.defense = ghost_temp.get_defense(); + temp.level = ghost_temp.get_level(); + temp.xp = ghost_temp.get_xp(); + temp.value = ghost_temp.get_value(); + temp.hp_max = ghost_temp.get_hp_max(); + temp.hp = ghost_temp.get_hp(); + + ghost_fsm.push_back(temp); + //printf("Added Ghost UID %i to fsm\n", temp.uid); + printf("%s\n", temp.name.c_str()); + } + + for(int i = 0; i < ghost_fsm.size(); i++) { + if (i == 0) { + int next[2] = {1, ghost_fsm.size() - 1}; + ghost_fsm[i].next[0] = next[0]; + ghost_fsm[i].next[1] = next[1]; + } else if (i == ghost_fsm.size() - 1) { + int next[2] = {0, ghost_fsm.size() - 2}; + ghost_fsm[i].next[0] = next[0]; + ghost_fsm[i].next[1] = next[1]; + } else { + int next[2] = {i + 1, i - 1}; + ghost_fsm[i].next[0] = next[0]; + ghost_fsm[i].next[1] = next[1]; + } + } + + return ghost_fsm; } @@ -497,10 +904,21 @@ correct_files.push_back(files[i]); } } - + return correct_files; } + +// Function taken from https://stackoverflow.com/a/874160/10436605 +bool hasEnding (std::string const &fullString, std::string const &ending) +{ + if (fullString.length() >= ending.length()) { + return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); + } else { + return false; + } +} + void int_to_bin_digit(unsigned int in, int count, int* out) { /* assert: count <= sizeof(int)*CHAR_BIT */ @@ -512,20 +930,42 @@ } } + // Button A event-triggered interrupt void buttonX_isr() { g_buttonX_flag = 1; // set flag in ISR + if(!g_buttonTesting) { + wait_ms(g_button_sesnsitivity); + } else { + wait_ms(g_buttonSensitivityTest); + } } // Button A event-triggered interrupt void buttonA_isr() { g_buttonA_flag = 1; // set flag in ISR + wait_ms(g_button_sesnsitivity); } // Button A event-triggered interrupt void buttonB_isr() { g_buttonB_flag = 1; // set flag in ISR + wait_ms(g_button_sesnsitivity); +} + +// Button A event-triggered interrupt +void buttonY_isr() +{ + g_buttonY_flag = 1; // set flag in ISR + wait_ms(g_button_sesnsitivity); +} + +// Button A event-triggered interrupt +void buttonStart_isr() +{ + g_buttonStart_flag = 1; // set flag in ISR + wait_ms(g_button_sesnsitivity); } \ No newline at end of file