12
Quick Greenfoot Score Counter For the purposes of this demo, I will be using MIK's Trick The Turtle scenario, as used at the start of Episode 3 of Joy of Code, plus a score counter I have used elsewhere. The PDF you are reading should have been supplied with these files. Open TrickAtStart in Greenfoot – this is the file we will be working from. It is called TrickAtStart, so you know it is the version of Trick the Turtle at the start of this tutorial (as opposed to, TrickAtEnd). It opens, with no objects loaded. The only class that exists is a Turtle. The Turtle has no code inside it to tell it to do anything (right click on Turtle, open editor). In this lesson we will: Create a flower for the Turtle to eat; Make the Turtle move; Tell the Turtle to count how many flowers it has eaten; Create a Scoreboard object, to ask the Turtle how many flowers it has eaten. Quick Greenfoot Score Counter 1 of 12

Quick Greenfoot Score Counter - Adventures in teaching ICT and

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Quick Greenfoot Score Counter

For the purposes of this demo, I will be using MIK's Trick The Turtle scenario, as used at the start of Episode 3 of Joy of Code, plus a score counter I have used elsewhere. The PDF you are reading should have been supplied with these files.

Open TrickAtStart in Greenfoot – this is the file we will be working from. It is called TrickAtStart, so you know it is the version of Trick the Turtle at the start of this tutorial (as opposed to, TrickAtEnd).

It opens, with no objects loaded. The only class that exists is a Turtle.

The Turtle has no code inside it to tell it to do anything (right click on Turtle, open editor).

In this lesson we will:• Create a flower for the Turtle to eat;• Make the Turtle move;• Tell the Turtle to count how many flowers it has

eaten;• Create a Scoreboard object, to ask the Turtle how

many flowers it has eaten.

Quick Greenfoot Score Counter 1 of 12

Creating a FlowerOn the right-hand menu (see image), right-click on Animal and choose New Subclass.

You will see the dialogue below. Type Flower as the New Class Name. Yes, the capital “F” does matter. You will create problems for yourself when creating a scoreboard if you ignore this.

Then, under Image Categories choose Nature and choose flower.png. Then press OK.

That's it. Our flower will just sit there doing nothing, so there is no need to give it any code. At this stage, put a single Turtle into the world and put in four flowers. Then Save The World (by right clicking on the left-hand panel, and saving the world). You might need to compile before you do this.

Quick Greenfoot Score Counter 2 of 12

Ta-da! You now have a Turtle and some flowers that don't move. But it looks nice, doesn't it?

Quick Greenfoot Score Counter 3 of 12

2: Make the Turtle MoveOpen the Turtle's code. Currently, it does nothing. We are going to make it move, with a simple move() command and user-driven left/right/forward controls. It's not fancy, but for our purposes today, it doesn't need to be.

Type the following in:

• A single command in the act() method, to call a new method moveTurtle()

• A new method, moveTurtle(), with the left/right/up cursor keys used for control.

Compile it and check the Turtle moves. If it doesn't compile, you have a typo.

Quick Greenfoot Score Counter 4 of 12

3: Turtle Eats and Counts FlowersNow we want the Turtle to eat flowers and count the flowers it has eaten.

Put a single line, public int flowersNibbled=0; just before the act() method, as shown. This will be the counter.

Now create a nibbleFlower() method, underneath moveTurtle().

Finally, don't forget to tell the act() method to call the nibbleFlower() method, otherwise your Turtle will run around and eat nothing.

You won't see a scoreboard yet. If you cannot wait for proof that the score is being counted, put a single line, System.out.println (flowersNibbled); into the nibbleFlower() method. An annoying console window will appear with the value of flowersNibbled once you eat something. Remove this line of code once your curiosity is satisfied.

Quick Greenfoot Score Counter 5 of 12

4: ScoreboardTo do this we will need to:

• Give the Turtle a returnScore() method. This will make the Turtle report its score back to the Scoreboard once it is asked.

• Create the Scoreboard.

• Tell the Scoreboard to talk to the Turtle.

returnScore()Immediately after the nibbleFlower() method, and before the bracket that closes off the Turtle class, add a method called getScore(), as shown.

getScore() will not do anything yet, because nothing is asking it to do anything.

At this stage, it may be useful to explain what public int and public void, etc. mean.

• public means the method can be called by any other object.

◦ In the case of getScore(), we want the Scoreboard to be able to talk to this method, so that's fine.

◦ You may notice that nibbleFlower() is public too. This means other objects can call it, which might cause problems. Strictly speaking, this should have been private, but as long as we don't try to make the flower call nibbleFlower(), there won't be an issue.

◦ Knowing when to make something private/public doesn't become a serious issue till A-level.

• int means the method sends back an integer. So, any object that calls getScore() will need to expect an integer to be sent back to it.

• void means nothing is sent back. So, nibbleFlower() is called and just gets on with things.

Quick Greenfoot Score Counter 6 of 12

Create the ScoreboardOkay, we're not creating anything. We're doing some copying, pasting and editing. Like programmers the world over, we're going to re-use something that already exists.

