I am trying to create a game where an object falls but you have to save it from falling by clicking it. (Kind of like "Keep it up.")
Here is my code.
{World}
{Ball}
{Intro}
{Ground}
{Text}
import greenfoot.*;
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
private int timer = 1800;
Text timerText = new Text();
/**
* 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);
addObject(timerText, 100, 15);
timerText.setText("Time Left" + (timer/60));
prepare();
}
public void act()
{
if (timer>0)
{
timer--;
if (timer%60==0) timerText.setText("Time Left" + (timer/60));
if(timer == 0) Greenfoot.stop();
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Ball ball = new Ball();
addObject(ball, 301, 217);
Ground ground = new Ground();
addObject(ground, 10, 398);
Ground ground2 = new Ground();
addObject(ground2, 50, 395);
Ground ground3 = new Ground();
addObject(ground3, 76, 396);
Ground ground4 = new Ground();
addObject(ground4, 104, 399);
Ground ground5 = new Ground();
addObject(ground5, 141, 397);
Ground ground6 = new Ground();
addObject(ground6, 128, 399);
Ground ground7 = new Ground();
addObject(ground7, 178, 399);
Ground ground8 = new Ground();
Ground ground9 = new Ground();
Ground ground10 = new Ground();
addObject(ground10, 207, 398);
Ground ground11 = new Ground();
addObject(ground11, 178, 397);
Ground ground12 = new Ground();
addObject(ground12, 238, 398);
Ground ground13 = new Ground();
addObject(ground13, 263, 398);
Ground ground14 = new Ground();
addObject(ground14, 283, 398);
Ground ground15 = new Ground();
addObject(ground15, 310, 399);
Ground ground16 = new Ground();
addObject(ground16, 333, 398);
Ground ground17 = new Ground();
addObject(ground17, 363, 398);
Ground ground18 = new Ground();
addObject(ground18, 392, 395);
Ground ground19 = new Ground();
addObject(ground19, 421, 397);
Ground ground20 = new Ground();
addObject(ground20, 435, 399);
Ground ground21 = new Ground();
addObject(ground21, 462, 392);
Ground ground22 = new Ground();
addObject(ground22, 478, 399);
Ground ground23 = new Ground();
addObject(ground23, 503, 398);
removeObject(ground22);
removeObject(ground21);
Ground ground24 = new Ground();
addObject(ground24, 457, 399);
Ground ground25 = new Ground();
addObject(ground25, 485, 399);
Ground ground26 = new Ground();
addObject(ground26, 522, 399);
Ground ground27 = new Ground();
Ground ground28 = new Ground();
addObject(ground28, 552, 396);
Ground ground29 = new Ground();
addObject(ground29, 580, 398);
Ground ground30 = new Ground();
addObject(ground30, 594, 398);
ball.setLocation(287, 18);
Intro intro = new Intro();
addObject(intro, 298, 181);
intro.setLocation(288, 190);
}
}
import greenfoot.*;
/**
* Write a description of class Ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends Actor
{
private int score = 0;
private final int GRAV = 1;
private int gravity = 0;
private int velocity;
private double speed = -9.0;
public Ball(){
velocity = 0;
getImage().scale(50,50);
}
/**
* Act - do whatever the Ball 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.
//moveAround();
fall();
if (Greenfoot.mouseClicked(this))
{
score++;
//int amo = -30 + Greenfoot.getRandomNumber(70);
//turn(amo);
//jump();
setLocation (getX(), getY()-120);
}
setLocation(getX()+5, getY());
int x = getX();
int y = getY();
int ww = getWorld().getWidth();
int wh = getWorld().getHeight();
if (x == 0) setLocation(ww-2, y);
if (x == ww-1) setLocation(1, y);
if (y == 0) setLocation(x, wh-2);
if (y == wh-1) setLocation(x, 1);
}
public void moveAround()
{
move(6);
if (Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(90) - 45);
}
if (getX() <= 5 | getX() >= getWorld().getWidth() - 5)
{
turn(180);
}
if (getY() <= 5 | getY() >= getWorld().getHeight() - 5)
{
turn(180);
}
}
public void tryToCatch()
{
if (Greenfoot.mouseClicked(this))
{
((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
//getWorld().removeObject(actor);
//Greenfoot.stop();
}
}
public void jump(){
gravity -= 20;
}
public void fall(){
setLocation(getX(), getY() + velocity);
if (getY() > getWorld().getHeight() - getImage().getHeight()/2){
velocity = 0;
} else {
velocity += GRAV;
}
}
}
import greenfoot.*;
/**
* Write a description of class Intro here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Intro extends Actor
{
public Intro()
{
getImage().scale(650,450);
}
/**
* Act - do whatever the Intro wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
removeOnClick();
}
public void removeOnClick()
{
if (Greenfoot.isKeyDown("enter")) {
World world;
world = getWorld();
world.removeObject(this);
Greenfoot.stop();
}
}
}
import greenfoot.*;
/**
* Write a description of class Ground here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ground extends Actor
{
public Ground()
{
getImage().scale(30,30);
}
/**
* Act - do whatever the Ground 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.
if(isTouching(Ball.class))
{
removeTouching(Ball.class);
}
}
}
import greenfoot.*;
import java.awt.Color;
public class Text extends Actor
{
public Text()
{
this("");
}
public Text(String text)
{
setText(text);
}
public void setText(String text)
{
setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));
}
}

