In this article we’ll add scoring and a hi-score. A video is available also here:
https://www.youtube.com/endscreen?feature=vm&v=bHiYkNIQ824
Here’s the full code to paste into your IDE, click to expand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
#include <Adafruit_SSD1306.h> #include <Adafruit_GFX.h> #include <EEPROM.h> // DISPLAY SETTINGS #define OLED_ADDRESS 0x3C #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 // Input settings #define FIRE_BUT 6 #define RIGHT_BUT 5 #define LEFT_BUT 4 // Mothership #define MOTHERSHIP_HEIGHT 4 #define MOTHERSHIP_WIDTH 16 #define MOTHERSHIP_SPEED 2 #define MOTHERSHIP_SPAWN_CHANCE 250 //HIGHER IS LESS CHANCE OF SPAWN #define DISPLAY_MOTHERSHIP_BONUS_TIME 20 // how long bonus stays on screen for displaying mothership // Alien Settings #define NUM_ALIEN_COLUMNS 7 #define NUM_ALIEN_ROWS 3 #define X_START_OFFSET 6 #define SPACE_BETWEEN_ALIEN_COLUMNS 5 #define LARGEST_ALIEN_WIDTH 11 #define SPACE_BETWEEN_ROWS 9 #define INVADERS_DROP_BY 4 // pixel amount that invaders move down by #define INVADERS_SPEED 12 // speed of movement, lower=faster. #define INVADER_HEIGHT 8 #define EXPLOSION_GFX_TIME 7 // How long an ExplosionGfx remains on screen before dissapearing #define INVADERS_Y_START MOTHERSHIP_HEIGHT-1 // Player settingsc #define TANKGFX_WIDTH 13 #define TANKGFX_HEIGHT 8 #define PLAYER_X_MOVE_AMOUNT 2 #define PLAYER_Y_START 56 #define PLAYER_X_START 0 #define MISSILE_HEIGHT 4 #define MISSILE_WIDTH 1 #define MISSILE_SPEED 4 // Status of a game object constants #define ACTIVE 0 #define EXPLODING 1 #define DESTROYED 2 // graphics // aliens const unsigned char MotherShipGfx [] PROGMEM = { B00111111,B11111100, B01101101,B10110110, B11111111,B11111111, B00111001,B10011100 }; const unsigned char InvaderTopGfx [] PROGMEM = { B00011000, B00111100, B01111110, B11011011, B11111111, B00100100, B01011010, B10100101 }; const unsigned char InvaderTopGfx2 [] PROGMEM = { B00011000, B00111100, B01111110, B11011011, B11111111, B01011010, B10000001, B01000010 }; const unsigned char PROGMEM InvaderMiddleGfx []= { B00100000,B10000000, B00010001,B00000000, B00111111,B10000000, B01101110,B11000000, B11111111,B11100000, B10111111,B10100000, B10100000,B10100000, B00011011,B00000000 }; const unsigned char PROGMEM InvaderMiddleGfx2 [] = { B00100000,B10000000, B00010001,B00000000, B10111111,B10100000, B10101110,B10100000, B11111111,B11100000, B00111111,B10000000, B00100000,B10000000, B01000000,B01000000 }; const unsigned char PROGMEM InvaderBottomGfx [] = { B00001111,B00000000, B01111111,B11100000, B11111111,B11110000, B11100110,B01110000, B11111111,B11110000, B00111001,B11000000, B01100110,B01100000, B00110000,B11000000 }; const unsigned char PROGMEM InvaderBottomGfx2 [] = { B00001111,B00000000, B01111111,B11100000, B11111111,B11110000, B11100110,B01110000, B11111111,B11110000, B00111001,B11000000, B01000110,B00100000, B10000000,B00010000 }; static const unsigned char PROGMEM ExplosionGfx [] = { B00001000,B10000000, B01000101,B00010000, B00100000,B00100000, B00010000,B01000000, B11000000,B00011000, B00010000,B01000000, B00100101,B00100000, B01001000,B10010000 }; // Player grafix const unsigned char PROGMEM TankGfx [] = { B00000010,B00000000, B00000111,B00000000, B00000111,B00000000, B01111111,B11110000, B11111111,B11111000, B11111111,B11111000, B11111111,B11111000, B11111111,B11111000, }; static const unsigned char PROGMEM MissileGfx [] = { B10000000, B10000000, B10000000, B10000000 }; // Game structures struct GameObjectStruct { // base object which most other objects will include signed int X; signed int Y; unsigned char Status; //0 active, 1 exploding, 2 destroyed }; struct AlienStruct { GameObjectStruct Ord; unsigned char ExplosionGfxCounter; // how long we want the ExplosionGfx to last }; struct PlayerStruct { GameObjectStruct Ord; unsigned int Score; unsigned char Lives; }; // general global variables Adafruit_SSD1306 display(1); //alien global vars //The array of aliens across the screen AlienStruct Alien[NUM_ALIEN_COLUMNS][NUM_ALIEN_ROWS]; AlienStruct MotherShip; // widths of aliens // as aliens are the same type per row we do not need to store their graphic width per alien in the structure above // that would take a byte per alien rather than just three entries here, 1 per row, saving significnt memory byte AlienWidth[]={8,11,12}; // top, middle ,bottom widths char AlienXMoveAmount=2; signed char InvadersMoveCounter; // counts down, when 0 move invaders, set according to how many aliens on screen bool AnimationFrame=false; // two frames of animation, if true show one if false show the other // Mothership signed char MotherShipSpeed; unsigned int MotherShipBonus; signed int MotherShipBonusXPos; // pos to display bonus at unsigned char MotherShipBonusCounter; // how long bonus amount left on screen // Player global variables PlayerStruct Player; GameObjectStruct Missile; // game variables unsigned int HiScore; void setup() { display.begin(SSD1306_SWITCHCAPVCC,OLED_ADDRESS); InitAliens(0); InitPlayer(); pinMode(RIGHT_BUT, INPUT_PULLUP); pinMode(LEFT_BUT, INPUT_PULLUP); pinMode(FIRE_BUT, INPUT_PULLUP); display.setTextSize(1); display.setTextColor(WHITE); EEPROM.get(0,HiScore); if(HiScore==65535) // new unit never written to { HiScore=0; EEPROM.put(0,HiScore); } } void loop() { Physics(); UpdateDisplay(); } void Physics() { AlienControl(); MotherShipPhysics(); PlayerControl(); MissileControl(); CheckCollisions(); } unsigned char GetScoreForAlien(int RowNumber) { // returns value for killing and alien at the row indicated switch (RowNumber) { case 0:return 30; case 1:return 20; case 2:return 10; } } void MotherShipPhysics() { if(MotherShip.Ord.Status==ACTIVE) {// spawned, move it MotherShip.Ord.X+=MotherShipSpeed; if(MotherShipSpeed>0) // going left to right , check if off right hand side { if(MotherShip.Ord.X>=SCREEN_WIDTH) { MotherShip.Ord.Status=DESTROYED; } } else // going right to left , check if off left hand side { if(MotherShip.Ord.X+MOTHERSHIP_WIDTH<0) { MotherShip.Ord.Status=DESTROYED; } } } else { // try to spawn mothership if(random(MOTHERSHIP_SPAWN_CHANCE)==1) { // Spawn a mother ship, starts just off screen at top MotherShip.Ord.Status=ACTIVE; // need to set direction if(random(2)==1) // values between 0 and 1 { MotherShip.Ord.X=SCREEN_WIDTH; MotherShipSpeed=-MOTHERSHIP_SPEED; // if we go in here swaps to right to left } else { MotherShip.Ord.X=-MOTHERSHIP_WIDTH; MotherShipSpeed=MOTHERSHIP_SPEED; // set to go left ot right } } } } void PlayerControl() { // user input checks if((digitalRead(RIGHT_BUT)==0)&(Player.Ord.X+TANKGFX_WIDTH<SCREEN_WIDTH)) Player.Ord.X+=PLAYER_X_MOVE_AMOUNT; if((digitalRead(LEFT_BUT)==0)&(Player.Ord.X>0)) Player.Ord.X-=PLAYER_X_MOVE_AMOUNT; if((digitalRead(FIRE_BUT)==0)&(Missile.Status!=ACTIVE)) { Missile.X=Player.Ord.X+(TANKGFX_WIDTH/2); Missile.Y=PLAYER_Y_START; Missile.Status=ACTIVE; } } void MissileControl() { if(Missile.Status==ACTIVE) { Missile.Y-=MISSILE_SPEED; if(Missile.Y+MISSILE_HEIGHT<0) // If off top of screen destroy so can be used again Missile.Status=DESTROYED; } } void AlienControl() { if((InvadersMoveCounter--)<0) { bool Dropped=false; if((RightMostPos()+AlienXMoveAmount>=SCREEN_WIDTH) |(LeftMostPos()+AlienXMoveAmount<0)) // at edge of screen { AlienXMoveAmount=-AlienXMoveAmount; // reverse direction Dropped=true; // and indicate we are dropping } // update the alien postions for(int Across=0;Across<NUM_ALIEN_COLUMNS;Across++) { for(int Down=0;Down<3;Down++) { if(Alien[Across][Down].Ord.Status==ACTIVE) { if(Dropped==false) Alien[Across][Down].Ord.X+=AlienXMoveAmount; else Alien[Across][Down].Ord.Y+=INVADERS_DROP_BY; } } } InvadersMoveCounter=INVADERS_SPEED; AnimationFrame=!AnimationFrame; ///swap to other frame } } void CheckCollisions() { MissileAndAlienCollisions(); MotherShipCollisions(); } void MotherShipCollisions() { if((Missile.Status==ACTIVE)&(MotherShip.Ord.Status==ACTIVE)) { if(Collision(Missile,MISSILE_WIDTH,MISSILE_HEIGHT,MotherShip.Ord,MOTHERSHIP_WIDTH,MOTHERSHIP_HEIGHT)) { MotherShip.Ord.Status=EXPLODING; MotherShip.ExplosionGfxCounter=EXPLOSION_GFX_TIME; Missile.Status=DESTROYED; // generate the score for the mothership hit, note in the real arcade space invaders the score was not random but // just appeared so, a player could infulence its value with clever play, but we'll keep it a little simpler MotherShipBonus=random(4); // a random number between 0 and 3 switch(MotherShipBonus) { case 0:MotherShipBonus=50;break; case 1:MotherShipBonus=100;break; case 2:MotherShipBonus=150;break; case 3:MotherShipBonus=300;break; } Player.Score+=MotherShipBonus; MotherShipBonusXPos=MotherShip.Ord.X; if(MotherShipBonusXPos>100) // to ensure isn't half off right hand side of screen MotherShipBonusXPos=100; if(MotherShipBonusXPos<0) // to ensure isn't half off right hand side of screen MotherShipBonusXPos=0; MotherShipBonusCounter=DISPLAY_MOTHERSHIP_BONUS_TIME; } } } void MissileAndAlienCollisions() { for(int across=0;across<NUM_ALIEN_COLUMNS;across++) { for(int down=0;down<NUM_ALIEN_ROWS;down++) { if(Alien[across][down].Ord.Status==ACTIVE) { if(Missile.Status==ACTIVE) { if(Collision(Missile,MISSILE_WIDTH,MISSILE_HEIGHT,Alien[across][down].Ord,AlienWidth[down],INVADER_HEIGHT)) { // missile hit Alien[across][down].Ord.Status=EXPLODING; Missile.Status=DESTROYED; Player.Score+=GetScoreForAlien(down); } } } } } } bool Collision(GameObjectStruct Obj1,unsigned char Width1,unsigned char Height1,GameObjectStruct Obj2,unsigned char Width2,unsigned char Height2) { return ((Obj1.X+Width1>Obj2.X)&(Obj1.X<Obj2.X+Width2)&(Obj1.Y+Height1>Obj2.Y)&(Obj1.Y<Obj2.Y+Height2)); } int RightMostPos() { //returns x pos of right most alien int Across=NUM_ALIEN_COLUMNS-1; int Down; int Largest=0; int RightPos; while(Across>=0){ Down=0; while(Down<NUM_ALIEN_ROWS){ if(Alien[Across][Down].Ord.Status==ACTIVE) { // different aliens have different widths, add to x pos to get rightpos RightPos= Alien[Across][Down].Ord.X+AlienWidth[Down]; if(RightPos>Largest) Largest=RightPos; } Down++; } if(Largest>0) // we have found largest for this coloum return Largest; Across--; } return 0; // should never get this far } int LeftMostPos() { //returns x pos of left most alien int Across=0; int Down; int Smallest=SCREEN_WIDTH*2; while(Across<NUM_ALIEN_COLUMNS){ Down=0; while(Down<3){ if(Alien[Across][Down].Ord.Status==ACTIVE) if(Alien[Across][Down].Ord.X<Smallest) Smallest=Alien[Across][Down].Ord.X; Down++; } if(Smallest<SCREEN_WIDTH*2) // we have found smalest for this coloum return Smallest; Across++; } return 0; // should nevr get this far } void UpdateDisplay() { int i; display.clearDisplay(); // Mothership bonus display if required if(MotherShipBonusCounter>0) { // mothership bonus display.setCursor(MotherShipBonusXPos,0); display.print(MotherShipBonus); MotherShipBonusCounter--; } else { // draw score and lives, anything else can go above them display.setCursor(0,0); display.print(Player.Score); display.setCursor(SCREEN_WIDTH-7,0); display.print(Player.Lives); } //Invaders for(int across=0;across<NUM_ALIEN_COLUMNS;across++) { for(int down=0;down<NUM_ALIEN_ROWS;down++) { if(Alien[across][down].Ord.Status==ACTIVE){ switch(down) { case 0: if(AnimationFrame) display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderTopGfx, AlienWidth[down],INVADER_HEIGHT,WHITE); else display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderTopGfx2, AlienWidth[down],INVADER_HEIGHT,WHITE); break; case 1: if(AnimationFrame) display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderMiddleGfx, AlienWidth[down],INVADER_HEIGHT,WHITE); else display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderMiddleGfx2, AlienWidth[down],INVADER_HEIGHT,WHITE); break; default: if(AnimationFrame) display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderBottomGfx, AlienWidth[down],INVADER_HEIGHT,WHITE); else display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, InvaderBottomGfx2, AlienWidth[down],INVADER_HEIGHT,WHITE); } } else { if(Alien[across][down].Ord.Status==EXPLODING){ Alien[across][down].ExplosionGfxCounter--; if(Alien[across][down].ExplosionGfxCounter>0) { display.drawBitmap(Alien[across][down].Ord.X, Alien[across][down].Ord.Y, ExplosionGfx, 13, 8,WHITE); } else Alien[across][down].Ord.Status=DESTROYED; } } } } // player display.drawBitmap(Player.Ord.X, Player.Ord.Y, TankGfx, TANKGFX_WIDTH, TANKGFX_HEIGHT,WHITE); //missile if(Missile.Status==ACTIVE) display.drawBitmap(Missile.X, Missile.Y, MissileGfx, MISSILE_WIDTH, MISSILE_HEIGHT,WHITE); // mothership (not bonus if hit) if(MotherShip.Ord.Status==ACTIVE) display.drawBitmap(MotherShip.Ord.X, MotherShip.Ord.Y, MotherShipGfx, MOTHERSHIP_WIDTH, MOTHERSHIP_HEIGHT,WHITE); else { if(MotherShip.Ord.Status==EXPLODING) { for(i=0;i<MOTHERSHIP_WIDTH;i+=2) { display.drawBitmap(MotherShip.Ord.X+i, MotherShip.Ord.Y, ExplosionGfx, random(4)+2, MOTHERSHIP_HEIGHT,WHITE); } MotherShip.ExplosionGfxCounter--; if(MotherShip.ExplosionGfxCounter==0) { MotherShip.Ord.Status=DESTROYED; } } } display.display(); } void InitPlayer() { Player.Ord.Y=PLAYER_Y_START; Player.Ord.X=PLAYER_X_START; Missile.Status=DESTROYED; Player.Score=0; } void InitAliens(int YStart) { for(int across=0;across<NUM_ALIEN_COLUMNS;across++) { for(int down=0;down<3;down++) { // we add down to centralise the aliens, just happens to be the right value we need per row! // we need to adjust a little as row zero should be 2, row 1 should be 1 and bottom row 0 Alien[across][down].Ord.X=X_START_OFFSET+(across*(LARGEST_ALIEN_WIDTH+SPACE_BETWEEN_ALIEN_COLUMNS))-(AlienWidth[down]/2); Alien[across][down].Ord.Y=YStart+(down*SPACE_BETWEEN_ROWS); Alien[across][down].Ord.Status=ACTIVE; Alien[across][down].ExplosionGfxCounter=EXPLOSION_GFX_TIME; } } MotherShip.Ord.Y=0; MotherShip.Ord.X=-MOTHERSHIP_WIDTH; MotherShip.Ord.Status=DESTROYED; } |
Let’s go through it in detail. Instantly at line 3 we have this extra line
#include <EEPROM.h>
In every processor used in the Arduino range of boards there is some memory that is referred to as EEPROM (Electrically Erasable Programmable Read Only Memory). This is memory that we can access to store information during program runtime that will not be lost when the processor is powered down. It will remain there for all intents and purposes for ever. You may wonder why not have all dynamic memory (storage for program data, variables etc.) in EEPROM memory. Well, generally speaking it is slower to access than normal volatile memory so would reduce performance for general programs. So to access this facility on our Arduino’s we use this library written for that purpose. Why do we need to use this special memory? To store the Hi-Score, we want this to be kept even when the unit is switched off.
Player Score
174 175 176 177 178 |
struct PlayerStruct { GameObjectStruct Ord; unsigned int Score; unsigned char Lives; }; |
We’ve added a extra property to the player structure, their score. Putting in the players structure would allow for an easier expansion to a two player option in the future if we decided to implement one. Also note we’ve added the Lives property to the player but for now we will do nothing more with it. The hi-score is defined at line 213:
unsigned int HiScore;
Checking the Hi-Score on startup
When we power up our unit it needs to retrieve the hi-score, these lines accomplish that task.
229 230 231 232 233 234 |
EEPROM.get(0,HiScore); if(HiScore==65535) // new unit never written to { HiScore=0; EEPROM.put(0,HiScore); } |
The variable HiScore is a integer variable and takes up 2 bytes to store up to a maximum value of 65535, which for this implementation will be adequate. Note that with a brand new Arduino all the EEPROM bytes are set to “1”s. Two bytes that are all “1”s is 65535. So on initialisation if the value read using line EEPROM.get(0,HiScore) is this value then we clear it to 0. Later we will add in a button to reset the Hi-Score manually should you wish to. Let’s look closer at that line
EEPROM.get(0,HiScore);
This calls a routine called get that will start retrieving bytes from the location passed (0, which is the start of EEPROM storage). It will read the number of bytes that the type HiScore needs, in this case as its an INT then it will read two bytes (0 and 1) and store them in HiScore. If you wanted to store another value in EEPROM then it must start at location 2 and not 1 as 1 is already being used to store part of the hi-score. It is very important to know how much storage space certain variable types require and to keep track of your EEPROM storage usage otherwise your code will not work correctly.
So if we are setting the Hi-Score to 0 then the line EEPROM.put(0,HiScore); will achieve this. It puts the values of the two bytes for the HiScore variable into the EEPROM starting at location 0 (so will use location 0 and 1 because we need two bytes).
Different Aliens – Different Scores
Different Aliens score a differing amount, the higher up the more points are awarded. The following routine returns the score for a particular Invader on a particular row.
251 252 253 254 255 256 257 258 259 260 |
unsigned char GetScoreForAlien(int RowNumber) { // returns value for killing and alien at the row indicated switch (RowNumber) { case 0:return 30; case 1:return 20; case 2:return 10; } } |
All that is needed is to pass in the row number of the Alien that has just been destroyed and it will return the appropriate score. 0 is the top row and 2 is the bottom row. We call this routine in the function MissileAndAlienCollisions, listed below;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
void MissileAndAlienCollisions() { for(int across=0;across<NUM_ALIEN_COLUMNS;across++) { for(int down=0;down<NUM_ALIEN_ROWS;down++) { if(Alien[across][down].Ord.Status==ACTIVE) { if(Missile.Status==ACTIVE) { if(Collision(Missile,MISSILE_WIDTH,MISSILE_HEIGHT,Alien[across][down].Ord,AlienWidth[down],INVADER_HEIGHT)) { // missile hit Alien[across][down].Ord.Status=EXPLODING; Missile.Status=DESTROYED; Player.Score+=GetScoreForAlien(down); } } } } } } |
Line 410 passes in the current row of the destroyed Invader and the players score is updated accordingly.
We also update the players score when the Mystery-ship is destroyed, here’s the collision checking routine from the main code.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
void MotherShipCollisions() { if((Missile.Status==ACTIVE)&(MotherShip.Ord.Status==ACTIVE)) { if(Collision(Missile,MISSILE_WIDTH,MISSILE_HEIGHT,MotherShip.Ord,MOTHERSHIP_WIDTH,MOTHERSHIP_HEIGHT)) { MotherShip.Ord.Status=EXPLODING; MotherShip.ExplosionGfxCounter=EXPLOSION_GFX_TIME; Missile.Status=DESTROYED; // generate the score for the mothership hit, note in the real arcade space invaders the score was not random but // just appeared so, a player could infulence its value with clever play, but we'll keep it a little simpler MotherShipBonus=random(4); // a random number between 0 and 3 switch(MotherShipBonus) { case 0:MotherShipBonus=50;break; case 1:MotherShipBonus=100;break; case 2:MotherShipBonus=150;break; case 3:MotherShipBonus=300;break; } Player.Score+=MotherShipBonus; MotherShipBonusXPos=MotherShip.Ord.X; if(MotherShipBonusXPos>100) // to ensure isn't half off right hand side of screen MotherShipBonusXPos=100; if(MotherShipBonusXPos<0) // to ensure isn't half off right hand side of screen MotherShipBonusXPos=0; MotherShipBonusCounter=DISPLAY_MOTHERSHIP_BONUS_TIME; } } } |
Here you can see that line 383 updates the players score with whatever bonus has been awarded.
And finally we clear the players score back to 0 when the game starts:
557 558 559 560 561 562 |
void InitPlayer() { Player.Ord.Y=PLAYER_Y_START; Player.Ord.X=PLAYER_X_START; Missile.Status=DESTROYED; Player.Score=0; } |
What if the player beats the hi-score?
Well, at present nothing happens, that bit of code to handle checking and storing new hi-scores will come in a later article when we will display a congratulations screen as well but for now it will wait until we’ve more structure to the game.
Displaying the score
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
if(MotherShipBonusCounter>0) { // mothership bonus display.setCursor(MotherShipBonusXPos,0); display.print(MotherShipBonus); MotherShipBonusCounter--; } else { // draw score and lives, anything else can go above them display.setCursor(0,0); display.print(Player.Score); display.setCursor(SCREEN_WIDTH-7,0); display.print(Player.Lives); } |
Looking in the display routine we find the previous lines for Mystery-Ship bonus and now we’ve added some more for score and lives. We can see that we only display them if there is no bonus being displayed. This is because if the bonus is printing over the score then having both together on screen at once makes the bonus illegible.
See you next time.