Here's the code you are going to copy. I used it in another game, to do with Lobsters and Crabs. The code is shown in one typeface, with my commentary in another.

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;

import java.awt.Graphics;

As well as using the greenfoot code libraries, this imports graphics libraries that are provided with Java.

/**

* Counter that displays a text and number.

*

* @author CWR

* @version 1 - based on MIK's asteroid counter

*/

OK, I own up. It wasn't my counter to start with, it was taken from elsewhere...

public class TimeCounter extends Actor

The place it came from called it TimeCounter. We'll be renaming it to FlowerCounter eventually.

{

private static final Color TEXT_COLOR = new Color(200, 0, 0);

private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);

All this stuff sets the colour of the counter (transparent) and the text.

private Lobster lobster;

This tells the counter to talk to an instance of the Lobster class called 'lobster'. It's case sensitive.

public TimeCounter(Lobster lobster)

Defines TimeCounter as using a Lobster called 'lobster'.

{

this.lobster = lobster;

Tells time counter to refer to this very lobster we are dealing with as.... 'lobster'. This clunkiness is an annoyance in Java.

updateImage();

Calls a method to refresh the score counter at the start of execution.

}

Quick Greenfoot Score Counter 7 of 12

public void act() {

updateImage();

this keeps updating the score counter;

}

private void updateImage()

{

String text = "Time: " + lobster.getTime();

talks to lobster and asks it to send back the results of getTime().

GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR, TRANSPARENT_COLOR);

setImage(image);

builds a new image for the score counter

}

}

Assuming you are not confused yet, all you need to do is create a FlowerCounter object and change the plumbing in the above method to talk to a Turtle and not a lobster. The changes you will need to make are shown below, in blue. They are also in italics, in case you are working from a photocopy:

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;import java.awt.Graphics;

/** * Counter that displays a text and number. * * @author CWR * @version 1 - based on MIK's asteroid counter */public class FlowerCounter extends Actor{ private static final Color TEXT_COLOR = new Color(200, 0, 0); private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0); private Turtle turtle; public FlowerCounter(Turtle turtle) { this.turtle = turtle; updateImage(); } public void act() { updateImage(); }

private void updateImage() { String text = "Time: " + turtle.getScore();

Quick Greenfoot Score Counter 8 of 12

GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR, TRANSPARENT_COLOR); setImage(image); }}

Right, let's get on with it.In Greenfoot, on the right-hand side, right-click on Actor and choose new subclass.

NB: Make sure you choose Actor and not Animal!

Give it the name FlowerCounter and don't choose an image. Just press OK.

A new object has appeared in the list on the right, called FlowerCounter. Right-click on it and open the code editor. You will see a lot of skeleton text that ultimately tells it to do nothing.

Quick Greenfoot Score Counter 9 of 12

Now, delete all of this code. Every word of it. Leave nothing.

Once you have done that, paste in the contents of TimeCounter.java. This file should open up for you in your system's text editor (e.g. Notepad).

Now, press Compile and wait for the error messages. The Compiler will complain that it has no idea what a Lobster is. Make the changes that were highlighted in blue on the previous pages.

You might be wondering why I didn't give you a working score-counter to begin with. There are two reasons:

• I am evil and enjoy making people suffer.• It is in your own interests to learn how to take code from elsewhere and 'rewire' it to work in a

new setting.

Once you have made the changes and compiled it (to make sure you made the changes correctly), you need to put the FlowerCounter into the world. Look for the list of objects on the right-hand side and right-click on FlowerCounter. Now choose New FlowerCounter (Turtle turtle).

Your cursor changes to a little green foot with a no-entry sign over it. This just means 'you can't drop that here'. Move the cursor to the left, until it is over the background of TurtleWorld...

Quick Greenfoot Score Counter 10 of 12

...and you see a little green foot moving among the flowers.

Now click on the background of TurtleWorld and wait for the really unhelpful dialogue:

It's simply saying, “OK, so you want to create a new FlowerCounter, I understand that – but what Turtle is it supposed to talk to?” Don't waste your time clicking the drop-down list, there's nothing in it.

To those who just clicked the drop-down list: read the previous sentence.

So, anyway, what Turtle will it talk to. Well, do you remember a Lobster called lobster. Now we have a Turtle called.... turtle. Type in turtle and press OK.

Press run, and watch the count increase.

Quick Greenfoot Score Counter 11 of 12

“But it says 'Time' – what's going on?”Simple, you reused a counter that used to ask a Lobster how long was left. You should edit the text of the FlowerCounter code to say “Flowers Nibbled”.

Extension work:• Create a separate timer. For this, you will need an int called time that starts off at 1000 and

that is reduced by a method called setTime, using a command: time--◦ If you are really clever, you will make the game end when the time reaches zero. Otherwise,

it will keep counting backwards.• Create a score counter for a game you already have. Or build an entirely new scenario (say,

cats and dogs), with dogs counting cats attacked.

Quick Greenfoot Score Counter 12 of 12