Missile Control Game with uLCD
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
Diff: main.cpp
- Revision:
- 2:eba4ed0263a4
- Parent:
- 1:3da29f1d84b4
--- a/main.cpp Wed Oct 29 02:58:53 2014 +0000 +++ b/main.cpp Tue Oct 20 19:13:03 2015 +0000 @@ -1,26 +1,3 @@ -/* Gatech ECE2035 2014 FALL missile command - * Copyright (c) 2014 Gatech ECE2035 - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -/** @file main.cpp */ -// Include header files for platform #include "mbed.h" #include "uLCD_4DGL.h" #include "wave_player.h" @@ -34,23 +11,114 @@ #include "player.h" // Platform initialization -DigitalIn left_pb(p23); DigitalIn right_pb(p21); DigitalIn fire_pb(p22); +DigitalIn left_pb(p23); DigitalIn right_pb(p21); DigitalIn fire_pb(p22); DigitalIn reset_pb(p24); uLCD_4DGL uLCD(p28,p27,p29); AnalogOut DACout(p18); wave_player waver(&DACout); SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs -// Example of the decleration of your implementation + void playSound(char * wav); +//Initialize variables +PLAYER_MISSILE missileArr [5]; +PLAYER player; +int radius = 10; +int missileCount = 5; + + -/** Main() is where you start your implementation - @brief The hints of implementation are in the comments. <br> - @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. -*/ -int main() -{ - // [Demo of 7-segment display] - // 1.Initialize the segment display +//draw the anti missile +void player_missile_draw(PLAYER_MISSILE missile, int color) { + + int init_x, init_y, current_x, current_y; + init_x = missile.source_x; + init_y = 96; + current_x = init_x; + current_y = missile.y; + uLCD.line(init_x, init_y, current_x, current_y, color); + } + // create the anti missile + void player_missile_create(void){ + int z; + for(z=0;z<5;z++){ + if(missileArr[z].status == PLAYER_MISSILE_DEACTIVE){ + missileArr[z].y = 96; + //each missile has its own tick + + //set a random source for the missile + missileArr[z].source_x = player.x + 5; + //set a random target for the missile + missileArr[z].origin_y = 96; + //the missile starts at its source + + missileArr[z].status = PLAYER_MISSILE_ACTIVE; + break; + } + } +} + + + +void player_missile_update_position(void){ + int b; + //controls how fast the missile will move + + //delta_x and delta_y account for the slope of the missile + double delta_y; + for(b=0;b<5;b++){ + if(missileArr[b].status == PLAYER_MISSILE_ACTIVE){ + // update missile position + delta_y = 5; + + + missileArr[b].y = missileArr[b].y - delta_y; + + + // draw missile + player_missile_draw(missileArr[b], PLAYER_COLOR); + } + if(missileArr[b].y < 0) { + + player_missile_draw(missileArr[b], BLACK_COLOR ); + missileArr[b].status = PLAYER_MISSILE_DEACTIVE; + + + + } + else if(missileArr[b].status == PLAYER_MISSILE_EXPLODED){ + // clear the missile on the screen + player_missile_draw(missileArr[b], BLACK_COLOR); + + + // we have done with this missile, remove it from record + missileArr[b].status = PLAYER_MISSILE_DEACTIVE; + //resets the missile's internal tick + missileCount = missileCount + 1; + + + } + } + + } + +void player_set_exploded(int index){ + // Error check + ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_UPDATE_STATUS); + + missileArr[index].status = PLAYER_MISSILE_EXPLODED; + missileCount = missileCount + 1; +} + + + + +int main() { + + uLCD.locate(0, 0); + //uLCD.printf("Lvl:", level()); + //uLCD.printf("Score: %2d", s); + //uLCD.prinf("Missiles: ", + //uLCD.printf("Lives: ", setup_sequence(); seg_driver_initialize(); // 2.show numbers @@ -60,66 +128,236 @@ wait(0.2); } - // [Demo of play sound file] + playSound("/sd/wavfiles/BUZZER.wav"); - /// [Example of missile command implementation] - /// Here is a rough flow to implement the missile command: <br><br> - /// 1.Initialize and draw the city landscape + // Draw cities city_landscape_init(4); // Initialize the buttons left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed fire_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed + reset_pb.mode(PullUp); - /// 2.Begin the game loop + ///Begin the game loop + + player.x = 60; + player_draw(player.x,100); + int d, w, l; + int s = 0; + int cityCount = 4; + int lives = 3; + + while(1){ - /// 3.Example of drawing the player - player_draw(60,100); // draws a player at the center of the screen + + //print Score and Lives + uLCD.locate(0,0); + uLCD.color(0x00FF00); + uLCD.printf("Score: %2d \n", s); + uLCD.printf("Lives: %2d" , lives); - /// 4.Example of calling the missile API. - missile_generator(); /// It updates all incoming missiles on the screen + if (reset_pb == 0) { + while(1) { + wait(1); + if(reset_pb == 0) { + break; + } + } + } + - /// 5.Implement the code to get user input and update the player - /// -[Hint] You could see city_landscape.cpp to get some ideas about how to implement your player. <br> - if(left_pb == 0){ - /// -[Hint] Implement the code to move player left <br> + //If all 4 pushbuttons are pressed, jump to next level + if (((left_pb == 0) && (right_pb == 0) && (fire_pb == 0) && (reset_pb == 0))) { + while(1) { + uLCD.filled_rectangle(0, 0, 128, 128, 0x000000); + uLCD.locate(4, 5); + uLCD.printf("Level 2!! \n \n"); + uLCD.printf("Incoming missiles are faster!"); + + wait(5); + uLCD.locate(4,5); + uLCD.color(0x000000); + uLCD.printf("Level 2!! \n \n"); + uLCD.printf("Incoming missiles are faster!"); + break; + } + city_landscape_init(4); + set_missile_speed(3); + player_draw(player.x,100); + + } + + missileCount = 5; + for(l = 0; l<5; l++) { + if(missileArr[l].status == PLAYER_MISSILE_ACTIVE) { + missileCount = missileCount - 1; + } + seg_driver(missileCount); + } + + + + + + missile_generator(); /// updates all incoming missiles on the screen + + + if(left_pb == 0 && player.x>0){ + //Move player left + player_draw(player.x,100); + player_delete(player.x,100); + player.x = player.x - 10; + player_draw(player.x,100); + } - if(right_pb == 0){ - /// -[Hint] Implement the code to move player right <br> + if(right_pb == 0 && player.x<110){ + //Move player right + player_draw(player.x,100); + player_delete(player.x,100); + player.x = player.x + 10; + player_draw(player.x,100); } if(fire_pb == 0){ - /// -[Hint] Implement the code to fire player-missile <br> + + + //fire missile and make laser sound + player_missile_create(); + + // + playSound("/sd/wavfiles/LASER.wav"); + } + player_missile_update_position(); - // [Demo of play sound file] - playSound("/sd/wavfiles/BUZZER.wav"); + //missile-player collision + for ( int p=0; p<5; p++) { + MISSILE missile_record = missile_get_info(p); + + int q,w, r, t; + q = player.x + 10; + w = 100; + r = player.x; + t = 96; + if((missile_record.x > r & missile_record.x<q) & (missile_record.y<w & missile_record.y>t & (missile_record.status==MISSILE_ACTIVE))) { + lives -= 1; + missile_set_exploded(p); + wait(1); + if(lives == 0) { + gameover(s); + + break; + } + + for(int j=0; j<11; j=j+3) { + uLCD.circle(missile_record.x, missile_record.y, j, YELLOW_COLOR); + uLCD.circle(missile_record.x, missile_record.y, j, BLACK_COLOR); + + + } + + } - - /// 6.Implement the code to draw a user missile - /// -[Hint] You could see missile.cpp or missile_generator() for your reference <br> - - /// 7.Implement the code to detect the collision between missiles and cities - /// -[Hint] You could use city_get_info() and missile_get_info() <br> - /// -[Hint] You could use missile_set_exploded() to notify the missile module that the missile was exploded. <br> - /// -[Hint] You need to check which city was hit by the missile, and call city_destroy() to destroy it. <br> - /// -[Hint] You might also check whether the missile hit the land <br> - - /// 8.Implement the code to detect a collision between player-missiles and incoming missiles - /// -[Hint] You could use missile_get_info() to get the position of incoming missile <br> - /// -[Hint] You could use missile_set_exploded() to notify the missile module that the missile was exploded. <br> - /// -[Hint] You might also check whether the missile hit the player <br> - - /// 9.Implement the code to check the end of game - /// -[Hint] For example, if there is no more city, it should be gameover. <br> - } -} + + + + //missile-city collision + for ( int e=0; e<5; e++) { + MISSILE missile_record = missile_get_info(e); + for (int d = 0; d<5; d++) { + CITY city_record = city_get_info(d); + int f, g, h, i; + f = city_record.x + city_record.width; + g = city_record.y - city_record.height; + h = city_record.x; + i = city_record.y; + if((missile_record.x > h & missile_record.x<f) & (missile_record.y<i & missile_record.y>g) & (city_record.status == EXIST)) { + for(int j=0; j<11; j=j+3) { + uLCD.circle(missile_record.x, missile_record.y, j, YELLOW_COLOR); + uLCD.circle(missile_record.x, missile_record.y, j, BLACK_COLOR); + + + } + missile_set_exploded(e); + city_destroy(d); + cityCount = cityCount -1; + if(cityCount == 0) { + gameover(s); + } + + break; + + } + + } + + + } + + + + + + //missile-player_missile collision + for(d=0; d<5; d++) { + for(w=0; w<5; w++) { + MISSILE missile_record = missile_get_info(w); + double f; + f = (int)sqrt(pow((double)(missile_record.x-missileArr[d].source_x),2.0) + pow((double)(missile_record.y-missileArr[d].y),2.0)); + + if((f<radius) && (missile_record.status == MISSILE_ACTIVE) && (missileArr[d].status== PLAYER_MISSILE_ACTIVE)) { + for (int j=0; j< (radius+1); j = j+3) { + uLCD.circle(missile_record.x, missile_record.y, j, 0xFFFF00); + + } + for (int j=0; j<(radius+1); j=j+3) { + uLCD.circle(missile_record.x, missile_record.y, j, BLACK_COLOR); + + } + missile_set_exploded(w); + + player_set_exploded(d); + playSound("/sd/wavfiles/hit.wav"); + s= s+1; + + if (s==10 ) { + while(1) { + uLCD.filled_rectangle(0, 0, 128, 128, 0x000000); + uLCD.locate(4, 5); + uLCD.printf("Level 2!! \n \n"); + uLCD.printf("Incoming missiles are faster!"); + + wait(5); + uLCD.locate(4,5); + uLCD.color(0x000000); + uLCD.printf("Level 2!! \n \n"); + uLCD.printf("Incoming missiles are faster!"); + break; + } + city_landscape_init(4); + set_missile_speed(3); + player_draw(player.x,100); + + } + + + } + + } + + } + + + + + } + } -// Example of implementation of your functions -void playSound(char * wav) -{ + + +void playSound(char * wav) { // open wav file FILE *wave_file; wave_file=fopen(wav,"r"); @@ -130,3 +368,4 @@ // close wav file fclose(wave_file); } +