Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
missile.cpp
00001 #include "missile_private.h" 00002 00003 MISSILE missile_record[MAX_NUM_MISSILE]; 00004 int missile_tick=0; 00005 int missile_interval = 10; 00006 int missile_speed = 4; 00007 00008 // See the comments in missile_public.h 00009 void missile_generator(void){ 00010 missile_tick++; 00011 // only fire the missile at certain ticks 00012 if((missile_tick % missile_interval)==0 || missile_tick==0){ 00013 missile_create(); 00014 } 00015 00016 // update the missiles and draw them 00017 missile_update_position(); 00018 } 00019 00020 // set missile speed (default speed is 4) 00021 void set_missile_speed(int speed){ 00022 ASSERT_P(speed>=1 && speed<=8,ERROR_MISSILE_SPEED); 00023 if(speed>=1 && speed<=8){ 00024 missile_speed = speed; 00025 } 00026 } 00027 00028 // set missile interval (default interval is 10) 00029 void set_missile_interval(int interval){ 00030 ASSERT_P(interval>=1 && interval<=100,ERROR_MISSILE_INTERVAL); 00031 if(interval>=1 && interval<=100){ 00032 missile_interval = interval; 00033 } 00034 } 00035 00036 // See the comments in missile_public.h 00037 MISSILE missile_get_info(int index){ 00038 // All interface for user should have error checking 00039 ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_GET_INFO); 00040 00041 return missile_record[index]; 00042 } 00043 00044 // See the comments in missile_public.h 00045 void missile_set_exploded(int index){ 00046 // All interface for user should have error checking 00047 ASSERT_P(index<MAX_NUM_MISSILE,ERROR_MISSILE_INDEX_UPDATE_STATUS); 00048 00049 missile_record[index].status = MISSILE_EXPLODED; 00050 } 00051 00052 /** This function finds an empty slot of missile record, and active it. 00053 */ 00054 void missile_create(void){ 00055 int i; 00056 for(i=0;i<MAX_NUM_MISSILE;i++){ 00057 if(missile_record[i].status == MISSILE_DEACTIVE){ 00058 missile_record[i].y = 0; 00059 //each missile has its own tick 00060 missile_record[i].tick = 0; 00061 //set a random source for the missile 00062 missile_record[i].source_x = rand() % SIZE_X; 00063 //set a random target for the missile 00064 missile_record[i].target_x = rand() % SIZE_X; 00065 //the missile starts at its source 00066 missile_record[i].x = missile_record[i].source_x; 00067 missile_record[i].status = MISSILE_ACTIVE; 00068 break; 00069 } 00070 } 00071 } 00072 00073 /** This function update the position of all missiles and draw them 00074 */ 00075 void missile_update_position(void){ 00076 int i; 00077 //controls how fast the missile will move 00078 int rate = missile_speed * 25; 00079 //delta_x and delta_y account for the slope of the missile 00080 double delta_x,delta_y; 00081 for(i=0;i<MAX_NUM_MISSILE;i++){ 00082 if(missile_record[i].status == MISSILE_ACTIVE){ 00083 // update missile position 00084 delta_y = 200/rate; 00085 delta_x = (missile_record[i].target_x - missile_record[i].source_x)/rate; 00086 missile_draw(missile_record[i], BACKGROUND_COLOR); 00087 missile_record[i].y = (int)(delta_y*(missile_record[i].tick%rate)); 00088 missile_record[i].x = (int)(missile_record[i].source_x + delta_x*(missile_record[i].tick%rate)); 00089 // draw missile 00090 missile_draw(missile_record[i], MISSILE_COLOR); 00091 //update missile's internal tick 00092 missile_record[i].tick++; 00093 } 00094 else if(missile_record[i].status == MISSILE_EXPLODED){ 00095 // clear the missile on the screen 00096 missile_draw(missile_record[i], BACKGROUND_COLOR); 00097 00098 // we have done with this missile, remove it from record 00099 missile_record[i].status = MISSILE_DEACTIVE; 00100 //resets the missile's internal tick 00101 missile_record[i].tick = 0; 00102 } 00103 00104 } 00105 } 00106 00107 /** This function draw a missile. 00108 @param missile The missile to be drawn 00109 @param color The color of the missile 00110 */ 00111 void missile_draw(MISSILE missile, int color){ 00112 int init_x,init_y,current_x,current_y; 00113 00114 init_x = missile.source_x; 00115 init_y = 0; 00116 current_x = missile.x; 00117 current_y = missile.y; 00118 uLCD.line(init_x, init_y, current_x, current_y, color); 00119 } 00120 00121
Generated on Wed Jul 13 2022 03:12:58 by
1.7.2
