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
city_landscape.cpp
00001 #include "city_landscape_private.h" 00002 00003 CITY city_record[MAX_NUM_CITY]; 00004 int building_height[NUM_BUILDING]; 00005 00006 // See the comments in city_landscape_public.h 00007 void city_landscape_init(int num_city) { 00008 int i; 00009 int city_distance = (SIZE_X-CITY_TO_SCREEN_MARGIN*2)/num_city; 00010 00011 // All interface for user should have error checking 00012 ASSERT_P(num_city<=MAX_NUM_CITY,ERROR_CITY_NUMBER); 00013 00014 //initialize the record of cities 00015 for(i=0;i<MAX_NUM_CITY;i++){ 00016 if(i<num_city){ 00017 // See the definition of CITY structure in city_landscape.h 00018 city_record[i].y = REVERSE_Y(LANDSCAPE_HEIGHT)-1; 00019 city_record[i].x = i*city_distance + CITY_TO_SCREEN_MARGIN; 00020 city_record[i].width = CITY_WIDTH; // the width is fix number 00021 city_record[i].height = MAX_BUILDING_HEIGHT; // the height is fix number 00022 city_record[i].status = EXIST; 00023 } 00024 else{ 00025 city_record[i].status = DESTROYED; 00026 } 00027 } 00028 00029 //initialize the height of the buildings 00030 srand(1); 00031 for(i=0;i<NUM_BUILDING;i++){ 00032 building_height[i] = (rand() % MAX_BUILDING_HEIGHT*2/3)+MAX_BUILDING_HEIGHT/3; 00033 } 00034 00035 //draw city landscape on the screen 00036 draw_cities(); 00037 draw_landscape(); 00038 00039 } 00040 00041 CITY city_get_info(int index){ 00042 // All interface for user should have error checking 00043 ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_GET_INFO); 00044 00045 return city_record[index]; 00046 } 00047 00048 void city_destroy(int index){ 00049 int j; 00050 int city_x, city_y, building_x, building_y; 00051 int height; 00052 00053 // error checking. the index must smaller than its max. 00054 ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_DESTROY); 00055 00056 // remove the record 00057 city_record[index].status = DESTROYED; 00058 00059 // use the background color to cover the city 00060 city_x = city_record[index].x; 00061 city_y = city_record[index].y; 00062 for(j=0;j<NUM_BUILDING;j++){ 00063 building_x = city_x+j*BUILDING_WIDTH; 00064 building_y = city_y; 00065 height = building_y-building_height[j]+1; 00066 uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BACKGROUND_COLOR); 00067 } 00068 } 00069 00070 void draw_cities(void){ 00071 int i,j; 00072 int city_x, city_y, building_x, building_y; 00073 int height; 00074 00075 for(i=0;i<MAX_NUM_CITY;i++){ 00076 00077 // draw each city 00078 if(city_record[i].status==EXIST){ 00079 city_x = city_record[i].x; 00080 city_y = city_record[i].y; 00081 00082 // draw each building 00083 for(j=0;j<NUM_BUILDING;j++){ 00084 building_x = city_x+j*BUILDING_WIDTH; 00085 building_y = city_y; 00086 height = building_y-building_height[j]+1; 00087 uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BUILDING_COLOR); 00088 } 00089 } 00090 } 00091 } 00092 00093 void draw_landscape(void){ 00094 uLCD.filled_rectangle(0, SIZE_Y-1, SIZE_X-1, REVERSE_Y(LANDSCAPE_HEIGHT), LANDSCAPE_COLOR); 00095 }
Generated on Wed Jul 13 2022 03:12:57 by
1.7.2
