lesson 10: simon - sequence and scoring€¦ · page 1 - lesson 10: simon - sequence and scoring...

20
Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the full sequence up and running, and try some scoring. You will even speed it up as the sequence gets longer. You will be using a little help in the way of a code library, but we all need a little help sometimes. There is code breakdown at every step of the way, but even if you just type it, and don’t fully understand it yet, it will still work. You can pick it apart afterwards. Goals • Make a tab • Make the sequence longer • Make the game more playable Activity Checklist Save your project Save Lesson 10: Simon - Sequence and scoring Test your Project Type code Type this code

Upload: others

Post on 12-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Page 1 - Lesson 10: Simon - Sequence and scoring

Introduction:

Now you are nearly there, but it is getting tougher! Time to get the full

sequence up and running, and try some scoring. You will even speed it up as

the sequence gets longer. You will be using a little help in the way of a code

library, but we all need a little help sometimes. There is code breakdown at

every step of the way, but even if you just type it, and don’t fully understand it

yet, it will still work. You can pick it apart afterwards.

Goals

• Make a tab

• Make the sequence longer

• Make the game more playable

Activity Checklist

Save your project Save

Lesson 10: Simon - Sequence and scoring

Test your Project

Type code Type this code

Page 2: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Lesson 10: Simon - Sequence and scoring

Page 2 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

1. Open SimonAdvancedTab Sketch

Click then CodeClub > SimonAdvancedTab

Select the whole sketch using (command A - mac) or (ctrl A - windows)

2. Copy to clipboard

Press Command or ctrl and C to copy the code. This contains code that will help you to set up your scoring.

3. Open SimonYourName

Click then find your saved sketch from last session. The sketch Simon03 will be at the same stage if you need a catch up.

4. Click on the menu item button on the righthand side of the sketch

Click then select new tab from the drop down menu.

Name this new tab ‘Advanced’ and click OK.

Keep track of your progress by ticking off the boxes below:

Create an new tabStep 1:

Page 3: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Activity Checklist

5. Paste the code saved on the clipboard into your new Advanced tab

Press Command or ctrl and V to paste the code.

6. Well done

You should now have 2 tabs, 1 containing your original sketch, the other containing some extra code to help with the scoring.

7. Click back on Simon tab

This is where your code still lives and where you are going to edit it.

Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Create an new tabStep 1:

Page 3 - Lesson 10: Simon - Sequence and scoring

Page 4: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Activity Checklist

Let’s make the sequences more than one arrow long. That is no fun at all!

1. Find your void Loop

Add the following code to create a longer sequence of arrows:

void loop() { addToSequence(); gamer.clear(); delay(delayTime); boolean success = true; for(int i=0;i<sequenceLength;i++) { int key = waitForButtonPress(); gamer.clear(); // Quickly clear delay(100); gamer.printImage(framesSimon[key]); if(key != sequence[i]) { //don’t miss this little i out! success = false; break; //skip the rest of the sequence } } delay(delayTime); if(success) { gamer.printImage(right); sequenceLength++; // Make it HARDER! } else { // They got it wrong... gamer.printImage(wrong); delay(500); } delay(500);}

Keep track of your progress by ticking off the boxes below:

Make the sequence longerStep 2:

Lesson 10: Simon - Sequence and scoring

Page 4 - Lesson 10: Simon - Sequence and scoring

Type code

Page 5: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Lesson 10: Simon - Sequence and scoring

Page 5 - Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Make the sequence longerStep 2:

Activity Checklist

2. Code Comprehension!

Lets look at the code. You have now created a for loop within the void loop.

A for loop statement is used to repeat a block of statements enclosed in curly braces.

for(int i=0;i<sequenceLength;i++) {Set i to 0, if i is less than the sequence length, then add one to i and do the following.

int key = waitForButtonPress();Create a number called ‘key’ and sets it to the value returned by the function WaitForButtonPressed.

gamer.clear();Clear the screen.

delay(100);Wait.

gamer.printImage(framesSimon[key]);Display the array image with the name held by the variable “key” (which is last key pressed). if(key != sequence[i]) {If the key pressed is not equal to the sequence i. success = false;You have failed

break;Exit the testing loop.

}End of if statement.

}End of for loop.

delay(delayTime);Wait.

if(success) {If the success is still true.

Page 6: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Lesson 10: Simon - Sequence and scoring

Page 6 - Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Make the sequence longerStep 2:

Activity Checklist

2. Code Comprehension continued!

gamer.printImage(right);Print the tick image.

sequenceLength++;Add one more to the sequence length.

} else {If it is not true (they failed).

gamer.printImage(wrong);Print the cross image.

delay(500);Leave it on the screen for half a second.

}End of if statement.

delay(500););Wait.

}End of void loop.

