2035

Dependencies:   4DGL-uLCD-SE mbed wave_player

Fork of missile_command by ECE 2035 TA

Committer:
tianyeapply
Date:
Wed Nov 09 17:04:42 2016 +0000
Revision:
3:7e33224a6f1d
Parent:
1:3da29f1d84b4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arvahsu 0:532cb55d6136 1 /* Gatech ECE2035 2014 FALL missile command
arvahsu 0:532cb55d6136 2 * Copyright (c) 2014 Gatech ECE2035
arvahsu 0:532cb55d6136 3 *
arvahsu 0:532cb55d6136 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
arvahsu 0:532cb55d6136 5 * of this software and associated documentation files (the "Software"), to deal
arvahsu 0:532cb55d6136 6 * in the Software without restriction, including without limitation the rights
arvahsu 0:532cb55d6136 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
arvahsu 0:532cb55d6136 8 * copies of the Software, and to permit persons to whom the Software is
arvahsu 0:532cb55d6136 9 * furnished to do so, subject to the following conditions:
arvahsu 0:532cb55d6136 10 *
arvahsu 0:532cb55d6136 11 * The above copyright notice and this permission notice shall be included in
arvahsu 0:532cb55d6136 12 * all copies or substantial portions of the Software.
arvahsu 0:532cb55d6136 13 *
arvahsu 0:532cb55d6136 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
arvahsu 0:532cb55d6136 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
arvahsu 0:532cb55d6136 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
arvahsu 0:532cb55d6136 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
arvahsu 0:532cb55d6136 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
arvahsu 0:532cb55d6136 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
arvahsu 0:532cb55d6136 20 * SOFTWARE.
arvahsu 0:532cb55d6136 21 */
arvahsu 0:532cb55d6136 22 /** @file main.cpp */
arvahsu 0:532cb55d6136 23 // Include header files for platform
arvahsu 0:532cb55d6136 24 #include "mbed.h"
arvahsu 0:532cb55d6136 25 #include "uLCD_4DGL.h"
arvahsu 0:532cb55d6136 26 #include "wave_player.h"
arvahsu 0:532cb55d6136 27 #include "SDFileSystem.h"
arvahsu 0:532cb55d6136 28 #include "segment_display.h"
tianyeapply 3:7e33224a6f1d 29 #include <math.h>
tianyeapply 3:7e33224a6f1d 30 #include <stdio.h>
arvahsu 0:532cb55d6136 31
arvahsu 0:532cb55d6136 32 // Include header files for missile command project
arvahsu 0:532cb55d6136 33 #include "globals.h"
arvahsu 0:532cb55d6136 34 #include "city_landscape_public.h"
arvahsu 0:532cb55d6136 35 #include "missile_public.h"
arvahsu 0:532cb55d6136 36 #include "player.h"
arvahsu 0:532cb55d6136 37
arvahsu 0:532cb55d6136 38 // Platform initialization
tianyeapply 3:7e33224a6f1d 39 DigitalIn left_pb(p23); DigitalIn right_pb(p21); DigitalIn fire_pb(p22);DigitalIn fourth_pb(p24);
arvahsu 0:532cb55d6136 40 uLCD_4DGL uLCD(p28,p27,p29);
arvahsu 0:532cb55d6136 41 AnalogOut DACout(p18);
arvahsu 0:532cb55d6136 42 wave_player waver(&DACout);
arvahsu 0:532cb55d6136 43 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs
arvahsu 0:532cb55d6136 44
arvahsu 0:532cb55d6136 45 // Example of the decleration of your implementation
arvahsu 0:532cb55d6136 46 void playSound(char * wav);
arvahsu 0:532cb55d6136 47
arvahsu 0:532cb55d6136 48 /** Main() is where you start your implementation
arvahsu 0:532cb55d6136 49 @brief The hints of implementation are in the comments. <br>
arvahsu 0:532cb55d6136 50 @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.
arvahsu 0:532cb55d6136 51 */
arvahsu 0:532cb55d6136 52 int main()
arvahsu 0:532cb55d6136 53 {
arvahsu 0:532cb55d6136 54 setup_sequence();
arvahsu 0:532cb55d6136 55 seg_driver_initialize();
tianyeapply 3:7e33224a6f1d 56 // playSound("/sd/wavfiles/BUZZER.wav");
arvahsu 0:532cb55d6136 57
arvahsu 0:532cb55d6136 58 /// [Example of missile command implementation]
arvahsu 0:532cb55d6136 59 /// Here is a rough flow to implement the missile command: <br><br>
arvahsu 0:532cb55d6136 60
arvahsu 0:532cb55d6136 61 /// 1.Initialize and draw the city landscape
arvahsu 0:532cb55d6136 62 city_landscape_init(4);
arvahsu 0:532cb55d6136 63 // Initialize the buttons
arvahsu 0:532cb55d6136 64 left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed
arvahsu 0:532cb55d6136 65 right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed
arvahsu 0:532cb55d6136 66 fire_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed
tianyeapply 3:7e33224a6f1d 67 fourth_pb.mode(PullUp);
tianyeapply 3:7e33224a6f1d 68 int x=60;
tianyeapply 3:7e33224a6f1d 69 int y=100;
tianyeapply 3:7e33224a6f1d 70 int count=0;
tianyeapply 3:7e33224a6f1d 71 int tem=0;
tianyeapply 3:7e33224a6f1d 72 int num_city=4;
tianyeapply 3:7e33224a6f1d 73 int life=3;
tianyeapply 3:7e33224a6f1d 74 int delta=10;
tianyeapply 3:7e33224a6f1d 75 int level=0;
tianyeapply 3:7e33224a6f1d 76 int num_missile=0;;
tianyeapply 3:7e33224a6f1d 77 int speed=0,interval=0,player_speed=0,pre_point=0;
tianyeapply 3:7e33224a6f1d 78 int t=0;
tianyeapply 3:7e33224a6f1d 79 CITY city;
tianyeapply 3:7e33224a6f1d 80 MISSILE missile,player_missile;
arvahsu 0:532cb55d6136 81 /// 2.Begin the game loop
tianyeapply 3:7e33224a6f1d 82 int current_life;
tianyeapply 3:7e33224a6f1d 83 current_life=life_generate(life);
tianyeapply 3:7e33224a6f1d 84 //playSound("/sd/wavfiles/begin.wav");
tianyeapply 3:7e33224a6f1d 85 char begin[20];
tianyeapply 3:7e33224a6f1d 86 sprintf(begin,"%s" ,"Press PB4 To Start!");
tianyeapply 3:7e33224a6f1d 87 uLCD.text_string(begin,0,8,3,0xFF9900);
arvahsu 0:532cb55d6136 88 while(1){
tianyeapply 3:7e33224a6f1d 89 if(fourth_pb==0){t=1;
tianyeapply 3:7e33224a6f1d 90 playSound("/sd/wavfiles/start.wav");
tianyeapply 3:7e33224a6f1d 91 uLCD.text_string(begin,0,8,3,BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 92 break;}}
tianyeapply 3:7e33224a6f1d 93 while(t){
tianyeapply 3:7e33224a6f1d 94 display_score(count);
tianyeapply 3:7e33224a6f1d 95 display_level(level);
arvahsu 0:532cb55d6136 96 /// 3.Example of drawing the player
tianyeapply 3:7e33224a6f1d 97 player_draw(x,y); // draws a player at the center of the screen
arvahsu 0:532cb55d6136 98
arvahsu 0:532cb55d6136 99 /// 4.Example of calling the missile API.
arvahsu 0:532cb55d6136 100 missile_generator(); /// It updates all incoming missiles on the screen
arvahsu 0:532cb55d6136 101
arvahsu 0:532cb55d6136 102 /// 5.Implement the code to get user input and update the player
arvahsu 0:532cb55d6136 103 /// -[Hint] You could see city_landscape.cpp to get some ideas about how to implement your player. <br>
arvahsu 0:532cb55d6136 104 if(left_pb == 0){
tianyeapply 3:7e33224a6f1d 105 if(x<=109){
tianyeapply 3:7e33224a6f1d 106 player_clear(x,y);
tianyeapply 3:7e33224a6f1d 107 x=x+3;
tianyeapply 3:7e33224a6f1d 108 player_draw(x,y);
tianyeapply 3:7e33224a6f1d 109 }
arvahsu 0:532cb55d6136 110 /// -[Hint] Implement the code to move player left <br>
arvahsu 0:532cb55d6136 111 }
arvahsu 0:532cb55d6136 112 if(right_pb == 0){
tianyeapply 3:7e33224a6f1d 113 if(x>=3){
tianyeapply 3:7e33224a6f1d 114 player_clear(x,y);
tianyeapply 3:7e33224a6f1d 115 x=x-3;
tianyeapply 3:7e33224a6f1d 116 player_draw(x,y);
tianyeapply 3:7e33224a6f1d 117 }
arvahsu 0:532cb55d6136 118 /// -[Hint] Implement the code to move player right <br>
arvahsu 0:532cb55d6136 119 }
arvahsu 0:532cb55d6136 120
tianyeapply 3:7e33224a6f1d 121 //player_missile_generator(x, y);
tianyeapply 3:7e33224a6f1d 122 if(fire_pb == 0){
tianyeapply 3:7e33224a6f1d 123 while( fire_pb==0){
tianyeapply 3:7e33224a6f1d 124 }
tianyeapply 3:7e33224a6f1d 125 //implement the code to fire player-missile <br>
tianyeapply 3:7e33224a6f1d 126 player_missile_create(x+8, y-8.5);
tianyeapply 3:7e33224a6f1d 127 }
tianyeapply 3:7e33224a6f1d 128 if(fourth_pb==0&&num_missile<4){
tianyeapply 3:7e33224a6f1d 129 while( fourth_pb==0){
tianyeapply 3:7e33224a6f1d 130 }
tianyeapply 3:7e33224a6f1d 131 use_fourth_pb();
tianyeapply 3:7e33224a6f1d 132 }
tianyeapply 3:7e33224a6f1d 133
tianyeapply 3:7e33224a6f1d 134 if(left_pb==0&&right_pb==0){
tianyeapply 3:7e33224a6f1d 135 speed++;interval=interval+5;
tianyeapply 3:7e33224a6f1d 136 level++;
tianyeapply 3:7e33224a6f1d 137 advanced_level(8-speed%8,100-interval%100);
tianyeapply 3:7e33224a6f1d 138 }
tianyeapply 3:7e33224a6f1d 139 player_missile_update_position();
tianyeapply 3:7e33224a6f1d 140 int i,j,k;
tianyeapply 3:7e33224a6f1d 141 for (i=0; i<MAX_NUM_MISSILE;i++){
tianyeapply 3:7e33224a6f1d 142 missile=missile_get_info(i);
tianyeapply 3:7e33224a6f1d 143 if(abs(missile.x-x)<10 &&abs(missile.y-y)<10&&missile.status==MISSILE_ACTIVE||abs(missile.x-x-10)<10 &&abs(missile.y-y)<10&&missile.status==MISSILE_ACTIVE){
tianyeapply 3:7e33224a6f1d 144 missile_set_exploded(i);
tianyeapply 3:7e33224a6f1d 145 life--;
tianyeapply 3:7e33224a6f1d 146 life_reduce(current_life);
tianyeapply 3:7e33224a6f1d 147 current_life=current_life-7;
tianyeapply 3:7e33224a6f1d 148
tianyeapply 3:7e33224a6f1d 149 }
tianyeapply 3:7e33224a6f1d 150 for(k=0;k<MAX_NUM_CITY;k++){
tianyeapply 3:7e33224a6f1d 151 city=city_get_info(k);
tianyeapply 3:7e33224a6f1d 152 if(missile.y<127-city.height){
tianyeapply 3:7e33224a6f1d 153 if(abs(missile.x-city.x)<10 && abs(missile.y-city.y)<10 &&missile.status==MISSILE_ACTIVE&&city.status==EXIST){
tianyeapply 3:7e33224a6f1d 154 missile_set_exploded(i);
tianyeapply 3:7e33224a6f1d 155 city_destroy(k);
tianyeapply 3:7e33224a6f1d 156 num_city--;
tianyeapply 3:7e33224a6f1d 157 uLCD.filled_circle(missile.x , missile.y , 4, 0xCCAA00);
tianyeapply 3:7e33224a6f1d 158 wait(0.4);
tianyeapply 3:7e33224a6f1d 159 circle_explosion(missile.x , missile.y);
tianyeapply 3:7e33224a6f1d 160
tianyeapply 3:7e33224a6f1d 161 uLCD.filled_circle(missile.x , missile.y , 4, BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 162 //count=count-10;
tianyeapply 3:7e33224a6f1d 163 display_score(count);
tianyeapply 3:7e33224a6f1d 164 city_destory();
tianyeapply 3:7e33224a6f1d 165
tianyeapply 3:7e33224a6f1d 166
tianyeapply 3:7e33224a6f1d 167 }
tianyeapply 3:7e33224a6f1d 168 }
tianyeapply 3:7e33224a6f1d 169 else{
tianyeapply 3:7e33224a6f1d 170 if(abs(missile.x-(city.x-5))<10&&missile.status==MISSILE_ACTIVE &&city.status==EXIST ||abs(missile.x-(city.x+5))<10&&missile.status==MISSILE_ACTIVE&&city.status==EXIST){
tianyeapply 3:7e33224a6f1d 171 uLCD.filled_circle(missile.x , missile.y , 2, 0xCCAA00);
tianyeapply 3:7e33224a6f1d 172 missile_set_exploded(i);
tianyeapply 3:7e33224a6f1d 173 city_destroy(k);
tianyeapply 3:7e33224a6f1d 174 num_city--;
tianyeapply 3:7e33224a6f1d 175 uLCD.filled_circle(missile.x , missile.y , 4, 0xCCAA00);
tianyeapply 3:7e33224a6f1d 176 wait(0.4);
tianyeapply 3:7e33224a6f1d 177 circle_explosion(missile.x , missile.y);
tianyeapply 3:7e33224a6f1d 178 uLCD.filled_circle(missile.x , missile.y , 4, BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 179 //count=count-10;
tianyeapply 3:7e33224a6f1d 180 display_score(count);
tianyeapply 3:7e33224a6f1d 181 city_destory();
tianyeapply 3:7e33224a6f1d 182
tianyeapply 3:7e33224a6f1d 183 }
tianyeapply 3:7e33224a6f1d 184 }
tianyeapply 3:7e33224a6f1d 185 }
tianyeapply 3:7e33224a6f1d 186 for(j=0; j<MAX_NUM_PLAYER_MISSILE;j++){
tianyeapply 3:7e33224a6f1d 187
tianyeapply 3:7e33224a6f1d 188 player_missile=player_missile_get_info(j);
tianyeapply 3:7e33224a6f1d 189 int radius_x=abs(missile.x-player_missile.x);
tianyeapply 3:7e33224a6f1d 190 int radius_y=abs(player_missile.y-missile.y);
tianyeapply 3:7e33224a6f1d 191 if(radius_x<delta &&radius_y<delta){
tianyeapply 3:7e33224a6f1d 192
tianyeapply 3:7e33224a6f1d 193 //delta can change depend on current level
tianyeapply 3:7e33224a6f1d 194 if(missile.status==MISSILE_ACTIVE &&player_missile.status==MISSILE_ACTIVE){
tianyeapply 3:7e33224a6f1d 195 uLCD.filled_circle(missile.x , missile.y , 2, 0xC90000);
tianyeapply 3:7e33224a6f1d 196
tianyeapply 3:7e33224a6f1d 197 missile_set_exploded(i);
tianyeapply 3:7e33224a6f1d 198 player_missile_set_exploded(j);
tianyeapply 3:7e33224a6f1d 199 wait(0.2);
tianyeapply 3:7e33224a6f1d 200 uLCD.filled_circle(missile.x , missile.y , 2, BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 201 interval=interval+5;
tianyeapply 3:7e33224a6f1d 202 set_missile_interval(interval);
tianyeapply 3:7e33224a6f1d 203 count++;
tianyeapply 3:7e33224a6f1d 204 display_score(count);
tianyeapply 3:7e33224a6f1d 205 point_get();
tianyeapply 3:7e33224a6f1d 206
tianyeapply 3:7e33224a6f1d 207
tianyeapply 3:7e33224a6f1d 208 }//if
tianyeapply 3:7e33224a6f1d 209
tianyeapply 3:7e33224a6f1d 210 }//if
tianyeapply 3:7e33224a6f1d 211
tianyeapply 3:7e33224a6f1d 212 }//for j
tianyeapply 3:7e33224a6f1d 213 }//for i
tianyeapply 3:7e33224a6f1d 214
arvahsu 0:532cb55d6136 215 /// 9.Implement the code to check the end of game
arvahsu 0:532cb55d6136 216 /// -[Hint] For example, if there is no more city, it should be gameover. <br>
tianyeapply 3:7e33224a6f1d 217 num_missile=num_missile_avi(tem);
tianyeapply 3:7e33224a6f1d 218 seg_driver(5-num_missile%6);
tianyeapply 3:7e33224a6f1d 219 //||num_city==0
tianyeapply 3:7e33224a6f1d 220 if (life==-1||num_city==0){
tianyeapply 3:7e33224a6f1d 221 //player_clear(x,y);
tianyeapply 3:7e33224a6f1d 222 break;}
tianyeapply 3:7e33224a6f1d 223 if(count%10==0 && count!=0&&count>pre_point){
tianyeapply 3:7e33224a6f1d 224 player_set_missile_speed((8-player_speed%8));
tianyeapply 3:7e33224a6f1d 225 char level_up[10];
tianyeapply 3:7e33224a6f1d 226 sprintf(level_up,"%s" ,"Level up");
tianyeapply 3:7e33224a6f1d 227 uLCD.text_string(level_up,0,8,3,0xFF9900);
tianyeapply 3:7e33224a6f1d 228 wait(0.3);
tianyeapply 3:7e33224a6f1d 229
tianyeapply 3:7e33224a6f1d 230 uLCD.text_string(level_up,0,8,3,BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 231 char speed_up[10];
tianyeapply 3:7e33224a6f1d 232 sprintf(speed_up,"%s" ,"Speed up");
tianyeapply 3:7e33224a6f1d 233 uLCD.text_string(speed_up,0,8,3,0xFF9900);
tianyeapply 3:7e33224a6f1d 234 wait(0.2);
tianyeapply 3:7e33224a6f1d 235 uLCD.text_string(level_up,0,8,3,BACKGROUND_COLOR);
tianyeapply 3:7e33224a6f1d 236 playSound("/sd/wavfiles/level.wav");
tianyeapply 3:7e33224a6f1d 237 speed++;interval=interval+5;
tianyeapply 3:7e33224a6f1d 238 level++;
tianyeapply 3:7e33224a6f1d 239 advanced_level(8-speed%8,100-interval%100);
tianyeapply 3:7e33224a6f1d 240 pre_point=count;
tianyeapply 3:7e33224a6f1d 241 }
tianyeapply 3:7e33224a6f1d 242 } //while
tianyeapply 3:7e33224a6f1d 243 uLCD.text_char('g', 6, 2, 0xCCAA00); uLCD.text_char('a', 7, 2, 0xCCAA00); uLCD.text_char('m', 8, 2, 0xCCAA00); uLCD.text_char('e', 9, 2, 0xCCAA00);
tianyeapply 3:7e33224a6f1d 244 uLCD.text_char('o', 6, 4, 0xCCAA00);uLCD.text_char('v', 7, 4, 0xCCAA00);uLCD.text_char('e', 8, 4, 0xCCAA00);uLCD.text_char('r', 9, 4, 0xCCAA00);
tianyeapply 3:7e33224a6f1d 245 playSound("/sd/wavfiles/end.wav");
tianyeapply 3:7e33224a6f1d 246 }//main
arvahsu 0:532cb55d6136 247
arvahsu 0:532cb55d6136 248 // Example of implementation of your functions
arvahsu 0:532cb55d6136 249 void playSound(char * wav)
arvahsu 0:532cb55d6136 250 {
arvahsu 0:532cb55d6136 251 // open wav file
arvahsu 0:532cb55d6136 252 FILE *wave_file;
arvahsu 0:532cb55d6136 253 wave_file=fopen(wav,"r");
arvahsu 0:532cb55d6136 254
arvahsu 0:532cb55d6136 255 // play wav file
arvahsu 0:532cb55d6136 256 waver.play(wave_file);
arvahsu 0:532cb55d6136 257
arvahsu 0:532cb55d6136 258 // close wav file
arvahsu 0:532cb55d6136 259 fclose(wave_file);
arvahsu 0:532cb55d6136 260 }