MicroWorlds Web Player is officially supported under Internet Explorer or Netscape 4 or higher
Announcements
Want to know what's happening today? Check here.
Friday, August 5
Expo day! Test the projects you plan to show and make sure at least one other person has them tried out.
Thursday, August 4
Expo tomorrow! Let me know if you need help finishing up your projects. Send me finished projects by the end of the day.
Wednesday, August 3
Keep up the great project work! Only two full work days left! Make sure you project is fully functioning and tested before you add any new features.
Tuesday, August 2
Final Projects
For the next three days, the main focus of class is to finish final projects. If you are planning a project, here is a guide to project planning:
Focus your planning on the next step. Think to yourself, If I only had ten minutes to complete this project, what would I build? That's the thing to plan and build and test next. The object to have a complete project after each step.
You can make longer term plans, but only in brief outline. That way, you haven't wasted much time if your plans change.
Each build should be the simplest code that fulfills the requirements of your project.
Each build should be thoroughly tested.
Estimate how long it will take you to complete a step and then see how long it takes you. The more you practice this, the better you will get at estimation.
Discuss your plan with me before you begin each build and show me what you've got when you're finished.
Breaks
Everyone has put in a lot of work this session, and we don't want to run out of steam in the last few days. We will continue to have outdoor breaks (weather permitting), but we will also have optional online breaks of five or ten minutes at the completion of each development cycle. As long as these breaks support productivity, we'll continue them.
Monday, August 1
Homework: Scratch Group
In MicroWorlds Help, read the Programming Fundamentals sections "MicroWorlds' Rules of Grammar" and "Iteration and Conditionals"
Homework: Greenfoot Group
If you are doing the Asteroids project, start reading the chapter. Write down questions if anything in the reading isn't clear.
If you are doing the Robotron project, read up on Robotron 2084 in Wikipedia and play Robotron at Kongregate.com. Answer the following questions:
Character abilities
How does your avatar fight enemies?
How does your avatar protect himself?
Does your avatar have a way of gaining new power or abilities?
What weakens or kills your avatar?
What kills the enemies?
Obstacles and enemies
What are the other characters in the game?
What are their powers?
How are they defeated?
What else is interesting about obstacles or enemies in this game?
Levels
What changes from one level to the next?
What stays the same?
Health and Lives
Are there changes in health or lives for Robotron characters?
If so, what changes health or lives?
Graphics and Interface
Is Robotron a 2D game or a 3D game?
Scoring and victory
How do you get points in Robotron?
How do you win?
If you are doing a platform game, read up on platform games. Write up a plan for your game: Who will the characters be? How will players score?
What features could you have in place by end of day Thursday?
Learning Objectives: Scratch Group
Pick one of the following for your final project:
Pong. A complete version with two paddles. One paddle is moved by the player and the other paddle is moved by your program.
Space Invaders.
Pacman. You may use the MicroWorlds EX documentation.
If you want to do some Java after you finish your project work, let me know.
Before we investigate the if command, let's take a closer look at the words it likes for its first inputs: "true or "false. In your project's Procedure tab, write the following procedure:
to turtleLight :ok?
ifelse :ok? [setcolor "green] [setcolor "red]
end
The turtleLight command needs one input. The turtleLight command likes "true or "false as input. What does it do when its input is "true? What does it do when its input is "false?
Notice that reporters that output "true or "false usually have names that end with a question mark.
All the inputs we gave to turtleLight above can be used as the first input to if. Remember, the second input to if must be an instruction list. Try these:
if "false [left 124 back 40]
if "true [left 124 back 40]
if key? [left 124 back 40]
if equal? who "t1 [left 124 back 40]
if less? 999 1 [left 124 back 40]
if number? 5 [left 124 back 40]
if member? "x [a b c] [left 124 back 40]
if empty? [] [left 124 back 40]
The if command can be very powerful when it is part of some forever process. For example, add this procedure to a turtle's backpack:
to handleClick
fd 5 wait 1
if greater? ycor 100 [sety -100]
if greater? xcor 100 [setx -100]
end
Put handleClick into the OnClick text box in the turtle's Rules tab. Click on the turtle. What happens? Why? Try changing the heading of the turtle and click on it again. What happens now? Draw a tank diagram for this instruction:
if greater? xcor 100 [setx -100]
What happens if the turtle has a heading of 270 when we click on it? A heading of 180? How could you fix this problem? Think of ways using more if commands or the abs reporter.
Another procedure that likes "true or "false as its first input is ifelse. What else does ifelse like as input? Check the MicroWorlds vocabulary if you're not sure. Try out this handleClick procedure in a different turtle:
to handleClick
ifelse greater? size 159 [setsize 5] [setsize sum size 1]
end
What does it do?
Now let's write a procedure called trafficLight. This will be a command that needs one input. The input can be "true or "false. If the input is "true, it will change the shape of the turtle to a green traffic light. If the input is "false, the command will change the shape of the turtle to a red traffic light.
On a blank page, add a turtle to the screen and give it two shapes: the green traffic light and the red traffic light.
Copy the code from turtleLight above. Rename the procedure trafficLight. The input to this procedure will be stored in the :ok? variable. How is the :ok? variable used in the body of the procedure? Why does the input to trafficLight have to be "true or "false?
So far, we have only changed the name of our procedure. Try these instructions:
trafficLight "true
trafficLight "false
The turtle still changes color depending on its input. How do we make it so that it changes the turtle's shape to the green light when the input is "true? How do we change it so that it changes the turtle's shape to the red light when the input is "false? Do it and test it out.
Incorporate elements from Robotron or the platform game into one of your existing MicroWorlds projects.
Extend one of the Greenfoot scenarios you have created, or one of the book scenarios from the Chapter 10 scenarios.
Today we will continue with the piano scenario
Replace the code in your own makeKeys method with the following loop:
int i = 0;
while (i < 12)
{
addObject(new Key("g", "3a.wav"), 300, 140);
i = i + 1;
}
Try it out. What happens? We can think of while as a primitive that is kind of like repeat in MicroWorlds. The difference is that while has the form of a Java if-statement. In a Java if-statement, the expression in parentheses is either true or false. If it is true, then Java executes the instructions in the following curly brackets. With while, the instructions in curly brackets are executed over and over again as long as the expression in parentheses is true.
The while statement can get you into trouble. For example, the following loop would go on forever, or until Greeenfoot ran out of memory:
int i = 0;
while (i < 12)
{
addObject(new Key("g", "3a.wav"), 300, 140);
}
Why does this loop go on and on while the loop above it only creates 12 keys and then stops?
In the last step, you actually created 12 keys, but it only looks like you created one. That is because all of the keys are in exactly the same position: 300, 140. Try moving the keys in the piano world with your mouse and you will see that they are all there.
How can you change your code so that the keys do not all appear at exactly the same place? Can you change your code so they are placed exactly next to each other? What if we use our loop variable i?
How many times does our loop body execute? What are the values of i during each of the executions?
Try replacing the old version of the addObject() method call with this:
We can create lists of Strings for keys to press and sounds to play. In Java, these lists are called arrays. Here's how they should look in your Piano class:
public class Piano extends World
{
private String[] whiteKeys =
{
"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "\\"};
private String[] whiteNotes =
{
"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g"};
// leave in your constructor and methods
}
Now we have arrays we can use create our keys. Before we do, there are a couple of important things to compare with MicroWorlds here. In MicroWorlds, if we want to pick a particular item out of a list, we use the reporter called item:
setkey item 5 [a s d f g h j k l ; ' \]
setsound item 5 [3c 3d 3e 3f 3g 3a 3b 4c 4d 4e 4f 4g]
In Java, we use the name of our array followed by brackets with the number of the item we want. For example, to set our String variable key to the 5th key in our whiteKeys array, we would write this:
key = whiteKeys[5];
In MicroWorlds, when we want to put two words together into one, we we use the reporter called word:
setsound word "3g ".wav
In Java, we put two strings together with the + operator:
sound = whiteNotes[5] + ".wav";
The code above picks the fifth string out of the whiteNotes array and puts it together with ".wav" to make the String "3g.wav". Putting Strings together in this way is called concatenation.
Here's how you should change makeKeys():
private void makeKeys()
{
int i = 0;
while (i < whiteKeys.length)
{
Key key = new Key(whiteKeys[i], whiteNotes[i] + ".wav");
addObject(key, 54 + (i*63), 140);
i = i + 1;
}
}
Try it out and make sure it works.
So far, our piano only has white keys. We have hard-coded the file names of the key images ("white-key.png" and "white-key-down.png"). Let's use abstraction to modify the Key class so that it can show either white or black keys. This is siilar to what we did with the key name and sound file name: Introduce two fields and two parameters for the two image file names, and then use the variables instead of the hard-coded file names. Test by creating some black and some white keys.
Modify your Piano class so that it adds two black keys at an arbitrary location.
Add two more arrays to the Piano class for the keyboard keys and notes of the black keys:
private String[] blackKeys =
{ "w", "e", "", "t", "y", "u", "", "o", "p", "", "]" };
private String[] blackNotes =
{ "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" };
Notice that some of the items in the arrays are empty Strings. The black keys are not as evenly spaced as the wide keys. The empty Strings stand for the gaps between black keys.
In your makeKeys() method, you will need to add code to add the black keys after you add the white keys. Notice the test for empty strings. How does that help us make sure that we create the right number of black keys and put them in the right places?
// make the black keys
for(int i = 0; i < whiteKeys.length-1; i++) {
if( ! blackKeys[i].equals("") ) {
Key key = new Key(blackKeys[i], blackNotes[i]+".wav", "black-key.png", "black-key-down.png");
addObject(key, 85 + (i*63), 86);
}
}
Test out your changes and make sure they work.
Friday, July 29
Learning Objectives: Scratch Group
So far, we've been looking at procedures that have no input or like numbers as input. Today we're going to look at procedures that like instruction lists as input. Here are some of them:
ask
cancel
carefully
done?
everyone
forever
if
ifelse
launch
newbutton
repeat
run
when
The only input for some of these procedures is an instruction list. Others need more than one instruction list, or an instruction list and some other input. Look up the commands below in the MicroWorlds vocabulary. For each procedure, answer the questions: How many inputs does it need? Does it need more than one instruction list, or only one instruction list? Which of its inputs need to be instruction lists? What does it like for the other inputs? Draw tanks for each.
ask
everyone
forever
repeat
The ask and everyone procedures help you direct your instructions to particular turtles. The forever and repeat commands give instructions to run over and over. Remember, an instruction list is a special kind of list. Try out these instructions:
announce [Center for Talent Development]
everyone [Center for Talent Development]
show [fd 50 rt 90]
everyone [fd 50 rt 90]
repeat 4 [fd 50 rt 90]
show [happy new year]
repeat 4 [happy new year]
How can you use error messages to tell if a procedure likes any kind of list or only instructions lists as input?
Repeat and if
The commands repeat and if both need two inputs, and both like an instruction list as their second input. What does repeat like as its first input? What does if like as its first input? Try these:
As long as repeat gets a number for its first input, it doesn't care where that number came from. Notice that when a reporter needs inputs, we write all the inputs for the reporters before we write the second input to repeat.
Learning Objectives: Greenfoot Group
Today we are going to work on an on-screen piano. These exercises are taken from chapter 5 of Introduction to Programming with Greenfoot, with Michael Kolling.
Open the scenario piano-1 and examine the code for the two existing classes, Piano and Key. Make sure you know what code is present and what it does.
Create an object of class Key and place it into the world. Create several of them and place them next to each other.
So far, we don't have much code. The Piano class only sets the size of the world and its cells (the world size is set to width 800 cells and height 340 cells, and the cell size is set to 1). We can add code to make it look like a piano key has been pressed when we press a key on the keyboard. In Greenfoot, we can use Greenfoot.isKeyDown() to see if a particular key is up or down. Change the act() method for the Key class to look like this:
public void act()
{
if (Greenfoot.isKeyDown("g")) {
setImage("white-key-down.png");
}
else {
setImage("white-key.png");
}
}
Test this code. Don't forget you need to press Run (Why?). Could you implement this in MicroWorlds?
This code works, but there's a problem with it. The image changes every time the act() method is called. In general, it's not a good idea to ask your program to do unnecessary work. It wastes processor time. However, there's another reason why this code will give us trouble later. We're going to add sound. To simulate a piano, we should hear a sound once when we press a key. We should not hear the sound over and over as long as the key is down. So what do we do?
We can add a new boolean field to the Key class to keep track of whether or not the key is down. Field is another way of saying instance variable, what we call state variables in MicroWorlds. For example, in MicroWorlds, you might have created a variable like startpos or homepos to save a starting position for all of your turtles. In the crab scenario, we created fields (instance variables) called image1, image2, and wormsEaten.
The declaration for our boolean field isDown looks like this:
private boolean isDown;
We will store true in this field when the piano key is down, and false while it isn't.
public void act()
{
if (!isDown && Greenfoot.isKeyDown("g")) {
setImage("white-key-down.png");
isDown = true;
}
if (isDown && !Greenfoot.isKeyDown("g")) {
setImage("white-key.png");
isDown = false;
}
}
The ! operator in Java is the same as the not operator in Logo. If its input is true, it returns false. If its input is false, it returnns true. The && operator in Java is the same as the and operator in Logo. It takes two boolean inputs. If both inputs are true, it returns true. Otherwise, it returns false.
Implement this change and test it. You won't see any difference in the way things work yet, but this will make sound work correctly when we implement sound.
Now let's implement sound. First, we'll make a stub for the Key's play() method:
public void play()
{
}
This method doesn't do anyting yet, but it should compile.
To play a sound, we use a method from the Greenfoot class: Greenfoot.playSound("3a.wav");
Add this method call to your play() method. Make sure it compiles.
Test your play() method. After you compile, add a key to your world and from the key's popup menu invoke the play() method.
Add code to your Key class to invoke play() when you press letter "g" on your keyboard.
What happens when you create two keys, run the scenario and press the "g" key? Do you have any ideas about what we need to do to make them react to different keyboard keys? Think about the way we store different information in different turtles in MicroWorlds.
Suppose we give the Key class a field for key we press ("g" or "h" or whatever) and another field for the name of the sound file (for example, "3a.wav"). Then every time we create a new member of the Key class, we can give it a key value and a sound value of its own (just like we can give a turtle a homepos variable, or a health variable or whatever, and give each turtle its own values for those variables). To do this, we'll add String variables that we will initialize in the Key constructor. Programmers use the term initialize to mean "set a variable to its initial (beginning) value." We do this in MicroWorlds, for example, when we set a score to 0 at the beginning of game. Here's what the top of your Key class file should look like when you've added code to create key and sound variables and initialize them:
public class Key extends Actor
{
private boolean isDown;
private String key;
private String sound;
/** Create a new key linked to a given keyboard key, and
* with a given sound
*/
public Key(String keyName, String soundFile)
{
key = keyName;
sound = soundFile;
}
Make sure this compiles. What do we need to change so the Key class uses the variables key and sound instead of "g" and "3a.wav"?
Remember when you added code to CrabWorld so that it would automatically populate the world with a crab, worms and lobsters? We can do something like to Piano so it automatically adds keys. You can add code like this to the Piano constructor:
addObject(new Key("g", "3a.wav"), 300, 180);
Test this and make sure it works.
Change the y-coordinate at which the key is placed, so that the piano key appears exactly at the top of the piano (i.e., the top of the piano key should line up the top of the piano itself). Hint: The key image is 280 pixels high and 63 pixels wide.
Write code to create a second piano key that plays a middle-g (sound file name 3g.wav) when the "f" key is pressed on the keyboard. Place this key exactly to the left of the first key (without any gap or overlap).
In the Piano class, create a new method named makeKeys(). Move your code that creates your keys into this method. Call this method from the Piano's constructor. Make sure to write a comment for your new method.
Thursday, July 28
Evaluations
If you're curious, here's what a blank evaluation form looks like
Learning Objectives: Scratch Group
Yesterday, we saw how to use these error messages to give us information about how to use a procedure:
We know that reporters don't make any changes to the MicroWorlds environment. They don't move the turtle or make text appear in a text or cause anything to be printed in a text box. They just provide input.
Since reporters don't work as commands, we get an error message, for example, when you type xcor or sum 5 8 into the Command Center and press Return (What is the error message?).
Commands don't work as reporters, either. For example, suppose we tried to use the commands pd or st to give input to another procedure:
fd pd
pd didn't report anything to fd
show sum st 7
st didn't report anything to sum
Commands don't report anything, so pd and st didn't report anything to fd or sum.
Things to Try
Try using the following procedures as input to the show command. Which ones are commands and which ones are reporters? Draw tanks for each.
cg
clickon
fill
heading
ht
size
shape
st
xcor
Draw tank diagrams for the following instructions. How can you tell from the diagrams which instructions will work and which ones won't? If it won't work, what error message do you think you'll get? If it will work, what do you think it will do? After you make your predictions, try them out.
glide xcor
sum size shape
forward size
setsize pu
left ycor
setshape sum heading
product 8 size
show home
Picky Procedures
Some procedures are very picky about the input they use. For example, try out these forward commands:
forward "cake
forward does not like cake as input
forward [0 78]
forward does not like [0 78] as input
forward pos
forward does not like [-63 9] as input
forward key?
forward does not like false as input
What kind of input does forward like? Numbers. It doesn't matter to forward where the number comes from. It might be typed in directly, like this:
forward 50
Or the number might be the output of some reporter:
The instructions below use the reporters char, cos, difference, empty? greater? and heading. Each command expects a number as input. Each reporter has the correct number of inputs. Use error messages to tell which of these reporters have numbers for output and which ones don't. Draw a tank for each reporter. Show the correct number of inputs for each reporter. Show what kind of output comes out of each reporter by drawing it coming out of the tank.
forward char 65
setsize cos 60
right difference 92 37
setcolor empty? [a b c]
back greater? 8 7
left heading
Use the MicroWorlds tutorial to create a simple Pong game in MicroWorlds.
Open the Scratch If examples. Click on the flag. What happens? How could you animate the action with a forever block? Try it. Create the same scene in MicroWorlds with a turtle instead of a cat. Test it to make sure it works.
Explain the use of everyone, ask, turtle-name-followed-by-comma, and who. Compare the use of the green flag and the when clicked block in Scratch with everyone [clickon] and the OnClick rule in MicroWorlds.
Learning Objectives: Greenfoot Group
Things to try (these exercises are from Introduction to Programming with Greenfoot, by Michael Kollin). Today we finish up the Crab Scenario. You can open the crab scenario you've been working with, or open up the pre-written scenario little-crab-4.
First, we'll add code to the CrabWorld constructor to create a crab automatically. To do this, add the following method call: addObject( new Crab(), 150, 100);
to the last line of your CrabWorld() constructor. A constructor of a class is a special kind of method that is executed automatically whenever a new instance is created. It has no return type, and its name is the same as the name of the class. Notice that we create a new crab by using the keyword new followed by a method call to the constructor, for example, new Crab().
Add code to the CrabWorld constructor to automatically add three lobsters to the CrabWorld(). Pick whatever x coordinates and y coordinates you like. The call to super(560, 560, 1) tells us that the crab world has a width of 560 cells and a height of 560 cells, and each cell is 1 pixel (For more detail, see the class documentation for World's constructor).
Add code to create 10 worms at arbitrary locations in the CrabWorld.
Move all the code that creates the objects into a separate method, called populateWorld(), in the CrabWorld class. You need to declare the populateWorld() method yourself (it takes no parameters and returns nothing) and call it from the constructor. Test what you've done.
Use random numbers for the coordinates of the worms. You can do this by replacing your coordinate values with calls to get random numbers from the Greenfoot class.
In MicroWorlds, we make turtles change shape with the setshape command. There is a similar command in Greenfoot called setImage(). This command is a method in the Actor class. All animals (including crabs) inherit this method. Here is one signature for setImage(): void setImage(GreenfootImage image);
If you're curious about the other signature for this method, see the documentation for the Actor class.
To create a GreenfootImage for crab2.png, we use new GreenfootImage("crab2.png"). To set the image, we could use: setImage(new GreenfootImage("crab2.png");
We're going to store the images for crab.png and crab2.png so we can switch between them the way we switch between stored shapes in MicroWorlds using setshape
Before we store the images, let's inspect a crab object to see what is already in its backpack. Right click on a crab on a crab in your world and choose Inspect. What do you see?
We'll add new variables for the two images by adding these lines to the top of the Crab class (above the constructor): private GreenfootImage image1; private GreenfootImage image2;
A private Crab variable can only be seen by the Crab class.
Recompile your code and put a new crab in your world. Inspect this crab. Do you see new entries for image1 and image2?
In MicroWorlds, we assign a value to a state variable with set commands like setpos homepos or setlives sum lives 1. In Java, we use an equal sign. Inside the Crab constructor, add these assignment statements: image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png");
Remember, a single equal sign in Java works like setname in MicroWorlds. To compare two values, we use equal? in MicroWorlds and == in Java.
When a new crab is created, we want to set its image to image1, so after the second assignment statement, we need to add a method call to setImage(): setImage(image1);
Make sure your changes compile and you can still add a crab to your world. Inspect the crab again. In the next steps, we'll add code to actually switch between images.
In MicroWorlds, if we want to see what shape our turtle has, we use the shape reporter. In Greenfoot, we use getImage(). To alternate between image1 and image2, we need to add the following code to the act() method of the Crab class: if (getImage() == image1) { setImage(image2) } else { setImage(image1) }
Try out the code. If it doesn't work, fix it. Use the Act button instead of the Run button to observe the crab's behavior more closely.
Create a method called switchImage() that uses the if-statement we created to switch between crab images. Replace the if-statement in the act() method with a call to the switchImage() method.
Test out the switchImage() method by calling it interactively from the crab's popup menu. Does it work?
In MicroWorlds, we created state variables to keep track of things like a turtle's health or lives. We've already seen how we can add private variable to a class in Java. In Java, these variables are called instance variables. We'll add another one to keep track of the number of worms a crab has eaten. For this, we need a number, so we'll add a private variable like this: private int wormsEaten;
This definition belongs above the Crab constructor with the definitions for image1 and image2
In MicroWorlds, we've seen that we can run into trouble if we don't set our variables to a certain value at the beginning of a game. For our crab game, we're going to set wormsEaten to 0 whenever we create a new Crab. We do that by adding this line to our Crab constructor: wormsEaten = 0;
Every time we eat a worm, we want to add one to the number of worms eaten. Where does the crab eat a worm? In the lookForWorm() method. Find that method in the Crab class. After the method call to eat(), add the following line: wormsEaten = wormsEaten + 1;
Test this out. Put your crab and three worms into the world. Each time the crab eats a worm, inspect the crab. Is the value of wormsEaten correct?
Let's have our player win once the crab eats eight worms. Add the following code after we add 1 to wormsEaten: if (wormsEaten == 0) { Greenfoot.playSound("fanfare.wav"); Greenfoot.stop(); }
Test it out. What happens when the crab eats 8 worms?
When your are finished, here are some other things you might want to try:
Using different images for the backgrund and the actors
Using more different kinds of actors
Not oving forward automatically, but only when the up-arrow key is pressed
Building a two-player game by introducing a second keyboard-controlled class that listens to different keys
Making new worms pop up when one is eaten (or at random times)
Anything else you may think of!
Wednesday, July 27
Learning Objectives: Scratch Group
So far, we've been comparing Scratch blocks with MicroWorlds procedures to get a clearer understanding of commands and reporters and how they fit together. We'll do more of that later. Today, we're going to see how error messages in MicroWorlds can give us clues about kinds of procedures and their information needs.
Commands and Reporters as Text Messages
We can imagine the Command Center as a place where we send text messages to an alien named Foo (see the video on Logo Programming Basics for more detail). Foo lives to serve you. Foo expects every message you send to be a command. For example, you can send Foo the command to put the turtle at the position [0 0] (the middle of the Main Window):
home
If you send Foo a message that is not command, Foo gets confused and sends back a message. For example, if you type the number 32 into the Command Center and press Return, you'll see this:
32
I don't know what to do with 32
Some procedures don't tell Foo what to do. They just give information that other procedures can use as input. These are reporters. If you type in a reporter without using it as input to another procedure, Foo is just as confused as before. Here's an example:
xcor
I don't know what to do with -63
In this case, the turtle's x coordinate was -63. The reporter xcor passed that information on to Foo, but Foo didn't know what to do with the information.
Things to Try
Use the message from Foo to tell if the following procedures are commands or reporters. Draw a tank for each.
heading
ht
pd
pe
pos
pu
size
shape
st
xcor
ycor
Procedures with Inputs
Some procedures need information in order to do their jobs. For example, the forward command needs a number as input so Foo knows how far forward to move the turtle. When Foo doesn't get the required input, Foo sends a message:
forward
forward needs more inputs
Some procedures need more than one input:
glide
glide needs more inputs
glide 50
glide needs more inputs
glide 50 0.1
There was no error message after the last instruction because glide got all the inputs required.
Things to try
The procedures below are all commands. Some of the commands need inputs, some don't. The ones that need input like numbers for input. Use error messages to see how many inputs are needed. Draw tanks for each.
cg
clickon
fill
left
setcolor
Some reporters also need inputs. Here are examples:
show sum 52 6
58
The numbers 5 and 6 are inputs to the reporter sum. What is the output of sum? How does show use the output? Try sending this message to Foo:
fd sum 52 6
What happens?
Try sending this message:
right sum 52 6
What happens?
When a reporter doesn't enough inputs, it sends the same message as a command that doesn't get enough inputs. Try these:
left sum
left sum 1
left sum 3 4
What happens?
Now Try This
For each of the following reporters, use error messages from Foo to see how many inputs they need. Remember, you can't have a reporter at the beginning of an instruction because Foo expects ever message to be a command. Draw a tank for each reporter.
abs
bg
color
difference
int
minus
opacity
Use the video below to make a simple Pong game:
Homework
Give two examples of instructions that would give you each of the error messages below.
Draw tank diagrams for each of the following instructions:
forward 50
left ycor
show sum 10 7
17
right sum 10 heading
glide xcor ycor
Learning Objectives: Greenfoot Group
Things to try (these exercises are from Introduction to Programming with Greenfoot, by Michael Kollin). You will need to know the Java operators used to compare values:
<
less than
>
greater than
<≡
less than or equal
>≡
greater than or equal
≡≡
equal
!=
not equal
Try to write down, on paper, an expression using the getRandomNumber() method and the less-than operator that, when executed, is true exactly 10 percent of the time
Write down another expression that is true 7 percent of the time.
Try making random course changes by adding a second if-statement to the Crab's act() method. This new if-statement should get its boolean input from Greenfoot.getRandomNumber(100) < 10 and should run turn(5) if the boolean expression is true.
Experiment with different probabilities for turning.
Make the crab turn a random amount between 0 and 44 degrees. Try it out. Does the crab turn different amounts when it turns?
Make the crab turn either right or left by up to 45 degrees each time it turns.
Try running your scenario with multiple crabs in the world. Do they all turn at the same time, or independently? Why?
Create a new subclass of Animal, the Worm class. Be sure to give it the worm.png image.
Add some worms and some crabs to your world. Does anything interesting happen when you run the scenario? Why or why not?
Look up the canSee() and eat() methods in the Documentation view of the Animal class. For each method, answer the questions: Is it a command or a reporter? How many inputs does it need? What does it like for inputs? If it is a command, what is its effect? If it is a reporter, what is its output?
Try adding this code to the end of the Crab's act() method: if (canSee(Worm.class)) { eat(Worm.class); }
What happens when you test out this new code?
Create a new Crab method called lookForWorm() that looks like this:
/**
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
}
}
Replace the last if-statement in the Crab's act() method with a method call to lookForWorm()
Create another new Crab method called randomTurn(). This method should be a command with no inputs. Use the code that does the random turning in the Crab's act() method and then replace that code with a method call to randomTurn().
Create yet another new Crab method called turnAtEdge() using the code in Crab's act() method that turns the crab when it reaches the edge. Replace this code in the act() method with a method call to turnAtEdge().
Add a new subclass of Animal called Lobster. Use lobster.png for its image.
What do you think will happen when you add lobsters to the world? Try it and see.
Program your lobsters to look for crabs and eat them the way that crabs look for worms and eat worms. Copy the complete act() method from the Crab class into the Lobster class. Also copy the complete lookForWorm, turnAtEdge, and randomTurn methods.
Change the Lobster code so it looks for crabs instead of worms. To do this, change every occurrence of "Worm" in the Lobster source code to "Crab". Change Worm.class to Crab.class everywhere in the Lobster class and change lookForWorm to lookForCrab. Update your comments.
Place a crab, three lobsters, and lots of worms into the world. Run the scenario. Does the crab manage to eat all worms before it is caught by the lobster?
Now let's add keyboard control. The first thing you need to do is learn how to detect key strokes in Greenfoot. The Greenfoot environment has a boolean method called isKeyDown() that checks to see if a particular key has been pressed. The method's signature looks like this: static boolean isKeyDown(String key)
Notice that the parameter expected by isKeyDown() has type String. A String is a piece of text written in double quotes. For example,
"This is a String"
"name"
"A"
Notice that Strings, unlike words in MicroWorlds, need quotation marks at the beginning and the end, not just the beginning. To test to see if the left arrow key has been pressed, you need to use the method call Greenfoot.isKeyDown("left"). This will return a boolean value of true if the left arrow key is down.
Remove the random turning code from the crab. We're going to replace it with code for controlling the crab with the arrow keys.
Remove the code from the crab that does the turn at the edge of the world.
Add code into the Crab's act() method that makes the crab turn left whenever the left arrow key is pressed. Test it.
Add another, similar bit of code to the Crab's act() method that make the crab turn right whenever the right arrow key is pressed.
If you have not done so in the first place, make sure that the code that checks the key-presses and does the turning is not written directly in the act() method but is instead in a separate method, maybe called checkKeyPress(). This method should be called from the act() method.
Open the Greenfoot API in your browser. Select the Greenfoot class. In its documentation, find the section titled "Method Summary". In thsi section, try to find a method that stops the execution of the running scenario. What is this method called?
Does this method expect any parameters? What is its return type?
Add code to your own scenario that stops the game when a lobster catches the crab. You will need to decide where this code needs to me added. Find the place in your code that gets executed when a lobster eats a crab, and add this line of code there.
Open the Greenfoot Class Documentation (from the Help menu), and look at the documentation of class Greenfoot. Find the details of the method that can be used to play a sound. What is its name? What parameters does it expect?
Add playing of sounds to your scenario: Whne a crab eats a worm, play the "slurp.wav" sound. You will need to make a method call like this: Greenfoot.playSound("slurp.wav");
Where do you need to add this code so the method call is made when a crab eats a worm? Put the method call where it belongs.
Test it. Add code so that when a lobster eats a crab, it makes the "au.wav" sound. Test it.
Look around Greenfoot web site to see more things you can do with Greenfoot.
Greenfoot is only one Java development environment. It was designed to get students started quickly with graphics. Professional Java developers generally use more complex development environments like Eclipse. Check out this page for an example of a game built with a more sophisticated development environments.
Changle the color of your sprite when a space key is pressed
Control a sprite's graphic effects with the mouse
Move your sprite to a drum beat
Import a new background, animate your sprite and create a music loop
Add your own background and create a dialog for a story
Implement as many of these as you like in Scratch. Pick out at least one to implement in both Scratch and MicroWorlds. Remember, in MicroWorlds, you can only see a turtle change color when it has shape 0 (the default turtle shape). Also, MicroWorlds doesn't have the graphic effects capabilities of Scratch, so you would have to change color or transparency or something else.
Explain the use of everyone, ask, turtle-name-followed-by-comma, and who. Compare the use of the green flag and the when clicked block in Scratch with everyone [clickon] and the OnClick rule in MicroWorlds.
Review this Scratch Collision Detection example. Make two similar examples in MicroWorlds. The first example should not use the OnTouching rule. The second example should use the OnTouching rule and the OnColor rule.
Learning Objectives: Greenfoot Group
Things to try (these exercises are from Introduction to Programming with Greenfoot, by Michael Kollin). You will need to download the Greefoot book scenarios.
Open the little-crab scenario. Place a crab in the world and press the Run button. What happens?
Change the act() method in the Crab class to invoke the move() method. Now what happens when you press Run with a crab in the world?
Add multiple crabs and Run. What do you see?
In the Crab's act() method, replace the method call to move() with a method call to turn(5). Test it out.
How can you make the crab turn left?
Change the act() method so the crab will turn and move. To do this, you need a move() method call and a turn(5) method call. Try other inputs to turn() besides 5.
What happens if you take out the semicolon after the method call move() in Crab's act() method and then try to compile? What error message do you get?
Make other changes in your code to get other error messages. Find five different error messages. Write them down.
Open the editor for the Animal class. Switch to Documentation view. How many methods does this class have?
Create a crab. Right-click it, and find the boolean atWorldEdge() method. You'll need to look in the inherited from Animal submenu. Call this method. What does it return?
Let the crab run to the edge of the screen and then call the atWorldEdge() method again. What does it return now?
Change the Crab's act() method so it calls turn(17) if the crab is at the world's edge.
Test it out. Try different values for the parameter to turn().
Place the move() statement inside the if-statement. What changes? Why? Put the move() statement back where it was.
Things to know:
What is a method call?
In the method call turn(5), what is the parameter?
In what order are instructions executed in Greenfoot? In MicroWorlds?
When are error messages generated?
What do we mean when we say that a subclass inherits methods from its superclass?
Methods with a void return type are commands. They give orders. All other methods are reporters. They answer questions.
How to write an if-statement in Logo using if or ifelse and how to write an if-statement in Java. You should also be able to explain how Logo if-statements and Java if-statements are similar and different.
Make sure you're clear on what causes these messages.
Friday, July 22
Learning Objectives
By the end of the day, you should be able use downloaded images for backgrounds and sprites and to add wallpaper to a project. You should also know how to add and use sound or music to your project and how to add and use the following objects: sliders, check boxes, round button sets, and list boxes.
Thursday, July 21
Learning Objectives
Give examples of each of these kinds of data: word, list, word-or-list, (word-or-list1, word-or-list2), number, (number1, number2), true-or-false, (true-or-false1, true-or-false2), char, who turtle-name, path, [x y].
Give examples of each of these kinds of primitives: setter, getter, predicate, math operator, word and list manipulator (selector and constructor).
From the vocabulary definition of a primitive procedure, you should be able write the signature (top line) of the procedure as it would appear in a Procedure tab. You should also be able to tell if that procedure would need the key word OUTPUT or not.
From the written recipe of a procedure in a Procedure tab, you should be able to write a vocabulary definition of the procedure (programmers are often required to do this when they write libraries of procedures for other to use).
You should be able to draw a picture of a tank for a procedure based on the vocabulary definition of a primitive procedure or the written recipe of a procedure in a Procedure tab.
From the vocabulary definition of a primitive procedure or the written recipe for a procedure in a Procedure tab or picture of a tank for a procedure, you should be able to give an example of how you could use the procedure in an instruction.
From a written instruction, you should be able to draw a tank diagram, looking up procedure names in the MicroWorlds vocabulary or Procedure tabs as necessary.
Daily Challenge
Yesterday's challenge will be complete when you demonstrate the changes you made to me. When that is done, you can check out the Wombat project. Open it up and see if you can figure out how it works. I'll come around so you can tell me how you think it works and ask me any questions.
Homework
Read Infix Operations. Make sure you understand the Solved Problems that start on page 8. Try the Supplementary Problems that start on page 11 without looking at the the solutions, then check to see that your solutions are correct. If any are incorrect, go back to the Solved Problems to make sure you understand where you went wrong.
Wednesday, July 20
Classroom Management
We need to set up times for demos and for playing each other's games. We also need to talk about support for your programming efforts. Resources include MicroWorlds Help, other students and me. Since we just have a short period of time, I need to focus on teaching you how to manage complex programs rather than debugging. How do we make that happen? Would it help to have a couple of group debugging sessions for people who struggle with this?
Learning Objectives
Today we'll talk some more about managing your project as it gets more complex. The concepts of classes and objects will be introduced.
Daily Challenge
Yesterday we talked about managing the complexity of a multi-player game with multiple levels. Today I want to talk about games with lots of players of the same type. For example, you might have multiple frogs, robots, butterflies, or rockets that all have the same behavior (all frogs eat butterflies, all robots attack the game player's avatar, and so on).
One way to handle classes of objects is to use private procedures. For example, if robots attack your avatar in a different way from zombies, you could put one attack procedure in all the robots and a different attack procedure in all the zombies. Then you can invoke the attack procedure for all enemy turtles without having to check first which kind of turtle it is.
Today's example is based on a project that Seamus created called Circle of Life. I took out steering to simplify the project. In this project, dogs eat cats, cats eat birds, and birds eat butterflies. Each animal gets one energy point when it eats another animal.
Your challenge: change the project so that
each animal starts with 5 energy points
each animal gets 5 energy points when it eats something
each animal loses 1 energy point every 2 seconds
each animal still dies when it is eaten, but also dies when its energy points get to zero.
Extra challenge: Write a procedure called random.between.
random.between is a reporter
random.between needs two inputs
random.between likes numbers as inputs
random.between reports a random number between the two input numbers. Either input should be one of the possible outputs.
Use your random.between procedure to randomly change the behavior or appearance of turtle in Circle of Life. For example, you could randomly change the size of a turtle to some number between 5 and 160. Apply this change to a particular class of objects (for example, all Birds or all Dogs).
We need to be at Norris at 9:50 this morning for IDs. We'll leave around 9:40 to get there in time.
Demos
Interested in doing a demo? Maybe somebody can set up a sign-up document through your Google account. We'll schedule them as we have time. Harry should be first if he likes, since he was the first one to ask me about demos.
Learning Objectives
How do you use run and carefully with word to select procedures you want to execute? How do you use word and written reporters to keep track of turtles on different pages?
Daily Challenge
Starting with the multiplayer, multi-level starter project, create a game that is fun to play that requires at least two pages and two characters.
Monday, July 18
Welcome to Bits and Blocks Level 2!
We will spend the morning getting acquainted with each other and reacquainting ourselves with MicroWorlds.
Lunch will be from 11:00 to 11:50. An RA will take you to and from the dining hall.
ID Photos will be taken at 12:30 in Norris University Center.
Learning Objectives
Below are our immediate learning objectives. Much of this will be review. More will be added as the course continues. There is review material in the links on the left side of this page.
What is a procedure? What is a primative procedure? What do we mean when we say that a procedure is public or private? When should we use private procedures? When should we use public procedures?
Looking at the definition of a primitive procedure in the MicroWorlds vocabulary or the recipe for a procedure in a Procedure tab, you should be able to draw a tank diagram for the procedure and answer these questions:
Is the procedure a command or a reporter?
How many inputs does it need?
What does it like as inputs?
If it is a command, what is its effect?
If it is a reporter, what is its output?
You should be able to define superprocedures, subprocedures, and variables and give examples of their use.
Error messages
Error messages are your friends. Today, make sure you're clear what error message you get when you try to use a reporter like a command or try to use a command like a reporter.
Daily Challenge
Every day we will have challenges for students to work on. If you want to try the challenges without a lesson, you may try them and get help as needed. If they like, students may choose to see a lesson or a worked example first.
Today we will review commands, reporters, and tank diagrams. A discussion of tank diagrams and practice exercises can be found in Logo: An Introduction. The challenge for today will be to draw a tank diagram for the following instruction:
if not member? who [t1 t2] [remove who]
and explain what the instruction does.
Extra Challenge: Write a procedure that removes all of the turtles from a page except for "t1 and "t2.
Reflections
Students will be expected to complete daily reflections before leaving for the day.
Center for Talent Development (CTD), housed at Northwestern University's School of Education and Social Policy, is an accredited learning center and research facility that identifies, educates and supports gifted students and their families and serves as a leader in gifted education. Learn more about the Center for Talent Development.