Click

This will compile your code. Is everything happy with your code?

Test your project

3. Test your code

Page 7: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

4. Find the function called addToSequence

Add the following code this to make the sequence longer

void addToSequence() { sequence[sequenceLength] = random(0,4); for(int i=0;i<sequenceLength;i++) { gamer.clear(); delay(delayTime); gamer.printImage(framesSimon[sequence[i]]); //you are now referencing i as the latest frame number delay(delayTime); } //this finishes the for loop}

5. Code Comprehension

So what does the bit of code you have just written do...?

void addToSequence() {This names our function addToSequence.

sequence[sequenceLength] = random(0,4);Set the next arrow in the sequence to a random number between 0 and 4 (1,2,3 or 4).

for(int i=0;i<sequenceLength;i++) {Creates a for loop that loops as long as the sequence length.

gamer.clear();Clear the screen.

delay(delayTime);Wait for amount of time set by the variable delayTime.

gamer.printImage(framesSimon[sequence[i]]);Display the sequence on the screen.

Type code

Activity Checklist

Lesson 10: Simon - Sequence and scoring

Page 7 - Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Make the sequence longerStep 2:

Page 8: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Click to save your sketch.

You have made such monumental progress, you wouldn’t want to lose it all now!

Activity Checklist

5. Code Comprehension continued!

delay(delayTime);Wait for amount of time set by the variable delayTime.

}This is the end of the for loop.

}This is the end of the function.

Lesson 10: Simon - Sequence and scoring

Page 8 - Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Make the sequence longerStep 2:

Click

This will compile your code. Is everything happy with your code?

Test your project

6. Test your code

7. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

You should now be able to play the game Simon. But if you get it wrong, it just keeps giving you more chances, we need to reset the sequence, so when you lose, you start from the beginning.

8 . Save your sketch

Save your project Save

Page 9: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Activity Checklist

1. Write a new function at the very bottom of the sketch, after the addToSequence function

This will reset the sequence so that it can be played more than once!

void resetSimon() { gamer.clear(); // Clear the gamer screen delay(100); for(byte b=0;b<sequenceLength;b++) sequence[b]=0; { } sequenceLength=0; // Clear the sequence and set its length to 0 delayTime = 300; // Set it back to normal speed. sequence[0] = random(0,4); sequenceLength++;}

2. Code Comprehension

void resetSimon() {This is an instance called resetSimon.

gamer.clear(); Clears the screen.

delay(100);Wait for 100 milliseconds.

for(byte b=0;b<sequenceLength;b++) sequence[b]=0;Clear the sequence length. A 1-line for loop doesn’t need parenthesis, but we have added them for consistency.

sequenceLength=0;Set its length to zero.

delayTime = 300;Set the delay between arrows to 300 milliseconds.

Keep track of your progress by ticking off the boxes below:

Step 1: Resetting the sequenceStep 3:

Lesson 10: Simon - Sequence and scoring

Page 9 - Lesson 10: Simon - Sequence and scoring

Type code

Page 10: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Keep track of your progress by ticking off the boxes below:

Step 1: Resetting the sequenceStep 3:

Lesson 10: Simon - Sequence and scoring

Page 10 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

2. Code Comprehension

sequence[0] = random(0,4);Set the sequence to a random number between 0 and 4.

sequenceLength++;Add 1 to the sequence length.

}End of resetSimon function.

3. Edit Void loop

This is the point in the sketch where it has detected that the sequence is wrong.This is where you summon the resetSimon function that you have just written.

if(success) { gamer.printImage(right); sequenceLength++; } else { gamer.printImage(wrong); delay(1000); resetSimon(); } delay(500);}

Type code

Page 11: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

4. Edit Void setup

You should also reset the game at the very beginning, so that it is ready to play. This is good practice.

void setup() { gamer.begin(); // Fire up the Gamer! resetSimon(); // Reset the game so that it’s ready}

Type code

Keep track of your progress by ticking off the boxes below:

Step 1: Resetting the sequenceStep 3:

Lesson 10: Simon - Sequence and scoring

Page 11 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

Click to save your sketch.

You have made such monumental progress, you wouldn’t want to lose it all now!

Click

This will compile your code. Is everything happy with your code?

Test your project

5. Test your code

6. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

You should now be able to play the game Simon and have it reset if you get the sequence wrong. Time to add some scoring and tidy up a bit.

7. Save your sketch

Save your project Save

Page 12: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

1. Edit Void setup

You are now going to access a function in the advanced tab that you imported earlier.

void setup() { gamer.begin(); // Fire up the Gamer! setupScore(); // This is for the “Advanced” tab resetSimon(); // Reset the game so that it’s ready}

2. Edit Void loop

Find the for loop that checks the success and summon the showScore function and a short delay.

if(success) { // Did they get the whole way through? gamer.printImage(right); // Print a tick! sequenceLength++; // Make it HARDER! } else { // They got it wrong... gamer.printImage(wrong); delay(1000); showScore(sequenceLength); // Show their score delay(1000); resetSimon(); // And reset } delay(500);}

Type code

Type code

Keep track of your progress by ticking off the boxes below:

Step 1: Accessing the score via the advanced tabStep 4:

Lesson 10: Simon - Sequence and scoring

Page 12 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

Page 13: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Click to save your sketch.

You know it makes sense!

Click

This will compile your code. Is everything happy with your code?

Test your project

3. Test your code

4. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

You should now have a score after each game.

5. Save your sketch

Save your project Save

Keep track of your progress by ticking off the boxes below:

Step 1: Accessing the score via the advanced tabStep 4:

Lesson 10: Simon - Sequence and scoring

Page 13 - Lesson 10: Simon - Sequence and scoring

Page 14: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

This makes the game play clearer. Communicating where you are in a game is important.

1. Edit resetSimon function

When you reset the gamer, you are now going to display the array image GO.

void resetSimon() { gamer.clear(); delay(100); for(byte b=0;b<sequenceLength;b++) sequence[b]=0; sequenceLength=0; // Clear the sequence and set its length to 0 delayTime = 300; // Set it back to normal speed. sequence[0] = random(0,4); sequenceLength++; delay(1000); //wait for 1 second gamer.printImage(go); //print go image delay(250); //wait for half a second }

Type code

Keep track of your progress by ticking off the boxes below:

Step 1: Add a start screenStep 5:

Lesson 10: Simon - Sequence and scoring

Page 14 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

Click

This will compile your code. Is everything happy with your code?

Test your project

2. Test your code

3. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

You should now have a go at the beginning and a score at the end. Awesome.

Page 15: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

This is now the cherry on the cake, the finishing touch, the final polish....

1. Edit the void loop function

Everytime the void loop function is run ( after every new addition to the sequence) lets reduce the delay between each arrow by dividing it by 1/40th , making the game speed up the longer you play it

void loop() { addToSequence(); gamer.clear(); delay(delayTime); boolean success = true; for(int i=0;i<sequenceLength;i++) { int key = waitForButtonPress(); gamer.clear(); // Quickly clear delay(100); gamer.printImage(framesSimon[key]); if(key != sequence[i]) { success = false; break; //skip the rest of the sequence } } delay(delayTime); delayTime-=(delayTime/40); // We’ll reduce the delay by 1/40th each time the loop is run

Type code

Lesson 10: Simon - Sequence and scoring

Page 15 - Lesson 10: Simon - Sequence and scoring

Activity Checklist

Keep track of your progress by ticking off the boxes below:

Step 1: Make it harder by speeding things upStep 6:

Page 16: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Lesson 10: Simon - Sequence and scoring

Page 16 - Lesson 10: Simon - Sequence and scoring

Keep track of your progress by ticking off the boxes below:

Step 1: Make it harder by speeding things upStep 6:

Click to save your sketch.

You know it makes sense!

Click

This will compile your code. Is everything happy with your code?

Test your project

2. Test your code

3. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

How is it playing now, better? More dynamic don’t you think?

4. Save your sketch

Save your project Save

Well done! You did it!

With the help if a few code libraries, you have constructed a working game, and improved it’s playability. It is far from perfect, and sometimes gets a bit confused, but it is your job to see if you can make it better. It’s called bug testing.

Page 17: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Your code should now look like this...

/* * SIMON SAYS! (structure) * A TWSU Gamer game by YOU!*/

#include <Gamer.h>

Gamer gamer; byte sequence[50];int sequenceLength = 0;int delayTime = 300;

// These are our arrow imagesbyte framesSimon[4][8] = { { // up B00000000, B00011000, B00111100, B01111110, B00011000, B00011000, B00011000, B00000000 }, { // down B00000000, B00011000, B00011000, B00011000, B01111110, B00111100, B00011000, B00000000 },{ // left B00000000, B00010000, B00110000, B01111110, B01111110, B00110000, B00010000, B00000000 },

Lesson 10: Simon - Sequence and scoring

Page 17 - Lesson 10: Simon - Sequence and scoring

Page 18: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Your code should now look like this...

{ // right B00000000, B00001000, B00001100, B01111110, B01111110, B00001100, B00001000, B00000000 }};

// This is our “GO!” imagebyte go[8] = { B00000000, B01101110, B10001010, B10001010, B10001010, B10101010, B01101110, B00100000};

// This is our tick imagebyte right[8] = { B00000001, B00000011, B00000111, B00001110, B11011100, B11111000, B01110000, B00100000};

// This is our cross imagebyte wrong[8] = { B11000011, B01100110, B00111100, B00011000, B00011000, B00111100, B01100110, B11000011};

Lesson 10: Simon - Sequence and scoring

Page 18 - Lesson 10: Simon - Sequence and scoring

Page 19: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Your code should now look like this...

void setup() { gamer.begin(); // Fire up the Gamer! setupScore(); // This is for the “Advanced” tab resetSimon(); // Reset the game so that it’s ready}

void loop() { /* Let’s add a countdown! for(int i=3;i>0;i--) { showScore(i); delay(delayTime); } gamer.printImage(go); //GO! delay(delayTime); */ addToSequence(); gamer.clear(); delay(delayTime); boolean success = true; for(int i=0;i<sequenceLength;i++) { int key = waitForButtonPress(); gamer.clear(); // Quickly clear delay(100); gamer.printImage(framesSimon[key]); if(key != sequence[i]) { success = false; break; //skip the rest of the sequence } } delay(delayTime); // Let’s make it faster! // We’ll reduce the delay by 1/40th delayTime-=(delayTime/40);

if(success) { // Did they get the whole way through? gamer.printImage(right); // Print a tick! sequenceLength++; // Make it HARDER! } else { // They got it wrong... gamer.printImage(wrong); delay(500); showScore(sequenceLength); // Show their score delay(500); resetSimon(); // And reset } delay(500);}

Lesson 10: Simon - Sequence and scoring

Page 19 - Lesson 10: Simon - Sequence and scoring

Page 20: Lesson 10: Simon - Sequence and scoring€¦ · Page 1 - Lesson 10: Simon - Sequence and scoring Introduction: Now you are nearly there, but it is getting tougher! Time to get the

Your code should now look like this...

/* Now, when the game is over, we’ll run this to reset the game so it can be played again! */ void resetSimon() { gamer.clear(); // Clear the gamer screen delay(100); for(byte b=0;b<sequenceLength;b++) sequence[b]=0; sequenceLength=0; // Clear the sequence and set its length to 0 delayTime = 300; // Set it back to normal speed. sequence[0] = random(0,4); sequenceLength++;}

/* Include addToSequence() and waitForButtonPress() here! */ void addToSequence() { sequence[sequenceLength] = random(0,4); for(int i=0;i<sequenceLength;i++) { gamer.clear(); delay(delayTime); gamer.printImage(framesSimon[sequence[i]]); delay(delayTime); }}

int waitForButtonPress() { int key = 4; while(key==4) { if(gamer.isPressed(UP)) key=0; if(gamer.isPressed(DOWN)) key=1; if(gamer.isPressed(LEFT)) key=2; if(gamer.isPressed(RIGHT)) key=3; } return key;}

Lesson 10: Simon - Sequence and scoring

Page 20 - Lesson 10: Simon - Sequence and scoring