Monday, December 17, 2012


import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}

Monday, December 10, 2012

Begin Greenfoot Game

Before we begin, check to see if Greenfoot is still installed on your station.  If not, follow the previous instruction and install it.  Then we will resume our basic game creation.

We will again be accessing the Joy of Code video tutorials for Greenfoot: http://blogs.kent.ac.uk/mik/category/joy-of-code/page/2/

Last week we used tutorial #4 to begin "Trick the Turtle".  Basically we were able to add "instances" of the turtles to our "world" and have them behave as we programmed them to.

This week we will be continuing with the Trick the Turtle project using tutorial #5 (in Clearlake) and tutorial #6 (in both Clearlake and Lakeport).  These two tutorials will add "If Statements" and "Random Behavior" to our game.

Time permitting we will also work through tutorial #7 to give the turtles some food, and then on to tutorial #8 to do some basic housekeeping and clean up our code.