0% found this document useful (0 votes)
77 views20 pages

CCLesson10 DIY Gamer Lessonplan New1

This document describes steps to make the Simon game sequence longer and more challenging by: 1. Adding a for loop to the void loop() to test multiple elements of the sequence array with each pass through the loop. 2. Creating a new function called addToSequence() that adds a random element to the sequence array and displays it, increasing the sequence length each time. 3. Testing the code and uploading it to the Arduino to play the updated Simon game, which now has longer, randomly generated sequences to repeat. However, the game still needs updating so a player starts from the beginning after getting a sequence wrong.

Uploaded by

asmonov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views20 pages

CCLesson10 DIY Gamer Lessonplan New1

This document describes steps to make the Simon game sequence longer and more challenging by: 1. Adding a for loop to the void loop() to test multiple elements of the sequence array with each pass through the loop. 2. Creating a new function called addToSequence() that adds a random element to the sequence array and displays it, increasing the sequence length each time. 3. Testing the code and uploading it to the Arduino to play the updated Simon game, which now has longer, randomly generated sequences to repeat. However, the game still needs updating so a player starts from the beginning after getting a sequence wrong.

Uploaded by

asmonov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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 dont 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

Type
Activity Checklist Type this code
code

Test your Project

Save Save your project

Page 1 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 1: Create an new tab progress by ticking
off the boxes below:

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.

Page 2 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 1: Create an new tab progress by ticking
off the boxes below:

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.

Page 3 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 2: Make the sequence longer progress by ticking
off the boxes below:

Activity Checklist

Lets 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:

Type
code

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]) { //dont 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);
}

Page 4 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 2: Make the sequence longer progress by ticking
off the boxes below:

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 5 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 2: Make the sequence longer progress by ticking
off the boxes below:

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.

Test your project

3. Test your code

Click

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

Page 6 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 2: Make the sequence longer progress by ticking
off the boxes below:

Activity Checklist

4. Find the function called addToSequence

Add the following code this to make the sequence longer

Type
code

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.
Page 7 - Lesson 10: Simon - Sequence and scoring
Lesson 10: Simon - Sequence and scoring

Keep track of your


Step 2: Make the sequence longer progress by ticking
off the boxes below:

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.

Test your project

6. Test your code

Click

This will compile your code. Is everything happy with 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.

Save Save your project

8 . Save your sketch


Click to save your sketch.

You have made such monumental progress, you wouldnt want to lose it all now!

Page 8 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


3: Resetting the sequence
Step 1: progress by ticking
off the boxes below:

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!

Type
code

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 doesnt 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.

Page 9 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


3: Resetting the sequence
Step 1: progress by ticking
off the boxes below:

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.

Type
code

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

Page 10 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


3: Resetting the sequence
Step 1: progress by ticking
off the boxes below:

Activity Checklist

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.

Type
code

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

Test your project

5. Test your code

Click

This will compile your code. Is everything happy with 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.

Save Save your project

7. Save your sketch


Click to save your sketch.

You have made such monumental progress, you wouldnt want to lose it all now!
Page 11 - Lesson 10: Simon - Sequence and scoring
Lesson 10: Simon - Sequence and scoring

Keep track of your


4: Accessing the score via the advanced tab
Step 1: progress by ticking
off the boxes below:

Activity Checklist

1. Edit Void setup

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

Type
code

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

2. Edit Void loop

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

Type
code

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);
}

Page 12 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


4: Accessing the score via the advanced tab
Step 1: progress by ticking
off the boxes below:

Test your project

3. Test your code

Click

This will compile your code. Is everything happy with 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.

Save Save your project

5. Save your sketch


Click to save your sketch.

You know it makes sense!

Page 13 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


5: Add a start screen
Step 1: progress by ticking
off the boxes below:

Activity Checklist

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.

Type
code

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

Test your project

2. Test your code

Click

This will compile your code. Is everything happy with 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 14 - Lesson 10: Simon - Sequence and scoring
Lesson 10: Simon - Sequence and scoring

Keep track of your


6: Make it harder by speeding things up
Step 1: progress by ticking
off the boxes below:

Activity Checklist

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

Type
code

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);
// Well reduce the delay by 1/40th each time the loop is run

Page 15 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Keep track of your


6: Make it harder by speeding things up
Step 1: progress by ticking
off the boxes below:

Test your project

2. Test your code

Click

This will compile your code. Is everything happy with 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 dont you think?

Save Save your project

4. Save your sketch


Click to save your sketch.

You know it makes sense!

Well done! You did it!

With the help if a few code libraries, you have constructed a working game, and
improved its 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. Its called bug testing.

Page 16 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

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 images


byte 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
},

Page 17 - Lesson 10: Simon - Sequence and scoring


Lesson 10: Simon - Sequence and scoring

Your code should now look like this...

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

// This is our GO! image


byte go[8] = {
B00000000,
B01101110,
B10001010,
B10001010,
B10001010,
B10101010,
B01101110,
B00100000
};

// This is our tick image


byte right[8] = {
B00000001,
B00000011,
B00000111,
B00001110,
B11011100,
B11111000,
B01110000,
B00100000
};

// This is our cross image


byte wrong[8] = {
B11000011,
B01100110,
B00111100,
B00011000,
B00011000,
B00111100,
B01100110,
B11000011
};
Page 18 - Lesson 10: Simon - Sequence and scoring
Lesson 10: Simon - Sequence and scoring

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 its ready
}

void loop() {
/* Lets 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);
// Lets make it faster!
// Well 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);
}
Page 19 - Lesson 10: Simon - Sequence and scoring
Lesson 10: Simon - Sequence and scoring

Your code should now look like this...

/*
Now, when the game is over, well 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;
}

Page 20 - Lesson 10: Simon - Sequence and scoring

You might also like