CCLesson10 DIY Gamer Lessonplan New1
CCLesson10 DIY Gamer Lessonplan New1
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
Activity Checklist
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
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.
Activity Checklist
5. Paste the code saved on the clipboard into your new Advanced tab
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.
This is where your code still lives and where you are going to edit it.
Activity Checklist
Lets make the sequences more than one arrow long. That is no fun at all!
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);
}
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.
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.
Activity Checklist
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?
Activity Checklist
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
Activity Checklist
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.
Click
This will compile your code. Is everything happy with your code?
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.
You have made such monumental progress, you wouldnt want to lose it all now!
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.
sequenceLength=0;
Set its length to zero.
delayTime = 300;
Set the delay between arrows to 300 milliseconds.
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.
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);
}
Activity Checklist
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
}
Click
This will compile your code. Is everything happy with your code?
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.
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
Activity Checklist
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
}
Find the for loop that checks the success and summon the showScore function and a
short delay.
Type
code
Click
This will compile your code. Is everything happy with your code?
Click to transfer the code onto the Arduino in the DIY Gamer.
Activity Checklist
This makes the game play clearer. Communicating where you are in a game
is important.
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++;
Click
This will compile your code. Is everything happy with your code?
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
Activity Checklist
This is now the cherry on the cake, the finishing touch, the final polish....
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
Click
This will compile your code. Is everything happy with your code?
Click to transfer the code onto the Arduino in the DIY Gamer.
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.
/*
* SIMON SAYS! (structure)
* A TWSU Gamer game by YOU!
*/
#include <Gamer.h>
Gamer gamer;
byte sequence[50];
int sequenceLength = 0;
int delayTime = 300;
{ // right
B00000000,
B00001000,
B00001100,
B01111110,
B01111110,
B00001100,
B00001000,
B00000000
}
};
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);
/*
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++;
}
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;
}