Greenfoot

From CompSciWiki
Revision as of 13:01, 3 April 2012 by AndrewS (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Back to Extra Labs

Introduction

This lab is an introduction to Greenfoot. Greenfoot is an interactive tool for building Java programs using a mixture of programming and interactivity. With Greenfoot, you are able to get a 2D graphic program up and running much faster than you would using regular Java.

To familiarize you with Greenfoot, we will show you how to build a simple Breakout game.

Step 0: Starting a New Scenario

Step 1: Making a New World

Step 2: Adding a Paddle

First, right-click the following image and select Save As... and save it in the folder where you created your Greenfoot program.

Paddle.gif

On the right-hand side of your Greenfoot window, there are two boxes: One under "World classes" called "World" and another under "Actor classes" called "Actor".

Right-Click "Actor" and select "New subclass..."

Name your subclass Paddle by entering it into the New class name textbox. Now, for the image, click the "Import from file..." box in the bottom-left of this window. Find and select the Paddle.gif you just saved.

Press OK on that window to close it and create the new subclass.

In your main window, you want to add this newly created Paddle to your world. Press the compile button again to allow the Paddle you created to be usable. Then, right-click the Paddle box that now shows underneath the Actor box and select "new Paddle()".

There are two ways to add the Paddle object to the game screen:

The first way is to select the "Paddle" box, which is now located under the "Actor" box on the right-hand side, and select "new Paddle()". When you do this, you'll now see the image of the paddle following your mouse cursor. You can now place the paddle in the game screen by clicking anywhere within it.

There is a problem with this way, however. If you click the "Compile" button, your paddle disappears! This is because it recompiled all the code within the world and classes to rebuild your game screen! To make your paddle stay in the world, you have to add it directly to the code. This is the second way of adding the paddle to the game screen.

To add your paddle to the code of the MyWorld class you made earlier, right-click the MyWorld box and select "Open editor".

You are now in the thick of actual Java programming!

In the editor, you will see the following code:

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

/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{

    /**
    * Constructor for objects of class MyWorld
    * 
    */
    public MyWorld()
    {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
    }
} 

There's a couple important things you should know about this code in order to understand it.

 // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

...


/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/

...

/**
* Constructor for objects of class MyWorld
* 
*/

...

// Create a new world with 600x400 cells with a cell size of 1x1 pixels. 

These are all called "comments". They are used to explain your code so you or others can use it later. the /* */ symbols are used for multi-line comments, where any text in between the /* and the */ are commented out and the // is for one-line comments. Any text after the // are commented out until the next line.

As a rule of thumb, you should always comment code if you feel like you should explain what it is doing and why you chose to do it that way.

To start yourself off on good commenting habits, change the (your name) and (a version number or a date) in the top multi-line comments to your name and the current date.

Step 3: Creating a Bouncing Ball

Step 4: Breakout Needs Bricks

 System.out.println("Hello Greenfoot");