0% found this document useful (0 votes)
14 views28 pages

Greenfoot Practicals

The document provides instructions for changing the behavior of objects in Greenfoot by modifying the act method. It includes steps to create a character class, define movement behavior in the act and other methods, add the character to the scenario, and test the modified behavior. It also includes an example character class code that defines movement and jumping behavior using keyboard keys.

Uploaded by

Safna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

Greenfoot Practicals

The document provides instructions for changing the behavior of objects in Greenfoot by modifying the act method. It includes steps to create a character class, define movement behavior in the act and other methods, add the character to the scenario, and test the modified behavior. It also includes an example character class code that defines movement and jumping behavior using keyboard keys.

Uploaded by

Safna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1

1. To change the behavior of an object

Aim
To change the behavior of an object in green foot.
Algorithm
1. Open Greenfoot and create a new scenario.

2. Create an object class for the character. Let's call it `Character`.

3. Inside the `Character` class, locate the `act` method. This method is called each
frame and is where you'll define the behavior.

4. Modify the `act` method to include the new behavior.


5. Add the `Character` object to your scenario.

6. Run the scenario and test the modified behavior of the character object. It should
move left and right using arrow keys and jump using the "Space" key.

Program

import greenfoot.*;

public class Character extends Actor {

private int speed = 3;

private int jumpHeight = 10;

private int jumpSpeed = 0;

public void act() {

checkKeys();

applyGravity();

private void checkKeys() {

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

move(-speed);

if (Greenfoot.isKeyDown("right")) {
2

move(speed);

if (Greenfoot.isKeyDown("space")) {

jump();

private void jump() {

if (onGround()) {

jumpSpeed = -jumpHeight;

setLocation(getX(), getY() + jumpSpeed);

private void applyGravity() {

if (!onGround()) {

jumpSpeed++;

setLocation(getX(), getY() + jumpSpeed);

private boolean onGround() {

int groundHeight = getImage().getHeight() / 2;

return getY() >= getWorld().getHeight() - groundHeight;

}
3

Output

Result

The behavior of an object is changed in green foot world.


4

2. For changing images

Aim

To change an image in green foot world.

Algorithm

1. Open Greenfoot and create a new scenario.


2. Create an object class for the character. Let's call it `mouse`.
3. Modify the `act` method to include the new behavior.
4. Add the `mouse` object to your scenario.
5. Paste an image named ‘butterfly.png’ in the image folder of the scenario
6. Run the scenario

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

/**
* Write a description of class mouse here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class mouse extends Actor
{private int Walk=(50);
/**
* Act - do whatever the mouse wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(2);
Walk--;
if(Walk==0)
{
setImage("butterfly.png");

}
}
}
5

Output

Result
The image in green foot world is changed.
6

3. To make your own scenarios

Aim:
To write a program to make our own scenarios in green foot.

Algorithm

Step 1: Open Greenfoot and click on new scenario


Step 2: Give a name for the scenario and choose a location to save it
Step 3: Create a new class for the World class and choose an image
Step 4: Create a new class for the Actor class and choose an image
Step 4: Right click the subclass and open the text editor
Step 5: Write the code for the actor subclass
Step 6: Add instances of actors to the world
Step 7: Compile and run the scenario

Program

import greenfoot.*;
public class balloon extends Actor
{
public void act()
{
move(2);
if (isAtEdge())
{
turn(120);
}
}
}
7

Output

Result
The program has been executed successfully.
8

4. To create a new world subclass and compile the scenario

Aim
To write a greenfoot program to create a new world subclass and compile the
scenario.

Procedure

Step 1: Open Greenfoot


Step 2: From the project menu select Scenario option and Choose New Java Scenario.
Step 3: Select the location and press ok.
Step 4: Right click on the My World->new subclass->Right click and select an image.
Step 5: Save the background and compile the scenario

Output

Result

The program has been executed successfully.


9

5. Create an actor subclass and add an instance of it to the Greenfoot scenario

Aim
To create an actor subclass and add an instance of it to the Greenfoot scenario.

Algorithm

1. Open Greenfoot and create a new scenario:


- File > New Scenario

2. Create an actor subclass:


- Right-click on the "classes" folder in the scenario and select "New actor subclass..."
- Give your subclass a name, such as "MyActor"
- Click "OK"

3. Edit your actor subclass:


- Open the newly created actor subclass (e.g., MyActor.java) by double-clicking it in the
"classes" folder
- Customize the subclass by adding any behaviors or methods
4. Add an instance of the actor subclass to the world:
- Open your world subclass (e.g., MyWorld.java) and go to its constructor
- Add an instance of your actor subclass
5. Compile and run the scenario

Program

Code in the actor subclass:

java
import greenfoot.*;

public class MyActor extends Actor {


public MyActor() {
GreenfootImage image = new GreenfootImage(50, 50);
image.setColor(Color.BLUE);
image.fill();
setImage(image);
}

public void act() {


// Add behavior for the actor
}
}
10

Code on MyWorld subclass:

java
import greenfoot.*;

public class MyWorld extends World {


public MyWorld() {
super(800, 600, 1);
addObject(new MyActor(), getWidth() / 2, getHeight() / 2); // Add an instance of MyActor
}
}

Output

Result
An Actor subclass instance is added to the scenario.

6.To set up the scenario for gameplay


11

Aim
Greenfoot program to set up the scenario for gameplay
Procedure
1. Open a new java scenario in greenfoot
2. Create two subclasses as Catcher class and FallingObject for actor class
3. Set images for the two subclasses
4. Set MyWorld as the starting class
5. Write code for all the class
6. Run the program
Program
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
{
private int score = 0;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(400, 600, 1); // Create a world with dimensions (width, height, cell size)
addObject(new Catcher(), getWidth() / 2, getHeight() - 20); // Add the player character
}
public void act() {
// Add falling objects at random positions and intervals
if (Greenfoot.getRandomNumber(100) < 3) {
addObject(new FallingObject(), Greenfoot.getRandomNumber(getWidth()), 0);
}
// Update and display the score
showText("Score: " + score, 60, 20);
}
}
12

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

/**
* Write a description of class Catcher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Catcher extends Actor
{
/**
* Act - do whatever the Catcher wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Move the catcher left or right based on arrow key input
if (Greenfoot.isKeyDown("left")) {
move(-5);
}
if (Greenfoot.isKeyDown("right")) {
move(5);
}

// Check for collisions with falling objects


if (isTouching(FallingObject.class)) {
removeTouching(FallingObject.class);
// Increase score or implement game logic here
}
}
}

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

/**
* Write a description of class FallingObject here.
*
* @author (your name)
13

* @version (a version number or a date)


*/
public class FallingObject extends Actor
{
/**
* Act - do whatever the FallingObject wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Make the object fall downward
setLocation(getX(), getY() + 3);

// Remove the object if it reaches the bottom of the world


if (getY() >= getWorld().getHeight() - 1) {
getWorld().removeObject(this);
}
}
}

Output
14

Result

The greenfoot scenario for gameplay is executed successfully.

7. Program keyboard interaction


15

Aim
To create a green foot scenario with keyboard interaction

Algorithm

Step 1: Start the green foot from the desktop


Step 2: Click on scenario- new java scenario
Step 3: Give the file name
Step 4: Right click on the actor class and create a new sub class
Step 5: Type a name for the sub class and choose an image
Step 6: right click the actor and choose open editor
Step 7: Key in the code for the actor subclass
Step 8: Compile and run the code

Program

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

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

// get a random number between 0 and 3...

// ...an turn left that many times.

public void act()


{
move();

}
public void move()
{
int y=getY();
16

int x=getX();
if(Greenfoot.isKeyDown("up"))
{
setLocation(x,y-3);
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(x,y+3);
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(x-3,y);
}
if(Greenfoot.isKeyDown("right"))
{
setLocation(x+3,y);
}
}
}

Output
17

Result
The green foot program is created with keyboard interaction.

8. Using the playsound() method


18

Aim
To write a program in greenfoot to make use of playsound() method.

Procedure

Step 1: Start the green foot from the desktop


Step 2: Click on scenario- new java scenario and give the file name
Step 4: Right click on the actor class and create a new sub class
Step 5: Type a name for the sub class and choose an image
Step 6: Right click the actor and choose open editor
Step 7: Key in the code for the actor subclass to play the sound
Step 8: Compile and run the code

Program

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

/**
* Write a description of class car here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class car extends Actor
{
/**
* Act - do whatever the car wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();

}
public void move()
{
int y=getY();
int x=getX();
if(Greenfoot.isKeyDown("up"))
{

setLocation(x,y-3);
Greenfoot.playSound("car.wav");

}
if(Greenfoot.isKeyDown("down"))
19

{
setLocation(x,y+3);
Greenfoot.playSound("carhorn.mp3");
}
}

Output

Result
The program has been executed successfully.

9. To record sounds in greenfoot

Aim
20

To write a green foot program to record sound.

Procedure
Step 1: Start the green foot from the desktop
Step 2: Click on scenario- new java scenario and give the file name
Step 4: Right click on the actor class and create a new sub class
Step 5: Type a name for the sub class and choose an image
Step 6: Right click the actor and choose open editor
Step 7: Text the code for the actor
Step 8: Choose Tools – show sound recorder option from the scenario
Step 9: Press record button to record a sound and then save it with a file name.
Step 10: Run the scenario

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

/**
* Write a description of class telephone here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class telephone extends Actor
{
private int shiverStrength = 5; // Adjust the shivering strength
/**
* Act - do whatever the telephone wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
shiver();
if (Greenfoot.mouseClicked(this)) {
// Change the image to image2 when the actor is clicked
Greenfoot.playSound("telephone.wav");
}
}
private void shiver() {
int xOffset = Greenfoot.getRandomNumber(shiverStrength * 2) - shiverStrength;
int yOffset = Greenfoot.getRandomNumber(shiverStrength * 2) - shiverStrength;
setLocation(getX() + xOffset, getY() + yOffset);
}
}

Output
21

Result

The green foot program was created to record a sound.

10.To change the image of an instance summarized

Aim
22

To write a greenfoot program to change the image of an instance.

Procedure
Step 1: Start the green foot from the desktop
Step 2: Click on scenario- new java scenario and give the file name
Step 4: Right click on the actor class and create a new sub classes
MyActor,MyActor1
Step 5: Write the code for both the classes.
Step 6: Drag the instance on to the scenario
Step 7: Run the scenario.

Program

MyActor Class

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

/**
* Write a description of class MyActor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyActor extends Actor
{private GreenfootImage image1; // Load image1
private GreenfootImage image2; // Load image2
private GreenfootImage image3;
public MyActor() {
image1 = new GreenfootImage("tiger.jpg"); // Load image1.png
image2 = new GreenfootImage("wombat.png"); // Load image2.png
image2 = new GreenfootImage("burger.jpg");
setImage(image1); // Set the initial image to image1
}
/**
* Act - do whatever the MyActor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this)) {
// Change the image to image2 when the actor is clicked
setImage(image2);
}
if (Greenfoot.mouseClicked(this)) {
// Change the image to image2 when the actor is clicked
setImage(image3);
23

}
}
}

MyActor1 Class

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

/**
* Write a description of class MyActor1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyActor1 extends Actor
{private int Walk=(50);
private GreenfootImage image1; // Load image1
private GreenfootImage image2; // Load image2
public MyActor1() {
image1 = new GreenfootImage("tomato.jfif"); // Load image1.png
image2 = new GreenfootImage("tomatonew.png"); // Load image2.png
setImage(image1); // Set the initial image to image1
}
/**
* Act - do whatever the MyActor1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this)) {
// Change the image to image2 when the actor is clicked
setImage(image2);
}
move(2);
Walk--;
if(Walk==0)
{
setImage("seal.png");

}
}
}

Output
24

Result
The greenfoot program to change the image of an instance is executed
successfully.

11.To view the images stored in the scenario.


25

Aim
To write a greenfoot program to view the images stored in the scenario.

Procedure

Step 1: Create a new Greenfoot scenario.


Step 2: Import or add the images you want to use into the scenario. These images should
be placed in the scenario's image folder.
Step 3: Create a subclass of the `Actor` class for the object that will display the images as
`ImageChanger`.
Step 4: In the code for the ImageChanger class, initialize the `ImageChanger` with the
first image in the array.
- In the `act` method, we check for mouse clicks on the `ImageChanger` object using
`Greenfoot.mouseClicked(this)`. When clicked, we increment the `currentIndex` to cycle through
the images and set the new image using `setImage`.

Step 5: Add instances of the `ImageChanger` class to the scenario. You can do this by
right-clicking in the scenario and selecting "New ImageChanger."

Program

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

/**
* Write a description of class ImageChanger here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ImageChanger extends Actor
{
private String[] imageFiles = {"boy1.png", "seal.png",
"house.png","tiger.jpg","wombat.png","burger.jpg"};
private int currentIndex = 0;

public ImageChanger()
{
// Set the initial image
setImage(imageFiles[currentIndex]);
/**
* Act - do whatever the ImageChanger wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
}
public void act()
{
26

if (Greenfoot.mouseClicked(this))
{
// Change the image on mouse click
currentIndex = (currentIndex + 1) % imageFiles.length;
setImage(imageFiles[currentIndex]);
}
}
}

Output

Result

The green foot program has been executed to view all images stored in the
scenario.
27

12. Set an image using image filename

Aim
To set an image using image filename in green foot world

Procedure

Step 1: Start the green foot from the desktop


Step 2: Click on scenario- new java scenario and give the file name
Step 4: Right click on the actor class and create a new sub class
Step 5: Type a name for the sub class without choosing an image
Step 6: Paste an image into the image folder of the scenario
Step 7: Right click the actor and choose open editor
Step 8: Key in the code, compile
Step 9: Right click on actor class and drag a new instance of image into the scenario

Program

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

/**
* Write a description of class MyActor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyActor extends Actor
{
/**
* Act - do whatever the MyActor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public MyActor() {
setImage("tomatonew.png");

}
public void act()
{
setLocation(getX(), getY() + 2); // Adjust the speed as needed

// Remove the object when it goes out of the world


if (getY() >= getWorld().getHeight() - 1) {
getWorld().removeObject(this);
}
}
}
28

Output

Result
The program has been executed successfully.

You might also like