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: mbed wave_player 4DGL-uLCD-SE MMA8452
graphics.cpp
00001 #include "graphics.h" 00002 00003 #include "globals.h" 00004 00005 #define DARKGREEN 0x154f08 00006 #define N_BROWN 0x493505 00007 00008 00009 00010 00011 char heart[122] = "BBBBBBBBBBB" 00012 "BBPPBBBPPBB" 00013 "BPPPPBPPPPB" 00014 "BPPPPPPPPPB" 00015 "BBPPPPPPPBB" 00016 "BBPPPPPPPBB" 00017 "BBBPPPPPBBB" 00018 "BBBBPPPBBBB" 00019 "BBBBBPBBBBB" 00020 "BBBBBBBBBBB" 00021 "BBBBBBBBBBB"; 00022 00023 char matt[122] = "BBB11111BBB" 00024 "BBB1B1B1BBB" 00025 "BBB11111BBB" 00026 "BBWWWWWWWBB" 00027 "BBW44W44WBB" 00028 "BBW55W55WBB" 00029 "BBW66W66WBB" 00030 "BBW33W33WBB" 00031 "BBB22222BBB" 00032 "BBB22B22BBB" 00033 "BBB22B22BBB"; 00034 char mattwkey[122] = "BBB11111BBB" 00035 "BBB1B1B1BBB" 00036 "BBB11111BBB" 00037 "BBWWWWWWWBB" 00038 "BBW44W44WBB" 00039 "BBW55W55WRR" 00040 "BBW66W66WRR" 00041 "BBW33W33WRR" 00042 "BBB22222BBB" 00043 "BBB22B22BBB" 00044 "BBB22B22BBB"; 00045 00046 char shrub[122] = "BBBBBBBBBBB" 00047 "BBBBBBBBBBB" 00048 "BBBBBBBBBBB" 00049 "BBBBBBBBBBB" 00050 "BBBBBBBBBBB" 00051 "BBBBBBBBBBB" 00052 "BBBBBBBBBBB" 00053 "BBGGBGBBGGG" 00054 "GGGGGGGBGGB" 00055 "BGGGGGGGGGB" 00056 "BBGGGGGGGBB"; 00057 00058 char lbush_str[122] = "BBGGBGBBGGG" 00059 "GGGG6GGBGGB" 00060 "BGGGGGG6GGB" 00061 "BB6GBGBBGGG" 00062 "GGGGG6GBGGB" 00063 "BGGGGGGGGGB" 00064 "BBGG6GGGGBB" 00065 "BBGGB6BBGGG" 00066 "GG6GGGGBGGB" 00067 "BGGGG6GGGGB" 00068 "BBGGGGGGGBB"; 00069 00070 char redman[122] = "BBB77777BBB" 00071 "BBB7B7B7BBB" 00072 "BBB77777BBB" 00073 "BB7RRRRR7BB" 00074 "BB7RRRRR7BB" 00075 "BB7RRRRR7BB" 00076 "BB7RRRRR7BB" 00077 "BB7RRRRR7BB" 00078 "BBBVVVVVBBB" 00079 "BBBVVBVVBBB" 00080 "BBBVVBVVBBB"; 00081 00082 void draw_player(int u, int v, int key) 00083 { 00084 //uLCD.filled_rectangle(u, v, u+11, v+11, RED); 00085 if (key){ 00086 draw_img(u, v, mattwkey); 00087 } else{ 00088 draw_img(u, v, matt); 00089 } 00090 } 00091 00092 00093 void draw_npc(int u, int v) 00094 { 00095 draw_img(u, v, redman); 00096 } 00097 00098 00099 #define BROWN 0xD2691E 00100 #define DIRT BROWN 00101 #define CREAM 0xecd5c3 //1 00102 #define M_BROWN 0x5a300f //2 00103 #define M_BLUE 0x0834db //3 00104 #define M_RED 0xd92626 //4 00105 #define M_YELLOW 0xfff700 //5 00106 #define M_GREEN 0x2ddd03 //6 00107 void draw_img(int u, int v, const char* img) 00108 { 00109 int colors[11*11]; 00110 for (int i = 0; i < 11*11; i++) 00111 { 00112 if (img[i] == 'R') colors[i] = RED; 00113 else if (img[i] == 'Y') colors[i] = YELLOW; 00114 else if (img[i] == 'G') colors[i] = GREEN; 00115 else if (img[i] == 'D') colors[i] = DIRT; 00116 else if (img[i] == '5') colors[i] = LGREY; 00117 else if (img[i] == '3') colors[i] = DGREY; 00118 else if (img[i] == 'P') colors[i] = PINK1; 00119 else if (img[i] == 'V') colors[i] = LIGHTPINK; 00120 else if (img[i] == '1') colors[i] = CREAM; 00121 else if (img[i] == '2') colors[i] = M_BROWN; 00122 else if (img[i] == '3') colors[i] = M_BLUE; 00123 else if (img[i] == '4') colors[i] = M_RED; 00124 else if (img[i] == '5') colors[i] = M_YELLOW; 00125 else if (img[i] == '6') colors[i] = M_GREEN; 00126 else if (img[i] == '7') colors[i] = N_BROWN; 00127 else if (img[i] == 'W') colors[i] = 0xFFFFFF; 00128 else colors[i] = DARKGREEN; 00129 } 00130 uLCD.BLIT(u, v, 11, 11, colors); 00131 wait_us(250); // Recovery time! 00132 } 00133 00134 00135 00136 void draw_nothing(int u, int v) 00137 { 00138 // Fill a tile with blackness 00139 uLCD.filled_rectangle(u, v, u+10, v+10, DARKGREEN); 00140 } 00141 00142 void draw_rhouse1(int u, int v) 00143 { 00144 // Fill a tile with blackness 00145 uLCD.filled_rectangle(u, v, u+10, v+10, RED); 00146 } 00147 00148 void draw_rhouse2(int u, int v) 00149 { 00150 // Fill a tile with blackness 00151 uLCD.filled_rectangle(u, v, u+10, v+10, M_RED); 00152 } 00153 00154 void draw_bhouse1(int u, int v) 00155 { 00156 00157 uLCD.filled_rectangle(u, v, u+10, v+10, BLUE); 00158 } 00159 00160 void draw_bhouse2(int u, int v) 00161 { 00162 00163 uLCD.filled_rectangle(u, v, u+10, v+10, M_BLUE); 00164 } 00165 00166 void draw_house_floor(int u, int v) 00167 { 00168 // Fill a tile with brown (wood) 00169 uLCD.filled_rectangle(u, v, u+10, v+10, BROWN); 00170 } 00171 00172 void draw_wall(int u, int v) 00173 { 00174 uLCD.filled_rectangle(u, v, u+10, v+10, BLACK); 00175 } 00176 00177 void draw_door(int u, int v) 00178 { 00179 uLCD.filled_rectangle(u, v, u+10, v+10, N_BROWN); 00180 } 00181 00182 void draw_plant(int u, int v) 00183 { 00184 draw_img(u, v, shrub); 00185 } 00186 00187 void draw_lbush(int u, int v) 00188 { 00189 draw_img(u, v, lbush_str); 00190 } 00191 00192 00193 #define GRAY2 0x9096B0 00194 void draw_road(int u, int v) 00195 { 00196 00197 uLCD.filled_rectangle(u, v, u+10, v+10, GRAY2); 00198 } 00199 00200 void draw_spike(int u, int v) 00201 { 00202 char spikeg[122] = "BBBBBBBBBBB" 00203 "BBBBBBBBBBB" 00204 "BBBBBBBBBBB" 00205 "BBBBBBBBBBB" 00206 "BBBBBBBBBBB" 00207 "BBBBBBBBBBB" 00208 "BBB3BBBBBBB" 00209 "BB333BB33BB" 00210 "BB333333BBB" 00211 "B333333333B" 00212 "33333333333"; 00213 draw_img(u, v, spikeg); 00214 } 00215 00216 #define GRAY 0x9096A0 00217 void draw_rock(int u, int v) 00218 { 00219 00220 uLCD.filled_rectangle(u, v, u+10, v+10, GRAY); 00221 } 00222 00223 void draw_goal(int u, int v) 00224 { 00225 uLCD.filled_rectangle(u, v, u+10, v+10, PINK1); 00226 } 00227 00228 void draw_gem1(int u, int v) 00229 { 00230 uLCD.filled_circle(u+3, v+3, 3, RED); 00231 } 00232 00233 void draw_gem2(int u, int v) 00234 { 00235 uLCD.filled_circle(u+3, v+3, 3, YELLOW); 00236 } 00237 00238 void draw_gem3(int u, int v) 00239 { 00240 uLCD.filled_circle(u+3, v+3, 3, BLUE); 00241 } 00242 00243 void draw_upper_status(int health) 00244 { 00245 // Draw bottom border of status bar 00246 if (health == 0){ 00247 uLCD.filled_rectangle(health, 9, 127, 0, GREEN); 00248 } else{ 00249 uLCD.filled_rectangle(0, 9, health+10, 0, RED); 00250 } 00251 //draw_img(0, 0, heart); 00252 00253 // Add other status info drawing code here 00254 00255 00256 } 00257 00258 void draw_lower_status(int x, int y) 00259 { 00260 //uLCD.filled_rectangle(0, 128, 128, 110, 0x000000); 00261 // Draw top border of status bar 00262 //uLCD.line(0, 118, 127, 118, GREEN); 00263 00264 // Add other status info drawing code here 00265 char str[30]; 00266 sprintf(str, "Coord: (%d, %d)", x, y); 00267 uLCD.text_mode(OPAQUE); 00268 uLCD.textbackground_color(BLACK); 00269 uLCD.text_string(str, 0, 15, FONT_7X8, 0xFFFFFF); 00270 } 00271 00272 void draw_border() 00273 { 00274 uLCD.filled_rectangle(0, 9, 127, 14, WHITE); // Top 00275 uLCD.filled_rectangle(0, 13, 2, 114, WHITE); // Left 00276 uLCD.filled_rectangle(0, 114, 127, 117, WHITE); // Bottom 00277 uLCD.filled_rectangle(124, 14, 127, 117, WHITE); // Right 00278 } 00279 00280 void draw_game_over(){ 00281 00282 uLCD.filled_rectangle(0, 128, 128, 0, WHITE); 00283 uLCD.locate(1,5); 00284 uLCD.text_mode(TRANSPARENT); 00285 uLCD.color(BLACK); 00286 uLCD.printf("YOU WON!"); 00287 for (int i=0; i<500; i=i+100) { 00288 speaker.period(1.0/float(i)); 00289 speaker=0.25; 00290 wait(.1); 00291 } 00292 speaker=0.0; 00293 wait(2); 00294 } 00295
Generated on Fri Jul 15 2022 03:29:10 by
