A top-down space game with a focus on exploration and collection Jack Townsend 200874711
Fork of Space_Explorer by
Revision 1:6a8aedb2ff69, committed 2016-06-08
- Comitter:
- The_Jackal
- Date:
- Wed Jun 08 14:54:59 2016 +0000
- Parent:
- 0:7a4b26a3aef5
- Commit message:
- Initial release
Changed in this revision
beep.lib | Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/beep.lib Fri May 13 13:30:32 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/code/beep/#d8e14429a95f
--- a/main.cpp Fri May 13 13:30:32 2016 +0000 +++ b/main.cpp Wed Jun 08 14:54:59 2016 +0000 @@ -1,4 +1,10 @@ -//Include libraries +/** +@file main.cpp +@brief Main file containing the code for the game. +@brief Revision 1.0.2 +@author Jack A. Townsend +@date June 2016 +*/ #include "mbed.h" #include "N5110.h" #include "time.h" @@ -6,39 +12,83 @@ #define XCALI 0.5 #define YCALI 0.5 -//set up serial connection +/** +Allows transmission of information over USB via serial for debugging purposes +*/ Serial pc(USBTX, USBRX); -//Set display pins +/** +Sets the display pins for the N5110 LCD +*/ N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); -//Set tickers +/** +@namespace ticker1 +@brief Ticker to allow for repeated interrupts within the code +@namespace ticker2 +@brief Ticker to allow for repeated interrupts within the code +@namespace ticker3 +@brief Ticker to allow for repeated interrupts within the code +*/ Ticker ticker1; Ticker ticker2; Ticker ticker3; -//Set interrupts +/** +@namespace Button1 +@brief Interrupt for Button 1 +@namespace Button2 +@brief Interrupt for Button 2 +@namespace Button3 +@brief Interrupt for Button 3 +@namespace Button4 +@brief Interrupt for Button 4 +@namespace JoyButton +@brief Interrupt for the Joystick Button +*/ InterruptIn Button1(PTB18); InterruptIn Button2(PTB19); InterruptIn Button3(PTC1); InterruptIn Button4(PTC8); InterruptIn JoyButton(PTB11); -//Set Analog Inputs +/** +@namespace Pot +@brief Analog input for the Potentiometer +@namespace XJoy +@brief Analog input for the Joystick X-Axis +@namespace YJoy +@brief Analog input for the Joystick Y-Axis +*/ AnalogIn Pot(PTB10); AnalogIn XJoy(PTB2); AnalogIn YJoy(PTB3); -//Set Digital Outputs +/** +@namespace rled +@brief GPIO output for the red LED +@namespace yled +@brief GPIO output for the yellow LED +@namespace gled +@brief GPIO output for the green LED +*/ DigitalOut rled(PTC10); DigitalOut yled(PTC11); DigitalOut gled(PTA1); -//Set Pwm Outputs +/** +@namespace flash +@brief PWM output for the flashing red LED +@namespace buzz +@brief PWM output for the piezo buzzer +*/ PwmOut flash(PTC2); PwmOut Buzz(PTA2); -//Set up the seeds array +/** +@namespace seed +@brief Holds seed data for map generation +*/ int seed[10][12] = { {13, 15, 1, 9, 2, 3, 0, 14, 7, 6, 12, 5}, {10, 0, 15, 3, 7, 2, 11, 12, 9, 8, 14, 5}, @@ -52,15 +102,15 @@ {5, 6, 12, 2, 9, 14, 8, 15, 13, 1, 10, 6}, }; -//Set up the map data array -int mapdata [16][46][60] = {0}; + +int mapdata [16][46][60] = {0}; /*!< Array for Map data*/ -//set up the screenframe array -int frame [1][48][84] = {0}; - -//Set up the sprite arrays +/** +@namespace objdata +@brief Holds sprite data for map generation +*/ int objdata [5][12][12] = {{ - //Station sprite + ///Station sprite {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, @@ -75,7 +125,7 @@ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, - //Asteroid cluster sprite + ///Asteroid cluster sprite { {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, @@ -90,7 +140,7 @@ {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0} }, - //Planet sprite + ///Planet sprite { {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0}, {0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0}, @@ -105,7 +155,7 @@ {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0} }, - //Gas cloud sprite + ///Gas cloud sprite { {0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0}, @@ -120,7 +170,7 @@ {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0} }, - //Cargo icon + ///Cargo icon { {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0}, @@ -135,7 +185,10 @@ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} }, }; - +/** +@namespace ship +@brief Holds ship sprites in various rotations +*/ int ship [4][4][4] = { { {0, 1, 1, 0}, {0, 1, 1, 0}, @@ -161,73 +214,92 @@ {0, 0, 1, 1} }, }; -//Set up ints and floats -int ZPos = 0; -int XPos = 0; -int YPos = 0; -int v = 0; -int i = 0; -int x = 0; -int y = 0; -int pixel = 0; -int s = 0; -int b = 0; -int joystate = 3; -int motion = 0; -int clear = 1; -int start = 1; -int cargo = 0; -int scan = 0; -int life = 3; -int g = 0; -double cash = 0; -int cost = 0; +int ZPos = 0; /*!< Z-position of player in the arrays */ +int XPos = 0; /*!< X-position of player in the arrays */ +int YPos = 0; /*!< Y-position of player in the arrays */ +int v = 0; /*!< Generic variable */ +int i = 0; /*!< Generic variable */ +int x = 0; /*!< Generic variable */ +int y = 0; /*!< Generic variable */ +int pixel = 0; /*!< Pixel variable used in drawing loops */ +int s = 0; /*!< Generic variable */ +int b = 0; /*!< Generic variable */ +int joystate = 3; /*!< Joystick position */ +int motion = 0; /*!< Motion detection result */ +int clear = 1; /*!< Variable for ensuring ship pacement in free space */ +int start = 1; /*!< Record of starting screen */ +int cargo = 0; /*!< Cargo amount */ +int life = 3; /*!< Life amount */ +int g = 0; /*!< Generic variable */ +double cash = 0; /*!< Cash amount */ +int cost = 0; /*!< Repair cost amount */ -//initialise isr variables -volatile int butx_flag[4] = {0}; -volatile int butj_flag = 0; -volatile int refresh_flag = 0; -//Declare isrs +volatile int butx_flag[4] = {0}; /*!<ISR flag for all main buttons */ +volatile int butj_flag = 0; /*!<ISR flag for the joystick button */ +volatile int refresh_flag = 0; /*!< ISR flag for screen refreshing */ + -void but1_isr(); -void but2_isr(); -void but3_isr(); -void but4_isr(); -void butj_isr(); -void refresh_isr(); +void but1_isr(); /** Unset Button one flag */ +void but2_isr(); /** Unset Button two flag */ +void but3_isr(); /** Unset Button three flag */ +void but4_isr(); /** Unset Button four flag */ +void butj_isr(); /** Unset Joystick Button flag */ +void refresh_isr(); /** Unset Refresh flag */ + -//Declare Functions -//Generates map at init +/** +Generates a new map based on the selected seed +*/ void MapGen(); -//Draws objects +/** +Used in conjunction with MapGen, draws objects from the sprite data into the map data +*/ void drawobj(); -//Updates the screen with sprites -void ScreenDraw(); -//Runs the game environment +/** +Based on player location, draws the data from the map array onto the screen itself +*/ +void ScreenDraw(); +/** +Runs the main game environment +*/ void run1(); -//Runs the ship menu -void run2(); -//determines analog positions -void ain(); -//refreshes the screen -void refresh(); -//repositions the player if blocked -void reposition(); -//Damage taken -void Damage(); -//checks for collisions before moving -void collision_check(); -//Draws the user interface + menu functions -int UI(int); -//reloads if a crash is detected -void crash(); -//repair minigame -void repair(); -//return to main game +/** +Gets the positions of the joystick axes and the potentiometer, updates the appropriate variables +*/ +void ain(); +/** +Updates the screen and handles ship rotation and movement detection/implementation +*/ +void refresh(); +/** +Repositions the player +*/ +void reposition(); +/** +Implements damage to the player +*/ +void Damage(); +/** +Collision detection +*/ +void collision_check(); +//defined in the code +int UI(int); +/** +Restarts map generation if a crash is detected +*/ +void crash(); +/** +Starts the repair minigame +*/ +void repair(); +/** +Returns to the main game +*/ void Return(); -//resets the button flags +//defined in the code int reset(int); @@ -235,11 +307,13 @@ - +/** +Initialises the display and interrupts, then launches the game +*/ int main() { - //pc.printf("Main\n \n"); + pc.printf("Main\n \n"); lcd.init(); //initialise display srand(time(NULL)); //Set up rand function JoyButton.rise(&butj_isr); @@ -257,10 +331,15 @@ lcd.printString("2016 Leeds",12,4); wait(3); lcd.clear(); - //pc.printf("Display initialised\n \n"); - //pc.printf("Rand funct set\n \n"); + pc.printf("Display initialised\n \n"); + pc.printf("Rand funct set\n \n"); UI(2); } +/** +@brief Draws a user interface based on what is required by the game +@param t - Interface number +@Returns The interface that will be implemented +*/ int UI(int t) { switch(t) { @@ -392,9 +471,9 @@ ticker3.attach(&crash, 2.5); i=1; MapGen(); - //pc.printf("\nMap generated\n \n"); + pc.printf("\nMap generated\n \n"); ScreenDraw(); - //pc.printf("Loading complete!\n"); + pc.printf("Loading complete!\n"); break; case 4: lcd.clear(); @@ -549,7 +628,11 @@ butj_flag = 0; } } - +/** +@brief Resets the selected flag +@param s - Flag selection +@returns a cleared flag +*/ int reset(int s) { for(i=0; i<4; i++) { @@ -694,7 +777,7 @@ void refresh() { lcd.refresh(); - //pc.printf("tick\n"); + pc.printf("tick\n"); if(motion == 1) { @@ -841,7 +924,7 @@ wait(0.05); switch(joystate) { case 0: - for(x=2; x<9; x++) { + for(x=2; x<12; x++) { if(lcd.getPixel(XPos-x,YPos+2)!= 0) { cargo++; } @@ -850,13 +933,13 @@ } lcd.refresh(); wait(0.075); - for(x=2; x<9; x++) { + for(x=2; x<12; x++) { lcd.clearPixel(XPos-x,YPos+2); } lcd.refresh(); break; case 2: - for(x=5; x<12; x++) { + for(x=5; x<15; x++) { if(lcd.getPixel(XPos+x,YPos+1)!= 0) { cargo++; } @@ -865,13 +948,13 @@ } lcd.refresh(); wait(0.075); - for(x=5; x<12; x++) { + for(x=5; x<15; x++) { lcd.clearPixel(XPos+x,YPos+1); } lcd.refresh(); break; case 1: - for(y=5; y<12; y++) { + for(y=5; y<15; y++) { if(lcd.getPixel(XPos+2,YPos+y)!= 0) { cargo++; } @@ -880,13 +963,13 @@ } lcd.refresh(); wait(0.075); - for(y=5; y<12; y++) { + for(y=5; y<15; y++) { lcd.clearPixel(XPos+2,YPos+y); } lcd.refresh(); break; case 3: - for(y=2; y<9; y++) { + for(y=2; y<12; y++) { if(lcd.getPixel(XPos+1,YPos-y)!= 0) { cargo++; } @@ -895,7 +978,7 @@ } lcd.refresh(); wait(0.075); - for(y=2; y<9; y++) { + for(y=2; y<12; y++) { lcd.clearPixel(XPos+1,YPos-y); } lcd.refresh(); @@ -914,20 +997,20 @@ } void drawobj() { - //pc.printf("Drawing object at:\n \n"); - //pc.printf("x %1d y %1d \n", XPos, YPos); - //pc.printf("on screen %1d \n \n \n", ZPos); + pc.printf("Drawing object at:\n \n"); + pc.printf("x %1d y %1d \n", XPos, YPos); + pc.printf("on screen %1d \n \n \n", ZPos); for (y = 0; y < 12; y++) { for (x = 0; x < 12; x++) { mapdata [ZPos][XPos+x][YPos+y] = objdata [s][x][y]; } } - //pc.printf("Draw Complete, v = %d \n", v); + pc.printf("Draw Complete, v = %d \n", v); } void ScreenDraw() { volatile int butx_flag[4] = {0}; - //pc.printf("Drawing screen\n \n"); + pc.printf("Drawing screen\n \n"); if(ZPos<0 | ZPos>15) { ZPos = rand()%16; } @@ -950,10 +1033,10 @@ lcd.setPixel(61,y); lcd.setPixel(62,y); } - //pc.printf("\n"); - //pc.printf("Buffer complete\n"); + pc.printf("\n"); + pc.printf("Buffer complete\n"); lcd.refresh(); - //pc.printf("Refreshed\n \n"); + pc.printf("Refreshed\n \n"); run1(); } @@ -966,7 +1049,7 @@ void MapGen() { - //pc.printf("Generating map\n"); + pc.printf("Generating map\n"); //Array is already declared //Generate 15 background stars on each screen in random locations for(ZPos=0; ZPos<16; ZPos++) { @@ -977,7 +1060,7 @@ } } - ////////pc.printf("160 Debris Generated\n"); + //pc.printf("160 Debris Generated\n"); s = 2; //Generate 9 gas clouds on the map in random locations for(v=0; v<9; v++) { @@ -987,7 +1070,7 @@ drawobj(); } - ////pc.printf("9 Gas Clouds Generated\n"); + pc.printf("9 Gas Clouds Generated\n"); s = 3; //Generate 6 planets on the map in seed locations for(v=0; v<6; v++) { @@ -997,7 +1080,7 @@ drawobj(); } - ////pc.printf("6 Planets Generated\n"); + pc.printf("6 Planets Generated\n"); s = 1; //Generate 5 asteroid clusters on the map in seed locations for(v=0; v<5; v++) { @@ -1007,14 +1090,14 @@ drawobj(); } - //pc.printf("5 Asteroid Clusters Generated\n"); + pc.printf("5 Asteroid Clusters Generated\n"); //Generate one station on the map in a seed location s = 0; ZPos = seed[b][11]; XPos = rand()%37 + 6; YPos = rand()%24 + 6; - //pc.printf("1 Station Generated\n"); + pc.printf("1 Station Generated\n"); drawobj(); //places the character XPos = 24;