Greenfoot Practicals
Greenfoot Practicals
Aim
To change the behavior of an object in green foot.
Algorithm
1. Open Greenfoot and create a new scenario.
3. Inside the `Character` class, locate the `act` method. This method is called each
frame and is where you'll define the behavior.
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.*;
checkKeys();
applyGravity();
if (Greenfoot.isKeyDown("left")) {
move(-speed);
if (Greenfoot.isKeyDown("right")) {
2
move(speed);
if (Greenfoot.isKeyDown("space")) {
jump();
if (onGround()) {
jumpSpeed = -jumpHeight;
if (!onGround()) {
jumpSpeed++;
}
3
Output
Result
Aim
Algorithm
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
Aim:
To write a program to make our own scenarios in green foot.
Algorithm
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
Aim
To write a greenfoot program to create a new world subclass and compile the
scenario.
Procedure
Output
Result
Aim
To create an actor subclass and add an instance of it to the Greenfoot scenario.
Algorithm
Program
java
import greenfoot.*;
java
import greenfoot.*;
Output
Result
An Actor subclass instance is added to the scenario.
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);
}
FallingObject Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class FallingObject here.
*
* @author (your name)
13
Output
14
Result
Aim
To create a green foot scenario with keyboard interaction
Algorithm
Program
/**
* Write a description of class camel here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class camel extends Actor
{
}
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.
Aim
To write a program in greenfoot to make use of playsound() method.
Procedure
Program
/**
* 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.
Aim
20
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
Aim
22
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
/**
* 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
/**
* 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.
Aim
To write a greenfoot program to view the images stored in the scenario.
Procedure
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
/**
* 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
Aim
To set an image using image filename in green foot world
Procedure
Program
/**
* 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
Output
Result
The program has been executed successfully.