Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1D Pong
- //created by Andrew Schmidt (aschmidt20)
- //June 21, 2013
- int ledPins[] = {2,3,4,5,6,7,8,9};
- int button1Pin = 10;
- int button2Pin = 11;
- int button1State, button2State;
- String gameInstance = "start";
- int player1Score, player2Score = 0;
- int delayTime;
- void setup()
- {
- int index;
- for(index = 0; index <= 7; index++)
- {
- pinMode(ledPins[index], OUTPUT);
- }
- pinMode(button1Pin, INPUT);
- pinMode(button2Pin, INPUT);
- delayTime = 100;
- }
- void loop()
- { // Different points of the game as String variables
- if (gameInstance == "start")
- {
- leftRight();
- }
- else if (gameInstance == "player 1 return")
- {
- delayTime = delayTime - 5; // increases speed of "ball"
- rightLeft();
- }
- else if (gameInstance == "player 2 return")
- {
- delayTime = delayTime - 5;
- leftRight();
- }
- else if (gameInstance == "player 1 point")
- {
- player1Score++;
- delayTime = 100; // resets delay after every point
- showScore();
- }
- else if (gameInstance == "player 2 point")
- {
- player2Score++;
- delayTime = 100;
- showScore();
- }
- }
- void leftRight() // LEDs light up from Left to Right then check if the button is pressed
- {
- int index;
- for(index = 0; index <= 6; index++)
- {
- digitalWrite(ledPins[index], HIGH);
- delay(delayTime);
- digitalWrite(ledPins[index], LOW);
- }
- digitalWrite(ledPins[7], HIGH);
- button1State = digitalRead(button1Pin);
- if (button1State == LOW) // sets gameInstance for when it goes through the main Loop
- {
- gameInstance = "player 1 return";
- }
- else
- {
- gameInstance = "player 2 point";
- }
- digitalWrite(ledPins[7], LOW);
- }
- void rightLeft() // LEDs light up from Right to Left then check if the button is pressed
- {
- int index;
- for(index = 7; index >=1; index--)
- {
- digitalWrite(ledPins[index], HIGH);
- delay(delayTime);
- digitalWrite(ledPins[index], LOW);
- }
- digitalWrite(ledPins[0], HIGH);
- button2State = digitalRead(button2Pin);
- if (button2State == LOW)
- {
- gameInstance = "player 2 return";
- }
- else
- {
- gameInstance = "player 1 point";
- }
- digitalWrite(ledPins[0], LOW);
- }
- void showScore()
- {
- // Show player 1 score
- if (player1Score == 1)
- {
- digitalWrite(ledPins[7], HIGH);
- }
- else if (player1Score == 2)
- {
- digitalWrite(ledPins[7], HIGH);
- digitalWrite(ledPins[6], HIGH);
- }
- else if (player1Score == 3)
- {
- digitalWrite(ledPins[7], HIGH);
- digitalWrite(ledPins[6], HIGH);
- digitalWrite(ledPins[5], HIGH);
- }
- else if (player1Score == 4)
- {
- digitalWrite(ledPins[7], HIGH);
- digitalWrite(ledPins[6], HIGH);
- digitalWrite(ledPins[5], HIGH);
- digitalWrite(ledPins[4], HIGH);
- }
- // Show player 2 score
- if (player2Score == 1)
- {
- digitalWrite(ledPins[0], HIGH);
- }
- else if (player2Score == 2)
- {
- digitalWrite(ledPins[1], HIGH);
- digitalWrite(ledPins[0], HIGH);
- }
- else if (player2Score == 3)
- {
- digitalWrite(ledPins[2], HIGH);
- digitalWrite(ledPins[1], HIGH);
- digitalWrite(ledPins[0], HIGH);
- }
- else if (player2Score == 4)
- {
- digitalWrite(ledPins[3], HIGH);
- digitalWrite(ledPins[2], HIGH);
- digitalWrite(ledPins[1], HIGH);
- digitalWrite(ledPins[0], HIGH);
- }
- delay(2000);
- ledsOff();
- if (player1Score == 4) // If the game is over, marquee in direction of winner
- {
- marqueeRight();
- }
- else if (player2Score == 4)
- {
- marqueeLeft();
- }
- gameInstance = "start";
- }
- void ledsOff() // Turns off all LEDs
- {
- int index;
- for(index = 0; index <= 7; index++)
- {
- digitalWrite(ledPins[index], LOW);
- }
- }
- void marqueeRight() // Marquee code from Inventor's Kit examples, modified for length and direction
- {
- int index;
- int indez;
- int delayTime = 100; // milliseconds to pause between LEDs
- // Make this smaller for faster switching
- // Step through the first four LEDs
- // (We'll light up one in the lower 4 and one in the upper 4)
- for(indez = 0; indez<= 5; indez++)
- {
- for(index = 0; index <= 3; index++) // Step from 0 to 3
- {
- digitalWrite(ledPins[index], HIGH); // Turn a LED on
- digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
- delay(delayTime); // Pause to slow down the sequence
- digitalWrite(ledPins[index], LOW); // Turn the LED off
- digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
- }
- }
- delay(2500);
- gameRestart();
- }
- void marqueeLeft()
- {
- int index;
- int indez;
- int delayTime = 100; // milliseconds to pause between LEDs
- // Make this smaller for faster switching
- // Step through the first four LEDs
- // (We'll light up one in the lower 4 and one in the upper 4)
- for(indez = 0; indez<= 5; indez++)
- {
- for(index = 3; index >= 0; index--) // Step from 0 to 3
- {
- digitalWrite(ledPins[index], HIGH); // Turn a LED on
- digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
- delay(delayTime); // Pause to slow down the sequence
- digitalWrite(ledPins[index], LOW); // Turn the LED off
- digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
- }
- }
- delay(2500);
- gameRestart();
- }
- void gameRestart() // Restarts game, variables back to original
- {
- player1Score = 0;
- player2Score = 0;
- gameInstance = "start";
- delayTime = 100;
- }
Advertisement
Add Comment
Please, Sign In to add comment