Mbed project for a FRDM-K64F microcontroller. Game made out of five sub-games including flappy birds, snake, stack, trivia and space invaders.
Fork of The_Children_of_Cronos_el15mggr by
cerinealib.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 #include "Gamepad.h" 00004 #include "cerinealib.h" 00005 00006 //constructor 00007 cerinealib::cerinealib() 00008 { 00009 00010 } 00011 int cerinealib::score () 00012 { 00013 return _score; 00014 } 00015 //initial values,only first block allowed to move(first=true) 00016 void cerinealib::init() 00017 { 00018 _size0=45; 00019 _size1=0; 00020 _size2=0; 00021 _size3=0; 00022 _size4=0; 00023 _pos0lft=20; 00024 _pos0rgt=65; 00025 _posy0=43; 00026 _k=1; 00027 _count=0; 00028 _score=0; 00029 _lifes=7; 00030 _border1=0; 00031 _border2=0; 00032 _border3=0; 00033 _border4=0,_border5=0; 00034 _first=true; 00035 _second=false; 00036 _third=false; 00037 _fourth=false; 00038 _fith=false; 00039 _dead=false; 00040 _pos1.x=85; 00041 _pos1.y=_posy0-7; 00042 _pos2.x=-_size1; 00043 _pos2.y=_pos1.y-7; 00044 _pos3.x=85; 00045 _pos3.y=_pos2.y-7; 00046 _pos4.x=-_size3; 00047 _pos4.y=_pos3.y-7; 00048 _pos5.x=85; 00049 _pos5.y=_pos4.y-7; 00050 } 00051 00052 //gameover screen with score on it 00053 //allows to restart the game 00054 void cerinealib::gameover(N5110 &lcd,DigitalOut &start) 00055 { 00056 lcd.clear(); 00057 lcd.printString("GAME OVER",10,2); 00058 sprintf(_buffer,"Score=%2d ",_score); 00059 lcd.printString(_buffer,10,4); // print formatted data to buffer 00060 lcd.refresh(); 00061 00062 if(start) { 00063 _lifes=_lifes-1; 00064 _dead=false; 00065 _count=0; 00066 _score=0; 00067 _pos0lft=20; 00068 _pos0rgt=65; 00069 _pos1.x=85; 00070 _pos2.x=-_size1; 00071 _pos3.x=85; 00072 _pos4.x=-_size2; 00073 _pos5.x=85; 00074 _size0=45; 00075 _size1=0; 00076 _size2=0; 00077 _size3=0; 00078 _size4=0; 00079 _pos1.y=_posy0-7; 00080 _pos2.y=_pos1.y-7; 00081 _pos3.y=_pos2.y-7; 00082 _pos4.y=_pos3.y-7; 00083 _pos5.y=_pos4.y-7; 00084 _border1=0; 00085 _border2=0; 00086 _border3=0; 00087 _border4=0,_border5=0; 00088 _first=true; 00089 _second=false; 00090 _third=false; 00091 _fourth=false; 00092 _fith=false; 00093 } 00094 } 00095 00096 00097 00098 00099 00100 void cerinealib::draw(N5110 &lcd) 00101 { 00102 lcd.clear(); 00103 //****true conditions**********// 00104 //draws moving blocks 00105 if(_first) { 00106 lcd.drawRect(_pos1.x,_pos1.y,_size0,size,FILL_BLACK); 00107 } 00108 00109 if(_second) { 00110 lcd.drawRect(_pos2.x,_pos2.y,_size1,size,FILL_BLACK); 00111 } 00112 00113 if(_third) { 00114 lcd.drawRect(_pos3.x,_pos3.y,_size2,size,FILL_BLACK); 00115 } 00116 00117 if(_fourth) { 00118 lcd.drawRect(_pos4.x,_pos4.y,_size3,size,FILL_BLACK); 00119 } 00120 00121 if(_fith) { 00122 lcd.drawRect(_pos5.x,_pos5.y,_size4,size,FILL_BLACK); 00123 } 00124 00125 //*****false conditions**********// 00126 //draws dropped blocks 00127 00128 if(!_first) { 00129 //out of the first block 00130 if(_pos1.x>_pos0rgt) { 00131 _dead=true; 00132 } 00133 //inside of the first block 00134 if(_pos1.x>_pos0lft && _pos1.x<_pos0rgt) { 00135 _size1=_size0-(_pos1.x+_size0-_pos0rgt); //new size of the block leaving only the part over the initial block in which it has falled 00136 _border1=_pos1.x; //set new left extreme, in this case the same as its x position 00137 lcd.drawRect(_border1,_pos1.y,_size1,size,FILL_BLACK); 00138 } 00139 if(_pos1.x<_pos0lft && (_pos1.x+_size0)>_pos0rgt) {//in the range 00140 _size1=_size0-(_pos0lft-_pos1.x); 00141 _border1=_pos0lft; //in this case the new left border corresponds to the previous block x position 00142 lcd.drawRect(_border1,_pos1.y,_size1,size,FILL_BLACK); 00143 } 00144 if(_pos1.x<_pos0lft && (_pos1.x+_size0)<_pos0rgt) {//out of range 00145 _dead=true; 00146 } 00147 if(_pos1.x==_pos0lft && _pos1.x+_size0==_pos0rgt) {//in the range.Perfect block dropped 00148 _size1=_size0; 00149 _border1=_pos0lft; 00150 lcd.drawRect(_border1,_pos1.y,_size1,size,FILL_BLACK); 00151 } 00152 } 00153 00154 00155 if(!_second && !_first) { 00156 if(_pos2.x>(_border1+_size1)) { 00157 _dead=true; 00158 } 00159 if(_pos2.x>_border1 && _pos2.x<(_border1+_size1)) { 00160 _size2=_size1-(_pos2.x+_size1-(_border1+_size1)); 00161 _border2=_pos2.x; 00162 lcd.drawRect(_border2,_pos2.y,_size2,size,FILL_BLACK); 00163 } 00164 if(_pos2.x<_border1 && (_pos2.x+_size1)<(_border1+_size1)) { 00165 _size2=_size1-(_border1-_pos2.x); 00166 _border2=_border1; 00167 lcd.drawRect(_border2,_pos2.y,_size2,size,FILL_BLACK); 00168 } 00169 if(_pos2.x<_border1 && (_pos2.x+_size1)>(_border1+_size1)) { 00170 _dead=true; 00171 } 00172 if(_pos2.x==_border1 && _pos2.x+_size1==(_border1+_size1)) { 00173 _size2=_size1; 00174 _border2=_border1; 00175 lcd.drawRect(_border2,_pos2.y,_size2,size,FILL_BLACK); 00176 } 00177 } 00178 00179 if(!_second && !_first && !_third) { 00180 if(_pos3.x>(_border2+_size2)) { 00181 _dead=true; 00182 } 00183 if(_pos3.x>_border2 && _pos3.x<(_border2+_size2)) { 00184 _size3=_size2-(_pos3.x+_size2-(_border2+_size2)); 00185 _border3=_pos3.x; 00186 lcd.drawRect(_border3,_pos3.y,_size3,size,FILL_BLACK); 00187 } 00188 if(_pos3.x<_border2 && (_pos3.x+_size2)<(_border2+_size2)) { 00189 _size3=_size2-(_border2-_pos3.x); 00190 _border3=_border2; 00191 lcd.drawRect(_border3,_pos3.y,_size3,size,FILL_BLACK); 00192 } 00193 if(_pos3.x<_border2 && (_pos3.x+_size2)>(_border2+_size2)) { 00194 _dead=true; 00195 } 00196 if(_pos3.x==_border2 && _pos3.x+_size2==(_border2+_size2)) { 00197 _size3=_size2; 00198 _border3=_border2; 00199 lcd.drawRect(_border3,_pos3.y,_size3,size,FILL_BLACK); 00200 } 00201 } 00202 00203 if(!_second && !_first && !_third && !_fourth) { 00204 if(_pos4.x>(_border3+_size3)) { 00205 _dead=true; 00206 } 00207 if(_pos4.x>_border3 && _pos4.x<(_border3+_size3)) { 00208 _size4=_size3-(_pos4.x+_size3-(_border3+_size3)); 00209 _border4=_pos4.x; 00210 lcd.drawRect(_border4,_pos4.y,_size4,size,FILL_BLACK); 00211 } 00212 if(_pos4.x<_border3 && (_pos4.x+_size3)<(_border3+_size3)) { 00213 _size4=_size3-(_border3-_pos4.x); 00214 _border4=_border3; 00215 lcd.drawRect(_border4,_pos4.y,_size4,size,FILL_BLACK); 00216 } 00217 if(_pos4.x<_border3 && (_pos4.x+_size3)>(_border3+_size3)) { 00218 _dead=true; 00219 } 00220 if(_pos4.x==_border3 && _pos4.x+_size3==(_border3+_size3)) { 00221 _size4=_size3; 00222 _border4=_border3; 00223 lcd.drawRect(_border4,_pos4.y,_size4,size,FILL_BLACK); 00224 } 00225 } 00226 00227 if(!_second && !_first && !_third && !_fourth && !_fith) { 00228 if(_pos5.x>(_border4+_size4)) { 00229 _dead=true; 00230 } 00231 if(_pos5.x>_border4 && _pos5.x<(_border4+_size4)) { 00232 _size5=_size4-(_pos5.x+_size4-(_border4+_size4)); 00233 _border5=_pos5.x; 00234 lcd.drawRect(_border5,_pos5.y,_size5,size,FILL_BLACK); 00235 } 00236 if(_pos5.x<_border4 && (_pos5.x+_size4)<(_border4+_size4)) { 00237 _size5=_size4-(_border4-_pos5.x); 00238 _border5=_border4; 00239 lcd.drawRect(_border5,_pos5.y,_size5,size,FILL_BLACK); 00240 } 00241 if(_pos5.x<_border4 && (_pos5.x+_size4)>(_border4+_size4)) { 00242 _dead=true; 00243 } 00244 if(_pos5.x==_border4 && _pos5.x+_size4==(_border4+_size4)) { 00245 _border5=_border4; 00246 lcd.drawRect(_border5,_pos5.y,_size4,size,FILL_BLACK); 00247 } 00248 } 00249 //prints difficulty level indicator in circles.When the difficulty level increases, a circle fills black 00250 sprintf(_buffer,"%2d ",_score); 00251 lcd.printString(_buffer,0,0); 00252 if(_score<=6) { 00253 lcd.drawCircle(50,3,3,FILL_BLACK); 00254 lcd.drawCircle(60,3,3,FILL_TRANSPARENT); 00255 lcd.drawCircle(70,3,3,FILL_TRANSPARENT); 00256 } 00257 if(_score>6 && _score<=12) { 00258 lcd.drawCircle(50,3,3,FILL_BLACK); 00259 lcd.drawCircle(60,3,3,FILL_BLACK); 00260 lcd.drawCircle(70,3,3,FILL_TRANSPARENT); 00261 } 00262 if(_score>12) { 00263 lcd.drawCircle(50,3,3,FILL_BLACK); 00264 lcd.drawCircle(60,3,3,FILL_BLACK); 00265 lcd.drawCircle(70,3,3,FILL_BLACK); 00266 } 00267 lcd.drawRect(_pos0lft,_posy0,_size0,size,FILL_BLACK); 00268 lcd.refresh(); 00269 00270 } 00271 00272 void cerinealib::commands(DigitalOut &b,DigitalOut &a,DigitalOut &x,DigitalOut &y,Gamepad &pad) 00273 { 00274 00275 if(b||a||x||y) { 00276 wait(0.1); //small button debouncing 00277 if(b||a||x||y) { 00278 pad.tone(NOTE_C6,0.5); 00279 _count++; //block counting added everytime a button is pressed 00280 } 00281 } 00282 00283 00284 00285 if(_count==1 && _first) { 00286 _first=false; //first set it to false,then set the next one to true 00287 _second=true; 00288 _score++; 00289 } 00290 00291 if(_second && _count==2) { 00292 _second=false; 00293 _third=true; 00294 _score++; 00295 } 00296 00297 if(_third && _count==3) { 00298 _third=false; 00299 _fourth=true; 00300 _score++; 00301 } 00302 00303 if(_fourth && _count==4) { 00304 _fourth=false; 00305 _fith=true; 00306 _score++; 00307 } 00308 00309 if(_fith && _count==5) { 00310 _fith=false; 00311 _score++; 00312 } 00313 00314 00315 } 00316 00317 00318 void cerinealib::updatescr() 00319 { 00320 00321 //**********true conditions************// 00322 //movement of the blocks in the screen from left to right or viceversa 00323 if(_first) { 00324 _pos1.x=_pos1.x-_k; 00325 if(_pos1.x==0) { 00326 _k=-1; 00327 } 00328 if ((_pos1.x+45)==84 && _k==-1) { 00329 _k=1; 00330 } 00331 } 00332 00333 if(_second) { 00334 if(_pos2.x<0) { 00335 _k=1; 00336 } 00337 _pos2.x=_pos2.x+_k; 00338 if(_pos2.x==0) { 00339 _k=1; 00340 } 00341 if ((_pos2.x+_size1)==84 && _k==1) { 00342 _k=-1; 00343 } 00344 } 00345 00346 if(_third) { 00347 if(_pos3.x>84) { 00348 _k=1; 00349 } 00350 _pos3.x=_pos3.x-_k; 00351 if(_pos3.x==0) { 00352 _k=-1; 00353 } 00354 if ((_pos3.x+_size2)==84 && _k==-1) { 00355 _k=1; 00356 } 00357 } 00358 00359 if(_fourth) { 00360 if(_pos4.x<0) { 00361 _k=1; 00362 } 00363 _pos4.x=_pos4.x+_k; 00364 if(_pos4.x==0) { 00365 _k=1; 00366 } 00367 if ((_pos4.x+_size3)==84 && _k==1) { 00368 _k=-1; 00369 } 00370 } 00371 00372 if(_fith) { 00373 _pos5.x=_pos5.x-_k; 00374 _pos5.y=_pos4.y-7; 00375 00376 if(_pos5.x>84) { 00377 _k=1; 00378 } 00379 if(_pos5.x<=0) { 00380 _k=-1; 00381 } 00382 if ((_pos5.x+_size4)==84 && _k==-1) { 00383 _k=1; 00384 } 00385 } 00386 00387 00388 //**************false conditions**********// 00389 00390 //blocks dropped movement 00391 if (!_first) { 00392 _pos1.y=_posy0-size; 00393 _pos1.x=_pos1.x; 00394 } 00395 00396 if (!_second) { 00397 _pos2.y=_pos1.y-size; 00398 _pos2.x=_pos2.x; 00399 } 00400 00401 if (!_third) { 00402 _pos3.y=_pos2.y-size; 00403 _pos3.x=_pos3.x; 00404 } 00405 00406 if (!_fourth) { 00407 _pos4.y=_pos3.y-size; 00408 _pos4.x=_pos4.x; 00409 } 00410 //resets the loop 00411 //everything switches position to the next one 00412 if (!_fith && _count==5) { 00413 _pos5.y=_pos4.y-size; 00414 _pos5.x=_pos5.x; 00415 _pos0lft=_pos1.x; 00416 _pos1.x=_pos2.x; 00417 _pos3.x=_pos4.x; 00418 _pos4.x=_pos5.x; 00419 _size0=_size1; 00420 _size1=_size2; 00421 _size2=_size3; 00422 _size3=_size4; 00423 _size4=_size5; 00424 _border1=_border2; 00425 _border2=_border3; 00426 _border3=_border4; 00427 _border4=_border5; 00428 //sets 5 to move again with the previous block size 00429 _fith=true; 00430 _pos5.x=-_size4; 00431 //goes back to 4 the count 00432 _count=4; 00433 } 00434 } 00435 //increse of velocities by waits depending on the score 00436 void cerinealib::velocities() 00437 { 00438 if(_score<=6) { 00439 wait(0.05); 00440 } else if(_score>=6 && _score<=12) { 00441 wait(0.03); 00442 } else if(_score>12) { 00443 wait(0.01); 00444 } 00445 } 00446 00447 //sends dead variable to main file 00448 bool cerinealib::die () 00449 { 00450 if (_dead) { 00451 return true; 00452 } 00453 return false; 00454 } 00455
Generated on Wed Jul 13 2022 07:38:17 by
1.7.2
