Missile Control Game with uLCD
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
city_landscape/city_landscape.cpp@2:eba4ed0263a4, 2015-10-20 (annotated)
- Committer:
- ajindia6
- Date:
- Tue Oct 20 19:13:03 2015 +0000
- Revision:
- 2:eba4ed0263a4
- Parent:
- 1:3da29f1d84b4
Mbed Mission Control Game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arvahsu | 0:532cb55d6136 | 1 | #include "city_landscape_private.h" |
arvahsu | 0:532cb55d6136 | 2 | |
arvahsu | 0:532cb55d6136 | 3 | CITY city_record[MAX_NUM_CITY]; |
arvahsu | 0:532cb55d6136 | 4 | int building_height[NUM_BUILDING]; |
arvahsu | 0:532cb55d6136 | 5 | |
arvahsu | 0:532cb55d6136 | 6 | // See the comments in city_landscape_public.h |
arvahsu | 0:532cb55d6136 | 7 | void city_landscape_init(int num_city) { |
arvahsu | 0:532cb55d6136 | 8 | int i; |
arvahsu | 0:532cb55d6136 | 9 | int city_distance = (SIZE_X-CITY_TO_SCREEN_MARGIN*2)/num_city; |
arvahsu | 0:532cb55d6136 | 10 | |
arvahsu | 0:532cb55d6136 | 11 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 12 | ASSERT_P(num_city<=MAX_NUM_CITY,ERROR_CITY_NUMBER); |
arvahsu | 0:532cb55d6136 | 13 | |
arvahsu | 0:532cb55d6136 | 14 | //initialize the record of cities |
arvahsu | 0:532cb55d6136 | 15 | for(i=0;i<MAX_NUM_CITY;i++){ |
arvahsu | 0:532cb55d6136 | 16 | if(i<num_city){ |
arvahsu | 0:532cb55d6136 | 17 | // See the definition of CITY structure in city_landscape.h |
arvahsu | 0:532cb55d6136 | 18 | city_record[i].y = REVERSE_Y(LANDSCAPE_HEIGHT)-1; |
arvahsu | 0:532cb55d6136 | 19 | city_record[i].x = i*city_distance + CITY_TO_SCREEN_MARGIN; |
arvahsu | 0:532cb55d6136 | 20 | city_record[i].width = CITY_WIDTH; // the width is fix number |
arvahsu | 0:532cb55d6136 | 21 | city_record[i].height = MAX_BUILDING_HEIGHT; // the height is fix number |
arvahsu | 0:532cb55d6136 | 22 | city_record[i].status = EXIST; |
arvahsu | 0:532cb55d6136 | 23 | } |
arvahsu | 0:532cb55d6136 | 24 | else{ |
arvahsu | 1:3da29f1d84b4 | 25 | city_record[i].status = DESTROYED; |
arvahsu | 0:532cb55d6136 | 26 | } |
arvahsu | 0:532cb55d6136 | 27 | } |
arvahsu | 0:532cb55d6136 | 28 | |
arvahsu | 0:532cb55d6136 | 29 | //initialize the height of the buildings |
arvahsu | 0:532cb55d6136 | 30 | srand(1); |
arvahsu | 0:532cb55d6136 | 31 | for(i=0;i<NUM_BUILDING;i++){ |
arvahsu | 0:532cb55d6136 | 32 | building_height[i] = (rand() % MAX_BUILDING_HEIGHT*2/3)+MAX_BUILDING_HEIGHT/3; |
arvahsu | 0:532cb55d6136 | 33 | } |
arvahsu | 0:532cb55d6136 | 34 | |
arvahsu | 0:532cb55d6136 | 35 | //draw city landscape on the screen |
arvahsu | 0:532cb55d6136 | 36 | draw_cities(); |
arvahsu | 0:532cb55d6136 | 37 | draw_landscape(); |
arvahsu | 0:532cb55d6136 | 38 | |
arvahsu | 0:532cb55d6136 | 39 | } |
arvahsu | 0:532cb55d6136 | 40 | |
arvahsu | 0:532cb55d6136 | 41 | CITY city_get_info(int index){ |
arvahsu | 0:532cb55d6136 | 42 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 43 | ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_GET_INFO); |
arvahsu | 0:532cb55d6136 | 44 | |
arvahsu | 0:532cb55d6136 | 45 | return city_record[index]; |
arvahsu | 0:532cb55d6136 | 46 | } |
arvahsu | 0:532cb55d6136 | 47 | |
arvahsu | 1:3da29f1d84b4 | 48 | void city_destroy(int index){ |
arvahsu | 0:532cb55d6136 | 49 | int j; |
arvahsu | 0:532cb55d6136 | 50 | int city_x, city_y, building_x, building_y; |
arvahsu | 0:532cb55d6136 | 51 | int height; |
arvahsu | 0:532cb55d6136 | 52 | |
arvahsu | 0:532cb55d6136 | 53 | // error checking. the index must smaller than its max. |
arvahsu | 0:532cb55d6136 | 54 | ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_DESTROY); |
arvahsu | 0:532cb55d6136 | 55 | |
arvahsu | 0:532cb55d6136 | 56 | // remove the record |
arvahsu | 1:3da29f1d84b4 | 57 | city_record[index].status = DESTROYED; |
arvahsu | 0:532cb55d6136 | 58 | |
arvahsu | 0:532cb55d6136 | 59 | // use the background color to cover the city |
arvahsu | 0:532cb55d6136 | 60 | city_x = city_record[index].x; |
arvahsu | 0:532cb55d6136 | 61 | city_y = city_record[index].y; |
arvahsu | 0:532cb55d6136 | 62 | for(j=0;j<NUM_BUILDING;j++){ |
arvahsu | 0:532cb55d6136 | 63 | building_x = city_x+j*BUILDING_WIDTH; |
arvahsu | 0:532cb55d6136 | 64 | building_y = city_y; |
arvahsu | 0:532cb55d6136 | 65 | height = building_y-building_height[j]+1; |
arvahsu | 0:532cb55d6136 | 66 | uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BACKGROUND_COLOR); |
arvahsu | 0:532cb55d6136 | 67 | } |
arvahsu | 0:532cb55d6136 | 68 | } |
arvahsu | 0:532cb55d6136 | 69 | |
arvahsu | 0:532cb55d6136 | 70 | void draw_cities(void){ |
arvahsu | 0:532cb55d6136 | 71 | int i,j; |
arvahsu | 0:532cb55d6136 | 72 | int city_x, city_y, building_x, building_y; |
arvahsu | 0:532cb55d6136 | 73 | int height; |
arvahsu | 0:532cb55d6136 | 74 | |
arvahsu | 0:532cb55d6136 | 75 | for(i=0;i<MAX_NUM_CITY;i++){ |
arvahsu | 0:532cb55d6136 | 76 | |
arvahsu | 0:532cb55d6136 | 77 | // draw each city |
arvahsu | 0:532cb55d6136 | 78 | if(city_record[i].status==EXIST){ |
arvahsu | 0:532cb55d6136 | 79 | city_x = city_record[i].x; |
arvahsu | 0:532cb55d6136 | 80 | city_y = city_record[i].y; |
arvahsu | 0:532cb55d6136 | 81 | |
arvahsu | 0:532cb55d6136 | 82 | // draw each building |
arvahsu | 0:532cb55d6136 | 83 | for(j=0;j<NUM_BUILDING;j++){ |
arvahsu | 0:532cb55d6136 | 84 | building_x = city_x+j*BUILDING_WIDTH; |
arvahsu | 0:532cb55d6136 | 85 | building_y = city_y; |
arvahsu | 0:532cb55d6136 | 86 | height = building_y-building_height[j]+1; |
arvahsu | 0:532cb55d6136 | 87 | uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BUILDING_COLOR); |
arvahsu | 0:532cb55d6136 | 88 | } |
arvahsu | 0:532cb55d6136 | 89 | } |
arvahsu | 0:532cb55d6136 | 90 | } |
arvahsu | 0:532cb55d6136 | 91 | } |
arvahsu | 0:532cb55d6136 | 92 | |
arvahsu | 0:532cb55d6136 | 93 | void draw_landscape(void){ |
arvahsu | 0:532cb55d6136 | 94 | uLCD.filled_rectangle(0, SIZE_Y-1, SIZE_X-1, REVERSE_Y(LANDSCAPE_HEIGHT), LANDSCAPE_COLOR); |
arvahsu | 0:532cb55d6136 | 95 | } |