Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ver 1.8
- int level = 0; // Used as a test to make sure the sequence output corresponds with the level
- int levelDisp = 1; //Used simply to show the level in the serial monitor
- /* I used the levelDisp variable because the level starts from zero internally
- but i want the system to show that it is infact starting from 1*/
- boolean startGame = false; // This boolean when set to true starts the gameand
- //begins giving out random sequences
- boolean gameWin = false;
- int highScore = 0; // Used to keep track of the High Score
- int currentScore = 0; // Used to keep track of the players current score
- int led1 = 3; // Pins for Leds 1 - 4
- int led2 = 4;
- int led3 = 5;
- int led4 = 6;
- int button1 = 8; // Pins for Buttons 1 - 4
- int button2 = 9;
- int button3 = 10;
- int button4 = 11;
- int piezo = 2; // Pin for the piezo
- int startButton = 12; // Pin for the start button
- int button1Read = LOW; // Makes sure that button 1 - 4 states are all set to LOW
- int button2Read = LOW;
- int button3Read = LOW;
- int button4Read = LOW;
- int startButtonRead = LOW; // Makes sure that the start button state is LOW
- /* (randomOutput/ playerInput)Basically for every level there is a new name given
- to the sequence. E.g. level one will have a randomOutput[1] and a playerInput[1]
- the output will have a sequence assigned to it
- and then the code will check if the player input matches the sequence assigned to the corresponding output.*/
- int randomOutput[100]; // Used for the output array of sequences max currently set at 100
- int playerInput[100]; //Used for the output array of sequences max currently set at 100
- int maxLevel = 0;
- int d1 = 50; // Delays have been defined to save time if one needs to change the delay on several points in the code.
- int d2 = 100;
- int d3 = 200; // This delay in particular (d3) if altered changed the speed of the random sequence output. The shorter it is set the harder the game becomes.
- int d4 = 500;
- void setup(){ //First essential function the setup
- Serial.begin(9600); // Begins communications with the serial monitor
- pinMode(led1, OUTPUT); //Sets Led's 1 - 4 to OUTPUT
- pinMode(led2, OUTPUT);
- pinMode(led3, OUTPUT);
- pinMode(led4, OUTPUT);
- pinMode(button1, INPUT); // Sets buttons 1 - 4 to INPUT
- pinMode(button2, INPUT);
- pinMode(button3, INPUT);
- pinMode(button4, INPUT);
- randomSeed(analogRead(0)); // Used to create more randomness by reading the voltage at pin 0 ( which is not in use in this code )
- Serial.println(" "); // Serial.println displays texts and after the line of text it enters one line down in the serial monitor.
- Serial.print(" ***** HIGH SCORE: ");
- Serial.print(highScore); // Here we are calling upon the highScore variable. This will display the current high score in the serial monitor.
- Serial.println(" ***** ");
- Serial.println("");
- Serial.println("Press and hold start to begin :D ");
- while(startGame == false){ //While startGame is set to false the game will not initiate.
- for (int a = 0; a <= 255; a++){
- // This for loop is used in combination with the PWM feature of the board( Pulse Width Modulation)
- analogWrite(led1, a); // I used this to change the brightness of the light from bright to dim.
- digitalWrite(led2, HIGH); // The yellow led is connected to pin 4 which does not support PWM thats why it doesnt display the
- analogWrite(led3, a); // effect. All pins that support PWM have a ~ next to them e.g. 3~, 5~, 6~.
- analogWrite(led4, a);
- delay(10);
- if(a == 255){
- digitalWrite(led2, LOW);
- delay(100);
- }
- startButtonRead = digitalRead(startButton);
- }
- if(startButtonRead == HIGH){ // If start button is pressed
- startGame = true; //startGame is set to true
- digitalWrite(led1, HIGH); // These led's flash when startGame = true
- delay(d2);
- digitalWrite(led2, HIGH);
- digitalWrite(led1, LOW);
- delay(d2);
- digitalWrite(led3, HIGH);
- digitalWrite(led2, LOW);
- delay(d2);
- digitalWrite(led4, HIGH);
- digitalWrite(led3, LOW);
- delay(d2);
- digitalWrite(led4, LOW);
- delay(1000);
- }
- }
- while(startGame == true){
- for(int a = 0; a <= 100; a++){// This for loop is used to loop through the functions.
- systemOutput();
- systemInput();
- }
- }
- }
- void loop(){ // Had to include the void loop as the arduino gave a compiling error if the void loop was not present even though it is not in use in the code currently.
- }
- void systemOutput(){ // This function is used to give out sequences assigned to different variable headings.
- for(int a = level; a <= level; a++){ // Sets it for this function that a is equal to to the level or it can be less than but it can never exceed the level and after every pass a goes up by 1.
- Serial.println("");
- Serial.print("Your current score: ");
- Serial.print(currentScore); // Displays current score.
- Serial.println("");
- Serial.print("level: ");
- Serial.print(levelDisp); // Displays current level.
- Serial.println("");
- Serial.println("");
- Serial.println("Sequence output...");
- randomOutput[a] = random(1, 5); // Picks a random number between 1 - 4 ( 5 is is never choosen.
- for(int b = 0; b <= level; b++){ /* B is used to assign a number in the variable in randomOutput.. randomOutput[b] = randomOutput[1] level
- two randomOutput[1] + randomOutput[2] .. etc. the previous sequence is carried forward into the next game. */
- Serial.print(randomOutput[b]); // Prints it on the screen E.g. for level 4 it could be 1433 for level 5 it would be somthing like 14332 only one number is added to the end.
- if(randomOutput[b] == 1){ // If randomOutput is = to 1
- tone(piezo, 261, 100); // Tone is played
- digitalWrite(led1, HIGH); // Led 1 is set to HIGH
- delay(d3); // A delay of 0.2sec
- digitalWrite(led1, LOW); // Then Led 1 is set to LOW
- delay(d2);
- }
- if(randomOutput[b] == 2){
- tone(piezo, 294, 100);
- digitalWrite(led2, HIGH);
- delay(d3);
- digitalWrite(led2, LOW);
- delay(d2);
- }
- if(randomOutput[b] == 3){
- tone(piezo, 349, 100);
- digitalWrite(led3, HIGH);
- delay(d3);
- digitalWrite(led3, LOW);
- delay(d2);
- }
- if(randomOutput[b] == 4){
- tone(piezo, 392, 100);
- digitalWrite(led4, HIGH);
- delay(d3);
- digitalWrite(led4, LOW);
- delay(d2);
- }
- }
- }
- }
- void systemInput(){ // This function just checks against the sequences given out in the systemOutput function.
- Serial.println("");
- Serial.println("");
- Serial.println("Sequence input...");
- for(int b = 0; b <= level;){
- button1Read = digitalRead(button1); //Tells the system where the button state information will be taken from.
- button2Read = digitalRead(button2);
- button3Read = digitalRead(button3);
- button4Read = digitalRead(button4);
- if(button1Read == HIGH){ //If button 1 is pressed then do the following
- tone(piezo, 261, 100);
- digitalWrite(led1, HIGH);
- delay(d3);
- digitalWrite(led1, LOW);
- playerInput[b] = 1; // If playerInput[b] is equal to 1 then it proceeds to the next step **
- delay(d1);
- Serial.print(1);
- if(playerInput[b] != randomOutput[b]){ // If playerInput and the randomOutput are not equal to each other then it does the following.
- oopsieDaisy(); // System goes to the oopsieDaisy function if the 'if' statement requierments are met.
- }
- b++; // ** The sysytem prooceeds to this section where one is added to b and it increases in length
- }
- if(button2Read == HIGH){
- tone(piezo, 294, 100);
- digitalWrite(led2, HIGH);
- delay(d3);
- digitalWrite(led2, LOW);
- playerInput[b] = 2;
- delay(d1);
- Serial.print(2);
- if(playerInput[b] != randomOutput[b]){
- oopsieDaisy();
- }
- b++;
- }
- if(button3Read == HIGH){
- tone(piezo, 349, 100);
- digitalWrite(led3, HIGH);
- delay(d3);
- digitalWrite(led3, LOW);
- playerInput[b] = 3;
- delay(d1);
- Serial.print(3);
- if(playerInput[b] != randomOutput[b]){
- oopsieDaisy();
- }
- b++;
- }
- if(button4Read == HIGH){
- tone(piezo, 392, 100);
- digitalWrite(led4, HIGH);
- delay(d3);
- digitalWrite(led4, LOW);
- playerInput[b] = 4;
- delay(d1);
- Serial.print(4);
- if(playerInput[b] != randomOutput[b]){
- oopsieDaisy();
- }
- b++;
- }
- }
- delay(d4);
- level++; // At the end of the function if the player has accomplished the level successfuly then the level is increased by 1.
- levelDisp++; // The level displayed on the serial monitor is also increased by 1.
- currentScore = currentScore + 100; // The current score is increased by one.
- if(levelDisp == 11){ //Sets what level you want to win the game at.
- gameWin = true; // (Always set to one level higher than your last level.
- yayYouWon();
- }
- else{
- Serial.println("");
- Serial.println("");
- Serial.println("");
- }
- }
- void oopsieDaisy(){ // This function is called when the player fails to match a sequence output.
- for(int oohPrettyLights = 0; oohPrettyLights <= 6; oohPrettyLights++){
- digitalWrite(led1, HIGH);
- digitalWrite(led2, HIGH);
- digitalWrite(led3, HIGH);
- digitalWrite(led4, HIGH);
- delay(50);
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- delay(50);
- }
- tone(piezo, 262, 100);
- delay(400);
- tone(piezo, 262,400);
- delay(400);
- tone(piezo, 262,600);
- delay(400);
- tone(piezo, 294,1700);
- delay(400);
- tone(piezo, 294,400);
- delay(400);
- tone(piezo, 294,900);
- delay(400);
- tone(piezo, 262,1000);
- delay(400);
- tone(piezo, 294, 1000);
- delay(400);
- tone(piezo, 262, 600);
- delay(400);
- tone(piezo, 262, 500);
- delay(d4);
- startGame = false; // The startGame boolean is set to false meaning that it will reset the whole game
- // and the player will have to press start to change the boolean to true in order to play again.
- delay(d2);
- Serial.println(" ");
- Serial.println(" ");
- Serial.println("|------------------------------| ");
- Serial.println("|-----------GAME OVER----------| ");
- Serial.println("|------------------------------| ");
- scoreSystem(); //Takes player to the score system function
- }
- void scoreSystem(){ // This function is used to help regulate the High Score at the end of the game.
- if(currentScore > highScore){ // If the player's current score 'currentScore' is higher than the previous high score 'highScore'
- highScore = currentScore + 0; // The high score is then set to the players current score.
- //I added a + 0 at the end of it because it stopped a bug in the code.
- delay(200);
- for(int a = 1; a <= 50; a++){ // This for loop is to enter blank lines on serial to clear screen
- Serial.println(" "); // Strictly for aperance on serial monitor.
- }
- Serial.println(" ");
- Serial.println(" Congratulations");
- Serial.println("you have beaten the High Score :D");
- Serial.print(" The new High Score is: ");
- Serial.println(highScore); // Shows the new high score
- delay(1000);
- delay(d1);
- theBigReset(); // goes to 'theBigReset' function
- }
- else{ // If the player has not beat the previous high score then their score for that
- //game is simply displayed.
- for(int a = 1; a <= 50; a++){ // This for loop is to enter blank lines on serial to clear screen
- Serial.println(" "); // Strictly for aperance on serial monitor.
- }
- delay(d2);
- Serial.println(" ");
- Serial.print(" You scored : ");
- Serial.print(currentScore);
- Serial.println(" points in that game!");
- delay(d1);
- theBigReset();
- }
- }
- /* This function has been put into place to help minimise any glitches in the game.
- This function resets all variables listed (currentScore, levelDisp, level) to prepare for the next
- game. The variables could have been reset in the oopsieDaisy function or at the end of a failed level
- but to help make the code simple and minimise risk of any errors and to make it easier to fix bugs
- and or make any changes to the reset values, they were all added in this one function. */
- void theBigReset(){
- Serial.println(" ");
- currentScore = -100;
- levelDisp = 0;
- level = -1;
- delay(400);
- setup(); //The game then at last returns to the setup to begin all over again. Once the start button is pressed ofcourse.
- }
- void yayYouWon(){ // This function is called upon for when you win the game
- for(int a = 0; a <= 10; a++){ // This for loop add's 50 blank lines into the serial monitor.
- Serial.println(" ");
- }
- Serial.println(" Congratulations You've won the game :D !!!!! ");
- Serial.println("");
- Serial.println(" Press and hold start to restart the game!");
- for(int a = 0; a <= 4; a++){
- Serial.println(" ");
- }
- startButtonRead = LOW;
- while(gameWin == true){
- for(int b = 1; b > -1; b++){
- digitalWrite(led1, HIGH); // These led's flash when startGame = true
- delay(d2);
- digitalWrite(led2, HIGH);
- digitalWrite(led1, LOW);
- delay(d2);
- digitalWrite(led3, HIGH);
- digitalWrite(led2, LOW);
- delay(d2);
- digitalWrite(led4, HIGH);
- digitalWrite(led3, LOW);
- delay(d2);
- digitalWrite(led4, LOW);
- delay(1000);
- startButtonRead = digitalRead(startButton);
- if(startButtonRead == HIGH){
- gameWin = false;
- startGame = false;
- scoreSystem();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement