A Video Game on my Trinket, take 2

Several posts ago, I wrote about writing a video game for the Trinket from Adafruit. Well, since then, I have added scoring, sound and a bit more animation (very crude, mind you, I can only do so much in the small memory and limited screen.)

The setup uses the Trinket, Parallax’s 2×16 LCD panel and a switch connected to Pin 0. The LCD panel is connected to Pin 2.  You will need to import the SoftwareSerial library for the LCD.

The premise of the game is based on the Battlestar Galactica TV show from 2003-2010.  The Galactica was engaged in heavy battle and barely managed to escape before certain destruction. All of her gun turrets, save the forward torpedo launcher, are damaged and non-functional, meaning she can only fight head on.  The Viper squads are still engaging the enemy.  A group of Cylon raiders have found the mighty Battlestar and are taking her on, one by one. Oh, you only have ten torpedo’s left, so use them wisely.

The game ends when you use up your ten torpedo’s.

Below is the revised listing for the game.  Apologies to Glen Larsen.

 

Listing 1.

#include <SoftwareSerial.h>

SoftwareSerial s(0,2);
 
// globals
int  cylonpos;
int  oldcylonpos;
int  cyDirection;
int  button;
int  posStart;
int  lin2Start;
int  bsgpos;
 
// special characters
int  bsg;
int  cylon;
 
// total number of torpedos the ship has left
int  tShots=10;
 
// the setup routine runs once when you press reset:
void waitforbutton();
void moveCylon();
 
void setup() {  // initialize the digital pin as an output. 
   s.begin(9600);
   s.write(12);
   s.write(17);
  
   //define the ship character
   s.write(249);
   s.write(0x4);
   s.write(0xE);
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(0x15);
   s.write(0xE);
   s.write(0xE);
  
   // baddies
   s.write(251);

   s.write(uint8_t(0x0));//to handle a goofy issue with ‘0’
   s.write(uint8_t(0x0));
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
 
// the button is on pin 0 
   pinMode(0,INPUT);
  
// the initial direction is right
   cyDirection=1;
  
} // the loop routine runs over and over again forever:
void loop() {
  int score;
  score=0;
 
  s.write(12); // clear the screen and show the instructions
  s.println("Shoot the Cylon\n");
  s.println("press btn 2 strt");
 
  // wait for the button
  waitforbutton();
 
  // define somethings before we start the game loop
  tShots=10;
  cylon=3;
  bsg=1;
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
 
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  oldcylonpos=cylonpos;
 
  s.write(12);  // clear screen
  s.write(17);  // turn on backlight so we can see
  s.write(posStart + cylonpos);  // uh oh…the Cylon appeared!
 
  s.write(cylon);
  s.write(lin2Start+bsgpos);
  s.write(bsg); // and here we are
 
  // begin the game
  while(1){
    /* Game loop */
    int button;
    s.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);  // check the button
    if(button==HIGH){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        s.write(132); // center ‘Hit’ on screen
        s.print("** HIT **");
        score ++;  // update score
        s.write(213); // noise (C at half note, 3rd scale)
        s.write(215); // play the note
        s.write(224);
        s.write(225);
        if(score==10){  // did we win?
           s.write(12); // clear screen
           s.write(128);// start at beginning
           s.print("You have saved\nthe Galactica.");
           delay(2000);
           loop();  // start game again
        }
        delay(3000); // did not win yet
        s.write(12); // clear screen
        s.write(lin2Start); // move cursor to beginning of line
        s.write("Score:");  // put up score
        s.print(score);
        s.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so…
        s.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        s.write(lin2Start+bsgpos); //…and, we are here
        s.write(bsg);
        s.write(posStart+cylonpos);
        s.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      }
     
      tShots –; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        s.write(12);  // clear screen
        s.write(posStart); // start at beginning of line
        s.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
          s.write(lin2Start+bsgpos);
          s.write(bsg);
          delay(500);
          s.write(lin2Start+bsgpos);
          s.write(‘*’);
          delay(500);
          count++;
        }       
        loop(); // Play again
      }
    } 
  }
}
void moveCylon(){
  // sneaky Cylons…they show up anywhere!
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
 
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
 
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  s.write(posStart+cylonpos);
  s.write(cylon);
  oldcylonpos=cylonpos;
}

void waitforbutton(){
  SoftwareSerial s(0,1);
 
  int button;
  button=digitalRead(0);
  while(button==LOW){
    button=digitalRead(0);
    delay(1);
  } 
  s.write(1); 
  delay(1000);
  s.write(12);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s