Sizhe Lin / Mbed 2 deprecated missile_command_Sizhe_Lin

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of missile_command by ECE 2035 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /* Gatech ECE2035 2014 FALL missile command
00002  * Copyright (c) 2014 Gatech ECE2035
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 /** @file main.cpp */
00023 // Include header files for platform
00024 #include "mbed.h"
00025 #include "uLCD_4DGL.h"
00026 #include "wave_player.h"
00027 #include "SDFileSystem.h"
00028 #include "segment_display.h"
00029 
00030 // Include header files for missile command project
00031 #include "globals.h"
00032 #include "city_landscape_public.h "
00033 #include "missile_public.h "
00034 #include "player.h"
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 
00038 // Platform initialization
00039 DigitalIn left_pb(p23); DigitalIn right_pb(p21); DigitalIn fire_pb(p22);
00040 DigitalIn fouth_pb(p24);
00041 uLCD_4DGL uLCD(p28,p27,p29);
00042 AnalogOut DACout(p18);
00043 wave_player waver(&DACout);
00044 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs
00045 
00046 // Example of the decleration of your implementation
00047 void playSound(char * wav);
00048 void check_interception();
00049 int within_range(MISSILE m, ANTIMISSILE a);
00050 int check_destruction_range(MISSILE m,CITY c);
00051 void check_city_destruction();
00052 void display_victory();
00053 void display_failure();
00054 void advance_level();
00055 
00056 
00057 PLAYER current_player;
00058 CITY city_record[];
00059 MISSILE missile_record[];
00060 ANTIMISSILE am[];
00061 EXPLOSION ex[];
00062 
00063 int number_of_cities = 4;
00064 
00065 /** Main() is where you start your implementation
00066     @brief The hints of implementation are in the comments. <br>
00067     @brief You are expected to implement your code in main.cpp and player.cpp. But you could modify any code if you want to make the game work better.
00068 */
00069 
00070 
00071 int main()
00072 {   
00073  
00074     setup_sequence();
00075     seg_driver_initialize(); 
00076     city_landscape_init(number_of_cities); 
00077     // Initialize the buttons        
00078     left_pb.mode(PullUp);  // The variable left_pb will be zero when the pushbutton for moving the player left is pressed    
00079     right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed        
00080     fire_pb.mode(PullUp); 
00081     fouth_pb.mode(PullUp); //the variable fouth_pb will be zero when the pushbutton for firing a missile is pressed
00082     player_init();//initalize the player
00083     antimissile_init();
00084     explosion_init();
00085     //display the game menu
00086     int k;
00087     while (1) {
00088         char str1[] = "Easy";
00089         char str2[] = "Medium";
00090         char str3[] = "hard";
00091         if (right_pb == 0) {
00092             k++;
00093         } 
00094         k = k % 3;
00095         if(k == 1) {
00096             uLCD.text_string(str1, 3, 3, FONT_7X8, GREEN);    
00097         } else {
00098             uLCD.text_string(str1,3, 3,  FONT_7X8, WHITE);    
00099         }
00100         
00101         if(k == 2) {
00102             uLCD.text_string(str2, 3, 4, FONT_7X8, GREEN);    
00103         } else {
00104             uLCD.text_string(str2, 3, 4,  FONT_7X8, WHITE);    
00105         } 
00106         
00107         if(k == 0) {
00108             uLCD.text_string(str3, 3, 5, FONT_7X8, GREEN);    
00109         } else {
00110             uLCD.text_string(str3, 3, 5,  FONT_7X8, WHITE);    
00111         } 
00112         
00113         if (fire_pb == 0) {
00114             if (k == 0) {
00115                 current_player.current_level = 3; 
00116             } else {
00117                 current_player.current_level = k;
00118             }
00119             set_missile_speed(5 - current_player.current_level);
00120             set_missile_interval(10 - current_player.current_level);
00121             uLCD.text_string(str1, 3, 3, FONT_7X8, BACKGROUND_COLOR);  
00122             uLCD.text_string(str2, 3, 4, FONT_7X8, BACKGROUND_COLOR);
00123             uLCD.text_string(str3, 3, 5, FONT_7X8, BACKGROUND_COLOR);
00124             char go[] = "READY!";
00125             uLCD.text_string(go, 6, 6, FONT_7X8, GREEN);
00126             playSound("/sd/wavfiles/BUZZER.wav");
00127             uLCD.text_string(go, 6, 6, FONT_7X8, BACKGROUND_COLOR);
00128             break;
00129         }
00130         
00131         
00132     }
00133     
00134     /// 2.Begin the game loop
00135     while(1){
00136         current_player.timer++;
00137         char str[] = {'s', 'c', 'o', 'r', 'e', ':', '0' + current_player.score};
00138         char life_str[] = {'l','i','f', 'e',':', '0' + current_player.life};
00139         char level_str[] = {'l', 'e', 'v', 'e', 'l' ,':','0' + current_player.current_level};
00140         uLCD.text_string(str, 11, 0, FONT_7X8, WHITE);
00141         uLCD.text_string(life_str, 0, 1, FONT_7X8, WHITE);
00142         uLCD.text_string(level_str, 0, 0, FONT_7X8, GREEN);
00143         
00144         seg_driver(current_player.am_remain);
00145         /// 3.Example of drawing the player
00146         player_draw(); // draws a player at the center of the screen
00147         draw_antimissiles();
00148         /// 4.Example of calling the missile API.
00149         missile_generator(); /// It updates all incoming missiles on the screen
00150         update_explosion();
00151         draw_explosion();
00152         if (current_player.timer % 150 == 0 && current_player.life < 6) {
00153             current_player.life++;
00154         }
00155         /// 5.Implement the code to get user input and update the player
00156             /// -[Hint] You could see city_landscape.cpp to get some ideas about how to implement your player. <br>
00157         if(left_pb == 0){
00158             /// -[Hint] Implement the code to move player left <br>
00159             player_move_left();
00160         }
00161         if(right_pb == 0){
00162             /// -[Hint] Implement the code to move player right <br>
00163             player_move_right();
00164         }
00165         if(fire_pb == 0){
00166             /// -[Hint] Implement the code to fire player-missile <br>
00167             shoot();
00168             // [Demo of play sound file]
00169             //playSound("/sd/wavfiles/BUZZER.wav");
00170         }
00171         if(fouth_pb == 0 && current_player.protector_num > 0) {
00172             current_player.protector.is_active = 1;
00173             current_player.protector.timer = 0;
00174             current_player.protector_num--;
00175         }
00176         if(left_pb == 0 && right_pb == 0 && fouth_pb != 0 && fire_pb != 0) {
00177             big_shoot();
00178         }
00179         update_protector();
00180         update_antimissile_positions();
00181         check_interception();
00182         check_city_destruction();
00183         /// 6.Implement the code to draw a user missile
00184             /// -[Hint] You could see missile.cpp or missile_generator() for your reference <br>
00185         if (current_player.score > 9 || (!left_pb && !right_pb && !fire_pb && !fouth_pb)) {
00186             advance_level(); 
00187             //display_victory();   
00188         } else if(!is_any_left()) {
00189             current_player.life--;
00190             city_landscape_init(number_of_cities);
00191             
00192         } else if(current_player.current_level >= 4) {
00193             display_victory();
00194             break;    
00195         } 
00196         
00197         if (current_player.life == 0) {
00198             display_failure();
00199             break;   
00200         }
00201             
00202         /// 7.Implement the code to detect the collision between missiles and cities
00203             /// -[Hint] You could use city_get_info() and  missile_get_info() <br>
00204             /// -[Hint] You could use missile_set_exploded() to notify the missile module that the missile was exploded. <br>
00205             /// -[Hint] You need to check which city was hit by the missile, and call city_destroy() to destroy it. <br>
00206             /// -[Hint] You might also check whether the missile hit the land <br>
00207         
00208         /// 8.Implement the code to detect a collision between player-missiles and incoming missiles
00209             /// -[Hint] You could use missile_get_info() to get the position of incoming missile <br>
00210             /// -[Hint] You could use missile_set_exploded() to notify the missile module that the missile was exploded. <br>
00211             /// -[Hint] You might also check whether the missile hit the player <br>
00212         
00213         /// 9.Implement the code to check the end of game
00214             /// -[Hint] For example, if there is no more city, it should be gameover. <br>
00215         
00216     }
00217 }
00218 
00219 // Example of implementation of your functions
00220 void playSound(char * wav)
00221 {
00222     // open wav file
00223     FILE *wave_file;
00224     wave_file=fopen(wav,"r");
00225 
00226     // play wav file
00227     waver.play(wave_file);
00228 
00229     // close wav file
00230     fclose(wave_file);
00231 }
00232 
00233 void advance_level() {
00234     current_player.timer = 0;
00235     current_player.current_level++;
00236     set_missile_speed(5 - current_player.current_level);
00237     set_missile_interval(10 - current_player.current_level);
00238     city_landscape_init(4);
00239     current_player.score = 0;
00240     current_player.protector_num = 3;
00241     char str[] = "Enter NEXT LEVEL";
00242     uLCD.text_string(str, 2, 6, FONT_7X8, WHITE);
00243     wait(1);
00244     uLCD.text_string(str, 2, 6, FONT_7X8, BACKGROUND_COLOR);
00245     int i;
00246     for (i = 0;i < MAX_NUM_MISSILE;i++) {
00247         missile_set_exploded(i);
00248     }
00249 }
00250 void check_interception() {
00251     int i,j,k;
00252     for (i = 0;i < MAX_NUM_MISSILE; i++) {
00253         for (j = 0; j < current_player.max_am; j++) {
00254             if (missile_record[i].status == MISSILE_ACTIVE && am[j].status == ACTIVE
00255              && within_range(missile_record[i], am[j])) {
00256                 for (k = 0; k < current_player.max_am; k++) {
00257                     if (ex[k].exploded == NO) {
00258                         //find a unused explosion activate it
00259                         ex[k].x = am[j].x;
00260                         ex[k].y = am[j].y;
00261                         ex[k].exploded = YES;
00262                         ex[k].color = PLAYER_COLOR;
00263                         break;   
00264                     }    
00265                 }
00266                 //set both the missile and anti missile to DEACTIVE
00267                 missile_record[i].status = MISSILE_EXPLODED;
00268                 am[j].status = DEACTIVE;
00269                 current_player.am_remain++;
00270                 current_player.score++;        
00271             }    
00272         }   
00273     }
00274 
00275 }
00276 
00277 void check_city_destruction() {
00278     int i,j,k;
00279     for (i = 0; i < MAX_NUM_MISSILE; i++) {
00280         for(j = 0;j < number_of_cities;j++) {
00281             if (missile_record[i].status == MISSILE_ACTIVE && city_get_info(j).status == EXIST && check_destruction_range(missile_record[i], city_get_info(j))) {
00282                  for (k = 0; k < current_player.max_am; k++) {
00283                     if (ex[k].exploded == NO) {
00284                         //find a unused explosion activate it
00285                         ex[k].x = missile_record[i].x;
00286                         ex[k].y = missile_record[i].y;
00287                         ex[k].exploded = YES;
00288                         ex[k].color = LANDSCAPE_COLOR;
00289                         break;   
00290                     }    
00291                 }
00292                 missile_record[i].status = MISSILE_EXPLODED;
00293                 city_destroy(j);
00294             }
00295         }
00296     }
00297 
00298 
00299 }
00300 
00301 void display_victory() {
00302     uLCD.cls();
00303     char str[] = "You Are Winnner";
00304     uLCD.text_string(str, 2, 9, FONT_7X8, WHITE);
00305     playSound("/sd/wavfiles/ding_dong.wav");
00306 }
00307 
00308 void display_failure() {
00309     uLCD.cls();
00310     char str[] = "Game Over";
00311     uLCD.text_string(str, 3, 9, FONT_7X8, WHITE);
00312     playSound("/sd/wavfiles/ding_dong.wav");
00313 }
00314 
00315 
00316 
00317 
00318 int check_destruction_range(MISSILE m,CITY c) {
00319     if(c.x  <= m.x
00320         && m.x <= c.x + c.width
00321         && c.y <= m.y
00322         && m.y <= c.y + c.height) {
00323         return 1;
00324     }
00325     return 0;    
00326 }
00327       
00328 int within_range(MISSILE m, ANTIMISSILE a) {
00329          if(m.x - 2 <=a.x 
00330             && a.x <= m.x  + 2
00331             && m.y - 2 <=a.y
00332             && a.y <= m.y + 2) {
00333                  return 1;
00334         }
00335         return 0;
00336 }       
00337