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
hydralib.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 #include "Gamepad.h" 00004 #include "hydralib.h" 00005 00006 00007 //constructor 00008 hydralib::hydralib() 00009 { 00010 00011 } 00012 00013 int hydralib::score() 00014 { 00015 return _score; 00016 } 00017 00018 //sets positions and values to initial ones and generated new random food 00019 void hydralib::init() 00020 { 00021 _food.x=rand() %78+3; 00022 _food.y=rand() %30+3; 00023 _pos.x=widthA/2; 00024 _pos.y=heightA/2; 00025 _vel.x=0; 00026 _vel.y=0; 00027 _lenght=0; 00028 _score=0; 00029 _lifes=7; 00030 _pause=true; 00031 } 00032 00033 //switches all the leds neccessary 00034 void hydralib::leds(Gamepad &pad) 00035 { 00036 for(int i=1; i<_lifes; i++) { 00037 pad.led(i,1); 00038 } 00039 } 00040 void hydralib::pausegame(DigitalOut &l,DigitalOut &r,N5110 &lcd,DigitalOut &back) 00041 { 00042 if(l||r||back) { 00043 _pause=true; 00044 _vel.x=0; 00045 _vel.y=0; 00046 } 00047 00048 } 00049 //first prints game over screen with the score on it 00050 //then it allows to restart the game to initial values by pressing start 00051 void hydralib::gameover(N5110 &lcd,DigitalOut &start,Gamepad &pad) 00052 { 00053 00054 lcd.clear(); 00055 lcd.printString("GAME OVER",10,2); 00056 sprintf(_buffer,"Score=%2d ",_score); 00057 lcd.printString(_buffer,15,4); // print formatted data to buffer 00058 lcd.refresh(); 00059 00060 if(start) { 00061 pad.leds_off(); 00062 _lifes=_lifes-1; 00063 _vel.x=0; 00064 _vel.y=0; 00065 _pos.x=widthA/2; 00066 _pos.y=heightA/2; 00067 _lenght=0; 00068 _score=0; 00069 00070 } 00071 } 00072 00073 //checks if the head pixels match with the food pixels 00074 bool hydralib::eat() 00075 { 00076 if ((_pos.x==_food.x && _pos.y==_food.y)|| 00077 (_pos.x==_food.x && _pos.y==_food.y+1)|| 00078 (_pos.x==_food.x && _pos.y+1==_food.y)|| 00079 (_pos.x==_food.x && _pos.y+1==_food.y+1)|| 00080 (_pos.x==_food.x+1 && _pos.y==_food.y)|| 00081 (_pos.x==_food.x+1 && _pos.y==_food.y+1)|| 00082 (_pos.x==_food.x+1 && _pos.y+1==_food.y)|| 00083 (_pos.x==_food.x+1 && _pos.y+1==_food.y+1)|| 00084 (_pos.x+1==_food.x && _pos.y==_food.y)|| 00085 (_pos.x+1==_food.x && _pos.y==_food.y+1)|| 00086 (_pos.x+1==_food.x && _pos.y+1==_food.y)|| 00087 (_pos.x+1==_food.x && _pos.y+1==_food.y+1)|| 00088 (_pos.x+1==_food.x+1 && _pos.y==_food.y)|| 00089 (_pos.x+1==_food.x+1 && _pos.y==_food.y+1)|| 00090 (_pos.x+1==_food.x+1 && _pos.y+1==_food.y)|| 00091 (_pos.x+1==_food.x+1 && _pos.y+1==_food.y+1) 00092 ) { 00093 return true; 00094 } 00095 return false; 00096 } 00097 00098 //depending on the score,fills certain rectangles to black or leave them transparent 00099 void hydralib::rectangles(N5110 &lcd) 00100 { 00101 lcd.drawRect(3,39,3,3,FILL_TRANSPARENT); 00102 lcd.drawRect(15,39,3,3,FILL_TRANSPARENT); 00103 lcd.drawRect(3,44,3,3,FILL_TRANSPARENT); 00104 lcd.drawRect(15,44,3,3,FILL_TRANSPARENT); 00105 lcd.drawRect(9,41,3,3,FILL_TRANSPARENT); 00106 if(_score>9 && _score<=29) { 00107 lcd.drawRect(3,39,3,3,FILL_BLACK); 00108 } 00109 if(_score>29 && _score<=44) { 00110 lcd.drawRect(3,39,3,3,FILL_BLACK); 00111 lcd.drawRect(15,39,3,3,FILL_BLACK); 00112 } 00113 if(_score>44 && _score<=59) { 00114 lcd.drawRect(3,39,3,3,FILL_BLACK); 00115 lcd.drawRect(15,39,3,3,FILL_BLACK); 00116 lcd.drawRect(9,41,3,3,FILL_BLACK); 00117 } 00118 if(_score>59 && _score<=74) { 00119 lcd.drawRect(3,39,3,3,FILL_BLACK); 00120 lcd.drawRect(15,39,3,3,FILL_BLACK); 00121 lcd.drawRect(9,41,3,3,FILL_BLACK); 00122 lcd.drawRect(3,44,3,3,FILL_BLACK); 00123 } 00124 if(_score>74) { 00125 lcd.drawRect(3,39,3,3,FILL_BLACK); 00126 lcd.drawRect(15,39,3,3,FILL_BLACK); 00127 lcd.drawRect(9,41,3,3,FILL_BLACK); 00128 lcd.drawRect(3,44,3,3,FILL_BLACK); 00129 lcd.drawRect(15,44,3,3,FILL_BLACK); 00130 } 00131 } 00132 00133 //by using waits at the end of the game and reducing them, i can control the game speed 00134 //i reccon i could have used tickers instead but i have some problems trying to implement it 00135 //so i left the waits that they didnt affect at all to the performance of my game 00136 void hydralib::velocities() 00137 { 00138 if(_score<=9) { 00139 wait(0.08); 00140 } 00141 if(_score>9 && _score<=29) { 00142 wait(0.06); 00143 } 00144 if(_score>29 && _score<=44) { 00145 wait(0.04); 00146 } 00147 if(_score>44 && _score<=59) { 00148 wait(0.035); 00149 } 00150 if(_score>59 && _score<=74) { 00151 wait(0.025); 00152 } 00153 if(_score>74) { 00154 wait(0.015); 00155 } 00156 } 00157 00158 //checks if the head pixels match with any tail parts pixels 00159 bool hydralib::touch() 00160 { 00161 int j=0; 00162 for(int i=_lenght+1; i>0; i--) { 00163 if(_pos.x==_body[i].X && _pos.y==_body[i].Y) { 00164 j=1; 00165 } 00166 } 00167 if (j==0) { 00168 return false; 00169 } 00170 return true; 00171 } 00172 00173 //checks if the head touches the extremes of the screen or the tail of the snake 00174 bool hydralib::border() 00175 { 00176 00177 if (_pos.x==0 || _pos.x==81) { 00178 return false; 00179 } else if(_pos.y==0 || _pos.y==35) { 00180 return false; 00181 } else if (hydralib::touch()) { 00182 return false; 00183 } else { 00184 return true; 00185 } 00186 } 00187 00188 //draws difficulty rectangles,borders,score in the screen,food,snake head and tail 00189 //and depending on the movement of it clears certain pixels for improving the looking of it 00190 void hydralib::draw(N5110 &lcd) 00191 { 00192 00193 lcd.clear(); 00194 if(_pause) { 00195 lcd.printChar('P',4,5); 00196 } 00197 lcd.drawRect(0,0,84,38,FILL_TRANSPARENT); 00198 hydralib::rectangles(lcd); 00199 00200 for (int i=_lenght; i>0; i--) { 00201 00202 lcd.drawRect(_body[i].X,_body[i].Y,sizeA,sizeA,FILL_BLACK); 00203 } 00204 sprintf(_buffer,"Score=%2d",_score); 00205 lcd.printString(_buffer,33,5); 00206 lcd.drawRect(_food.x,_food.y,sizeA,sizeA,FILL_BLACK); 00207 lcd.drawRect(_pos.x,_pos.y,sizeA,sizeA,FILL_BLACK); 00208 lcd.setPixel(_pos.x+1,_pos.y+1,false); 00209 if(_vel.y== -1 && _vel.x==0) { 00210 lcd.setPixel(_pos.x+1,_pos.y,false); 00211 } 00212 if(_vel.y==1 && _vel.x==0) { 00213 lcd.setPixel(_pos.x+1,_pos.y+2,false); 00214 } 00215 if(_vel.x==-1 && _vel.y==0) { 00216 lcd.setPixel(_pos.x,_pos.y+1,false); 00217 } 00218 if(_vel.x==1 && _vel.y==0) { 00219 lcd.setPixel(_pos.x+2,_pos.y+1,false); 00220 } 00221 00222 lcd.refresh(); 00223 } 00224 00225 //if the snake moves one pixel, it makes all the tail element to stay in the 00226 //position the previous element was , making the head as the first element dependent on the velocity 00227 void hydralib::updatescr() 00228 { 00229 if(!_pause) { 00230 _body[0].X=_pos.x; 00231 _body[0].Y=_pos.y; 00232 for(int i=_lenght; i>0; i--) { 00233 00234 _body[i].X=_body[i-1].X; 00235 _body[i].Y=_body[i-1].Y; 00236 } 00237 00238 _pos.x=_pos.x+_vel.x; 00239 _pos.y=_pos.y+_vel.y; 00240 } 00241 } 00242 00243 //depending on players input it will set the proper velocity that will lead to the addecuate direction 00244 void hydralib::commands(Gamepad &pad,DigitalOut &a,DigitalOut &b,DigitalOut &x,DigitalOut &y) 00245 { 00246 Direction d = pad.get_direction(); 00247 if (_vel.x==0) { 00248 if (d==W) { 00249 _pause=false; 00250 _vel.x=-1; 00251 _vel.y=0; 00252 } 00253 if(d==E) { 00254 _pause=false; 00255 _vel.x=1; 00256 _vel.y=0; 00257 } 00258 } 00259 if(_vel.y==0) { 00260 if (d==N) { 00261 _pause=false; 00262 _vel.y= -1; 00263 _vel.x=0; 00264 } 00265 if(d==S) { 00266 _pause=false; 00267 _vel.y= 1; 00268 _vel.x=0; 00269 } 00270 } 00271 00272 if(_vel.x==0) { 00273 if (b) { 00274 _pause=false; 00275 _vel.x=1; 00276 _vel.y=0; 00277 } 00278 if(x) { 00279 _pause=false; 00280 _vel.x=-1; 00281 _vel.y=0; 00282 } 00283 } 00284 if(_vel.y==0) { 00285 if (a) { 00286 _pause=false; 00287 _vel.y= 1; 00288 _vel.x=0; 00289 } 00290 if(y) { 00291 _pause=false; 00292 _vel.y= -1; 00293 _vel.x=0; 00294 } 00295 } 00296 } 00297 //sets randmon food and increases lenght and score 00298 void hydralib::auxiliar() 00299 { 00300 _food.x= rand()%78+3; 00301 _food.y= rand()%30+3; 00302 _lenght++; 00303 _score++; 00304 }
Generated on Wed Jul 13 2022 07:38:17 by
1.7.2
