Tuesday, March 26, 2013

Making a Game Part 3

I'm done.. I'm done... I'm done! I can't believe I've finished my first serious goal for a project. At some point I want to add different difficulty levels and fix it so there's an "introduction" light sequence. For now, I'm going to leave it. Code is after the break. I added comments so that you may understand it. if you have any questions feel free to email me.




int count=0;
int currentOn=7;                           //currentOn variable is the current LED position.
void setup()
{
 /* introSeq2();   //Couldn't successfully setup the loop so everytime the game was over the intro would start again.
  introSeq2();     //Chose to comment out the intro.
  introSeq();
  count=0;
  introSeq();
  count=0;
  introSeq();
  count=0;*/
  Serial.begin(9600);
  digitalWrite(currentOn,HIGH);
}

void loop() {
 leftright();
 if (currentOn>=12 || currentOn<=2){
   Serial.println(count);  
   gameOverFlash();
   gameOverFlash();
   gameOverFlash();
   delay(1000);
   asm volatile ("  jmp 0");
 }
}

//(***************Break up for first IntroSeq ********************************************
/*void introSeq2(){
    for (int i=2; i<12; i++){
  digitalWrite(i,HIGH);
  delay(100);
  digitalWrite(i,LOW);}
}
//(***************Break up for Second IntroSeq ********************************************
void introSeq(){
  for(int j=6; j>1; j--){
  digitalWrite(j,HIGH);
  digitalWrite(j+1+count,HIGH);
  delay(200);
  digitalWrite(j,LOW);
  digitalWrite(j+1+count,LOW);
  count += + 2;}}*/
// ****************Break up for Move Left *********************************
int leftright(){
  int right = analogRead(0);           //Read value from joystick right
  int left = analogRead(1);            //or left.
  //Serial.println(right);
  //Serial.println(left);
  delay(100);                          //Large delay for testing purposes.
  if (right == 0)
  {
    digitalWrite(currentOn,LOW);       //The current LED is turned off
    currentOn-=1;                      //the position is decreased by one
    count+=1;
    digitalWrite(currentOn,HIGH);      //and the new current LED is set to high.
    while(currentOn <= 12)                
    {
      delay(200);
      digitalWrite(currentOn,LOW);
      currentOn-=1;
      digitalWrite(currentOn,HIGH);
      int left = analogRead(1);
      if (left==0){break;}
      if (currentOn>=12 || currentOn<=2){break;}
    }
  }

  if (left == 0)                       //Same code as previous if statement except in the opposite direction.
  {
    digitalWrite(currentOn,LOW);      
    currentOn+=1;
    count+=1;
    digitalWrite(currentOn,HIGH);
    while(currentOn >= 1)
    {
      delay(200);
      digitalWrite(currentOn,LOW);
      currentOn+=1;
      digitalWrite(currentOn,HIGH);
      int right = analogRead(0);
      if (right==0){break;}
      if (currentOn>=12 || currentOn<=2){break;}
    }
  }
}
//*******************************Game Over Sequence*******************************************************

void gameOverFlash()
{
    for (int i=2; i<12; i++){
      digitalWrite(i,HIGH);
    }
    delay(100);
    for (int i=2; i<12; i++){
      digitalWrite(i,LOW);
    }
    delay(100) ;
}

No comments:

Post a Comment