Adventure game written for ECE2035 at the Georgia Institute of Technology

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
trmontgomery
Date:
Tue Apr 17 17:17:20 2018 +0000
Revision:
2:0876296d9473
Child:
4:cdc54191ff07
Baseline completed + 3 features

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trmontgomery 2:0876296d9473 1 // Project includes
trmontgomery 2:0876296d9473 2 #include "globals.h"
trmontgomery 2:0876296d9473 3 #include "hardware.h"
trmontgomery 2:0876296d9473 4 #include "map.h"
trmontgomery 2:0876296d9473 5 #include "graphics.h"
trmontgomery 2:0876296d9473 6 #include "speech.h"
trmontgomery 2:0876296d9473 7
trmontgomery 2:0876296d9473 8 // Functions in this file
trmontgomery 2:0876296d9473 9 int get_menu_action (GameInputs inputs);
trmontgomery 2:0876296d9473 10 int update_menu (int action);
trmontgomery 2:0876296d9473 11 void draw_menu (int init);
trmontgomery 2:0876296d9473 12 void init_start_menu ();
trmontgomery 2:0876296d9473 13 int menu ();
trmontgomery 2:0876296d9473 14
trmontgomery 2:0876296d9473 15 int b2presses = 0;
trmontgomery 2:0876296d9473 16 int start;
trmontgomery 2:0876296d9473 17 int quit;
trmontgomery 2:0876296d9473 18 int start_color;
trmontgomery 2:0876296d9473 19 int quit_color;
trmontgomery 2:0876296d9473 20 int third_color;
trmontgomery 2:0876296d9473 21 int current_item = 0;
trmontgomery 2:0876296d9473 22
trmontgomery 2:0876296d9473 23 /**
trmontgomery 2:0876296d9473 24 * The main game state. Must include Player locations and previous locations for
trmontgomery 2:0876296d9473 25 * drawing to work properly. Other items can be added as needed.
trmontgomery 2:0876296d9473 26 */
trmontgomery 2:0876296d9473 27
trmontgomery 2:0876296d9473 28
trmontgomery 2:0876296d9473 29 /**
trmontgomery 2:0876296d9473 30 * Given the game inputs, determine what kind of update needs to happen.
trmontgomery 2:0876296d9473 31 * Possbile return values are defined below.
trmontgomery 2:0876296d9473 32 */
trmontgomery 2:0876296d9473 33 #define NO_ACTION 0
trmontgomery 2:0876296d9473 34 #define BUTTON1 1
trmontgomery 2:0876296d9473 35 #define BUTTON2 2
trmontgomery 2:0876296d9473 36 #define BUTTON3 3
trmontgomery 2:0876296d9473 37 #define MENU_BUTTON 4
trmontgomery 2:0876296d9473 38 #define GO_LEFT 5
trmontgomery 2:0876296d9473 39 #define GO_RIGHT 6
trmontgomery 2:0876296d9473 40 #define GO_UP 7
trmontgomery 2:0876296d9473 41 #define GO_DOWN 8
trmontgomery 2:0876296d9473 42 int get_menu_action(GameInputs inputs)
trmontgomery 2:0876296d9473 43 {
trmontgomery 2:0876296d9473 44 if(!inputs.b1){
trmontgomery 2:0876296d9473 45 return BUTTON1;
trmontgomery 2:0876296d9473 46 }
trmontgomery 2:0876296d9473 47 if(!inputs.b2){
trmontgomery 2:0876296d9473 48 return BUTTON2;
trmontgomery 2:0876296d9473 49 }
trmontgomery 2:0876296d9473 50 if (!inputs.b3){
trmontgomery 2:0876296d9473 51 return BUTTON3;
trmontgomery 2:0876296d9473 52 }
trmontgomery 2:0876296d9473 53 if ((inputs.ay > 0.05 && inputs.ay < 0.5) && abs(inputs.ax) < .5){
trmontgomery 2:0876296d9473 54 return GO_LEFT;
trmontgomery 2:0876296d9473 55 } else if ((inputs.ay < -0.05 && inputs.ay > -0.5) && abs(inputs.ax) < .5){
trmontgomery 2:0876296d9473 56 return GO_RIGHT;
trmontgomery 2:0876296d9473 57 }else if (inputs.ax > 0.05 && inputs.ax < 0.5){
trmontgomery 2:0876296d9473 58 return GO_DOWN;
trmontgomery 2:0876296d9473 59 }else if (inputs.ax < -0.05 && inputs.ax > -0.5){
trmontgomery 2:0876296d9473 60 return GO_UP;
trmontgomery 2:0876296d9473 61 }
trmontgomery 2:0876296d9473 62 return NO_ACTION;
trmontgomery 2:0876296d9473 63 }
trmontgomery 2:0876296d9473 64
trmontgomery 2:0876296d9473 65 /**
trmontgomery 2:0876296d9473 66 * Update the game state based on the user action. For example, if the user
trmontgomery 2:0876296d9473 67 * requests GO_UP, then this function should determine if that is possible by
trmontgomery 2:0876296d9473 68 * consulting the map, and update the Player position accordingly.
trmontgomery 2:0876296d9473 69 *
trmontgomery 2:0876296d9473 70 * Return values are defined below. FULL_DRAW indicates that for this frame,
trmontgomery 2:0876296d9473 71 * draw_game should not optimize drawing and should draw every tile, even if
trmontgomery 2:0876296d9473 72 * the player has not moved.
trmontgomery 2:0876296d9473 73 */
trmontgomery 2:0876296d9473 74 #define NO_RESULT 0
trmontgomery 2:0876296d9473 75 #define GAME_OVER 1
trmontgomery 2:0876296d9473 76 #define FULL_DRAW 2
trmontgomery 2:0876296d9473 77 int update_menu(int action)
trmontgomery 2:0876296d9473 78 {
trmontgomery 2:0876296d9473 79
trmontgomery 2:0876296d9473 80 // Do different things based on the each action.
trmontgomery 2:0876296d9473 81 // You can define functions like "go_up()" that get called for each case.
trmontgomery 2:0876296d9473 82 switch(action) //have a function called for each type of object. Pass a pointer to the object to the interact function
trmontgomery 2:0876296d9473 83 {
trmontgomery 2:0876296d9473 84 case BUTTON1:
trmontgomery 2:0876296d9473 85 //if button 1 pressed move to next menu item
trmontgomery 2:0876296d9473 86 current_item = current_item++;
trmontgomery 2:0876296d9473 87 break;
trmontgomery 2:0876296d9473 88 case BUTTON2:
trmontgomery 2:0876296d9473 89 //if button 2 pressed select menu item
trmontgomery 2:0876296d9473 90 //b2presses++;
trmontgomery 2:0876296d9473 91 if (current_item%3 == 0) {//Start game
trmontgomery 2:0876296d9473 92 start = true;
trmontgomery 2:0876296d9473 93 } else if (current_item%3 == 1){ //Quit (screen cut to black)
trmontgomery 2:0876296d9473 94 quit = true;
trmontgomery 2:0876296d9473 95 } else { //screen go white
trmontgomery 2:0876296d9473 96
trmontgomery 2:0876296d9473 97 }
trmontgomery 2:0876296d9473 98 break;
trmontgomery 2:0876296d9473 99 case MENU_BUTTON:
trmontgomery 2:0876296d9473 100 break;
trmontgomery 2:0876296d9473 101 default: break;
trmontgomery 2:0876296d9473 102 }
trmontgomery 2:0876296d9473 103 return NO_RESULT;
trmontgomery 2:0876296d9473 104 }
trmontgomery 2:0876296d9473 105
trmontgomery 2:0876296d9473 106 /**
trmontgomery 2:0876296d9473 107 * Entry point for frame drawing. This should be called once per iteration of
trmontgomery 2:0876296d9473 108 * the game loop. This draws all tiles on the screen, followed by the status
trmontgomery 2:0876296d9473 109 * bars. Unless init is nonzero, this function will optimize drawing by only
trmontgomery 2:0876296d9473 110 * drawing tiles that have changed from the previous frame.
trmontgomery 2:0876296d9473 111 */
trmontgomery 2:0876296d9473 112 void draw_menu(int init)
trmontgomery 2:0876296d9473 113 {
trmontgomery 2:0876296d9473 114
trmontgomery 2:0876296d9473 115
trmontgomery 2:0876296d9473 116 if (current_item%3 == 0){
trmontgomery 2:0876296d9473 117 start_color = GREEN;
trmontgomery 2:0876296d9473 118 } else if (current_item%3 == 1){
trmontgomery 2:0876296d9473 119 quit_color = GREEN;
trmontgomery 2:0876296d9473 120 } else{
trmontgomery 2:0876296d9473 121 third_color = GREEN;
trmontgomery 2:0876296d9473 122 }
trmontgomery 2:0876296d9473 123
trmontgomery 2:0876296d9473 124
trmontgomery 2:0876296d9473 125
trmontgomery 2:0876296d9473 126 uLCD.locate(1,4);
trmontgomery 2:0876296d9473 127 uLCD.color(start_color);
trmontgomery 2:0876296d9473 128 uLCD.printf("Start");
trmontgomery 2:0876296d9473 129
trmontgomery 2:0876296d9473 130 uLCD.locate(1,6);
trmontgomery 2:0876296d9473 131 uLCD.color(quit_color);
trmontgomery 2:0876296d9473 132 uLCD.printf("Quit");
trmontgomery 2:0876296d9473 133
trmontgomery 2:0876296d9473 134 uLCD.locate(1,8);
trmontgomery 2:0876296d9473 135 uLCD.color(third_color);
trmontgomery 2:0876296d9473 136 uLCD.printf("Thrid Option");
trmontgomery 2:0876296d9473 137
trmontgomery 2:0876296d9473 138 start_color = BLACK;
trmontgomery 2:0876296d9473 139 quit_color = BLACK;
trmontgomery 2:0876296d9473 140 third_color = BLACK;
trmontgomery 2:0876296d9473 141
trmontgomery 2:0876296d9473 142 }
trmontgomery 2:0876296d9473 143
trmontgomery 2:0876296d9473 144 /**
trmontgomery 2:0876296d9473 145 * Initialize the main world map. Add walls around the edges, interior chambers,
trmontgomery 2:0876296d9473 146 * and plants in the background so you can see motion.
trmontgomery 2:0876296d9473 147 */
trmontgomery 2:0876296d9473 148 void init_start_menu()
trmontgomery 2:0876296d9473 149 {
trmontgomery 2:0876296d9473 150 uLCD.locate(1,2);
trmontgomery 2:0876296d9473 151 //draw pink rectangle on whole screen
trmontgomery 2:0876296d9473 152 uLCD.filled_rectangle(0, 127, 127, 0, PINK1);
trmontgomery 2:0876296d9473 153 // write title
trmontgomery 2:0876296d9473 154 uLCD.textbackground_color(YELLOW);
trmontgomery 2:0876296d9473 155 uLCD.color(BLACK);
trmontgomery 2:0876296d9473 156 uLCD.printf("The Game");
trmontgomery 2:0876296d9473 157
trmontgomery 2:0876296d9473 158 if (current_item%3 == 0){
trmontgomery 2:0876296d9473 159 start_color = GREEN;
trmontgomery 2:0876296d9473 160 } else if (current_item%3 == 1){
trmontgomery 2:0876296d9473 161 quit_color = GREEN;
trmontgomery 2:0876296d9473 162 } else{
trmontgomery 2:0876296d9473 163 third_color = GREEN;
trmontgomery 2:0876296d9473 164 }
trmontgomery 2:0876296d9473 165
trmontgomery 2:0876296d9473 166 uLCD.locate(1,4);
trmontgomery 2:0876296d9473 167 uLCD.color(start_color);
trmontgomery 2:0876296d9473 168 uLCD.printf("Start");
trmontgomery 2:0876296d9473 169
trmontgomery 2:0876296d9473 170 uLCD.locate(1,6);
trmontgomery 2:0876296d9473 171 uLCD.color(quit_color);
trmontgomery 2:0876296d9473 172 uLCD.printf("Quit");
trmontgomery 2:0876296d9473 173
trmontgomery 2:0876296d9473 174 uLCD.locate(1,8);
trmontgomery 2:0876296d9473 175 uLCD.color(third_color);
trmontgomery 2:0876296d9473 176 uLCD.printf("Thrid Option");
trmontgomery 2:0876296d9473 177
trmontgomery 2:0876296d9473 178
trmontgomery 2:0876296d9473 179
trmontgomery 2:0876296d9473 180
trmontgomery 2:0876296d9473 181 }
trmontgomery 2:0876296d9473 182
trmontgomery 2:0876296d9473 183 /**
trmontgomery 2:0876296d9473 184 * Program entry point! This is where it all begins.
trmontgomery 2:0876296d9473 185 * This function orchestrates all the parts of the game. Most of your
trmontgomery 2:0876296d9473 186 * implementation should be elsewhere - this holds the game loop, and should
trmontgomery 2:0876296d9473 187 * read like a road map for the rest of the code.
trmontgomery 2:0876296d9473 188 */
trmontgomery 2:0876296d9473 189 int menu()
trmontgomery 2:0876296d9473 190 {
trmontgomery 2:0876296d9473 191 // First things first: initialize hardware
trmontgomery 2:0876296d9473 192 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
trmontgomery 2:0876296d9473 193
trmontgomery 2:0876296d9473 194 // Initialize the maps
trmontgomery 2:0876296d9473 195 init_start_menu();
trmontgomery 2:0876296d9473 196
trmontgomery 2:0876296d9473 197 // Initial drawing
trmontgomery 2:0876296d9473 198 draw_menu(true);
trmontgomery 2:0876296d9473 199
trmontgomery 2:0876296d9473 200 // Main game loop
trmontgomery 2:0876296d9473 201 while(1)
trmontgomery 2:0876296d9473 202 {
trmontgomery 2:0876296d9473 203 // Timer to measure game update speed
trmontgomery 2:0876296d9473 204 Timer t; t.start();
trmontgomery 2:0876296d9473 205
trmontgomery 2:0876296d9473 206 // Actually do the game update:
trmontgomery 2:0876296d9473 207 // 1. Read inputs
trmontgomery 2:0876296d9473 208 GameInputs in = read_inputs();
trmontgomery 2:0876296d9473 209 // 2. Determine action (get_action)
trmontgomery 2:0876296d9473 210 int a = get_menu_action(in);
trmontgomery 2:0876296d9473 211 // 3. Update game (update_game)
trmontgomery 2:0876296d9473 212 int u = update_menu(a);
trmontgomery 2:0876296d9473 213
trmontgomery 2:0876296d9473 214 // 4. Draw frame (draw_game)
trmontgomery 2:0876296d9473 215 draw_menu(u);
trmontgomery 2:0876296d9473 216
trmontgomery 2:0876296d9473 217 //check for start
trmontgomery 2:0876296d9473 218 if (start){
trmontgomery 2:0876296d9473 219 return start;
trmontgomery 2:0876296d9473 220 }
trmontgomery 2:0876296d9473 221
trmontgomery 2:0876296d9473 222
trmontgomery 2:0876296d9473 223 // 5. Frame delay
trmontgomery 2:0876296d9473 224 t.stop();
trmontgomery 2:0876296d9473 225 int dt = t.read_ms();
trmontgomery 2:0876296d9473 226 if (dt < 500) wait_ms(500 - dt);
trmontgomery 2:0876296d9473 227 }
trmontgomery 2:0876296d9473 228 }