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
aurigaslib.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 #include "Gamepad.h" 00004 #include "aurigaslib.h" 00005 //constructor 00006 aurigaslib::aurigaslib() 00007 { 00008 00009 } 00010 00011 int aurigaslib::score () 00012 { 00013 return _score; 00014 } 00015 //initial values and new random y top pipes positions 00016 void aurigaslib::init() 00017 { 00018 _check=false; 00019 _force=-1; 00020 _vel=0; 00021 _gravity=0; 00022 _velpipe=1; 00023 _score=0; 00024 _lifes=7; 00025 _pospipe0=60; 00026 _pospipe1=_pospipe0+21; 00027 _pospipe2=_pospipe1+21; 00028 _pospipe3=_pospipe2+21; 00029 _k=0; 00030 _pipe0.top= rand ()% 35+3; 00031 _pipe0.bottom= heightC-_pipe0.top-10; 00032 _pipe1.top= rand ()% 35+3; 00033 _pipe1.bottom=heightC-_pipe1.top-10; 00034 _pipe2.top= rand ()% 35+3; 00035 _pipe2.bottom=heightC-_pipe2.top-10; 00036 _pipe3.top= rand ()% 35+3; 00037 _pipe3.bottom=heightC-_pipe3.top-10; 00038 _posbird.x=10; 00039 _posbird.y=heightC/2; 00040 } 00041 //leds on depending on how many lifes left 00042 void aurigaslib::leds(Gamepad &pad) 00043 { 00044 for(int i=1; i<_lifes; i++) { 00045 pad.led(i,1); 00046 } 00047 } 00048 00049 //gameover screen with score displayed on the screen 00050 //let the player restart the game to its initial values 00051 void aurigaslib::gameover(N5110 &lcd,Gamepad &pad,DigitalOut &l,DigitalOut &r) 00052 { 00053 00054 lcd.clear(); 00055 lcd.printString("GAME OVER",15,0); 00056 sprintf(_buffer,"Score=%2d ",_score); 00057 lcd.printString(_buffer,18,1); // print formatted data to buffer 00058 lcd.printString("PRESS L/R",10,4); 00059 lcd.printString("for new game",5,5); 00060 lcd.drawLine(0,23,84,23,FILL_BLACK); 00061 lcd.refresh(); 00062 00063 if(l||r) { 00064 _k=0; 00065 _vel=0; 00066 _gravity=0; 00067 pad.leds_off(); 00068 _lifes=_lifes-1; 00069 pad.leds_off(); 00070 _posbird.x=10; 00071 _posbird.y=heightC/2; 00072 _pospipe0=60,_pospipe1=_pospipe0+21,_pospipe2=_pospipe1+21,_pospipe3=_pospipe2+21; 00073 _score=0; 00074 _check=true; 00075 lcd.clear(); 00076 } 00077 } 00078 00079 00080 //checks if the bird hits the pipes 00081 bool aurigaslib::touch() 00082 { 00083 if( 00084 ((_posbird.x==_pospipe0 ||_posbird.x==_pospipe0+1 || _posbird.x==_pospipe0+2 || _posbird.x==_pospipe0+3) && (_posbird.y<=_pipe0.top || _posbird.y+2>=(heightC-_pipe0.bottom)))|| 00085 ((_posbird.x==_pospipe1 ||_posbird.x==_pospipe1+1 || _posbird.x==_pospipe1+2 || _posbird.x==_pospipe1+3) && (_posbird.y<=_pipe1.top || _posbird.y+2>=(heightC-_pipe1.bottom)))|| 00086 ((_posbird.x==_pospipe2 ||_posbird.x==_pospipe2+1 || _posbird.x==_pospipe2+2 || _posbird.x==_pospipe2+3) && (_posbird.y<=_pipe2.top || _posbird.y+2>=(heightC-_pipe2.bottom)))|| 00087 ((_posbird.x==_pospipe3 ||_posbird.x==_pospipe3+1 || _posbird.x==_pospipe3+2 || _posbird.x==_pospipe3+3) && (_posbird.y<=_pipe3.top || _posbird.y+2>=(heightC-_pipe3.bottom)))) { 00088 00089 return false; 00090 } 00091 return true; 00092 } 00093 00094 //draws components 00095 //the pipes are drawed as follow:there are two main rectangles drawed over the pipe x position 00096 //the first one is the top part of the pipe.It starts from y=0 until the randomly generated value pipe.top 00097 //the second one is the bottom part of the pipe.It starts from (height-bottom).Bottom variable is the 00098 //position corresponding with the top one plus 1o pixels. Then it goes until the bottom of the screen with 00099 //the size of the bottom variable.In this way,the bird will have 10 pixels to pass in between the pipes 00100 void aurigaslib::draw(N5110 &lcd) 00101 { 00102 lcd.clear(); 00103 if(_k==0) { 00104 lcd.printString("PRESS",0,4); 00105 lcd.printString("START",0,5); 00106 } 00107 lcd.drawRect(_posbird.x,_posbird.y,3,3,FILL_BLACK); 00108 lcd.setPixel(_posbird.x-1,_posbird.y+1,1); 00109 lcd.setPixel(_posbird.x+3,_posbird.y+1,1); 00110 lcd.setPixel(_posbird.x+1,_posbird.y+1,0); 00111 lcd.drawRect(_pospipe0,0,sizeC,_pipe0.top,FILL_BLACK); 00112 lcd.drawRect(_pospipe0,heightC-_pipe0.bottom,sizeC,_pipe0.bottom,FILL_BLACK); 00113 lcd.drawRect(_pospipe1,0,sizeC,_pipe1.top,FILL_BLACK); 00114 lcd.drawRect(_pospipe1,heightC-_pipe1.bottom,sizeC,_pipe1.bottom,FILL_BLACK); 00115 lcd.drawRect(_pospipe2,0,sizeC,_pipe2.top,FILL_BLACK); 00116 lcd.drawRect(_pospipe2,heightC-_pipe2.bottom,sizeC,_pipe2.bottom,FILL_BLACK); 00117 lcd.drawRect(_pospipe3,0,sizeC,_pipe3.top,FILL_BLACK); 00118 lcd.drawRect(_pospipe3,heightC-_pipe3.bottom,sizeC,_pipe3.bottom,FILL_BLACK); 00119 00120 lcd.refresh(); 00121 } 00122 00123 //updates movements and allows to jump with a determined force against the gravity 00124 void aurigaslib::updatescr(DigitalOut &a,DigitalOut &b,DigitalOut &x,DigitalOut &y,Gamepad &pad) 00125 { 00126 if (_posbird.y<0) { 00127 _posbird.y=0; 00128 } 00129 00130 if(_posbird.y>heightC) { 00131 _posbird.y=heightC-_k; 00132 _vel=0; 00133 } 00134 00135 00136 if((a | b | x | y)&& _k==1) { 00137 pad.tone(NOTE_DS5,0.5); 00138 _vel=_force; 00139 _posbird.y+=_vel; 00140 } 00141 00142 _vel+=_gravity; 00143 _posbird.y+=_vel; 00144 _pospipe0=_pospipe0-_k; 00145 _pospipe1=_pospipe1-_k; 00146 _pospipe2=_pospipe2-_k; 00147 _pospipe3=_pospipe3-_k; 00148 00149 if(_pospipe0<0) { 00150 _pospipe0=84; 00151 } 00152 if(_pospipe1<0) { 00153 _pospipe1=84; 00154 } 00155 if(_pospipe2<0) { 00156 _pospipe2=84; 00157 } 00158 if(_pospipe3<0) { 00159 _pospipe3=84; 00160 } 00161 00162 if(_posbird.x==_pospipe0) { 00163 _score+=10; 00164 } 00165 if(_posbird.x==_pospipe1) { 00166 _score+=10; 00167 } 00168 if(_posbird.x==_pospipe2) { 00169 _score+=10; 00170 } 00171 if(_posbird.x==_pospipe3) { 00172 _score+=10; 00173 } 00174 } 00175 00176 //generates random top values and set the bottom as the height minus the top minus a margin of 10 pixels for leaving some space in between 00177 00178 void aurigaslib::pipegenerator(Gamepad &pad,DigitalOut &start) 00179 { 00180 if(start) { 00181 _k=1; 00182 _gravity=0.3; 00183 } 00184 aurigaslib::leds(pad); 00185 if(_pospipe0==0) { 00186 00187 _pipe0.top= rand ()% 35+3; 00188 _pipe0.bottom= heightC-_pipe0.top-10; 00189 } 00190 if(_pospipe1==0) { 00191 00192 _pipe1.top= rand ()% 35+3; 00193 _pipe1.bottom= heightC-_pipe1.top-10; 00194 } 00195 if(_pospipe2==0) { 00196 00197 _pipe2.top= rand ()% 35+3; 00198 _pipe2.bottom=heightC-_pipe2.top-10; 00199 } 00200 if(_pospipe3==0) { 00201 00202 _pipe3.top= rand ()% 35+3; 00203 _pipe3.bottom=heightC-_pipe3.top-10; 00204 } 00205 00206 }
Generated on Wed Jul 13 2022 07:38:17 by
1.7.2
