Code for 4180 mini project
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Fork of Pacman by
main.cpp@2:610d5194c64e, 2015-10-21 (annotated)
- Committer:
- rollschild
- Date:
- Wed Oct 21 19:53:10 2015 +0000
- Revision:
- 2:610d5194c64e
- Parent:
- 1:b86030cf57c4
Code for 4180 lab4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
srigdon3 | 0:0a900ff9a788 | 1 | #include "mbed.h" |
srigdon3 | 0:0a900ff9a788 | 2 | #include "rtos.h" |
srigdon3 | 0:0a900ff9a788 | 3 | #include "uLCD_4DGL.h" |
rollschild | 2:610d5194c64e | 4 | #include "SDFileSystem.h" |
rollschild | 2:610d5194c64e | 5 | #include "wave_player.h" |
srigdon3 | 0:0a900ff9a788 | 6 | |
rollschild | 2:610d5194c64e | 7 | #define PAC_SIZE 5 // The radius of Pacman and the ghost |
rollschild | 2:610d5194c64e | 8 | #define STEP_SIZE 8 // The number of pixels each character moves at once |
rollschild | 2:610d5194c64e | 9 | #define CLEARANCE 12 // The number of pixels each character checks ahead before moving |
srigdon3 | 0:0a900ff9a788 | 10 | |
rollschild | 2:610d5194c64e | 11 | DigitalIn left_pb(p21); // push button |
rollschild | 2:610d5194c64e | 12 | DigitalIn right_pb(p22); // push button |
rollschild | 2:610d5194c64e | 13 | DigitalIn up_pb(p23); // push button |
rollschild | 2:610d5194c64e | 14 | DigitalIn down_pb(p24); // push button |
rollschild | 2:610d5194c64e | 15 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
rollschild | 2:610d5194c64e | 16 | AnalogOut DACout(p18); |
rollschild | 2:610d5194c64e | 17 | wave_player waver(&DACout); // wave player |
srigdon3 | 0:0a900ff9a788 | 18 | uLCD_4DGL uLCD(p28, p27, p29); |
srigdon3 | 0:0a900ff9a788 | 19 | Mutex lcd_mutex; |
srigdon3 | 0:0a900ff9a788 | 20 | |
rollschild | 2:610d5194c64e | 21 | void checkMOVE(void); // This function is defined below. It was written here since other functions return to it that it also calls. |
srigdon3 | 0:0a900ff9a788 | 22 | |
rollschild | 2:610d5194c64e | 23 | // several variables are used by multiple threads |
rollschild | 2:610d5194c64e | 24 | volatile bool win=false; // True when pacman has eaten all coins |
rollschild | 2:610d5194c64e | 25 | volatile bool lose=false; // True when the position of the ghost and pacman are the same |
rollschild | 2:610d5194c64e | 26 | volatile int x = 64; // x and y are pacman's position. The starting position is defined here. |
srigdon3 | 0:0a900ff9a788 | 27 | volatile int y = 88; |
rollschild | 2:610d5194c64e | 28 | volatile int gx1 = 64; // Starting position of the blue ghost |
srigdon3 | 0:0a900ff9a788 | 29 | volatile int gy1 = 40; |
srigdon3 | 0:0a900ff9a788 | 30 | int i; |
srigdon3 | 0:0a900ff9a788 | 31 | bool clearRIGHT,clearLEFT,clearUP,clearDOWN,bgcr,bgcl,bgcu,bgcd; |
srigdon3 | 1:b86030cf57c4 | 32 | |
rollschild | 2:610d5194c64e | 33 | // An array containing the locations of the 81 coins pacman must eat |
srigdon3 | 0:0a900ff9a788 | 34 | int coins[81][2] = { |
srigdon3 | 0:0a900ff9a788 | 35 | {40,88},{48,88},{56,88},{72,88},{80,88},{88,88}, |
srigdon3 | 0:0a900ff9a788 | 36 | {40,40},{48,40},{56,40},{64,40},{72,40},{80,40},{88,40}, |
srigdon3 | 0:0a900ff9a788 | 37 | {40,48},{40,56},{40,64},{40,72},{40,80}, |
srigdon3 | 0:0a900ff9a788 | 38 | {88,48},{88,56},{88,64},{88,72},{88,80}, |
srigdon3 | 0:0a900ff9a788 | 39 | {56,96},{56,104},{56,112}, |
srigdon3 | 0:0a900ff9a788 | 40 | {48,112},{40,112},{32,112},{24,112},{16,112}, |
srigdon3 | 0:0a900ff9a788 | 41 | {16,104},{16,96},{16,88},{16,80},{16,72}, |
srigdon3 | 0:0a900ff9a788 | 42 | {24,64},{32,64}, |
srigdon3 | 0:0a900ff9a788 | 43 | {16,64},{16,56},{16,48},{16,40},{16,32},{16,24},{16,16}, |
srigdon3 | 0:0a900ff9a788 | 44 | {24,16},{32,16},{40,16},{48,16},{56,16}, |
srigdon3 | 0:0a900ff9a788 | 45 | {56,24},{56,32}, |
srigdon3 | 0:0a900ff9a788 | 46 | {72,96},{72,104},{72,112}, |
srigdon3 | 0:0a900ff9a788 | 47 | {80,112},{88,112},{96,112},{104,112},{112,112}, |
srigdon3 | 0:0a900ff9a788 | 48 | {112,104},{112,96},{112,88},{112,80},{112,72}, |
srigdon3 | 0:0a900ff9a788 | 49 | {104,64},{96,64}, |
srigdon3 | 0:0a900ff9a788 | 50 | {112,64},{112,56},{112,48},{112,40},{112,32},{112,24},{112,16}, |
srigdon3 | 0:0a900ff9a788 | 51 | {104,16},{96,16},{88,16},{80,16},{72,16}, |
srigdon3 | 0:0a900ff9a788 | 52 | {72,24},{72,32} |
srigdon3 | 0:0a900ff9a788 | 53 | }; |
srigdon3 | 0:0a900ff9a788 | 54 | |
rollschild | 2:610d5194c64e | 55 | // This function is used in the ghost thread to replace coins as it passes over them |
srigdon3 | 0:0a900ff9a788 | 56 | void replaceCOINS(void) |
srigdon3 | 0:0a900ff9a788 | 57 | { |
srigdon3 | 0:0a900ff9a788 | 58 | for(int n=0; n<81; n++) |
srigdon3 | 0:0a900ff9a788 | 59 | { |
srigdon3 | 1:b86030cf57c4 | 60 | lcd_mutex.lock(); //The coins array is used by both threads |
srigdon3 | 0:0a900ff9a788 | 61 | if(gx1 == coins[n][0] && gy1 == coins[n][1]) |
srigdon3 | 0:0a900ff9a788 | 62 | { |
srigdon3 | 1:b86030cf57c4 | 63 | uLCD.filled_circle(gx1,gy1,1,0xFFFF00); //compare the set of coins to the ghost's previous position and if there is a match redraw coin |
srigdon3 | 0:0a900ff9a788 | 64 | } |
srigdon3 | 0:0a900ff9a788 | 65 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 66 | } |
srigdon3 | 0:0a900ff9a788 | 67 | } |
srigdon3 | 0:0a900ff9a788 | 68 | |
rollschild | 2:610d5194c64e | 69 | // Checks if the ghost can move right (there is no boundary immediately to the right) |
srigdon3 | 0:0a900ff9a788 | 70 | void BGclearRIGHT(void) |
srigdon3 | 0:0a900ff9a788 | 71 | { |
srigdon3 | 0:0a900ff9a788 | 72 | bgcr = true; |
srigdon3 | 0:0a900ff9a788 | 73 | for(int p=gx1; p <= gx1+CLEARANCE; p++) |
srigdon3 | 0:0a900ff9a788 | 74 | { |
srigdon3 | 0:0a900ff9a788 | 75 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 76 | if(uLCD.read_pixel(p,gy1)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 77 | { |
rollschild | 2:610d5194c64e | 78 | bgcr = false; // compare the pixels immediately in front of the ghost to the boundary up to the spec. clearance |
rollschild | 2:610d5194c64e | 79 | } // if they are the same color, determine the ghost can't move right |
srigdon3 | 0:0a900ff9a788 | 80 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 81 | } |
srigdon3 | 0:0a900ff9a788 | 82 | } |
srigdon3 | 0:0a900ff9a788 | 83 | |
srigdon3 | 1:b86030cf57c4 | 84 | //Checks if ghost can move left |
srigdon3 | 0:0a900ff9a788 | 85 | void BGclearLEFT(void) |
srigdon3 | 0:0a900ff9a788 | 86 | { |
srigdon3 | 0:0a900ff9a788 | 87 | bgcl = true; |
srigdon3 | 0:0a900ff9a788 | 88 | for(int p=gx1; p >= gx1-CLEARANCE; p--) |
srigdon3 | 0:0a900ff9a788 | 89 | { |
srigdon3 | 0:0a900ff9a788 | 90 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 91 | if(uLCD.read_pixel(p,gy1)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 92 | { |
srigdon3 | 0:0a900ff9a788 | 93 | bgcl = false; |
srigdon3 | 0:0a900ff9a788 | 94 | } |
srigdon3 | 0:0a900ff9a788 | 95 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 96 | } |
srigdon3 | 0:0a900ff9a788 | 97 | } |
srigdon3 | 0:0a900ff9a788 | 98 | |
srigdon3 | 1:b86030cf57c4 | 99 | //Checks if ghost can move up |
srigdon3 | 0:0a900ff9a788 | 100 | void BGclearUP(void) |
srigdon3 | 0:0a900ff9a788 | 101 | { |
srigdon3 | 0:0a900ff9a788 | 102 | bgcu = true; |
srigdon3 | 0:0a900ff9a788 | 103 | for(int p=gy1; p >= gy1-CLEARANCE; p--) |
srigdon3 | 0:0a900ff9a788 | 104 | { |
srigdon3 | 0:0a900ff9a788 | 105 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 106 | if(uLCD.read_pixel(gx1,p)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 107 | { |
srigdon3 | 0:0a900ff9a788 | 108 | bgcu = false; |
srigdon3 | 0:0a900ff9a788 | 109 | } |
srigdon3 | 0:0a900ff9a788 | 110 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 111 | } |
srigdon3 | 0:0a900ff9a788 | 112 | } |
srigdon3 | 0:0a900ff9a788 | 113 | |
srigdon3 | 1:b86030cf57c4 | 114 | //Checks if ghost can move down |
srigdon3 | 0:0a900ff9a788 | 115 | void BGclearDOWN(void) |
srigdon3 | 0:0a900ff9a788 | 116 | { |
srigdon3 | 0:0a900ff9a788 | 117 | bgcd = true; |
srigdon3 | 0:0a900ff9a788 | 118 | for(int p=gy1; p <= gy1+CLEARANCE; p++) |
srigdon3 | 0:0a900ff9a788 | 119 | { |
srigdon3 | 0:0a900ff9a788 | 120 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 121 | if(uLCD.read_pixel(gx1,p)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 122 | { |
srigdon3 | 0:0a900ff9a788 | 123 | bgcd = false; |
srigdon3 | 0:0a900ff9a788 | 124 | } |
srigdon3 | 0:0a900ff9a788 | 125 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 126 | } |
srigdon3 | 0:0a900ff9a788 | 127 | } |
srigdon3 | 0:0a900ff9a788 | 128 | |
srigdon3 | 1:b86030cf57c4 | 129 | //Moves the blue ghost to the right |
srigdon3 | 0:0a900ff9a788 | 130 | void bgRIGHT(void) |
srigdon3 | 0:0a900ff9a788 | 131 | { |
srigdon3 | 0:0a900ff9a788 | 132 | Thread::wait(50); |
srigdon3 | 0:0a900ff9a788 | 133 | lcd_mutex.lock(); |
srigdon3 | 1:b86030cf57c4 | 134 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1-PAC_SIZE,gx1+PAC_SIZE,gy1+PAC_SIZE,BLACK); //erase the previous ghost drawing |
srigdon3 | 0:0a900ff9a788 | 135 | lcd_mutex.unlock(); |
srigdon3 | 1:b86030cf57c4 | 136 | replaceCOINS(); //replace the coin the ghost was just on if there was one |
srigdon3 | 1:b86030cf57c4 | 137 | if(gx1>124) //This will cause the ghost to wrap around to the left side of the screen if there were no boundary on the far right |
srigdon3 | 0:0a900ff9a788 | 138 | { |
srigdon3 | 0:0a900ff9a788 | 139 | gx1 = 0; |
srigdon3 | 0:0a900ff9a788 | 140 | } |
srigdon3 | 1:b86030cf57c4 | 141 | gx1 = gx1+STEP_SIZE; //Move one step size in the x direction |
srigdon3 | 0:0a900ff9a788 | 142 | lcd_mutex.lock(); |
srigdon3 | 1:b86030cf57c4 | 143 | //redraw the ghost at the new position |
srigdon3 | 0:0a900ff9a788 | 144 | uLCD.filled_circle(gx1,gy1,PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 145 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1,gx1+PAC_SIZE,gy1+PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 146 | uLCD.filled_circle(gx1+2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 147 | uLCD.filled_circle(gx1-2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 148 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 149 | } |
srigdon3 | 0:0a900ff9a788 | 150 | |
srigdon3 | 1:b86030cf57c4 | 151 | //Moves the blue ghost left |
srigdon3 | 0:0a900ff9a788 | 152 | void bgLEFT(void) |
srigdon3 | 0:0a900ff9a788 | 153 | { |
srigdon3 | 0:0a900ff9a788 | 154 | Thread::wait(50); |
srigdon3 | 0:0a900ff9a788 | 155 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 156 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1-PAC_SIZE,gx1+PAC_SIZE,gy1+PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 157 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 158 | replaceCOINS(); |
srigdon3 | 0:0a900ff9a788 | 159 | if(gx1<4) |
srigdon3 | 0:0a900ff9a788 | 160 | { |
srigdon3 | 0:0a900ff9a788 | 161 | gx1 = 124; |
srigdon3 | 0:0a900ff9a788 | 162 | } |
srigdon3 | 0:0a900ff9a788 | 163 | gx1 = gx1-STEP_SIZE; |
srigdon3 | 0:0a900ff9a788 | 164 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 165 | uLCD.filled_circle(gx1,gy1,PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 166 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1,gx1+PAC_SIZE,gy1+PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 167 | uLCD.filled_circle(gx1+2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 168 | uLCD.filled_circle(gx1-2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 169 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 170 | } |
srigdon3 | 0:0a900ff9a788 | 171 | |
srigdon3 | 1:b86030cf57c4 | 172 | //Moves the blue ghost up |
srigdon3 | 0:0a900ff9a788 | 173 | void bgUP(void) |
srigdon3 | 0:0a900ff9a788 | 174 | { |
srigdon3 | 0:0a900ff9a788 | 175 | Thread::wait(50); |
srigdon3 | 0:0a900ff9a788 | 176 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 177 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1-PAC_SIZE,gx1+PAC_SIZE,gy1+PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 178 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 179 | replaceCOINS(); |
srigdon3 | 0:0a900ff9a788 | 180 | if(gy1<4) |
srigdon3 | 0:0a900ff9a788 | 181 | { |
srigdon3 | 0:0a900ff9a788 | 182 | gy1 = 124; |
srigdon3 | 0:0a900ff9a788 | 183 | } |
srigdon3 | 0:0a900ff9a788 | 184 | gy1 = gy1-STEP_SIZE; |
srigdon3 | 0:0a900ff9a788 | 185 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 186 | uLCD.filled_circle(gx1,gy1,PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 187 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1,gx1+PAC_SIZE,gy1+PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 188 | uLCD.filled_circle(gx1+2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 189 | uLCD.filled_circle(gx1-2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 190 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 191 | } |
srigdon3 | 0:0a900ff9a788 | 192 | |
srigdon3 | 1:b86030cf57c4 | 193 | //Moves the blue ghost down |
srigdon3 | 0:0a900ff9a788 | 194 | void bgDOWN(void) |
srigdon3 | 0:0a900ff9a788 | 195 | { |
srigdon3 | 0:0a900ff9a788 | 196 | Thread::wait(50); |
srigdon3 | 0:0a900ff9a788 | 197 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 198 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1-PAC_SIZE,gx1+PAC_SIZE,gy1+PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 199 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 200 | replaceCOINS(); |
srigdon3 | 0:0a900ff9a788 | 201 | if(gy1>124) |
srigdon3 | 0:0a900ff9a788 | 202 | { |
srigdon3 | 0:0a900ff9a788 | 203 | gy1 = 0; |
srigdon3 | 0:0a900ff9a788 | 204 | } |
srigdon3 | 0:0a900ff9a788 | 205 | gy1 = gy1+STEP_SIZE; |
srigdon3 | 0:0a900ff9a788 | 206 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 207 | uLCD.filled_circle(gx1,gy1,PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 208 | uLCD.filled_rectangle(gx1-PAC_SIZE,gy1,gx1+PAC_SIZE,gy1+PAC_SIZE,BLUE); |
srigdon3 | 0:0a900ff9a788 | 209 | uLCD.filled_circle(gx1+2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 210 | uLCD.filled_circle(gx1-2,gy1-2,1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 211 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 212 | } |
srigdon3 | 0:0a900ff9a788 | 213 | |
srigdon3 | 1:b86030cf57c4 | 214 | //Force ghost to chase Pacman |
srigdon3 | 0:0a900ff9a788 | 215 | void follow(void) |
srigdon3 | 0:0a900ff9a788 | 216 | { |
rollschild | 2:610d5194c64e | 217 | if((gx1==x) && (y == gy1) ) //if the ghost and Pacman are at the same position trigger losing condition |
srigdon3 | 0:0a900ff9a788 | 218 | { |
srigdon3 | 1:b86030cf57c4 | 219 | win = true; //This is set to true just to exit the check for a win loop and terminate other loops without writing additional conditions |
srigdon3 | 0:0a900ff9a788 | 220 | lose = true; |
rollschild | 2:610d5194c64e | 221 | if(lose) //Print game over message if lose (determined in the follow function) |
rollschild | 2:610d5194c64e | 222 | { |
rollschild | 2:610d5194c64e | 223 | uLCD.cls(); |
rollschild | 2:610d5194c64e | 224 | uLCD.printf("Sorry\nGame Over"); |
rollschild | 2:610d5194c64e | 225 | // if loss, ring the buzzer |
rollschild | 2:610d5194c64e | 226 | FILE *wave_file; |
rollschild | 2:610d5194c64e | 227 | wave_file=fopen("/sd/test.wav","r"); |
rollschild | 2:610d5194c64e | 228 | waver.play(wave_file); |
rollschild | 2:610d5194c64e | 229 | fclose(wave_file); |
rollschild | 2:610d5194c64e | 230 | Thread::wait(1000); |
rollschild | 2:610d5194c64e | 231 | return void(); |
rollschild | 2:610d5194c64e | 232 | } |
rollschild | 2:610d5194c64e | 233 | |
srigdon3 | 0:0a900ff9a788 | 234 | } |
srigdon3 | 1:b86030cf57c4 | 235 | while(x==gx1 && gy1<y && !win) //If the ghost is directly above Pacman check to see if moving down is possible, then move down |
srigdon3 | 0:0a900ff9a788 | 236 | { |
srigdon3 | 1:b86030cf57c4 | 237 | BGclearDOWN(); |
srigdon3 | 0:0a900ff9a788 | 238 | bgDOWN(); |
srigdon3 | 0:0a900ff9a788 | 239 | } |
srigdon3 | 0:0a900ff9a788 | 240 | while(x==gx1 && gy1>y && !win) |
srigdon3 | 0:0a900ff9a788 | 241 | { |
srigdon3 | 0:0a900ff9a788 | 242 | BGclearUP(); |
srigdon3 | 0:0a900ff9a788 | 243 | bgUP(); |
srigdon3 | 0:0a900ff9a788 | 244 | } |
srigdon3 | 0:0a900ff9a788 | 245 | while(y==gy1 && gx1<x && !win) |
srigdon3 | 0:0a900ff9a788 | 246 | { |
srigdon3 | 0:0a900ff9a788 | 247 | BGclearRIGHT(); |
srigdon3 | 0:0a900ff9a788 | 248 | bgRIGHT(); |
srigdon3 | 0:0a900ff9a788 | 249 | } |
srigdon3 | 0:0a900ff9a788 | 250 | while(y==gy1 && gx1>x && !win) |
srigdon3 | 0:0a900ff9a788 | 251 | { |
srigdon3 | 0:0a900ff9a788 | 252 | BGclearLEFT(); |
srigdon3 | 0:0a900ff9a788 | 253 | bgLEFT(); |
srigdon3 | 0:0a900ff9a788 | 254 | } |
srigdon3 | 0:0a900ff9a788 | 255 | } |
srigdon3 | 0:0a900ff9a788 | 256 | |
srigdon3 | 1:b86030cf57c4 | 257 | //Ghost selects a direction to move |
srigdon3 | 0:0a900ff9a788 | 258 | void pickMOVE(void) |
rollschild | 2:610d5194c64e | 259 | { |
rollschild | 2:610d5194c64e | 260 | |
srigdon3 | 1:b86030cf57c4 | 261 | while((gx1==x || gy1==y) && abs(x-gx1)+abs(y-gy1)<=16 && !win) //If Pacman is close by give chase |
srigdon3 | 0:0a900ff9a788 | 262 | { |
srigdon3 | 0:0a900ff9a788 | 263 | follow(); |
srigdon3 | 0:0a900ff9a788 | 264 | } |
srigdon3 | 1:b86030cf57c4 | 265 | int dec = rand()%4; //randomly generate a number from the set 0,1,2,3, which serves as the direction decision |
srigdon3 | 0:0a900ff9a788 | 266 | if(dec == 0) |
srigdon3 | 0:0a900ff9a788 | 267 | { |
srigdon3 | 0:0a900ff9a788 | 268 | BGclearRIGHT(); |
srigdon3 | 1:b86030cf57c4 | 269 | while(bgcr && !win) //If decision 0 was reached, check to the the move right until a boundary is reached |
srigdon3 | 0:0a900ff9a788 | 270 | { |
srigdon3 | 0:0a900ff9a788 | 271 | bgRIGHT(); |
srigdon3 | 0:0a900ff9a788 | 272 | BGclearRIGHT(); |
srigdon3 | 0:0a900ff9a788 | 273 | } |
srigdon3 | 0:0a900ff9a788 | 274 | } |
srigdon3 | 0:0a900ff9a788 | 275 | else if(dec == 1) |
srigdon3 | 0:0a900ff9a788 | 276 | { |
srigdon3 | 0:0a900ff9a788 | 277 | BGclearLEFT(); |
srigdon3 | 0:0a900ff9a788 | 278 | while(bgcl && !win) |
srigdon3 | 0:0a900ff9a788 | 279 | { |
srigdon3 | 0:0a900ff9a788 | 280 | bgLEFT(); |
srigdon3 | 0:0a900ff9a788 | 281 | BGclearLEFT(); |
srigdon3 | 0:0a900ff9a788 | 282 | } |
srigdon3 | 0:0a900ff9a788 | 283 | } |
srigdon3 | 0:0a900ff9a788 | 284 | else if(dec == 2) |
srigdon3 | 0:0a900ff9a788 | 285 | { |
srigdon3 | 0:0a900ff9a788 | 286 | BGclearUP(); |
srigdon3 | 0:0a900ff9a788 | 287 | while(bgcu && !win) |
srigdon3 | 0:0a900ff9a788 | 288 | { |
srigdon3 | 0:0a900ff9a788 | 289 | bgUP(); |
srigdon3 | 0:0a900ff9a788 | 290 | BGclearUP(); |
srigdon3 | 0:0a900ff9a788 | 291 | } |
srigdon3 | 0:0a900ff9a788 | 292 | } |
srigdon3 | 0:0a900ff9a788 | 293 | else |
srigdon3 | 0:0a900ff9a788 | 294 | { |
srigdon3 | 0:0a900ff9a788 | 295 | BGclearDOWN(); |
srigdon3 | 0:0a900ff9a788 | 296 | while(bgcd && !win) |
srigdon3 | 0:0a900ff9a788 | 297 | { |
srigdon3 | 0:0a900ff9a788 | 298 | bgDOWN(); |
srigdon3 | 0:0a900ff9a788 | 299 | BGclearDOWN(); |
srigdon3 | 0:0a900ff9a788 | 300 | } |
srigdon3 | 0:0a900ff9a788 | 301 | } |
srigdon3 | 0:0a900ff9a788 | 302 | } |
srigdon3 | 0:0a900ff9a788 | 303 | |
srigdon3 | 1:b86030cf57c4 | 304 | //Check if Pacman can move one step size to the right (Essentially the same as checking for the ghost) |
srigdon3 | 0:0a900ff9a788 | 305 | void CHECKclearRIGHT(void) |
srigdon3 | 0:0a900ff9a788 | 306 | { |
srigdon3 | 0:0a900ff9a788 | 307 | clearRIGHT = true; |
srigdon3 | 0:0a900ff9a788 | 308 | for(i=x; i <= x+CLEARANCE; i++) |
srigdon3 | 0:0a900ff9a788 | 309 | { |
srigdon3 | 0:0a900ff9a788 | 310 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 311 | if(uLCD.read_pixel(i,y)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 312 | { |
srigdon3 | 0:0a900ff9a788 | 313 | clearRIGHT = false; |
srigdon3 | 0:0a900ff9a788 | 314 | } |
srigdon3 | 0:0a900ff9a788 | 315 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 316 | } |
srigdon3 | 0:0a900ff9a788 | 317 | } |
srigdon3 | 0:0a900ff9a788 | 318 | |
srigdon3 | 1:b86030cf57c4 | 319 | //Check if Pacman can move left |
srigdon3 | 0:0a900ff9a788 | 320 | void CHECKclearLEFT(void) |
srigdon3 | 0:0a900ff9a788 | 321 | { |
srigdon3 | 0:0a900ff9a788 | 322 | clearLEFT = true; |
srigdon3 | 0:0a900ff9a788 | 323 | for(i=x; i >= x-CLEARANCE; i--) |
srigdon3 | 0:0a900ff9a788 | 324 | { |
srigdon3 | 0:0a900ff9a788 | 325 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 326 | if(uLCD.read_pixel(i,y)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 327 | { |
srigdon3 | 0:0a900ff9a788 | 328 | clearLEFT = false; |
srigdon3 | 0:0a900ff9a788 | 329 | } |
srigdon3 | 0:0a900ff9a788 | 330 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 331 | } |
srigdon3 | 0:0a900ff9a788 | 332 | } |
srigdon3 | 0:0a900ff9a788 | 333 | |
srigdon3 | 1:b86030cf57c4 | 334 | //Check if Pacman can move up |
srigdon3 | 0:0a900ff9a788 | 335 | void CHECKclearUP(void) |
srigdon3 | 0:0a900ff9a788 | 336 | { |
srigdon3 | 0:0a900ff9a788 | 337 | clearUP = true; |
srigdon3 | 0:0a900ff9a788 | 338 | for(i=y; i >= y-CLEARANCE; i--) |
srigdon3 | 0:0a900ff9a788 | 339 | { |
srigdon3 | 0:0a900ff9a788 | 340 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 341 | if(uLCD.read_pixel(x,i)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 342 | { |
srigdon3 | 0:0a900ff9a788 | 343 | clearUP = false; |
srigdon3 | 0:0a900ff9a788 | 344 | } |
srigdon3 | 0:0a900ff9a788 | 345 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 346 | } |
srigdon3 | 0:0a900ff9a788 | 347 | } |
srigdon3 | 0:0a900ff9a788 | 348 | |
srigdon3 | 1:b86030cf57c4 | 349 | //Check if Pacman can move down |
srigdon3 | 0:0a900ff9a788 | 350 | void CHECKclearDOWN(void) |
srigdon3 | 0:0a900ff9a788 | 351 | { |
srigdon3 | 0:0a900ff9a788 | 352 | clearDOWN = true; |
srigdon3 | 0:0a900ff9a788 | 353 | for(i=y; i <= y+CLEARANCE; i++) |
srigdon3 | 0:0a900ff9a788 | 354 | { |
srigdon3 | 0:0a900ff9a788 | 355 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 356 | if(uLCD.read_pixel(x,i)==uLCD.read_pixel(4,4)) |
srigdon3 | 0:0a900ff9a788 | 357 | { |
srigdon3 | 0:0a900ff9a788 | 358 | clearDOWN = false; |
srigdon3 | 0:0a900ff9a788 | 359 | } |
srigdon3 | 0:0a900ff9a788 | 360 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 361 | } |
srigdon3 | 0:0a900ff9a788 | 362 | } |
srigdon3 | 0:0a900ff9a788 | 363 | |
srigdon3 | 1:b86030cf57c4 | 364 | //This function tracks the coin Pacman eats as he passes over it |
srigdon3 | 0:0a900ff9a788 | 365 | void changeCOINS(void) |
srigdon3 | 0:0a900ff9a788 | 366 | { |
srigdon3 | 0:0a900ff9a788 | 367 | for(int m=0; m<81; m++) |
srigdon3 | 0:0a900ff9a788 | 368 | { |
srigdon3 | 0:0a900ff9a788 | 369 | lcd_mutex.lock(); |
srigdon3 | 1:b86030cf57c4 | 370 | if(x == coins[m][0] && y == coins[m][1]) //Compare Pacman's position to the set of coins |
srigdon3 | 0:0a900ff9a788 | 371 | { |
srigdon3 | 1:b86030cf57c4 | 372 | coins[m][0]=64; //If there is a match, change that coins location to the center of the board where Pacman |
srigdon3 | 1:b86030cf57c4 | 373 | coins[m][1]=64; //cannot go, but do not draw the coin |
srigdon3 | 0:0a900ff9a788 | 374 | } |
srigdon3 | 0:0a900ff9a788 | 375 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 376 | } |
srigdon3 | 0:0a900ff9a788 | 377 | } |
srigdon3 | 0:0a900ff9a788 | 378 | |
srigdon3 | 1:b86030cf57c4 | 379 | //Move Pacman one step size to the right |
srigdon3 | 0:0a900ff9a788 | 380 | void PACmoveRIGHT(void) |
srigdon3 | 0:0a900ff9a788 | 381 | { |
srigdon3 | 1:b86030cf57c4 | 382 | while(clearRIGHT && !win) //Not win indicates the game has not ended |
srigdon3 | 0:0a900ff9a788 | 383 | { |
srigdon3 | 0:0a900ff9a788 | 384 | lcd_mutex.lock(); |
srigdon3 | 1:b86030cf57c4 | 385 | uLCD.filled_circle(x,y,PAC_SIZE,BLACK); //Erase Pacman at his last location |
srigdon3 | 0:0a900ff9a788 | 386 | lcd_mutex.unlock(); |
srigdon3 | 1:b86030cf57c4 | 387 | if(x>124) //wrap around if moving off the board |
srigdon3 | 0:0a900ff9a788 | 388 | { |
srigdon3 | 0:0a900ff9a788 | 389 | x = 0; |
srigdon3 | 0:0a900ff9a788 | 390 | } |
srigdon3 | 1:b86030cf57c4 | 391 | x = x+STEP_SIZE; //move Pacman one step size to the right |
srigdon3 | 1:b86030cf57c4 | 392 | changeCOINS(); //Track the coin that was eaten at the last location |
srigdon3 | 0:0a900ff9a788 | 393 | |
srigdon3 | 1:b86030cf57c4 | 394 | if(x%(2*STEP_SIZE) == 0) //There are two drawings provided for Pacman. The if statement causes Pacman to open his mouth every other move. |
srigdon3 | 0:0a900ff9a788 | 395 | { |
srigdon3 | 0:0a900ff9a788 | 396 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 397 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 398 | uLCD.filled_rectangle(x+2,y-2,x+PAC_SIZE,y+2,BLACK); |
srigdon3 | 0:0a900ff9a788 | 399 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 400 | } |
srigdon3 | 0:0a900ff9a788 | 401 | else |
srigdon3 | 0:0a900ff9a788 | 402 | { |
srigdon3 | 0:0a900ff9a788 | 403 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 404 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 405 | uLCD.filled_rectangle(x+2,y,x+PAC_SIZE,y+1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 406 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 407 | } |
rollschild | 2:610d5194c64e | 408 | |
srigdon3 | 1:b86030cf57c4 | 409 | CHECKclearRIGHT(); //If the user remains in the loop, check for a boundary to the right |
srigdon3 | 0:0a900ff9a788 | 410 | Thread::wait(10); |
rollschild | 2:610d5194c64e | 411 | break; |
srigdon3 | 0:0a900ff9a788 | 412 | } |
srigdon3 | 0:0a900ff9a788 | 413 | } |
srigdon3 | 0:0a900ff9a788 | 414 | |
srigdon3 | 1:b86030cf57c4 | 415 | //Move Pacman left |
srigdon3 | 0:0a900ff9a788 | 416 | void PACmoveLEFT(void) |
srigdon3 | 0:0a900ff9a788 | 417 | { |
srigdon3 | 0:0a900ff9a788 | 418 | while(clearLEFT && !win) |
srigdon3 | 0:0a900ff9a788 | 419 | { |
srigdon3 | 0:0a900ff9a788 | 420 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 421 | uLCD.filled_circle(x,y,PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 422 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 423 | if(x<4) |
srigdon3 | 0:0a900ff9a788 | 424 | { |
srigdon3 | 0:0a900ff9a788 | 425 | x = 128; |
srigdon3 | 0:0a900ff9a788 | 426 | } |
srigdon3 | 0:0a900ff9a788 | 427 | x = x-STEP_SIZE; |
rollschild | 2:610d5194c64e | 428 | |
srigdon3 | 0:0a900ff9a788 | 429 | changeCOINS(); |
srigdon3 | 0:0a900ff9a788 | 430 | if(x%(2*STEP_SIZE) == 0) |
srigdon3 | 0:0a900ff9a788 | 431 | { |
srigdon3 | 0:0a900ff9a788 | 432 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 433 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 434 | uLCD.filled_rectangle(x-2,y-2,x-PAC_SIZE,y+2,BLACK); |
srigdon3 | 0:0a900ff9a788 | 435 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 436 | } |
srigdon3 | 0:0a900ff9a788 | 437 | else |
srigdon3 | 0:0a900ff9a788 | 438 | { |
srigdon3 | 0:0a900ff9a788 | 439 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 440 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 441 | uLCD.filled_rectangle(x-2,y,x-PAC_SIZE,y+1,BLACK); |
srigdon3 | 0:0a900ff9a788 | 442 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 443 | } |
rollschild | 2:610d5194c64e | 444 | |
srigdon3 | 0:0a900ff9a788 | 445 | CHECKclearLEFT(); |
srigdon3 | 0:0a900ff9a788 | 446 | Thread::wait(10); |
rollschild | 2:610d5194c64e | 447 | break; |
srigdon3 | 0:0a900ff9a788 | 448 | } |
srigdon3 | 0:0a900ff9a788 | 449 | } |
srigdon3 | 0:0a900ff9a788 | 450 | |
srigdon3 | 1:b86030cf57c4 | 451 | //Move Pacman up |
srigdon3 | 0:0a900ff9a788 | 452 | void PACmoveUP(void) |
srigdon3 | 0:0a900ff9a788 | 453 | { |
srigdon3 | 0:0a900ff9a788 | 454 | while(clearUP && !win) |
srigdon3 | 0:0a900ff9a788 | 455 | { |
srigdon3 | 0:0a900ff9a788 | 456 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 457 | uLCD.filled_circle(x,y,PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 458 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 459 | if(y<4) |
srigdon3 | 0:0a900ff9a788 | 460 | { |
srigdon3 | 0:0a900ff9a788 | 461 | y = 128; |
srigdon3 | 0:0a900ff9a788 | 462 | } |
srigdon3 | 0:0a900ff9a788 | 463 | y = y-STEP_SIZE; |
srigdon3 | 0:0a900ff9a788 | 464 | changeCOINS(); |
srigdon3 | 0:0a900ff9a788 | 465 | if(y%(2*STEP_SIZE) == 0) |
srigdon3 | 0:0a900ff9a788 | 466 | { |
srigdon3 | 0:0a900ff9a788 | 467 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 468 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 469 | uLCD.filled_rectangle(x-2,y-2,x+2,y-PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 470 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 471 | } |
srigdon3 | 0:0a900ff9a788 | 472 | else |
srigdon3 | 0:0a900ff9a788 | 473 | { |
srigdon3 | 0:0a900ff9a788 | 474 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 475 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 476 | uLCD.filled_rectangle(x,y-2,x+1,y-PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 477 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 478 | } |
rollschild | 2:610d5194c64e | 479 | |
srigdon3 | 0:0a900ff9a788 | 480 | CHECKclearUP(); |
srigdon3 | 0:0a900ff9a788 | 481 | Thread::wait(10); |
rollschild | 2:610d5194c64e | 482 | break; |
srigdon3 | 0:0a900ff9a788 | 483 | } |
srigdon3 | 0:0a900ff9a788 | 484 | } |
srigdon3 | 0:0a900ff9a788 | 485 | |
srigdon3 | 1:b86030cf57c4 | 486 | //Move Pacman down |
srigdon3 | 0:0a900ff9a788 | 487 | void PACmoveDOWN(void) |
srigdon3 | 0:0a900ff9a788 | 488 | { |
srigdon3 | 0:0a900ff9a788 | 489 | while(clearDOWN && !win) |
srigdon3 | 0:0a900ff9a788 | 490 | { |
srigdon3 | 0:0a900ff9a788 | 491 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 492 | uLCD.filled_circle(x,y,PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 493 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 494 | if(y>124) |
srigdon3 | 0:0a900ff9a788 | 495 | { |
srigdon3 | 0:0a900ff9a788 | 496 | y = 0; |
srigdon3 | 0:0a900ff9a788 | 497 | } |
srigdon3 | 0:0a900ff9a788 | 498 | y = y+STEP_SIZE; |
srigdon3 | 0:0a900ff9a788 | 499 | changeCOINS(); |
srigdon3 | 0:0a900ff9a788 | 500 | if(y%(2*STEP_SIZE) == 0) |
srigdon3 | 0:0a900ff9a788 | 501 | { |
srigdon3 | 0:0a900ff9a788 | 502 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 503 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 504 | uLCD.filled_rectangle(x-2,y+2,x+2,y+PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 505 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 506 | } |
srigdon3 | 0:0a900ff9a788 | 507 | else |
srigdon3 | 0:0a900ff9a788 | 508 | { |
srigdon3 | 0:0a900ff9a788 | 509 | lcd_mutex.lock(); |
srigdon3 | 0:0a900ff9a788 | 510 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 511 | uLCD.filled_rectangle(x,y+2,x+1,y+PAC_SIZE,BLACK); |
srigdon3 | 0:0a900ff9a788 | 512 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 513 | } |
rollschild | 2:610d5194c64e | 514 | |
srigdon3 | 0:0a900ff9a788 | 515 | CHECKclearDOWN(); |
srigdon3 | 0:0a900ff9a788 | 516 | Thread::wait(10); |
rollschild | 2:610d5194c64e | 517 | break; |
srigdon3 | 0:0a900ff9a788 | 518 | } |
srigdon3 | 0:0a900ff9a788 | 519 | } |
srigdon3 | 0:0a900ff9a788 | 520 | |
rollschild | 2:610d5194c64e | 521 | //Read the input from pushbuttons |
srigdon3 | 0:0a900ff9a788 | 522 | void checkMOVE(void) |
srigdon3 | 0:0a900ff9a788 | 523 | { |
rollschild | 2:610d5194c64e | 524 | if(!right_pb) { |
srigdon3 | 0:0a900ff9a788 | 525 | CHECKclearRIGHT(); |
srigdon3 | 0:0a900ff9a788 | 526 | PACmoveRIGHT(); |
rollschild | 2:610d5194c64e | 527 | } else if(!left_pb) { |
srigdon3 | 0:0a900ff9a788 | 528 | CHECKclearLEFT(); |
srigdon3 | 0:0a900ff9a788 | 529 | PACmoveLEFT(); |
rollschild | 2:610d5194c64e | 530 | } else if(!up_pb){ |
srigdon3 | 0:0a900ff9a788 | 531 | CHECKclearUP(); |
srigdon3 | 0:0a900ff9a788 | 532 | PACmoveUP(); |
rollschild | 2:610d5194c64e | 533 | } else if(!down_pb){ |
srigdon3 | 0:0a900ff9a788 | 534 | CHECKclearDOWN(); |
srigdon3 | 0:0a900ff9a788 | 535 | PACmoveDOWN(); |
srigdon3 | 0:0a900ff9a788 | 536 | } |
srigdon3 | 0:0a900ff9a788 | 537 | } |
srigdon3 | 0:0a900ff9a788 | 538 | |
srigdon3 | 1:b86030cf57c4 | 539 | //Draw the boudaries for the game using the uLCD graphics commands |
srigdon3 | 0:0a900ff9a788 | 540 | void drawBORDERS(void) |
srigdon3 | 0:0a900ff9a788 | 541 | { |
srigdon3 | 0:0a900ff9a788 | 542 | //Outer Border |
srigdon3 | 0:0a900ff9a788 | 543 | uLCD.rectangle(4,4,124,124,RED); |
srigdon3 | 0:0a900ff9a788 | 544 | uLCD.line(8,8,8,120,RED); |
srigdon3 | 0:0a900ff9a788 | 545 | uLCD.line(8,8,62,8,RED); |
srigdon3 | 0:0a900ff9a788 | 546 | uLCD.line(62,8,62,32,RED); |
srigdon3 | 0:0a900ff9a788 | 547 | uLCD.line(62,32,66,32,RED); |
srigdon3 | 0:0a900ff9a788 | 548 | uLCD.line(66,32,66,8,RED); |
srigdon3 | 0:0a900ff9a788 | 549 | uLCD.line(66,8,120,8,RED); |
srigdon3 | 0:0a900ff9a788 | 550 | uLCD.line(120,8,120,120,RED); |
srigdon3 | 0:0a900ff9a788 | 551 | uLCD.line(120,120,66,120,RED); |
srigdon3 | 0:0a900ff9a788 | 552 | uLCD.line(66,120,66,96,RED); |
srigdon3 | 0:0a900ff9a788 | 553 | uLCD.line(66,96,62,96,RED); |
srigdon3 | 0:0a900ff9a788 | 554 | uLCD.line(62,96,62,120,RED); |
srigdon3 | 0:0a900ff9a788 | 555 | uLCD.line(62,120,8,120,RED); |
srigdon3 | 0:0a900ff9a788 | 556 | //Inner Rectangle |
srigdon3 | 0:0a900ff9a788 | 557 | uLCD.rectangle(52,52,76,76,RED); |
srigdon3 | 0:0a900ff9a788 | 558 | uLCD.rectangle(48,48,80,80,RED); |
srigdon3 | 0:0a900ff9a788 | 559 | //Upper Left Corner |
srigdon3 | 0:0a900ff9a788 | 560 | uLCD.line(48,24,24,24,RED); |
srigdon3 | 0:0a900ff9a788 | 561 | uLCD.line(24,24,24,56,RED); |
srigdon3 | 0:0a900ff9a788 | 562 | uLCD.line(24,56,32,56,RED); |
srigdon3 | 0:0a900ff9a788 | 563 | uLCD.line(32,56,32,32,RED); |
srigdon3 | 0:0a900ff9a788 | 564 | uLCD.line(32,32,48,32,RED); |
srigdon3 | 0:0a900ff9a788 | 565 | uLCD.line(48,32,48,24,RED); |
srigdon3 | 0:0a900ff9a788 | 566 | //Upper Right Corner |
srigdon3 | 0:0a900ff9a788 | 567 | uLCD.line(80,24,104,24,RED); |
srigdon3 | 0:0a900ff9a788 | 568 | uLCD.line(104,24,104,56,RED); |
srigdon3 | 0:0a900ff9a788 | 569 | uLCD.line(104,56,96,56,RED); |
srigdon3 | 0:0a900ff9a788 | 570 | uLCD.line(96,56,96,32,RED); |
srigdon3 | 0:0a900ff9a788 | 571 | uLCD.line(96,32,80,32,RED); |
srigdon3 | 0:0a900ff9a788 | 572 | uLCD.line(80,32,80,24,RED); |
srigdon3 | 0:0a900ff9a788 | 573 | //Lower Left Corner |
srigdon3 | 0:0a900ff9a788 | 574 | uLCD.line(48,104,24,104,RED); |
srigdon3 | 0:0a900ff9a788 | 575 | uLCD.line(24,104,24,72,RED); |
srigdon3 | 0:0a900ff9a788 | 576 | uLCD.line(24,72,32,72,RED); |
srigdon3 | 0:0a900ff9a788 | 577 | uLCD.line(32,72,32,96,RED); |
srigdon3 | 0:0a900ff9a788 | 578 | uLCD.line(32,96,48,96,RED); |
srigdon3 | 0:0a900ff9a788 | 579 | uLCD.line(48,96,48,104,RED); |
srigdon3 | 0:0a900ff9a788 | 580 | //Lower Right Corner |
srigdon3 | 0:0a900ff9a788 | 581 | uLCD.line(80,104,104,104,RED); |
srigdon3 | 0:0a900ff9a788 | 582 | uLCD.line(104,104,104,72,RED); |
srigdon3 | 0:0a900ff9a788 | 583 | uLCD.line(104,72,96,72,RED); |
srigdon3 | 0:0a900ff9a788 | 584 | uLCD.line(96,72,96,96,RED); |
srigdon3 | 0:0a900ff9a788 | 585 | uLCD.line(96,96,80,96,RED); |
srigdon3 | 0:0a900ff9a788 | 586 | uLCD.line(80,96,80,104,RED); |
srigdon3 | 0:0a900ff9a788 | 587 | } |
srigdon3 | 0:0a900ff9a788 | 588 | |
srigdon3 | 0:0a900ff9a788 | 589 | void placeCOINS(void) |
srigdon3 | 0:0a900ff9a788 | 590 | { |
srigdon3 | 0:0a900ff9a788 | 591 | for(int j=0; j<81; j++) |
srigdon3 | 0:0a900ff9a788 | 592 | { |
srigdon3 | 1:b86030cf57c4 | 593 | uLCD.filled_circle(coins[j][0],coins[j][1],1,0xFFFF00); //Draw the coins in their initial locations |
srigdon3 | 0:0a900ff9a788 | 594 | } |
srigdon3 | 0:0a900ff9a788 | 595 | } |
srigdon3 | 0:0a900ff9a788 | 596 | |
srigdon3 | 1:b86030cf57c4 | 597 | //Draw all the initial states of the game |
srigdon3 | 0:0a900ff9a788 | 598 | void initialize(void) |
srigdon3 | 0:0a900ff9a788 | 599 | { |
srigdon3 | 0:0a900ff9a788 | 600 | drawBORDERS(); |
srigdon3 | 0:0a900ff9a788 | 601 | placeCOINS(); |
srigdon3 | 0:0a900ff9a788 | 602 | uLCD.filled_circle(x,y,PAC_SIZE,0xFFFF00); |
srigdon3 | 0:0a900ff9a788 | 603 | uLCD.filled_rectangle(x-2,y-2,x-PAC_SIZE,y+2,BLACK); |
srigdon3 | 0:0a900ff9a788 | 604 | } |
srigdon3 | 0:0a900ff9a788 | 605 | |
srigdon3 | 1:b86030cf57c4 | 606 | //Check to see if all the coins have been eaten |
srigdon3 | 0:0a900ff9a788 | 607 | void checkWIN(void) |
srigdon3 | 0:0a900ff9a788 | 608 | { |
srigdon3 | 0:0a900ff9a788 | 609 | win = true; |
srigdon3 | 0:0a900ff9a788 | 610 | for(int k=0; k<81; k++) |
srigdon3 | 0:0a900ff9a788 | 611 | { |
srigdon3 | 0:0a900ff9a788 | 612 | lcd_mutex.lock(); |
srigdon3 | 1:b86030cf57c4 | 613 | if(coins[k][0]!=64 || coins[k][1]!=64) //Check the locations of all coins and if 1 has coordinates other than (64,64) the user has not won |
srigdon3 | 0:0a900ff9a788 | 614 | { |
srigdon3 | 0:0a900ff9a788 | 615 | win = false; |
srigdon3 | 0:0a900ff9a788 | 616 | } |
srigdon3 | 0:0a900ff9a788 | 617 | lcd_mutex.unlock(); |
srigdon3 | 0:0a900ff9a788 | 618 | } |
srigdon3 | 0:0a900ff9a788 | 619 | } |
srigdon3 | 0:0a900ff9a788 | 620 | |
srigdon3 | 1:b86030cf57c4 | 621 | //Thread supervising the joystick inputs and moving Pacman accordingly |
srigdon3 | 0:0a900ff9a788 | 622 | void pacMOVE(void const *args) |
srigdon3 | 0:0a900ff9a788 | 623 | { |
srigdon3 | 0:0a900ff9a788 | 624 | while(!win) |
srigdon3 | 0:0a900ff9a788 | 625 | { |
srigdon3 | 0:0a900ff9a788 | 626 | checkMOVE(); |
rollschild | 2:610d5194c64e | 627 | Thread::wait(1); |
srigdon3 | 0:0a900ff9a788 | 628 | } |
srigdon3 | 0:0a900ff9a788 | 629 | } |
srigdon3 | 0:0a900ff9a788 | 630 | |
srigdon3 | 1:b86030cf57c4 | 631 | //Thread controlling the movement of the blue ghost |
srigdon3 | 0:0a900ff9a788 | 632 | void blueGHOST(void const *args) |
srigdon3 | 0:0a900ff9a788 | 633 | { |
srigdon3 | 0:0a900ff9a788 | 634 | while(!win) |
srigdon3 | 0:0a900ff9a788 | 635 | { |
srigdon3 | 0:0a900ff9a788 | 636 | pickMOVE(); |
srigdon3 | 0:0a900ff9a788 | 637 | } |
srigdon3 | 0:0a900ff9a788 | 638 | } |
srigdon3 | 0:0a900ff9a788 | 639 | |
srigdon3 | 0:0a900ff9a788 | 640 | int main() |
srigdon3 | 0:0a900ff9a788 | 641 | { |
rollschild | 2:610d5194c64e | 642 | left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed |
rollschild | 2:610d5194c64e | 643 | right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed |
rollschild | 2:610d5194c64e | 644 | up_pb.mode(PullUp); // the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
rollschild | 2:610d5194c64e | 645 | down_pb.mode(PullUp); // the variable fire_pb will be zero when the pushbutton for firing a missile is pressed |
srigdon3 | 0:0a900ff9a788 | 646 | uLCD.cls(); |
srigdon3 | 0:0a900ff9a788 | 647 | uLCD.baudrate(BAUD_3000000); |
rollschild | 2:610d5194c64e | 648 | initialize(); // Draw the level setup |
rollschild | 2:610d5194c64e | 649 | Thread pm(pacMOVE); // Start the thread for moving Pacman |
rollschild | 2:610d5194c64e | 650 | Thread bg(blueGHOST); // Start the thread for moving the blue ghost |
srigdon3 | 0:0a900ff9a788 | 651 | |
rollschild | 2:610d5194c64e | 652 | Thread::wait(3000000000); // Wait some time before checking the win conditions since it will take around 30 secs to eat all 81 coins |
rollschild | 2:610d5194c64e | 653 | while(!win) // Check to see if there was a win once every tenth of a second |
srigdon3 | 0:0a900ff9a788 | 654 | { |
srigdon3 | 0:0a900ff9a788 | 655 | checkWIN(); |
rollschild | 2:610d5194c64e | 656 | Thread::wait(1); |
srigdon3 | 0:0a900ff9a788 | 657 | } |
srigdon3 | 1:b86030cf57c4 | 658 | |
rollschild | 2:610d5194c64e | 659 | Thread::wait(500); // Wait .5 second before displaying end message |
srigdon3 | 1:b86030cf57c4 | 660 | |
rollschild | 2:610d5194c64e | 661 | if(lose) // Print game over message if lose (determined in the follow function) |
srigdon3 | 0:0a900ff9a788 | 662 | { |
srigdon3 | 0:0a900ff9a788 | 663 | uLCD.cls(); |
srigdon3 | 0:0a900ff9a788 | 664 | uLCD.printf("Sorry\nGame Over"); |
srigdon3 | 0:0a900ff9a788 | 665 | } |
srigdon3 | 0:0a900ff9a788 | 666 | else |
srigdon3 | 0:0a900ff9a788 | 667 | { |
srigdon3 | 0:0a900ff9a788 | 668 | uLCD.cls(); |
srigdon3 | 0:0a900ff9a788 | 669 | uLCD.printf("Congratulations!\nYou Won!"); |
srigdon3 | 0:0a900ff9a788 | 670 | } |
srigdon3 | 0:0a900ff9a788 | 671 | |
rollschild | 2:610d5194c64e | 672 | |
rollschild | 2:610d5194c64e | 673 | while(1){Thread::wait(1);} |
srigdon3 | 0:0a900ff9a788 | 674 | } |