Carter Montgomery / Mbed 2 deprecated 2035_Final_Project

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gameover.cpp Source File

gameover.cpp

00001 // Project includes
00002 #include "globals.h"
00003 #include "hardware.h"
00004 #include "map.h"
00005 #include "graphics.h"
00006 #include "speech.h"
00007 
00008 // Functions in this file
00009 int get_screen_action (GameInputs inputs);
00010 int update_screen (int action);
00011 void draw_screen (int init);
00012 void init_screen ();
00013 int gameover();
00014 
00015 //global variables
00016 int yes;
00017 int no;
00018 int yes_color;
00019 int no_color;
00020 int current_item2 = 0;
00021 
00022 
00023 /**
00024  * Given the game inputs, determine what kind of update needs to happen.
00025  * Possbile return values are defined below.
00026  */
00027 #define NO_ACTION 0
00028 #define CHANGE_OPTION 1
00029 #define SELECT 2
00030 
00031 int get_screen_action(GameInputs inputs)
00032 {
00033     if (!inputs.b1){
00034         return CHANGE_OPTION;
00035     } if (!inputs.b2){
00036         return SELECT;
00037     }
00038     return NO_ACTION;
00039 }
00040 
00041 
00042 #define NO_RESULT 0
00043 #define FULL_DRAW 2
00044 int update_screen(int action)
00045 {
00046     switch(action)
00047     {
00048         case CHANGE_OPTION: 
00049             //if button 1 pressed move to next menu item
00050             current_item2 = current_item2++;
00051             break;
00052         case SELECT:
00053             if (current_item2%2 == 0) {//Start game
00054                 yes = true;
00055             } else if (current_item2%2 == 1){ //Quit (screen cut to black)
00056                 no = true;
00057             }
00058             break;
00059     }
00060     return NO_RESULT;
00061 }
00062 
00063 /**
00064  * Entry point for frame drawing. This should be called once per iteration of
00065  * the game loop. This draws all tiles on the screen, followed by the status 
00066  * bars. Unless init is nonzero, this function will optimize drawing by only 
00067  * drawing tiles that have changed from the previous frame.
00068  */
00069 void draw_screen(int init)
00070 {
00071        
00072     if (current_item2%2 == 0){
00073         yes_color = GREEN;
00074     } else if (current_item2%2 == 1){
00075         no_color = GREEN;
00076     }
00077     
00078     uLCD.locate(1,9);
00079     uLCD.color(yes_color);
00080     uLCD.printf("YES");
00081     
00082     uLCD.locate(1,10);
00083     uLCD.color(no_color);
00084     uLCD.printf("NO");
00085  
00086     yes_color = BLACK;
00087     no_color = BLACK;    
00088 }
00089 
00090 /**
00091  * Initialize the main world map. Add walls around the edges, interior chambers,
00092  * and plants in the background so you can see motion.
00093  */
00094 void init_screen(int lives)
00095 {
00096     uLCD.locate(1,5);
00097     //draw pink rectangle on whole screen
00098     uLCD.filled_rectangle(0, 128, 128, 80, 0xFFFFFF);
00099     // write title
00100     uLCD.textbackground_color(0xFFFFFF);
00101     uLCD.color(BLACK);
00102     uLCD.printf("GAME OVER!");
00103     
00104     if (current_item2%2 == 0){
00105         yes_color = RED;
00106     } else if (current_item2%2 == 1){
00107         no_color = RED;
00108     }
00109     
00110     uLCD.locate(1,7);
00111     uLCD.printf("Continue Game?");
00112     
00113     uLCD.locate(1,8);
00114     char str[30];
00115     sprintf(str, "%d lives left.", lives);
00116     uLCD.printf(str);
00117     
00118     uLCD.locate(1,9);
00119     uLCD.color(yes_color);
00120     uLCD.printf("YES");
00121     
00122     uLCD.locate(1,10);
00123     uLCD.color(no_color);
00124     uLCD.printf("NO");
00125     
00126 }
00127 
00128 /**
00129  * Program entry point! This is where it all begins.
00130  * This function orchestrates all the parts of the game. Most of your
00131  * implementation should be elsewhere - this holds the game loop, and should
00132  * read like a road map for the rest of the code.
00133  */
00134 int gameover(int lives)
00135 {
00136     // Initialize the maps
00137     init_screen(lives);
00138 
00139     // Initial drawing
00140     draw_screen(true);
00141 
00142     // Main game loop
00143     while(1)
00144     {
00145         // Timer to measure game update speed
00146         Timer t; t.start();
00147         
00148         // Actually do the game update:
00149         // 1. Read inputs  
00150             GameInputs in = read_inputs();
00151         // 2. Determine action (get_action) 
00152             int a = get_screen_action(in);       
00153         // 3. Update game (update_game)
00154             int u = update_screen(a);
00155         // 4. Draw frame (draw_game)
00156         draw_screen(u);
00157         
00158         //check for start
00159         if (yes){
00160             return 1;
00161         }else if (no){
00162             return 0;
00163         }
00164         
00165         
00166         // 5. Frame delay
00167         t.stop();
00168         int dt = t.read_ms();
00169         if (dt < 500) wait_ms(500 - dt);
00170     }
00171 }