Greenfoot

From CompSciWiki
Revision as of 14:09, 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

Once you have Greenfoot open, the first thing you have to do is open a new scenario. To do this, click on the "Scenario" menu tab and click "New...". You will be prompted for a project name and the location where your project will be stored. Choose Breakout for the project name and save it either in your own folder or on the desktop so it is easy to find. Click the "Create" button and now you are ready to start making your game.

Step 1: Making a New World

The next step of this lab involves creating a new world for your game to exist in. To do this, right click on the "World" tab on the right hand side and select the "New subclass..." option. Name this class MyWorld and select an image from the background catagory, then click "Ok". You will notice right away that nothing changes. You must click the "Compile" button anytime you make changes to your porject. So compile your project to make the background you chose visible. Now you are ready to add objects to your game.

Step 2: Adding a Paddle

Setting Up the Paddle Class

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()".

Getting Your Paddle Onto the Game Screen

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);
    }
} 

The code in block:

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

Is code that is used to build the game screen. Currently, it just has one line of code that creates a 600x400 pixel world. Now we want to add your paddle object to the world.

Under the super(...) line, type in:

 Paddle myPaddle = new Paddle();
addObject(myPaddle, 300, 380); 

This will add a paddle to the world, 300 pixels from the left side and 380 pixels from the top.

Because all this code is within

 public class MyWorld extends World
{
   ... all your code is in here!
} 

When you say addObject(...) in your code, it is performing this action on MyWorld using myPaddle, the number 300, and the number 380.

These actions are called methods or functions. The values that are being used -- myPaddle, 300, 380 -- are called parameters, which are values sent to a method.

In Java, methods are always called on an value called an object. In the above example, addObject(...) is performed on the MyWorld object.

Do not worry if you do not understand what this means right now; it will come with more practice.

If you press Compile on the game screen window, you will now see the paddle on the screen and it will stay there!

One problem with our code is that, if the size of the world ever changes, the paddle might not show up properly. Let's fix this. Change the earlier addObject code to:

 Paddle myPaddle = new Paddle();
addObject(myPaddle, getWidth() / 2, getHeight() - 20); 

Now, your paddle will always be in the middle of the screen, 20 pixels from the bottom, even if you change the values in the super(...) call and Compile.

Moving the Paddle

Right now, if you click "Act" or "Run" on the game screen window and press the left and right arrow keys, nothing happens. This isn't very game-like. We want our paddle to move around!

To do this, we need to add code to the Paddle class so it can control its own actions.

Right-click the "Paddle" box in the Actor classes box of the main window and select "Open editor", just like you did to change the code in MyWorld.

This opens up an editor with the following:

 ...

public class Paddle extends Actor
{
    /**
    * Act - do whatever the Paddle wants to do. This method is called whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
    public void act()
    {
        // Add your action code here
    }
} 

The comments in this code tell it all -- if you want you paddle to do anything, you have to add your code within the act() method. Change it so it looks like:

 public void act()
{
    if (Greenfoot.isKeyDown("left"))
    {
        setLocation(getX() - 3, getY());
    }
    else if (Greenfoot.isKeyDown("right"))
    {
        setLocation(getX() + 3, getY());
    }
} 

Let's break this code down:

 if (Greenfoot.isKeyDown("left"))
{
    ...
} 

This says that, when the user is pressing the "left" key, which is the left arrow on your keyboard, then do the code that is in it's block. The block:

 else if (Greenfoot.isKeyDown("right"))
{
    ...
} 

Does the exact same thing, except when the "right" key is pressed.

These two blocks contain:

 setLocation(getX() - 3, getY());
...
setLocation(getX() + 3, getY()); 

These both change the location of the Paddle object, except one has "getX() - 3" while the other has "getX() + 3". This means that one moves the Paddle to the left of it's current X position by 3 and the other to the right by 3. Try changing these values and even flipping them around to see how it changes your Paddle!

To test the changes, compile the code, press "Run" on the main window, and press your right and left keys. The paddle now moves!

Step 3: Creating a Bouncing Ball

Step 4: Breakout Needs Bricks

To add bricks to the game we first need to create a brick actor. To do this, create a new subclass of Actor. Name this class Brick and select the image "brick.png" from the "objects catagory. You will not have to add any code to this class because they do not move.

Now open the source code file for "MyWorld" by double clicking on the tab. Add the following code to the "public MyWorld()" method:

 int x,y;
x = 20;
y = 20;
        
//Create the bricks
for(int i = 0; i < 195; i++)
{
     Bricks temp = new Bricks();
     addObject(temp,x,y);
     x+=40;
     if(x >= 600)
     {
          x=20;
          y+=20;
     }
} 

The above code is creating 195 bricks and adding them to the world.

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