![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Student project simplified version of classic game frogger
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
Fork of uLCD144G2_demo by
main.cpp@9:d33e61dab7dd, 2014-10-22 (annotated)
- Committer:
- acorness
- Date:
- Wed Oct 22 19:12:15 2014 +0000
- Revision:
- 9:d33e61dab7dd
- Parent:
- 8:31e63caf37e2
O______O what?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 8:31e63caf37e2 | 1 | // uLCD-144-G2 demo program for uLCD-4GL LCD driver library |
4180_1 | 0:cfcf73272647 | 2 | // |
4180_1 | 0:cfcf73272647 | 3 | #include "mbed.h" |
4180_1 | 2:75727e89a717 | 4 | #include "uLCD_4DGL.h" |
acorness | 9:d33e61dab7dd | 5 | #include "rtos.h" |
4180_1 | 0:cfcf73272647 | 6 | |
acorness | 9:d33e61dab7dd | 7 | DigitalOut led1(LED1); |
acorness | 9:d33e61dab7dd | 8 | DigitalOut led2(LED2); |
acorness | 9:d33e61dab7dd | 9 | DigitalOut led3(LED3); |
acorness | 9:d33e61dab7dd | 10 | DigitalOut led4(LED4); |
acorness | 9:d33e61dab7dd | 11 | DigitalIn pb1(p24); //Right |
acorness | 9:d33e61dab7dd | 12 | DigitalIn pb2(p23); //Left |
acorness | 9:d33e61dab7dd | 13 | DigitalIn pb3(p22); //Down |
acorness | 9:d33e61dab7dd | 14 | DigitalIn pb4(p21); //Up |
acorness | 9:d33e61dab7dd | 15 | uLCD_4DGL uLCD(p9,p10,p11); //Screen |
acorness | 9:d33e61dab7dd | 16 | Mutex key; //Pushbutton key |
4180_1 | 0:cfcf73272647 | 17 | |
acorness | 9:d33e61dab7dd | 18 | int i=2; //Win variable |
acorness | 9:d33e61dab7dd | 19 | int x,y; //Frogger Location |
acorness | 9:d33e61dab7dd | 20 | int u,v; //Previous Location |
acorness | 9:d33e61dab7dd | 21 | int k=1; //Change? |
acorness | 9:d33e61dab7dd | 22 | int l; |
acorness | 9:d33e61dab7dd | 23 | int gametime; //Game over condition |
acorness | 9:d33e61dab7dd | 24 | |
acorness | 9:d33e61dab7dd | 25 | struct cars |
acorness | 9:d33e61dab7dd | 26 | { |
acorness | 9:d33e61dab7dd | 27 | int x; |
acorness | 9:d33e61dab7dd | 28 | int y; |
acorness | 9:d33e61dab7dd | 29 | int u; |
acorness | 9:d33e61dab7dd | 30 | int v; |
acorness | 9:d33e61dab7dd | 31 | }; |
acorness | 9:d33e61dab7dd | 32 | |
acorness | 9:d33e61dab7dd | 33 | struct logs |
4180_1 | 2:75727e89a717 | 34 | { |
acorness | 9:d33e61dab7dd | 35 | int x1; |
acorness | 9:d33e61dab7dd | 36 | int x2; |
acorness | 9:d33e61dab7dd | 37 | int x3; |
acorness | 9:d33e61dab7dd | 38 | int y; |
acorness | 9:d33e61dab7dd | 39 | int u1; |
acorness | 9:d33e61dab7dd | 40 | int u2; |
acorness | 9:d33e61dab7dd | 41 | int u3; |
acorness | 9:d33e61dab7dd | 42 | int v; |
acorness | 9:d33e61dab7dd | 43 | int speed; |
acorness | 9:d33e61dab7dd | 44 | }; |
acorness | 9:d33e61dab7dd | 45 | |
acorness | 9:d33e61dab7dd | 46 | struct logs brown[5]; |
acorness | 9:d33e61dab7dd | 47 | struct cars red[5]; |
4180_1 | 7:7bd7397ab89f | 48 | |
acorness | 9:d33e61dab7dd | 49 | void Print_Background() //Prints Map |
acorness | 9:d33e61dab7dd | 50 | { |
acorness | 9:d33e61dab7dd | 51 | uLCD.filled_rectangle(gametime/12,1,128,5,0x000000); |
acorness | 9:d33e61dab7dd | 52 | if(gametime/12==35){i=0;} |
acorness | 9:d33e61dab7dd | 53 | gametime-=4; |
acorness | 9:d33e61dab7dd | 54 | l+=1; |
acorness | 9:d33e61dab7dd | 55 | l= l%4; |
acorness | 9:d33e61dab7dd | 56 | for(int j=0;j<5;j++) |
acorness | 9:d33e61dab7dd | 57 | { |
acorness | 9:d33e61dab7dd | 58 | switch(red[j].v) //Water or land? |
acorness | 9:d33e61dab7dd | 59 | { |
acorness | 9:d33e61dab7dd | 60 | case 12: |
acorness | 9:d33e61dab7dd | 61 | case 11: |
acorness | 9:d33e61dab7dd | 62 | case 10: |
acorness | 9:d33e61dab7dd | 63 | case 9: |
acorness | 9:d33e61dab7dd | 64 | case 8: |
acorness | 9:d33e61dab7dd | 65 | case 7: |
acorness | 9:d33e61dab7dd | 66 | case 6: |
acorness | 9:d33e61dab7dd | 67 | case 0: |
acorness | 9:d33e61dab7dd | 68 | uLCD.filled_rectangle(red[j].u*8,11+(red[j].v*9),(red[j].u*8)+7,19+(red[j].v*9),0x000000); |
acorness | 9:d33e61dab7dd | 69 | break; |
acorness | 9:d33e61dab7dd | 70 | default: |
acorness | 9:d33e61dab7dd | 71 | uLCD.filled_rectangle(red[j].u*8,11+(red[j].v*9),(red[j].u*8)+7,19+(red[j].v*9),0x0000FF); |
acorness | 9:d33e61dab7dd | 72 | } |
acorness | 9:d33e61dab7dd | 73 | uLCD.filled_rectangle(red[j].x*8,11+(red[j].y*9),(red[j].x*8)+7,19+(red[j].y*9), 0xFF0000); |
acorness | 9:d33e61dab7dd | 74 | red[j].u=red[j].x; |
acorness | 9:d33e61dab7dd | 75 | red[j].v=red[j].y; |
acorness | 9:d33e61dab7dd | 76 | red[j].x= (red[j].x+1)%16; |
acorness | 9:d33e61dab7dd | 77 | if(x==red[j].x&y==red[j].y){i=0;} |
acorness | 9:d33e61dab7dd | 78 | if(x==red[j].u&y==red[j].y){i=0;} |
4180_1 | 4:25a266a74a4c | 79 | } |
acorness | 9:d33e61dab7dd | 80 | for(int j=0;j<5;j++) |
acorness | 9:d33e61dab7dd | 81 | { |
acorness | 9:d33e61dab7dd | 82 | if(l==3) |
acorness | 9:d33e61dab7dd | 83 | { |
acorness | 9:d33e61dab7dd | 84 | uLCD.filled_rectangle(brown[j].u1*8,11+(brown[j].v*9),(brown[j].u1*8)+7,19+(brown[j].v*9),0x0000FF); |
acorness | 9:d33e61dab7dd | 85 | uLCD.filled_rectangle(brown[j].u2*8,11+(brown[j].v*9),(brown[j].u2*8)+7,19+(brown[j].v*9),0x0000FF); |
acorness | 9:d33e61dab7dd | 86 | uLCD.filled_rectangle(brown[j].u3*8,11+(brown[j].v*9),(brown[j].u3*8)+7,19+(brown[j].v*9),0x0000FF); |
acorness | 9:d33e61dab7dd | 87 | uLCD.filled_rectangle(brown[j].x1*8,11+(brown[j].y*9),(brown[j].x1*8)+7,19+(brown[j].y*9), 0x660000); |
acorness | 9:d33e61dab7dd | 88 | uLCD.filled_rectangle(brown[j].x2*8,11+(brown[j].y*9),(brown[j].x2*8)+7,19+(brown[j].y*9), 0x660000); |
acorness | 9:d33e61dab7dd | 89 | uLCD.filled_rectangle(brown[j].x3*8,11+(brown[j].y*9),(brown[j].x3*8)+7,19+(brown[j].y*9), 0x660000); |
acorness | 9:d33e61dab7dd | 90 | brown[j].u1=brown[j].x1; |
acorness | 9:d33e61dab7dd | 91 | brown[j].u2=brown[j].x2; |
acorness | 9:d33e61dab7dd | 92 | brown[j].u3=brown[j].x3; |
acorness | 9:d33e61dab7dd | 93 | brown[j].v=brown[j].y; |
acorness | 9:d33e61dab7dd | 94 | brown[j].x1= (brown[j].x1+brown[j].speed)%16; |
acorness | 9:d33e61dab7dd | 95 | brown[j].x2= (brown[j].x2+brown[j].speed)%16; |
acorness | 9:d33e61dab7dd | 96 | brown[j].x3= (brown[j].x3+brown[j].speed)%16; |
4180_1 | 3:454d1f4c8fd7 | 97 | } |
acorness | 9:d33e61dab7dd | 98 | if(y==brown[j].y) |
acorness | 9:d33e61dab7dd | 99 | { |
acorness | 9:d33e61dab7dd | 100 | if(x==brown[j].u1){} |
acorness | 9:d33e61dab7dd | 101 | else if(x==brown[j].u2){} |
acorness | 9:d33e61dab7dd | 102 | else if(x==brown[j].u3){} |
acorness | 9:d33e61dab7dd | 103 | else i=0; |
4180_1 | 5:a1ef40ff0f78 | 104 | } |
acorness | 9:d33e61dab7dd | 105 | |
4180_1 | 5:a1ef40ff0f78 | 106 | } |
4180_1 | 6:f752accd632c | 107 | } |
4180_1 | 7:7bd7397ab89f | 108 | |
4180_1 | 7:7bd7397ab89f | 109 | |
4180_1 | 8:31e63caf37e2 | 110 | |
acorness | 9:d33e61dab7dd | 111 | |
acorness | 9:d33e61dab7dd | 112 | void Print_Frogger(int x, int y) //Print Player |
acorness | 9:d33e61dab7dd | 113 | { |
acorness | 9:d33e61dab7dd | 114 | if(k) |
acorness | 9:d33e61dab7dd | 115 | { |
acorness | 9:d33e61dab7dd | 116 | key.lock(); //Prevents position change as writing onto screen |
acorness | 9:d33e61dab7dd | 117 | switch(v) //Water or land? |
acorness | 9:d33e61dab7dd | 118 | { |
acorness | 9:d33e61dab7dd | 119 | case 12: |
acorness | 9:d33e61dab7dd | 120 | case 11: |
acorness | 9:d33e61dab7dd | 121 | case 10: |
acorness | 9:d33e61dab7dd | 122 | case 9: |
acorness | 9:d33e61dab7dd | 123 | case 8: |
acorness | 9:d33e61dab7dd | 124 | case 7: |
acorness | 9:d33e61dab7dd | 125 | case 6: |
acorness | 9:d33e61dab7dd | 126 | case 0: |
acorness | 9:d33e61dab7dd | 127 | uLCD.filled_rectangle(u*8,11+(v*9),(u*8)+7,19+(v*9),0x000000); |
acorness | 9:d33e61dab7dd | 128 | break; |
acorness | 9:d33e61dab7dd | 129 | default: |
acorness | 9:d33e61dab7dd | 130 | uLCD.filled_rectangle(u*8,11+(v*9),(u*8)+7,19+(v*9),0x0000FF); |
acorness | 9:d33e61dab7dd | 131 | } |
acorness | 9:d33e61dab7dd | 132 | uLCD.filled_rectangle(x*8,11+(y*9),(x*8)+7,19+(y*9),0x00FF00); |
acorness | 9:d33e61dab7dd | 133 | if(y==0){i=1;} |
acorness | 9:d33e61dab7dd | 134 | u=x; |
acorness | 9:d33e61dab7dd | 135 | v=y; |
acorness | 9:d33e61dab7dd | 136 | k=0; |
acorness | 9:d33e61dab7dd | 137 | key.unlock(); |
acorness | 9:d33e61dab7dd | 138 | } |
acorness | 9:d33e61dab7dd | 139 | } |
acorness | 9:d33e61dab7dd | 140 | |
acorness | 9:d33e61dab7dd | 141 | void Pushbutton_Left(void const *args) //Check Left to change x |
acorness | 9:d33e61dab7dd | 142 | { |
acorness | 9:d33e61dab7dd | 143 | while(1) |
acorness | 9:d33e61dab7dd | 144 | { |
acorness | 9:d33e61dab7dd | 145 | if((x>0)&!pb2&!k) //Is it not at the left wall? |
acorness | 9:d33e61dab7dd | 146 | { |
acorness | 9:d33e61dab7dd | 147 | key.lock(); |
acorness | 9:d33e61dab7dd | 148 | x-=1; |
acorness | 9:d33e61dab7dd | 149 | Thread::wait(50); |
acorness | 9:d33e61dab7dd | 150 | k=1; |
acorness | 9:d33e61dab7dd | 151 | key.unlock(); |
acorness | 9:d33e61dab7dd | 152 | } |
acorness | 9:d33e61dab7dd | 153 | } |
acorness | 9:d33e61dab7dd | 154 | } |
acorness | 9:d33e61dab7dd | 155 | |
acorness | 9:d33e61dab7dd | 156 | void Pushbutton_Right(void const *args) //Check Right to change x |
acorness | 9:d33e61dab7dd | 157 | { |
acorness | 9:d33e61dab7dd | 158 | while(1) |
acorness | 9:d33e61dab7dd | 159 | { |
acorness | 9:d33e61dab7dd | 160 | if((x<15)&!pb1&!k) //Is it not at the right wall? |
acorness | 9:d33e61dab7dd | 161 | { |
acorness | 9:d33e61dab7dd | 162 | key.lock(); |
acorness | 9:d33e61dab7dd | 163 | x+=1; |
acorness | 9:d33e61dab7dd | 164 | Thread::wait(50); |
acorness | 9:d33e61dab7dd | 165 | k=1; |
acorness | 9:d33e61dab7dd | 166 | key.unlock(); |
acorness | 9:d33e61dab7dd | 167 | } |
acorness | 9:d33e61dab7dd | 168 | } |
acorness | 9:d33e61dab7dd | 169 | } |
acorness | 9:d33e61dab7dd | 170 | |
acorness | 9:d33e61dab7dd | 171 | void Pushbutton_Up(void const *args) //Check Up to change y |
acorness | 9:d33e61dab7dd | 172 | { |
acorness | 9:d33e61dab7dd | 173 | while(1) |
acorness | 9:d33e61dab7dd | 174 | { |
acorness | 9:d33e61dab7dd | 175 | if((y>0)&!pb4&!k) |
acorness | 9:d33e61dab7dd | 176 | { |
acorness | 9:d33e61dab7dd | 177 | key.lock(); |
acorness | 9:d33e61dab7dd | 178 | y-=1; |
acorness | 9:d33e61dab7dd | 179 | Thread::wait(50); |
acorness | 9:d33e61dab7dd | 180 | k=1; |
acorness | 9:d33e61dab7dd | 181 | key.unlock(); |
acorness | 9:d33e61dab7dd | 182 | } |
acorness | 9:d33e61dab7dd | 183 | } |
acorness | 9:d33e61dab7dd | 184 | } |
acorness | 9:d33e61dab7dd | 185 | |
acorness | 9:d33e61dab7dd | 186 | void Pushbutton_Down(void const *args) //Check Down to change y |
acorness | 9:d33e61dab7dd | 187 | { |
acorness | 9:d33e61dab7dd | 188 | while(1) |
acorness | 9:d33e61dab7dd | 189 | { |
acorness | 9:d33e61dab7dd | 190 | if((y<12)&!pb3&!k) |
acorness | 9:d33e61dab7dd | 191 | { |
acorness | 9:d33e61dab7dd | 192 | key.lock(); |
acorness | 9:d33e61dab7dd | 193 | y+=1; |
acorness | 9:d33e61dab7dd | 194 | Thread::wait(50); |
acorness | 9:d33e61dab7dd | 195 | k=1; |
acorness | 9:d33e61dab7dd | 196 | key.unlock(); |
acorness | 9:d33e61dab7dd | 197 | } |
acorness | 9:d33e61dab7dd | 198 | } |
acorness | 9:d33e61dab7dd | 199 | } |
acorness | 9:d33e61dab7dd | 200 | |
acorness | 9:d33e61dab7dd | 201 | |
acorness | 9:d33e61dab7dd | 202 | int main() |
acorness | 9:d33e61dab7dd | 203 | { |
acorness | 9:d33e61dab7dd | 204 | pb1.mode(PullUp); //initiation |
acorness | 9:d33e61dab7dd | 205 | pb2.mode(PullUp); |
acorness | 9:d33e61dab7dd | 206 | pb3.mode(PullUp); |
acorness | 9:d33e61dab7dd | 207 | pb4.mode(PullUp); |
acorness | 9:d33e61dab7dd | 208 | led1=1; |
acorness | 9:d33e61dab7dd | 209 | led2=1; |
acorness | 9:d33e61dab7dd | 210 | led3=1; |
acorness | 9:d33e61dab7dd | 211 | led4=1; |
acorness | 9:d33e61dab7dd | 212 | uLCD.baudrate(3000000); //Set Baud at MAX |
acorness | 9:d33e61dab7dd | 213 | |
acorness | 9:d33e61dab7dd | 214 | while(1) |
acorness | 9:d33e61dab7dd | 215 | { |
acorness | 9:d33e61dab7dd | 216 | uLCD.cls(); |
acorness | 9:d33e61dab7dd | 217 | switch(i) |
acorness | 9:d33e61dab7dd | 218 | { |
acorness | 9:d33e61dab7dd | 219 | case 2: |
acorness | 9:d33e61dab7dd | 220 | uLCD.printf("Simple Frogger"); //Start Screen |
acorness | 9:d33e61dab7dd | 221 | break; |
acorness | 9:d33e61dab7dd | 222 | case 1: |
acorness | 9:d33e61dab7dd | 223 | uLCD.printf("VICTORY"); |
acorness | 9:d33e61dab7dd | 224 | break; |
acorness | 9:d33e61dab7dd | 225 | case 0: |
acorness | 9:d33e61dab7dd | 226 | uLCD.printf("DEFEAT"); |
acorness | 9:d33e61dab7dd | 227 | } |
acorness | 9:d33e61dab7dd | 228 | while(pb1&pb2&pb3&pb4) //Push any button to start |
acorness | 9:d33e61dab7dd | 229 | { |
acorness | 9:d33e61dab7dd | 230 | led1=!led1; |
acorness | 9:d33e61dab7dd | 231 | wait(.1); |
acorness | 9:d33e61dab7dd | 232 | led2=!led2; |
acorness | 9:d33e61dab7dd | 233 | wait(.1); |
acorness | 9:d33e61dab7dd | 234 | led3=!led3; |
acorness | 9:d33e61dab7dd | 235 | wait(.1); |
acorness | 9:d33e61dab7dd | 236 | led4=!led4; |
acorness | 9:d33e61dab7dd | 237 | wait(.1); |
acorness | 9:d33e61dab7dd | 238 | } |
acorness | 9:d33e61dab7dd | 239 | |
acorness | 9:d33e61dab7dd | 240 | uLCD.cls(); |
acorness | 9:d33e61dab7dd | 241 | i=2; //Reset Win Condition |
acorness | 9:d33e61dab7dd | 242 | x=15; //Start at right wall |
acorness | 9:d33e61dab7dd | 243 | u=15; |
acorness | 9:d33e61dab7dd | 244 | y=12; //Start at bottom |
acorness | 9:d33e61dab7dd | 245 | v=12; |
acorness | 9:d33e61dab7dd | 246 | l=0; |
acorness | 9:d33e61dab7dd | 247 | gametime=1524; //Game Over condition |
acorness | 9:d33e61dab7dd | 248 | uLCD.locate(0,0); |
acorness | 9:d33e61dab7dd | 249 | uLCD.printf("Time:"); |
acorness | 9:d33e61dab7dd | 250 | uLCD.filled_rectangle(35,1,127,5,0x00FF00); //Time bar |
acorness | 9:d33e61dab7dd | 251 | uLCD.filled_rectangle(0,20,128,64,0x0000FF); //Water |
acorness | 9:d33e61dab7dd | 252 | |
acorness | 9:d33e61dab7dd | 253 | for(int j=0; j<5; j++) |
acorness | 9:d33e61dab7dd | 254 | { |
acorness | 9:d33e61dab7dd | 255 | brown[j].x1= rand()%12; |
acorness | 9:d33e61dab7dd | 256 | brown[j].x2= brown[j].x1+1; |
acorness | 9:d33e61dab7dd | 257 | brown[j].x3= brown[j].x1+2; |
acorness | 9:d33e61dab7dd | 258 | brown[j].y= j+1; |
acorness | 9:d33e61dab7dd | 259 | brown[j].u1= brown[j].x1; |
acorness | 9:d33e61dab7dd | 260 | brown[j].u2= brown[j].x2; |
acorness | 9:d33e61dab7dd | 261 | brown[j].u3= brown[j].x3; |
acorness | 9:d33e61dab7dd | 262 | brown[j].v=brown[j].y; |
acorness | 9:d33e61dab7dd | 263 | brown[j].speed= 1+rand()%2; |
acorness | 9:d33e61dab7dd | 264 | } |
acorness | 9:d33e61dab7dd | 265 | |
acorness | 9:d33e61dab7dd | 266 | for(int j=0;j<5;j++) |
acorness | 9:d33e61dab7dd | 267 | { |
acorness | 9:d33e61dab7dd | 268 | red[j].x= rand()%15; |
acorness | 9:d33e61dab7dd | 269 | red[j].y= 7+rand()%6; |
acorness | 9:d33e61dab7dd | 270 | red[j].u=red[j].x; |
acorness | 9:d33e61dab7dd | 271 | red[j].v=red[j].y; |
acorness | 9:d33e61dab7dd | 272 | } |
acorness | 9:d33e61dab7dd | 273 | |
acorness | 9:d33e61dab7dd | 274 | Thread push1(Pushbutton_Up); |
acorness | 9:d33e61dab7dd | 275 | Thread push2(Pushbutton_Down); |
acorness | 9:d33e61dab7dd | 276 | Thread push3(Pushbutton_Left); |
acorness | 9:d33e61dab7dd | 277 | Thread push4(Pushbutton_Right); |
acorness | 9:d33e61dab7dd | 278 | |
acorness | 9:d33e61dab7dd | 279 | while(i==2) //Check Win Condition |
acorness | 9:d33e61dab7dd | 280 | { |
acorness | 9:d33e61dab7dd | 281 | |
acorness | 9:d33e61dab7dd | 282 | Print_Background(); //Brackground print |
acorness | 9:d33e61dab7dd | 283 | Print_Frogger(x,y); //Frogger print |
acorness | 9:d33e61dab7dd | 284 | } |
acorness | 9:d33e61dab7dd | 285 | |
acorness | 9:d33e61dab7dd | 286 | } |
acorness | 9:d33e61dab7dd | 287 | } |
acorness | 9:d33e61dab7dd | 288 | |
acorness | 9:d33e61dab7dd | 289 | |
acorness | 9:d33e61dab7dd | 290 |