
A simple one-level platform game. Developed as part of ELEC2645 at University of Leeds, spring 2015.
Dependencies: N5110 PinDetect PowerControl mbed
An ARM mbed LPC1768 microcontroller have been used to develop a handheld arcade game in the style of an old-school platformer. This project is entirely my own independent work in all stages of the development; including design, defining project specifications, breadboard prototyping, schematic and PCB layout using CAD, assembly, testing and software development. Due to this being part of the ELEC2645 Embedded Systems Project module at University of Leeds, spring 2015, limitations were given on the available hardware components. Credit is due to the authors of the dependent libraries (N5110, Pin Detect, PowerControl and mbed). I would also like to thank the author of Game Programming Patterns as well as the authors of SFML Game Development for providing me with useful sources for programming design patterns.
Project aims
- Implement simple gameplay:
- A single, fixed (no scrolling) level.
- Player can move left to right, jump and shoot.
- Enemies will drop from the top of the screen.
- The player gets points for shooting enemies.
- The player dies when it gets hits by an enemy.
- Implement a simple menu system.
- Enable the user to adjust the brightness of the display.
- Output sound to enhance the user experience.
Software
The program flow is controlled by a finite state machine. The implemented design was inspired by the State design pattern from the books Game Programming Patterns and SFML Game Development. The StateManager class is responsible for updating and rendering the current selected state. It also changes the state based on request from the current state. The framework built for the state machine used in this project makes it easy to add new screens.
The different main states (indicated by the background colour) and how the user interaction is shown below:
Hardware
Schematic:
Printed circuit board (PCB):
Images
A seperate program was written to convert images (png) to text-representation of the maps. Enemies and numbers on the screen are also collected from a sprite-sheet created in the same manner.
Revision 10:f2488a0ecab7, committed 2015-05-08
- Comitter:
- Siriagus
- Date:
- Fri May 08 14:39:36 2015 +0000
- Parent:
- 9:da608ae65df9
- Child:
- 11:adb68da98262
- Commit message:
- Encapsluated PinDetect buttons in InputManager (made private)
Changed in this revision
--- a/InputManager.cpp Sun May 03 11:48:42 2015 +0000 +++ b/InputManager.cpp Fri May 08 14:39:36 2015 +0000 @@ -25,31 +25,35 @@ delete btnC; } -void InputManager::addBtnPressInterrupt(PinDetect *btn, void (*func)(void)) +void InputManager::addBtnPressInterrupt(Input::Button button, void (*func)(void)) +{ + PinDetect *btn = getBtnPtr(button); + if (btn) // if not null pointer + btn->attach_asserted(func); +} + +PinDetect* InputManager::getBtnPtr(Input::Button button) { - btn->attach_asserted(func); + switch(button) + { + case Input::ButtonA: + return btnA; + + case Input::ButtonB: + return btnB; + + case Input::ButtonC: + return btnC; + + default: + return 0; // Return 0 (nullptr) if invalid input + } } int InputManager::read(Input::Button button) { - int pressed; - switch(button) - { - case Input::ButtonA: - pressed = *btnA; - break; - - case Input::ButtonB: - pressed = *btnB; - break; - - case Input::ButtonC: - pressed = *btnC; - break; - - default: - pressed = 0; - } + PinDetect *btn = getBtnPtr(button); + if (!btn) return 0; // Invalid button - return pressed; + return *btn; // Value of button } \ No newline at end of file
--- a/InputManager.h Sun May 03 11:48:42 2015 +0000 +++ b/InputManager.h Fri May 08 14:39:36 2015 +0000 @@ -5,25 +5,59 @@ #include "PinDetect.h" #include "Joystick.h" +/** @file InputManager.h */ + struct Input { + /** Used as identificator for the different buttons */ enum Button{ButtonA, ButtonB, ButtonC}; }; +/// Used to manage user input from buttons and thumb joystick class InputManager { public: + /** Creates a new InputManager object + * @param pinA Pin connected to button A + * @param pinB Pin connected to button B + * @param pinc Pin connected to button C + * @param x Pin connected to the horizontal potentiometer of the joystick + * @param y Pin connected to the vertical potentiometer of the joystick + * @param button Pin connected to the button of the thumb joystick + */ InputManager(PinName pinA, PinName pinB, PinName pinC, PinName joyH, PinName joyV, PinName joyBtn); + + /** Deconstructor. Frees allocated memory related to the buttons and the joystick **/ ~InputManager(); Joystick *joystick; + + /** @brief Adds a button interrupt which is invoked when the button is pressed. Button needs to be released for the interrupt to occur again. + * @param button Name of the button. + * @param func Callback function. + */ + void addBtnPressInterrupt(Input::Button button, void (*func)(void)); + + + /** Reads the current value of a button. + * @param button The button we want to read. + * @return Returns 1 if button is pressed, 0 otherwise. + */ + int read(Input::Button button); + + private: + /// Button objects PinDetect *btnA; PinDetect *btnB; PinDetect *btnC; - void addBtnPressInterrupt(PinDetect *btn, void (*func)(void)); - int read(Input::Button button); // Returns the 1 if the button is currently pressed, otherwise 0. + /** Returns a pointer to the actual button object + * @param button The requested button. + * @return Pointer to the button. + */ + PinDetect* getBtnPtr(Input::Button button); + }; #endif
--- a/Joystick.h Sun May 03 11:48:42 2015 +0000 +++ b/Joystick.h Fri May 08 14:39:36 2015 +0000 @@ -1,37 +1,47 @@ /** @file Joystick.h +@brief Class used for controlling the button +@author Andreas Garmannslund +@date April 2015 */ #ifndef JOYSTICK_H #define JOYSTICK_H #include "mbed.h" + +/** @brief Enum used for the 8-directions of the joystick. */ enum JoystickDirection {CENTER, UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT, UNKNOWN}; +/// Joystick class class Joystick { public: - /** Creates a new Joystick object. - * @param x Pin connected to the horizontal potentiometer of the joystick. - * @param y Pin connected to the vertical potentiometer of the joystick. + /** Creates a new Joystick object + * @param x Pin connected to the horizontal potentiometer of the joystick + * @param y Pin connected to the vertical potentiometer of the joystick * @param button Pin connected to the button of the thumb joystick */ Joystick(PinName x, PinName y, PinName button); - /** Destructor. Delete all allocated memory. **/ + /** @brief Deconstructor. Frees allocated memory */ ~Joystick(); - /** Reads the current direction and button status of the joystick **/ - void update(); // Updates the direction and button status of the joystick. + /** @brief Updates the current direction and button status of the joystick */ + void update(); - /** Calibrates the joystick. The joystick must be centered while this function is called. **/ - void calibrate(); // Calibrates joystick by updating the center positions. + /** Calibrates the joystick. The joystick must be centered while this function is called */ + void calibrate(); // Calibrates joystick by updating the center positions - /** Returns the current JoystickDirection based on last update. **/ + /** Returns the current JoystickDirection based on last update + * @return The current JoystickDirection. + */ int getDirection() {return dir;} - /** Reads the value of the button. **/ + /** Reads the value of the button + * @return 1 if pressed, 0 otherwise + */ int readButton() {return *btn;}; /** Square set around the center of the joystick where the input is ignored. The axes are treated seperately. Can be varied from 0 (no dead-zone) to 0.5 (max value for dx and dy). **/
--- a/MainMenu.cpp Sun May 03 11:48:42 2015 +0000 +++ b/MainMenu.cpp Fri May 08 14:39:36 2015 +0000 @@ -1,7 +1,10 @@ #include "MainMenu.h" +/// States for the Main Menu's internal finite state machine enum MenuState {SELECT_PLAY, SELECT_HIGHSCORES, SELECT_CONTROLS, LOAD_GAME, HIGHSCORES, CONTROLS}; -// Static variables for internal fsm + +/// Static variables for internal fsm. + const int MainMenu::MENU_FSM[6][3] = { {LOAD_GAME, SELECT_PLAY, SELECT_HIGHSCORES}, // State: SELECT_PLAY {HIGHSCORES, SELECT_HIGHSCORES, SELECT_CONTROLS}, // State: SELECT_HIGHSCORES @@ -11,7 +14,7 @@ {SELECT_CONTROLS, SELECT_CONTROLS, CONTROLS} // State: CONTROLS }; -int MainMenu::currentState; +int MainMenu::currentState; /// Current state for the state machine // Callback functions (needs to be static) void MainMenu::btnAPress() @@ -32,15 +35,12 @@ void MainMenu::init() { MainMenu::currentState = SELECT_PLAY; - input->addBtnPressInterrupt(input->btnA, &btnAPress); - input->addBtnPressInterrupt(input->btnB, &btnBPress); - input->addBtnPressInterrupt(input->btnC, &btnCPress); + input->addBtnPressInterrupt(Input::ButtonA, &btnAPress); + input->addBtnPressInterrupt(Input::ButtonB, &btnBPress); + input->addBtnPressInterrupt(Input::ButtonC, &btnCPress); } -void MainMenu::update(float dt) -{ - -} +void MainMenu::update(float dt) {} // Does not do anything as program flow is controlled by interrupts, but needs to be defined as it is a virtual function. void MainMenu::render() {
--- a/MainMenu.h Sun May 03 11:48:42 2015 +0000 +++ b/MainMenu.h Fri May 08 14:39:36 2015 +0000 @@ -3,26 +3,34 @@ #include "State.h" +/// State: Main Menu class MainMenu : public State { public: + /// Creates a new MainMenu object MainMenu(StateManager* fsm, N5110 *lcd, InputManager* input) : State(fsm, lcd, input) {init();} - virtual void update(float dt); - virtual void render(); + virtual void update(float dt); /// Update logic + virtual void render(); /// Draw MainMenu to screen private: void init(); - static void btnAPress(); // Need to be static to be used for callback - static void btnBPress(); - static void btnCPress(); + static void btnAPress(); /// Interrupt callback function when button A is pressed + static void btnBPress(); /// Interrupt callback function when button B is pressed + static void btnCPress(); /// Interrupt callback function when button C is pressed // Variables private: - enum MenuState {SELECT_PLAY, SELECT_HIGHSCORES, SELECT_CONTROLS, LOAD_GAME, HIGHSCORES, CONTROLS}; // Internal states for internal finite state machine - static int currentState; // Current internal state - static const int MENU_FSM[6][3]; // Internal finite state machine - works on a different principle than the one that controls the main states + /// States for the main menu's internal finite state machine + enum MenuState {SELECT_PLAY, SELECT_HIGHSCORES, SELECT_CONTROLS, LOAD_GAME, HIGHSCORES, CONTROLS}; + static int currentState; /// Current state of the internal finite state machine + + /** Transition table for internal fsm + * Each row corresponds to a state + * Each column corresponds to the button pressed + */ + static const int MENU_FSM[6][3]; }; #endif \ No newline at end of file
--- a/Resources.h Sun May 03 11:48:42 2015 +0000 +++ b/Resources.h Fri May 08 14:39:36 2015 +0000 @@ -3,6 +3,7 @@ // Resource files - sprites/images stored as arrays +/// Images: Arrays consisting of 1 (opaque) or 0 (blank). namespace Image { // Player sprite @@ -13,8 +14,6 @@ {0, 0, 1, 1, 0}, {0, 1, 0, 0, 1} }; - - } @@ -24,15 +23,4 @@ - - - - - - - - - - - #endif \ No newline at end of file
--- a/State.h Sun May 03 11:48:42 2015 +0000 +++ b/State.h Fri May 08 14:39:36 2015 +0000 @@ -5,25 +5,44 @@ #include "PinDetect.h" #include "InputManager.h" +/** @file State.h +* @author Andreas Garmannslund +* @date April 2015 +*/ + class StateManager; -// List of main states in the game +/// States used in the finite state machine. enum MainState {MAIN_MENU, GAME, SUBMIT_HIGHSCORE, GAME_OVER, NO_STATE, TITLE_SCREEN}; -// Each main state is a derived from the State class. - +/// Abstract class for states in the program's main finite state machine. All state implementations is derived from this abstract class. class State { public: - + + /* Creates a new state object. Should be called from child's constructor. + * @param fsm Pointer to finished state machine. + * @param lcd Pointer to the N5110 lcd object. + * @param input Pointer to the InputManager object, used for controlling user input. + */ State(StateManager* fsm, N5110 *lcd, InputManager* input) :lcd(lcd), input(input), fsm(fsm){} + /// Handle user input and update logic. virtual void update(float dt) = 0; + + /// Draw to screen. virtual void render() = 0; protected: + /* Requests the finite state machine to switch to a new state when possible. + * @param newState The state the fsm should switch to. + **/ void requestStateChange(MainState newState); + + /** Draws an image to the lcd + * @param img Array with the same size as the display, where 1 is opaque, 0 is blank. + */ void drawImage(const int img[BANKS][WIDTH]); // Draws an image from array protected:
--- a/StateManager.h Sun May 03 11:48:42 2015 +0000 +++ b/StateManager.h Fri May 08 14:39:36 2015 +0000 @@ -6,30 +6,52 @@ #include "Game.h" #include "TitleScreen.h" -// Main Finite State Machine - controls the flow between the main states +/** @file StateManager.h +* @author Andreas Garmannslund +* @date April 2015 +*/ + +/// Finite State Machine for program flow class StateManager { public: + /** Creates a new finite state machine. The states are defined in State.h + * @param lcd Pointer to the lcd + * @param input Pointer to the InputManager which is controlling user input. + * @param firstState The initial state of the finite state machine. + */ StateManager(N5110 *lcd, InputManager* input, MainState firstState) : lcd(lcd), input(input){currentState = 0; nextState = NO_STATE; changeState(firstState);} + + /// Frees allocated memory ~StateManager() {if (currentState != 0) delete currentState;} + /// Update logic of the current state void update(float dt); + + /// Draw the current state to the lcd void render(); + /** Can be used to request the fsm to switch state. + * @param newState The requested state + */ void requestStateChange(MainState newState); - void processRequest(); // See if any request to change the state have been made + /// Sees if any requests to change the state have been made + void processRequest(); private: - void changeState(MainState newState); // Deletes the current main state and creates a new one + /** Deletes the current state and create a new one + * @param newState The state which the finite state machine switches to + */ + void changeState(MainState newState); - // Variables + // Variables private: N5110 *lcd; InputManager *input; State* currentState; // Current state object - MainState nextState; + MainState nextState; // requested state, NONE if no state is requested }; #endif \ No newline at end of file
--- a/StateManger.cpp Sun May 03 11:48:42 2015 +0000 +++ b/StateManger.cpp Fri May 08 14:39:36 2015 +0000 @@ -1,6 +1,5 @@ #include "StateManager.h" -// Delete the old state and create a new one void StateManager::changeState(MainState newState) { if (currentState != 0) // if a state exist
--- a/TitleScreen.cpp Sun May 03 11:48:42 2015 +0000 +++ b/TitleScreen.cpp Fri May 08 14:39:36 2015 +0000 @@ -22,9 +22,9 @@ void TitleScreen::init() { - input->addBtnPressInterrupt(input->btnA, &btnPress); - input->addBtnPressInterrupt(input->btnB, &btnPress); - input->addBtnPressInterrupt(input->btnC, &btnPress); + input->addBtnPressInterrupt(Input::ButtonA, &btnPress); + input->addBtnPressInterrupt(Input::ButtonB, &btnPress); + input->addBtnPressInterrupt(Input::ButtonC, &btnPress); }
--- a/main.cpp Sun May 03 11:48:42 2015 +0000 +++ b/main.cpp Fri May 08 14:39:36 2015 +0000 @@ -1,8 +1,8 @@ /** -@brief Simple platform game developed for ELEC2645 Embedded Systems Project at University of Leeds - -@author Andreas Garmannslund -**/ +* @file main.cpp +* @brief Simple platform game developed for ELEC2645 Embedded Systems Project at University of Leeds +* @author Andreas Garmannslund +*/ #include "mbed.h" #include "N5110.h" @@ -33,9 +33,9 @@ #define BUTTON_C p29 // Input and Output -N5110 *lcd; // VCC, SCE, RST, D/C, MOSI, SCLK and LED +N5110 *lcd; /// Display -InputManager *input; +InputManager *input; /// Responsible for managing user input // Brightness potentiometer AnalogIn ledPot(LED_POT); @@ -56,13 +56,8 @@ lcd->refresh(); } -// States - - - - -void init(); -void cleanUp(); +void init(); /// Set up initial variables +void cleanUp(); /// Frees remaining allocated memory StateManager* fsm; @@ -94,7 +89,7 @@ timer.reset(); } - cleanUp(); + cleanUp(); // Not really reached as the program never terminates. Added for completeness. return 0; }