tao lao

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
hnguyen403
Date:
Tue Nov 24 05:05:10 2020 +0000
Revision:
3:33bf11645fe1
Parent:
2:4947d6a82971
tao lao;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DCchico 2:4947d6a82971 1 //=================================================================
DCchico 2:4947d6a82971 2 // The main program file.
DCchico 2:4947d6a82971 3 //
DCchico 2:4947d6a82971 4 // Copyright 2020 Georgia Tech. All rights reserved.
DCchico 2:4947d6a82971 5 // The materials provided by the instructor in this course are for
DCchico 2:4947d6a82971 6 // the use of the students currently enrolled in the course.
DCchico 2:4947d6a82971 7 // Copyrighted course materials may not be further disseminated.
DCchico 2:4947d6a82971 8 // This file must not be made publicly available anywhere.
DCchico 2:4947d6a82971 9 //==================================================================
DCchico 2:4947d6a82971 10
DCchico 2:4947d6a82971 11 // Project includes
DCchico 1:10330bce85cb 12 #include "globals.h"
DCchico 1:10330bce85cb 13 #include "hardware.h"
DCchico 1:10330bce85cb 14 #include "map.h"
DCchico 1:10330bce85cb 15 #include "graphics.h"
DCchico 1:10330bce85cb 16 #include "snake.h"
hnguyen403 3:33bf11645fe1 17 #include "mbed.h"
hnguyen403 3:33bf11645fe1 18 //#include "wave_player.h"
hnguyen403 3:33bf11645fe1 19 #include "SDFileSystem.h"
DCchico 1:10330bce85cb 20 #include <math.h>
DCchico 1:10330bce85cb 21 #include<stdio.h>
DCchico 1:10330bce85cb 22
DCchico 1:10330bce85cb 23 #define CITY_HIT_MARGIN 1
DCchico 1:10330bce85cb 24 #define CITY_UPPER_BOUND (SIZE_Y-(LANDSCAPE_HEIGHT+MAX_BUILDING_HEIGHT))
hnguyen403 3:33bf11645fe1 25 int go_right(int x, int y);
hnguyen403 3:33bf11645fe1 26 int go_left(int x, int y);
hnguyen403 3:33bf11645fe1 27 int go_up(int x, int y);
hnguyen403 3:33bf11645fe1 28 int go_down(int x, int y);
DCchico 1:10330bce85cb 29 // Helper function declarations
DCchico 1:10330bce85cb 30 void playSound(char* wav);
DCchico 1:10330bce85cb 31
DCchico 1:10330bce85cb 32 /**
DCchico 1:10330bce85cb 33 * The main game state. Must include snake locations and previous locations for
DCchico 1:10330bce85cb 34 * drawing to work properly. Other items can be added as needed.
DCchico 1:10330bce85cb 35 */
DCchico 1:10330bce85cb 36
DCchico 1:10330bce85cb 37 /**
DCchico 1:10330bce85cb 38 * Given the game inputs, determine what kind of update needs to happen.
DCchico 1:10330bce85cb 39 * Possbile return values are defined below.
DCchico 1:10330bce85cb 40 */
DCchico 1:10330bce85cb 41 Snake snake;
DCchico 1:10330bce85cb 42
DCchico 1:10330bce85cb 43 // Function prototypes
DCchico 2:4947d6a82971 44
DCchico 2:4947d6a82971 45 /**
DCchico 2:4947d6a82971 46 * Given the game inputs, determine what kind of update needs to happen.
DCchico 2:4947d6a82971 47 * Possible return values are defined below.
DCchico 2:4947d6a82971 48 */
DCchico 2:4947d6a82971 49 #define NO_RESULT 0
DCchico 2:4947d6a82971 50 #define NO_ACTION 0
DCchico 2:4947d6a82971 51 #define ACTION_BUTTON 1
DCchico 2:4947d6a82971 52 #define MENU_BUTTON 2
DCchico 2:4947d6a82971 53 #define GO_LEFT 3
DCchico 2:4947d6a82971 54 #define GO_RIGHT 4
DCchico 2:4947d6a82971 55 #define GO_UP 5
DCchico 2:4947d6a82971 56 #define GO_DOWN 6
DCchico 2:4947d6a82971 57 #define GAME_OVER 7
DCchico 2:4947d6a82971 58 #define FULL_DRAW 8
hnguyen403 3:33bf11645fe1 59 #define WON 9
hnguyen403 3:33bf11645fe1 60 #define FOODN 10
hnguyen403 3:33bf11645fe1 61 #define FOODS 11
hnguyen403 3:33bf11645fe1 62 #define FOODW 12
hnguyen403 3:33bf11645fe1 63 #define FOODE 13
hnguyen403 3:33bf11645fe1 64
DCchico 1:10330bce85cb 65 // Get Actions from User (push buttons & accelerometer)
DCchico 2:4947d6a82971 66 // Based on push button and accelerometer inputs, determine which action
DCchico 2:4947d6a82971 67 // needs to be performed (may be no action).
DCchico 1:10330bce85cb 68 int get_action(GameInputs inputs)
DCchico 1:10330bce85cb 69 {
hnguyen403 3:33bf11645fe1 70 MapItem* N=get_north(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 71 MapItem* S=get_south(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 72 MapItem* W=get_east(snake.head_x, snake.head_y);
hnguyen403 3:33bf11645fe1 73 MapItem* E=get_west(snake.head_x, snake.head_y);
hnguyen403 3:33bf11645fe1 74
hnguyen403 3:33bf11645fe1 75 if (E->type==WALL || N->type==WALL || S->type==WALL ||W->type==WALL) return GAME_OVER;
hnguyen403 3:33bf11645fe1 76 if (inputs.ay >= 0.20) return GO_UP;
hnguyen403 3:33bf11645fe1 77 if (inputs.ay < -0.20) return GO_DOWN;
hnguyen403 3:33bf11645fe1 78 if (inputs.ax < -0.20) return GO_LEFT;
hnguyen403 3:33bf11645fe1 79 if (inputs.ax >= 0.20) return GO_RIGHT;
hnguyen403 3:33bf11645fe1 80 else return NO_ACTION;
DCchico 1:10330bce85cb 81 }
DCchico 1:10330bce85cb 82 /**
DCchico 1:10330bce85cb 83 * Update the game state based on the user action. For example, if the user
DCchico 1:10330bce85cb 84 * requests GO_UP, then this function should determine if that is possible by
DCchico 1:10330bce85cb 85 * consulting the map, and update the snake position accordingly.
DCchico 1:10330bce85cb 86 *
DCchico 1:10330bce85cb 87 * Return values are defined below. FULL_DRAW indicates that for this frame,
DCchico 1:10330bce85cb 88 * draw_game should not optimize drawing and should draw every tile, even if
DCchico 1:10330bce85cb 89 * the snake has not moved.
DCchico 1:10330bce85cb 90 */
DCchico 1:10330bce85cb 91 int update_game(int action)
DCchico 1:10330bce85cb 92 {
hnguyen403 3:33bf11645fe1 93
hnguyen403 3:33bf11645fe1 94 snake.head_px = snake.head_x;
hnguyen403 3:33bf11645fe1 95 snake.head_py = snake.head_y;
hnguyen403 3:33bf11645fe1 96
hnguyen403 3:33bf11645fe1 97 switch(action) {
hnguyen403 3:33bf11645fe1 98 case GO_UP:
hnguyen403 3:33bf11645fe1 99 snake.head_y -= 1;
hnguyen403 3:33bf11645fe1 100
hnguyen403 3:33bf11645fe1 101 for (int i = 1; i < snake.length-1; i++){
hnguyen403 3:33bf11645fe1 102 int oldx = snake.head_px;
hnguyen403 3:33bf11645fe1 103 int oldy = snake.head_py;
hnguyen403 3:33bf11645fe1 104 int temx = snake.locations[i].x;
hnguyen403 3:33bf11645fe1 105 int temy = snake.locations[i].y;
hnguyen403 3:33bf11645fe1 106 snake.locations[i].x = oldx;
hnguyen403 3:33bf11645fe1 107 snake.locations[i].y = oldy;
hnguyen403 3:33bf11645fe1 108 oldx = temx;
hnguyen403 3:33bf11645fe1 109 oldy = temy;}
hnguyen403 3:33bf11645fe1 110 if (get_north(snake.head_x,snake.head_y)->type == GOODIE)
hnguyen403 3:33bf11645fe1 111 {map_erase(snake.head_x,snake.head_y-1);
hnguyen403 3:33bf11645fe1 112 return FULL_DRAW;}
hnguyen403 3:33bf11645fe1 113 break;
hnguyen403 3:33bf11645fe1 114 case GO_LEFT:
hnguyen403 3:33bf11645fe1 115 snake.head_x -= 1;
hnguyen403 3:33bf11645fe1 116 for (int i = 1; i < snake.length-1; i++){
hnguyen403 3:33bf11645fe1 117 int oldx = snake.head_px;
hnguyen403 3:33bf11645fe1 118 int oldy = snake.head_py;
hnguyen403 3:33bf11645fe1 119 int temx = snake.locations[i].x;
hnguyen403 3:33bf11645fe1 120 int temy = snake.locations[i].y;
hnguyen403 3:33bf11645fe1 121 snake.locations[i].x = oldx;
hnguyen403 3:33bf11645fe1 122 snake.locations[i].y = oldy;
hnguyen403 3:33bf11645fe1 123 oldx = temx;
hnguyen403 3:33bf11645fe1 124 oldy = temy;}
hnguyen403 3:33bf11645fe1 125 if (get_west(snake.head_x-1,snake.head_y)->type == GOODIE)
hnguyen403 3:33bf11645fe1 126 {map_erase(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 127 return FULL_DRAW;}
hnguyen403 3:33bf11645fe1 128 break;
hnguyen403 3:33bf11645fe1 129
hnguyen403 3:33bf11645fe1 130
hnguyen403 3:33bf11645fe1 131 case GO_DOWN:
hnguyen403 3:33bf11645fe1 132 snake.head_y += 1;
hnguyen403 3:33bf11645fe1 133
hnguyen403 3:33bf11645fe1 134 for (int i = 1; i < snake.length-1; i++){
hnguyen403 3:33bf11645fe1 135 int oldx = snake.head_px;
hnguyen403 3:33bf11645fe1 136 int oldy = snake.head_py;
hnguyen403 3:33bf11645fe1 137 int temx = snake.locations[i].x;
hnguyen403 3:33bf11645fe1 138 int temy = snake.locations[i].y;
hnguyen403 3:33bf11645fe1 139 snake.locations[i].x = oldx;
hnguyen403 3:33bf11645fe1 140 snake.locations[i].y = oldy;
hnguyen403 3:33bf11645fe1 141 oldx = temx;
hnguyen403 3:33bf11645fe1 142 oldy = temy;}
hnguyen403 3:33bf11645fe1 143 if (get_south(snake.head_x,snake.head_y+1)->type == GOODIE)
hnguyen403 3:33bf11645fe1 144 {map_erase(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 145 return FULL_DRAW;}
hnguyen403 3:33bf11645fe1 146 break;
hnguyen403 3:33bf11645fe1 147 case GO_RIGHT:
hnguyen403 3:33bf11645fe1 148 snake.head_x += 1;
hnguyen403 3:33bf11645fe1 149
hnguyen403 3:33bf11645fe1 150 for (int i = 1; i < snake.length-1; i++){
hnguyen403 3:33bf11645fe1 151 int oldx = snake.head_px;
hnguyen403 3:33bf11645fe1 152 int oldy = snake.head_py;
hnguyen403 3:33bf11645fe1 153 int temx = snake.locations[i].x;
hnguyen403 3:33bf11645fe1 154 int temy = snake.locations[i].y;
hnguyen403 3:33bf11645fe1 155 snake.locations[i].x = oldx;
hnguyen403 3:33bf11645fe1 156 snake.locations[i].y = oldy;
hnguyen403 3:33bf11645fe1 157 oldx = temx;
hnguyen403 3:33bf11645fe1 158 oldy = temy;}
hnguyen403 3:33bf11645fe1 159 if (get_here(snake.head_x,snake.head_y)->type == GOODIE)
hnguyen403 3:33bf11645fe1 160 {add_nothing(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 161 draw_snake_head(snake.head_x,snake.head_y);
hnguyen403 3:33bf11645fe1 162 snake.score= snake.score + 1;}
hnguyen403 3:33bf11645fe1 163
hnguyen403 3:33bf11645fe1 164 break;
hnguyen403 3:33bf11645fe1 165
hnguyen403 3:33bf11645fe1 166 case GAME_OVER:
hnguyen403 3:33bf11645fe1 167
hnguyen403 3:33bf11645fe1 168 {uLCD.color(RED);
hnguyen403 3:33bf11645fe1 169 uLCD.cls();
hnguyen403 3:33bf11645fe1 170 uLCD.text_width(3);
hnguyen403 3:33bf11645fe1 171 uLCD.text_height(3);
hnguyen403 3:33bf11645fe1 172 uLCD.printf("GAME\nOVER");
hnguyen403 3:33bf11645fe1 173 uLCD.color(GREEN);
hnguyen403 3:33bf11645fe1 174 uLCD.text_width(1);
hnguyen403 3:33bf11645fe1 175 uLCD.text_height(1);
hnguyen403 3:33bf11645fe1 176 uLCD.printf("\n\n\n\n\nScore %d", snake.score);
hnguyen403 3:33bf11645fe1 177 uLCD.color(GREEN);}
hnguyen403 3:33bf11645fe1 178 while(1 == 1);
hnguyen403 3:33bf11645fe1 179
hnguyen403 3:33bf11645fe1 180 }
hnguyen403 3:33bf11645fe1 181 return NO_RESULT;
hnguyen403 3:33bf11645fe1 182
DCchico 1:10330bce85cb 183 }
DCchico 1:10330bce85cb 184
DCchico 1:10330bce85cb 185 /**
DCchico 1:10330bce85cb 186 * Draw the upper status bar.
DCchico 1:10330bce85cb 187 */
DCchico 1:10330bce85cb 188 void draw_upper_status()
DCchico 1:10330bce85cb 189 {
DCchico 1:10330bce85cb 190 uLCD.line(0, 9, 127, 9, GREEN);
hnguyen403 3:33bf11645fe1 191
DCchico 1:10330bce85cb 192 }
DCchico 1:10330bce85cb 193
DCchico 1:10330bce85cb 194 /**
DCchico 1:10330bce85cb 195 * Draw the lower status bar.
DCchico 1:10330bce85cb 196 */
DCchico 1:10330bce85cb 197 void draw_lower_status()
DCchico 1:10330bce85cb 198 {
DCchico 1:10330bce85cb 199 uLCD.line(0, 118, 127, 118, GREEN);
DCchico 1:10330bce85cb 200 }
DCchico 1:10330bce85cb 201
DCchico 1:10330bce85cb 202 /**
DCchico 1:10330bce85cb 203 * Draw the border for the map.
DCchico 1:10330bce85cb 204 */
DCchico 1:10330bce85cb 205 void draw_border()
DCchico 1:10330bce85cb 206 {
DCchico 1:10330bce85cb 207 uLCD.filled_rectangle(0, 9, 127, 14, WHITE); // Top
DCchico 1:10330bce85cb 208 uLCD.filled_rectangle(0, 13, 2, 114, WHITE); // Left
DCchico 1:10330bce85cb 209 uLCD.filled_rectangle(0, 114, 127, 117, WHITE); // Bottom
DCchico 1:10330bce85cb 210 uLCD.filled_rectangle(124, 14, 127, 117, WHITE); // Right
DCchico 1:10330bce85cb 211 }
DCchico 1:10330bce85cb 212
DCchico 1:10330bce85cb 213 /**
DCchico 1:10330bce85cb 214 * Entry point for frame drawing. This should be called once per iteration of
DCchico 1:10330bce85cb 215 * the game loop. This draws all tiles on the screen, followed by the status
DCchico 1:10330bce85cb 216 * bars. Unless init is nonzero, this function will optimize drawing by only
DCchico 1:10330bce85cb 217 * drawing tiles that have changed from the previous frame.
DCchico 1:10330bce85cb 218 */
DCchico 1:10330bce85cb 219 void draw_game(int draw_option)
DCchico 1:10330bce85cb 220 {
DCchico 1:10330bce85cb 221 // Draw game border first
DCchico 1:10330bce85cb 222 if(draw_option == FULL_DRAW)
DCchico 1:10330bce85cb 223 {
DCchico 1:10330bce85cb 224 draw_border();
DCchico 1:10330bce85cb 225 int u = 58;
hnguyen403 3:33bf11645fe1 226 int v = 59;
DCchico 1:10330bce85cb 227 draw_snake_head(u, v);
DCchico 1:10330bce85cb 228 draw_snake_body(u-11, v);
DCchico 1:10330bce85cb 229 draw_snake_tail(u-22, v);
DCchico 1:10330bce85cb 230 return;
DCchico 1:10330bce85cb 231 }
DCchico 1:10330bce85cb 232 // Iterate over all visible map tiles
DCchico 1:10330bce85cb 233 for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
DCchico 1:10330bce85cb 234 for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
DCchico 1:10330bce85cb 235 // Here, we have a given (i,j)
DCchico 1:10330bce85cb 236
DCchico 1:10330bce85cb 237 // Compute the current map (x,y) of this tile
DCchico 1:10330bce85cb 238 int x = i + snake.head_x;
DCchico 1:10330bce85cb 239 int y = j + snake.head_y;
DCchico 1:10330bce85cb 240
DCchico 1:10330bce85cb 241 // Compute the previous map (px, py) of this tile
DCchico 1:10330bce85cb 242 int px = i + snake.head_px;
DCchico 1:10330bce85cb 243 int py = j + snake.head_py;
DCchico 1:10330bce85cb 244
DCchico 1:10330bce85cb 245 // Compute u,v coordinates for drawing
DCchico 1:10330bce85cb 246 int u = (i+5)*11 + 3;
DCchico 1:10330bce85cb 247 int v = (j+4)*11 + 15;
DCchico 1:10330bce85cb 248
DCchico 1:10330bce85cb 249 // Figure out what to draw
DCchico 1:10330bce85cb 250 DrawFunc draw = NULL;
DCchico 1:10330bce85cb 251 if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
DCchico 1:10330bce85cb 252 MapItem* curr_item = get_here(x, y);
DCchico 1:10330bce85cb 253 MapItem* prev_item = get_here(px, py);
DCchico 1:10330bce85cb 254 if (draw_option || curr_item != prev_item) { // Only draw if they're different
DCchico 1:10330bce85cb 255 if (curr_item) { // There's something here! Draw it
DCchico 1:10330bce85cb 256 draw = curr_item->draw;
DCchico 1:10330bce85cb 257 } else { // There used to be something, but now there isn't
DCchico 1:10330bce85cb 258 draw = draw_nothing;
DCchico 1:10330bce85cb 259 }
DCchico 1:10330bce85cb 260 } else if (curr_item && curr_item->type == CLEAR) {
DCchico 1:10330bce85cb 261 // This is a special case for erasing things like doors.
DCchico 1:10330bce85cb 262 draw = curr_item->draw; // i.e. draw_nothing
DCchico 1:10330bce85cb 263 }
DCchico 1:10330bce85cb 264 } else if (draw_option) { // If doing a full draw, but we're out of bounds, draw the walls.
DCchico 1:10330bce85cb 265 draw = draw_wall;
DCchico 1:10330bce85cb 266 }
DCchico 1:10330bce85cb 267
DCchico 1:10330bce85cb 268 // Actually draw the tile
DCchico 1:10330bce85cb 269 if (draw) draw(u, v);
DCchico 1:10330bce85cb 270 }
DCchico 1:10330bce85cb 271 }
DCchico 1:10330bce85cb 272
DCchico 1:10330bce85cb 273 // Draw status bars
DCchico 1:10330bce85cb 274 draw_upper_status();
DCchico 1:10330bce85cb 275 draw_lower_status();
DCchico 1:10330bce85cb 276 }
DCchico 1:10330bce85cb 277
DCchico 1:10330bce85cb 278 /**
DCchico 1:10330bce85cb 279 * Initialize the main world map. Add walls around the edges, interior chambers,
DCchico 1:10330bce85cb 280 * and plants in the background so you can see motion.
DCchico 1:10330bce85cb 281 */
DCchico 1:10330bce85cb 282 void init_main_map()
DCchico 1:10330bce85cb 283 {
hnguyen403 3:33bf11645fe1 284 uLCD.text_width(1);
hnguyen403 3:33bf11645fe1 285 uLCD.text_height(1);
hnguyen403 3:33bf11645fe1 286 uLCD.printf("Score %d", snake.score);
hnguyen403 3:33bf11645fe1 287 uLCD.color(GREEN);
DCchico 1:10330bce85cb 288 // "Random" plants
DCchico 1:10330bce85cb 289 Map* map = set_active_map(0);
DCchico 1:10330bce85cb 290 for(int i = map_width() + 3; i < map_area(); i += 39) {
DCchico 1:10330bce85cb 291 add_goodie(i % map_width(), i / map_width());
DCchico 1:10330bce85cb 292 }
DCchico 1:10330bce85cb 293 pc.printf("plants\r\n");
DCchico 1:10330bce85cb 294
DCchico 1:10330bce85cb 295 pc.printf("Adding walls!\r\n");
DCchico 1:10330bce85cb 296 add_wall(0, 0, HORIZONTAL, map_width());
DCchico 1:10330bce85cb 297 add_wall(0, map_height()-1, HORIZONTAL, map_width());
DCchico 1:10330bce85cb 298 add_wall(0, 0, VERTICAL, map_height());
DCchico 1:10330bce85cb 299 add_wall(map_width()-1, 0, VERTICAL, map_height());
DCchico 1:10330bce85cb 300 pc.printf("Walls done!\r\n");
DCchico 1:10330bce85cb 301
DCchico 1:10330bce85cb 302 add_snake_head(snake.locations[0].x, snake.locations[0].y);
DCchico 1:10330bce85cb 303 add_snake_body(snake.locations[1].x, snake.locations[1].y);
DCchico 1:10330bce85cb 304 add_snake_tail(snake.locations[2].x, snake.locations[2].y);
DCchico 1:10330bce85cb 305
DCchico 1:10330bce85cb 306 pc.printf("Add extra chamber\r\n");
DCchico 1:10330bce85cb 307 add_wall(30, 0, VERTICAL, 10);
DCchico 1:10330bce85cb 308 add_wall(30, 10, HORIZONTAL, 10);
DCchico 1:10330bce85cb 309 add_wall(39, 0, VERTICAL, 10);
DCchico 1:10330bce85cb 310 pc.printf("Added!\r\n");
DCchico 1:10330bce85cb 311
hnguyen403 3:33bf11645fe1 312
DCchico 1:10330bce85cb 313 // Add stairs to chamber (map 1)
DCchico 1:10330bce85cb 314 //add_stairs(15, 5, 1, 5, 5);
DCchico 1:10330bce85cb 315
DCchico 1:10330bce85cb 316 // profile_hashtable();
DCchico 1:10330bce85cb 317 print_map();
DCchico 1:10330bce85cb 318 }
DCchico 1:10330bce85cb 319
DCchico 1:10330bce85cb 320 /**
DCchico 1:10330bce85cb 321 * Program entry point! This is where it all begins.
DCchico 1:10330bce85cb 322 * This function or all the parts of the game. Most of your
DCchico 1:10330bce85cb 323 * implementation should be elsewhere - this holds the game loop, and should
DCchico 1:10330bce85cb 324 * read like a road map for the rest of the code.
DCchico 1:10330bce85cb 325 */
DCchico 1:10330bce85cb 326 int main()
DCchico 1:10330bce85cb 327 {
DCchico 1:10330bce85cb 328 // First things first: initialize hardware
DCchico 1:10330bce85cb 329 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
DCchico 1:10330bce85cb 330
DCchico 1:10330bce85cb 331 snake_init(&snake);
DCchico 2:4947d6a82971 332 // 0. Initialize the maps -- implement this function:
DCchico 1:10330bce85cb 333 maps_init();
DCchico 1:10330bce85cb 334 init_main_map();
hnguyen403 3:33bf11645fe1 335 playSound("/sd/wavfiles/track.wav");
DCchico 1:10330bce85cb 336
DCchico 1:10330bce85cb 337 // Initialize game state
DCchico 1:10330bce85cb 338 set_active_map(0);
hnguyen403 3:33bf11645fe1 339 snake.head_x = snake.head_y = 5 ;
DCchico 1:10330bce85cb 340 // Initial drawing
DCchico 1:10330bce85cb 341 draw_game(FULL_DRAW);
DCchico 1:10330bce85cb 342 // Main game loop
DCchico 1:10330bce85cb 343 while(1) {
DCchico 1:10330bce85cb 344 // Timer to measure game update speed
DCchico 1:10330bce85cb 345 Timer t;
DCchico 1:10330bce85cb 346 t.start();
DCchico 1:10330bce85cb 347
DCchico 2:4947d6a82971 348 // 1. Read inputs -- implement this function:
DCchico 2:4947d6a82971 349 GameInputs inputs = read_inputs();
DCchico 2:4947d6a82971 350
DCchico 2:4947d6a82971 351 // 2. Determine action (move, act, menu, etc.) -- implement this function:
DCchico 2:4947d6a82971 352 int action = get_action(inputs);
DCchico 2:4947d6a82971 353
DCchico 2:4947d6a82971 354 // 3. Update game -- implement this function:
DCchico 2:4947d6a82971 355 int result = update_game(action);
DCchico 2:4947d6a82971 356
DCchico 2:4947d6a82971 357 // 3b. Check for game over based on result
DCchico 2:4947d6a82971 358 // and if so, handle game over -- implement this.
DCchico 2:4947d6a82971 359
DCchico 2:4947d6a82971 360 // 4. Draw screen -- provided:
DCchico 2:4947d6a82971 361 draw_game(result);
DCchico 2:4947d6a82971 362
DCchico 1:10330bce85cb 363 // Compute update time
DCchico 1:10330bce85cb 364 t.stop();
DCchico 1:10330bce85cb 365 int dt = t.read_ms();
DCchico 1:10330bce85cb 366
DCchico 1:10330bce85cb 367 // Display and wait
DCchico 1:10330bce85cb 368 // NOTE: Text is 8 pixels tall
DCchico 1:10330bce85cb 369 if (dt < 100) wait_ms(100 - dt);
DCchico 1:10330bce85cb 370 }
DCchico 1:10330bce85cb 371 }
DCchico 1:10330bce85cb 372
DCchico 1:10330bce85cb 373 // Plays a wavfile
DCchico 1:10330bce85cb 374 void playSound(char* wav)
DCchico 1:10330bce85cb 375 {
hnguyen403 3:33bf11645fe1 376
DCchico 1:10330bce85cb 377 }