Adventure game written for ECE2035 at the Georgia Institute of Technology
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@2:0876296d9473, 2018-04-17 (annotated)
- Committer:
- trmontgomery
- Date:
- Tue Apr 17 17:17:20 2018 +0000
- Revision:
- 2:0876296d9473
- Parent:
- 0:35660d7952f7
- Child:
- 3:289762133fd6
Baseline completed + 3 features
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rconnorlawson | 0:35660d7952f7 | 1 | // Project includes |
rconnorlawson | 0:35660d7952f7 | 2 | #include "globals.h" |
rconnorlawson | 0:35660d7952f7 | 3 | #include "hardware.h" |
rconnorlawson | 0:35660d7952f7 | 4 | #include "map.h" |
rconnorlawson | 0:35660d7952f7 | 5 | #include "graphics.h" |
rconnorlawson | 0:35660d7952f7 | 6 | #include "speech.h" |
trmontgomery | 2:0876296d9473 | 7 | #include "startmenu.h" |
rconnorlawson | 0:35660d7952f7 | 8 | |
rconnorlawson | 0:35660d7952f7 | 9 | // Functions in this file |
rconnorlawson | 0:35660d7952f7 | 10 | int get_action (GameInputs inputs); |
rconnorlawson | 0:35660d7952f7 | 11 | int update_game (int action); |
rconnorlawson | 0:35660d7952f7 | 12 | void draw_game (int init); |
rconnorlawson | 0:35660d7952f7 | 13 | void init_main_map (); |
rconnorlawson | 0:35660d7952f7 | 14 | int main (); |
rconnorlawson | 0:35660d7952f7 | 15 | |
rconnorlawson | 0:35660d7952f7 | 16 | /** |
rconnorlawson | 0:35660d7952f7 | 17 | * The main game state. Must include Player locations and previous locations for |
rconnorlawson | 0:35660d7952f7 | 18 | * drawing to work properly. Other items can be added as needed. |
rconnorlawson | 0:35660d7952f7 | 19 | */ |
rconnorlawson | 0:35660d7952f7 | 20 | struct { |
rconnorlawson | 0:35660d7952f7 | 21 | int x,y; // Current locations |
rconnorlawson | 0:35660d7952f7 | 22 | int px, py; // Previous locations |
rconnorlawson | 0:35660d7952f7 | 23 | int has_key; |
trmontgomery | 2:0876296d9473 | 24 | int quest_complete; |
rconnorlawson | 0:35660d7952f7 | 25 | } Player; |
rconnorlawson | 0:35660d7952f7 | 26 | |
trmontgomery | 2:0876296d9473 | 27 | int door_open = false; |
trmontgomery | 2:0876296d9473 | 28 | int game_over = false; |
trmontgomery | 2:0876296d9473 | 29 | int health = 0; |
trmontgomery | 2:0876296d9473 | 30 | |
rconnorlawson | 0:35660d7952f7 | 31 | /** |
rconnorlawson | 0:35660d7952f7 | 32 | * Given the game inputs, determine what kind of update needs to happen. |
rconnorlawson | 0:35660d7952f7 | 33 | * Possbile return values are defined below. |
rconnorlawson | 0:35660d7952f7 | 34 | */ |
rconnorlawson | 0:35660d7952f7 | 35 | #define NO_ACTION 0 |
trmontgomery | 2:0876296d9473 | 36 | #define BUTTON1 1 |
trmontgomery | 2:0876296d9473 | 37 | #define BUTTON2 2 |
trmontgomery | 2:0876296d9473 | 38 | #define BUTTON3 3 |
trmontgomery | 2:0876296d9473 | 39 | #define MENU_BUTTON 4 |
trmontgomery | 2:0876296d9473 | 40 | #define GO_LEFT 5 |
trmontgomery | 2:0876296d9473 | 41 | #define GO_RIGHT 6 |
trmontgomery | 2:0876296d9473 | 42 | #define GO_UP 7 |
trmontgomery | 2:0876296d9473 | 43 | #define GO_DOWN 8 |
rconnorlawson | 0:35660d7952f7 | 44 | int get_action(GameInputs inputs) |
rconnorlawson | 0:35660d7952f7 | 45 | { |
trmontgomery | 2:0876296d9473 | 46 | if(!inputs.b1){ |
trmontgomery | 2:0876296d9473 | 47 | return BUTTON1; |
trmontgomery | 2:0876296d9473 | 48 | } |
trmontgomery | 2:0876296d9473 | 49 | if(!inputs.b2){ |
trmontgomery | 2:0876296d9473 | 50 | return BUTTON2; |
trmontgomery | 2:0876296d9473 | 51 | } |
trmontgomery | 2:0876296d9473 | 52 | if (!inputs.b3){ |
trmontgomery | 2:0876296d9473 | 53 | return MENU_BUTTON; |
trmontgomery | 2:0876296d9473 | 54 | } |
trmontgomery | 2:0876296d9473 | 55 | if ((inputs.ay > 0.05 && inputs.ay < 0.5) && abs(inputs.ax) < .5){ |
trmontgomery | 2:0876296d9473 | 56 | return GO_LEFT; |
trmontgomery | 2:0876296d9473 | 57 | } else if ((inputs.ay < -0.05 && inputs.ay > -0.5) && abs(inputs.ax) < .5){ |
trmontgomery | 2:0876296d9473 | 58 | return GO_RIGHT; |
trmontgomery | 2:0876296d9473 | 59 | }else if (inputs.ax > 0.05 && inputs.ax < 0.5){ |
trmontgomery | 2:0876296d9473 | 60 | return GO_DOWN; |
trmontgomery | 2:0876296d9473 | 61 | }else if (inputs.ax < -0.05 && inputs.ax > -0.5){ |
trmontgomery | 2:0876296d9473 | 62 | return GO_UP; |
trmontgomery | 2:0876296d9473 | 63 | } |
rconnorlawson | 0:35660d7952f7 | 64 | return NO_ACTION; |
rconnorlawson | 0:35660d7952f7 | 65 | } |
rconnorlawson | 0:35660d7952f7 | 66 | |
rconnorlawson | 0:35660d7952f7 | 67 | /** |
rconnorlawson | 0:35660d7952f7 | 68 | * Update the game state based on the user action. For example, if the user |
rconnorlawson | 0:35660d7952f7 | 69 | * requests GO_UP, then this function should determine if that is possible by |
rconnorlawson | 0:35660d7952f7 | 70 | * consulting the map, and update the Player position accordingly. |
rconnorlawson | 0:35660d7952f7 | 71 | * |
rconnorlawson | 0:35660d7952f7 | 72 | * Return values are defined below. FULL_DRAW indicates that for this frame, |
rconnorlawson | 0:35660d7952f7 | 73 | * draw_game should not optimize drawing and should draw every tile, even if |
rconnorlawson | 0:35660d7952f7 | 74 | * the player has not moved. |
rconnorlawson | 0:35660d7952f7 | 75 | */ |
rconnorlawson | 0:35660d7952f7 | 76 | #define NO_RESULT 0 |
rconnorlawson | 0:35660d7952f7 | 77 | #define GAME_OVER 1 |
rconnorlawson | 0:35660d7952f7 | 78 | #define FULL_DRAW 2 |
rconnorlawson | 0:35660d7952f7 | 79 | int update_game(int action) |
rconnorlawson | 0:35660d7952f7 | 80 | { |
rconnorlawson | 0:35660d7952f7 | 81 | // Save player previous location before updating |
rconnorlawson | 0:35660d7952f7 | 82 | Player.px = Player.x; |
rconnorlawson | 0:35660d7952f7 | 83 | Player.py = Player.y; |
rconnorlawson | 0:35660d7952f7 | 84 | |
rconnorlawson | 0:35660d7952f7 | 85 | // Do different things based on the each action. |
rconnorlawson | 0:35660d7952f7 | 86 | // You can define functions like "go_up()" that get called for each case. |
trmontgomery | 2:0876296d9473 | 87 | switch(action) //have a function called for each type of object. Pass a pointer to the object to the interact function |
rconnorlawson | 0:35660d7952f7 | 88 | { |
trmontgomery | 2:0876296d9473 | 89 | case GO_UP: |
trmontgomery | 2:0876296d9473 | 90 | if (MapItem* object = get_north(Player.x, Player.y)){ |
trmontgomery | 2:0876296d9473 | 91 | if (object -> type == NPC){ |
trmontgomery | 2:0876296d9473 | 92 | NonPlayer* n = (NonPlayer*)object -> data; |
trmontgomery | 2:0876296d9473 | 93 | if(n -> quest_requested == false){ |
trmontgomery | 2:0876296d9473 | 94 | speech("Get dat money", "its dat way"); |
trmontgomery | 2:0876296d9473 | 95 | n -> quest_requested = true; |
trmontgomery | 2:0876296d9473 | 96 | } else if (Player.quest_complete == false){ //should quest complete be in the player struct? |
trmontgomery | 2:0876296d9473 | 97 | speech("What are u waitin for?", "get it boi"); |
trmontgomery | 2:0876296d9473 | 98 | } else if (n -> has_key == true) { |
trmontgomery | 2:0876296d9473 | 99 | speech("Congrats!", "Here's the keyyyy"); |
trmontgomery | 2:0876296d9473 | 100 | n -> has_key = false; |
trmontgomery | 2:0876296d9473 | 101 | Player.has_key = true; |
trmontgomery | 2:0876296d9473 | 102 | } else if(n -> has_key == false){ |
trmontgomery | 2:0876296d9473 | 103 | speech("Congrats!", "Quest complete."); |
trmontgomery | 2:0876296d9473 | 104 | } |
trmontgomery | 2:0876296d9473 | 105 | // else if (object -> type == DOOR){ |
trmontgomery | 2:0876296d9473 | 106 | // bool o = object -> data; |
trmontgomery | 2:0876296d9473 | 107 | // if (Player.has_key == true){ |
trmontgomery | 2:0876296d9473 | 108 | // o = true; |
trmontgomery | 2:0876296d9473 | 109 | // } |
trmontgomery | 2:0876296d9473 | 110 | //} |
trmontgomery | 2:0876296d9473 | 111 | |
trmontgomery | 2:0876296d9473 | 112 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 113 | } |
trmontgomery | 2:0876296d9473 | 114 | else if (object -> walkable == false){ |
trmontgomery | 2:0876296d9473 | 115 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 116 | } |
trmontgomery | 2:0876296d9473 | 117 | } |
trmontgomery | 2:0876296d9473 | 118 | Player.y = Player.y - 1; |
trmontgomery | 2:0876296d9473 | 119 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 120 | case GO_LEFT: |
trmontgomery | 2:0876296d9473 | 121 | if (MapItem* object = get_west(Player.x, Player.y)){ |
trmontgomery | 2:0876296d9473 | 122 | if (object -> type == ROCK){ |
trmontgomery | 2:0876296d9473 | 123 | add_rock(Player.x + 2, Player.y); |
trmontgomery | 2:0876296d9473 | 124 | map_erase(Player.x+1, Player.y); |
trmontgomery | 2:0876296d9473 | 125 | Player.x = Player.x + 1; |
trmontgomery | 2:0876296d9473 | 126 | Player.quest_complete = true; |
trmontgomery | 2:0876296d9473 | 127 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 128 | |
trmontgomery | 2:0876296d9473 | 129 | } else if(object -> type == SPIKE){ |
trmontgomery | 2:0876296d9473 | 130 | //lower health bar |
trmontgomery | 2:0876296d9473 | 131 | health++; |
trmontgomery | 2:0876296d9473 | 132 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 133 | |
trmontgomery | 2:0876296d9473 | 134 | } |
trmontgomery | 2:0876296d9473 | 135 | if (object -> walkable == false){ |
trmontgomery | 2:0876296d9473 | 136 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 137 | } |
trmontgomery | 2:0876296d9473 | 138 | } |
trmontgomery | 2:0876296d9473 | 139 | Player.x = Player.x + 1; |
trmontgomery | 2:0876296d9473 | 140 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 141 | case GO_DOWN: |
trmontgomery | 2:0876296d9473 | 142 | if (MapItem* object = get_south(Player.x, Player.y)){ |
trmontgomery | 2:0876296d9473 | 143 | if (object -> type == DOOR){ |
trmontgomery | 2:0876296d9473 | 144 | if (Player.has_key == true){ |
trmontgomery | 2:0876296d9473 | 145 | map_erase(Player.x, Player.y + 1); |
trmontgomery | 2:0876296d9473 | 146 | Player.y = Player.y + 1; |
trmontgomery | 2:0876296d9473 | 147 | door_open = true; |
trmontgomery | 2:0876296d9473 | 148 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 149 | } |
trmontgomery | 2:0876296d9473 | 150 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 151 | } |
trmontgomery | 2:0876296d9473 | 152 | if (object -> walkable == false){ |
trmontgomery | 2:0876296d9473 | 153 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 154 | } |
trmontgomery | 2:0876296d9473 | 155 | } |
trmontgomery | 2:0876296d9473 | 156 | Player.y = Player.y + 1; |
trmontgomery | 2:0876296d9473 | 157 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 158 | |
trmontgomery | 2:0876296d9473 | 159 | case GO_RIGHT: |
trmontgomery | 2:0876296d9473 | 160 | if (MapItem* object = get_east(Player.x, Player.y)){ |
trmontgomery | 2:0876296d9473 | 161 | if (object -> type == GOAL){ |
trmontgomery | 2:0876296d9473 | 162 | if(door_open == true){ |
trmontgomery | 2:0876296d9473 | 163 | game_over = true; |
trmontgomery | 2:0876296d9473 | 164 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 165 | } |
trmontgomery | 2:0876296d9473 | 166 | |
trmontgomery | 2:0876296d9473 | 167 | } |
trmontgomery | 2:0876296d9473 | 168 | if (object -> walkable == false){ |
trmontgomery | 2:0876296d9473 | 169 | return NO_RESULT; |
trmontgomery | 2:0876296d9473 | 170 | } |
trmontgomery | 2:0876296d9473 | 171 | } |
trmontgomery | 2:0876296d9473 | 172 | Player.x = Player.x - 1; |
trmontgomery | 2:0876296d9473 | 173 | return FULL_DRAW; |
trmontgomery | 2:0876296d9473 | 174 | |
trmontgomery | 2:0876296d9473 | 175 | case BUTTON1: |
trmontgomery | 2:0876296d9473 | 176 | omni(); |
trmontgomery | 2:0876296d9473 | 177 | break; |
trmontgomery | 2:0876296d9473 | 178 | case BUTTON2: |
trmontgomery | 2:0876296d9473 | 179 | break; |
trmontgomery | 2:0876296d9473 | 180 | case MENU_BUTTON: |
trmontgomery | 2:0876296d9473 | 181 | |
trmontgomery | 2:0876296d9473 | 182 | break; |
rconnorlawson | 0:35660d7952f7 | 183 | default: break; |
rconnorlawson | 0:35660d7952f7 | 184 | } |
rconnorlawson | 0:35660d7952f7 | 185 | return NO_RESULT; |
rconnorlawson | 0:35660d7952f7 | 186 | } |
rconnorlawson | 0:35660d7952f7 | 187 | |
rconnorlawson | 0:35660d7952f7 | 188 | /** |
rconnorlawson | 0:35660d7952f7 | 189 | * Entry point for frame drawing. This should be called once per iteration of |
rconnorlawson | 0:35660d7952f7 | 190 | * the game loop. This draws all tiles on the screen, followed by the status |
rconnorlawson | 0:35660d7952f7 | 191 | * bars. Unless init is nonzero, this function will optimize drawing by only |
rconnorlawson | 0:35660d7952f7 | 192 | * drawing tiles that have changed from the previous frame. |
rconnorlawson | 0:35660d7952f7 | 193 | */ |
rconnorlawson | 0:35660d7952f7 | 194 | void draw_game(int init) |
rconnorlawson | 0:35660d7952f7 | 195 | { |
trmontgomery | 2:0876296d9473 | 196 | // int param1; |
trmontgomery | 2:0876296d9473 | 197 | //int param2; |
rconnorlawson | 0:35660d7952f7 | 198 | // Draw game border first |
rconnorlawson | 0:35660d7952f7 | 199 | if(init) draw_border(); |
rconnorlawson | 0:35660d7952f7 | 200 | |
rconnorlawson | 0:35660d7952f7 | 201 | // Iterate over all visible map tiles |
rconnorlawson | 0:35660d7952f7 | 202 | for (int i = -5; i <= 5; i++) // Iterate over columns of tiles |
rconnorlawson | 0:35660d7952f7 | 203 | { |
rconnorlawson | 0:35660d7952f7 | 204 | for (int j = -4; j <= 4; j++) // Iterate over one column of tiles |
rconnorlawson | 0:35660d7952f7 | 205 | { |
rconnorlawson | 0:35660d7952f7 | 206 | // Here, we have a given (i,j) |
rconnorlawson | 0:35660d7952f7 | 207 | |
rconnorlawson | 0:35660d7952f7 | 208 | // Compute the current map (x,y) of this tile |
rconnorlawson | 0:35660d7952f7 | 209 | int x = i + Player.x; |
rconnorlawson | 0:35660d7952f7 | 210 | int y = j + Player.y; |
rconnorlawson | 0:35660d7952f7 | 211 | |
rconnorlawson | 0:35660d7952f7 | 212 | // Compute the previous map (px, py) of this tile |
rconnorlawson | 0:35660d7952f7 | 213 | int px = i + Player.px; |
rconnorlawson | 0:35660d7952f7 | 214 | int py = j + Player.py; |
rconnorlawson | 0:35660d7952f7 | 215 | |
rconnorlawson | 0:35660d7952f7 | 216 | // Compute u,v coordinates for drawing |
rconnorlawson | 0:35660d7952f7 | 217 | int u = (i+5)*11 + 3; |
rconnorlawson | 0:35660d7952f7 | 218 | int v = (j+4)*11 + 15; |
rconnorlawson | 0:35660d7952f7 | 219 | |
rconnorlawson | 0:35660d7952f7 | 220 | // Figure out what to draw |
rconnorlawson | 0:35660d7952f7 | 221 | DrawFunc draw = NULL; |
trmontgomery | 2:0876296d9473 | 222 | if (init && i == 0 && j == 0) // Only draw the player on init (this is when the loop hits the center of the screen) |
rconnorlawson | 0:35660d7952f7 | 223 | { |
rconnorlawson | 0:35660d7952f7 | 224 | draw_player(u, v, Player.has_key); |
trmontgomery | 2:0876296d9473 | 225 | |
rconnorlawson | 0:35660d7952f7 | 226 | continue; |
rconnorlawson | 0:35660d7952f7 | 227 | } |
rconnorlawson | 0:35660d7952f7 | 228 | else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map |
rconnorlawson | 0:35660d7952f7 | 229 | { |
rconnorlawson | 0:35660d7952f7 | 230 | MapItem* curr_item = get_here(x, y); |
rconnorlawson | 0:35660d7952f7 | 231 | MapItem* prev_item = get_here(px, py); |
rconnorlawson | 0:35660d7952f7 | 232 | if (init || curr_item != prev_item) // Only draw if they're different |
rconnorlawson | 0:35660d7952f7 | 233 | { |
rconnorlawson | 0:35660d7952f7 | 234 | if (curr_item) // There's something here! Draw it |
rconnorlawson | 0:35660d7952f7 | 235 | { |
trmontgomery | 2:0876296d9473 | 236 | //if (curr_item -> type == ROCK){ |
trmontgomery | 2:0876296d9473 | 237 | //set parameters |
trmontgomery | 2:0876296d9473 | 238 | // Rock* r = (Rock*) curr_item -> data; |
trmontgomery | 2:0876296d9473 | 239 | //param1 = r -> x; |
trmontgomery | 2:0876296d9473 | 240 | //param2 = r -> y; |
trmontgomery | 2:0876296d9473 | 241 | //} |
rconnorlawson | 0:35660d7952f7 | 242 | draw = curr_item->draw; |
rconnorlawson | 0:35660d7952f7 | 243 | } |
rconnorlawson | 0:35660d7952f7 | 244 | else // There used to be something, but now there isn't |
rconnorlawson | 0:35660d7952f7 | 245 | { |
rconnorlawson | 0:35660d7952f7 | 246 | draw = draw_nothing; |
rconnorlawson | 0:35660d7952f7 | 247 | } |
rconnorlawson | 0:35660d7952f7 | 248 | } |
rconnorlawson | 0:35660d7952f7 | 249 | } |
rconnorlawson | 0:35660d7952f7 | 250 | else if (init) // If doing a full draw, but we're out of bounds, draw the walls. |
rconnorlawson | 0:35660d7952f7 | 251 | { |
rconnorlawson | 0:35660d7952f7 | 252 | draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 253 | } |
rconnorlawson | 0:35660d7952f7 | 254 | |
rconnorlawson | 0:35660d7952f7 | 255 | // Actually draw the tile |
trmontgomery | 2:0876296d9473 | 256 | //MapItem* curr_item = get_here(x, y); |
trmontgomery | 2:0876296d9473 | 257 | //MapItem* prev_item = get_here(px, py); |
trmontgomery | 2:0876296d9473 | 258 | if (draw){ |
trmontgomery | 2:0876296d9473 | 259 | // if(curr_item){ |
trmontgomery | 2:0876296d9473 | 260 | // if(curr_item -> type == ROCK){ |
trmontgomery | 2:0876296d9473 | 261 | // draw(param1, param2); |
trmontgomery | 2:0876296d9473 | 262 | //} |
trmontgomery | 2:0876296d9473 | 263 | //} |
trmontgomery | 2:0876296d9473 | 264 | draw(u,v); |
trmontgomery | 2:0876296d9473 | 265 | } |
rconnorlawson | 0:35660d7952f7 | 266 | } |
rconnorlawson | 0:35660d7952f7 | 267 | } |
rconnorlawson | 0:35660d7952f7 | 268 | |
rconnorlawson | 0:35660d7952f7 | 269 | // Draw status bars |
trmontgomery | 2:0876296d9473 | 270 | draw_upper_status(health); |
trmontgomery | 2:0876296d9473 | 271 | draw_lower_status(Player.x, Player.y); |
rconnorlawson | 0:35660d7952f7 | 272 | } |
rconnorlawson | 0:35660d7952f7 | 273 | |
rconnorlawson | 0:35660d7952f7 | 274 | |
rconnorlawson | 0:35660d7952f7 | 275 | /** |
rconnorlawson | 0:35660d7952f7 | 276 | * Initialize the main world map. Add walls around the edges, interior chambers, |
rconnorlawson | 0:35660d7952f7 | 277 | * and plants in the background so you can see motion. |
rconnorlawson | 0:35660d7952f7 | 278 | */ |
rconnorlawson | 0:35660d7952f7 | 279 | void init_main_map() |
rconnorlawson | 0:35660d7952f7 | 280 | { |
rconnorlawson | 0:35660d7952f7 | 281 | // "Random" plants |
rconnorlawson | 0:35660d7952f7 | 282 | Map* map = set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 283 | for(int i = map_width() + 3; i < map_area(); i += 39) |
rconnorlawson | 0:35660d7952f7 | 284 | { |
rconnorlawson | 0:35660d7952f7 | 285 | add_plant(i % map_width(), i / map_width()); |
rconnorlawson | 0:35660d7952f7 | 286 | } |
trmontgomery | 2:0876296d9473 | 287 | |
trmontgomery | 2:0876296d9473 | 288 | srand(time(NULL)); |
trmontgomery | 2:0876296d9473 | 289 | for(int i = 0; i < 30; i++) |
trmontgomery | 2:0876296d9473 | 290 | { |
trmontgomery | 2:0876296d9473 | 291 | int x = rand()%((map_width()+1)-1) + 1; |
trmontgomery | 2:0876296d9473 | 292 | int y = rand()%((map_height()+1)-1) + 1; |
trmontgomery | 2:0876296d9473 | 293 | add_spike(x, y); |
trmontgomery | 2:0876296d9473 | 294 | } |
trmontgomery | 2:0876296d9473 | 295 | |
rconnorlawson | 0:35660d7952f7 | 296 | pc.printf("plants\r\n"); |
rconnorlawson | 0:35660d7952f7 | 297 | |
rconnorlawson | 0:35660d7952f7 | 298 | pc.printf("Adding walls!\r\n"); |
rconnorlawson | 0:35660d7952f7 | 299 | add_wall(0, 0, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 300 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 301 | add_wall(0, 0, VERTICAL, map_height()); |
rconnorlawson | 0:35660d7952f7 | 302 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
trmontgomery | 2:0876296d9473 | 303 | add_wall(20, 20, HORIZONTAL, 15); |
trmontgomery | 2:0876296d9473 | 304 | add_npc(13, 13); |
trmontgomery | 2:0876296d9473 | 305 | add_rock(6, 42); |
trmontgomery | 2:0876296d9473 | 306 | add_door(7, 48); |
trmontgomery | 2:0876296d9473 | 307 | add_goal(43, 5); |
rconnorlawson | 0:35660d7952f7 | 308 | pc.printf("Walls done!\r\n"); |
rconnorlawson | 0:35660d7952f7 | 309 | |
rconnorlawson | 0:35660d7952f7 | 310 | print_map(); |
rconnorlawson | 0:35660d7952f7 | 311 | } |
rconnorlawson | 0:35660d7952f7 | 312 | |
rconnorlawson | 0:35660d7952f7 | 313 | /** |
rconnorlawson | 0:35660d7952f7 | 314 | * Program entry point! This is where it all begins. |
rconnorlawson | 0:35660d7952f7 | 315 | * This function orchestrates all the parts of the game. Most of your |
rconnorlawson | 0:35660d7952f7 | 316 | * implementation should be elsewhere - this holds the game loop, and should |
rconnorlawson | 0:35660d7952f7 | 317 | * read like a road map for the rest of the code. |
rconnorlawson | 0:35660d7952f7 | 318 | */ |
rconnorlawson | 0:35660d7952f7 | 319 | int main() |
rconnorlawson | 0:35660d7952f7 | 320 | { |
trmontgomery | 2:0876296d9473 | 321 | int start = menu(); |
rconnorlawson | 0:35660d7952f7 | 322 | // First things first: initialize hardware |
rconnorlawson | 0:35660d7952f7 | 323 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
rconnorlawson | 0:35660d7952f7 | 324 | |
rconnorlawson | 0:35660d7952f7 | 325 | // Initialize the maps |
rconnorlawson | 0:35660d7952f7 | 326 | maps_init(); |
rconnorlawson | 0:35660d7952f7 | 327 | init_main_map(); |
rconnorlawson | 0:35660d7952f7 | 328 | |
rconnorlawson | 0:35660d7952f7 | 329 | // Initialize game state |
rconnorlawson | 0:35660d7952f7 | 330 | set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 331 | Player.x = Player.y = 5; |
trmontgomery | 2:0876296d9473 | 332 | Player.quest_complete = false; |
rconnorlawson | 0:35660d7952f7 | 333 | |
rconnorlawson | 0:35660d7952f7 | 334 | // Initial drawing |
rconnorlawson | 0:35660d7952f7 | 335 | draw_game(true); |
rconnorlawson | 0:35660d7952f7 | 336 | |
rconnorlawson | 0:35660d7952f7 | 337 | // Main game loop |
rconnorlawson | 0:35660d7952f7 | 338 | while(1) |
rconnorlawson | 0:35660d7952f7 | 339 | { |
rconnorlawson | 0:35660d7952f7 | 340 | // Timer to measure game update speed |
rconnorlawson | 0:35660d7952f7 | 341 | Timer t; t.start(); |
rconnorlawson | 0:35660d7952f7 | 342 | |
trmontgomery | 2:0876296d9473 | 343 | // Actually do the game update: |
trmontgomery | 2:0876296d9473 | 344 | // 1. Read inputs |
trmontgomery | 2:0876296d9473 | 345 | GameInputs in = read_inputs(); |
trmontgomery | 2:0876296d9473 | 346 | // 2. Determine action (get_action) |
trmontgomery | 2:0876296d9473 | 347 | int a = get_action(in); |
rconnorlawson | 0:35660d7952f7 | 348 | // 3. Update game (update_game) |
trmontgomery | 2:0876296d9473 | 349 | int u = update_game(a); |
rconnorlawson | 0:35660d7952f7 | 350 | // 3b. Check for game over |
trmontgomery | 2:0876296d9473 | 351 | if (game_over == true){ |
trmontgomery | 2:0876296d9473 | 352 | draw_game_over(); |
trmontgomery | 2:0876296d9473 | 353 | }else{ |
rconnorlawson | 0:35660d7952f7 | 354 | // 4. Draw frame (draw_game) |
trmontgomery | 2:0876296d9473 | 355 | draw_game(u); |
trmontgomery | 2:0876296d9473 | 356 | } |
rconnorlawson | 0:35660d7952f7 | 357 | |
rconnorlawson | 0:35660d7952f7 | 358 | // 5. Frame delay |
rconnorlawson | 0:35660d7952f7 | 359 | t.stop(); |
rconnorlawson | 0:35660d7952f7 | 360 | int dt = t.read_ms(); |
rconnorlawson | 0:35660d7952f7 | 361 | if (dt < 100) wait_ms(100 - dt); |
rconnorlawson | 0:35660d7952f7 | 362 | } |
rconnorlawson | 0:35660d7952f7 | 363 | } |