import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MyWorld extends World{
public MyWorld(){
super(1200, 800, 1);
Player player = new Player();
Friend friend = new Friend();
this.addObject(player, 1050, 225);
this.addObject(friend, 900, 600);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
public class Balloon extends Actor{
boolean enteredValues = false;
public void act(){
if(!enteredValues){
String str1 = JOptionPane.showInputDialog("Enter Balloon's vertical speed (m/s)");
horizontalSpeed = Integer.parseInt(str1);
String str2 = JOptionPane.showInputDialog("Enter acceleration (m/s^2)");
acceleration = Integer.parseInt(str2);
enteredValues = true;
}
fall();
if(getOneIntersectingObject(Friend.class) != null)
this.getWorld().removeObject(this);
else if(this.getY()+1 == getWorld().getHeight())
this.getWorld().removeObject(this);
}
int horizontalSpeed;
int verticalSpeed;
int acceleration;
boolean timeTaken = false;
long time1;
int initialX;
int initialY;
public void fall(){
if(!timeTaken){
time1 = System.currentTimeMillis();
timeTaken = true;
initialX = getX();
initialY = getY();
}
int newX = (int)(horizontalSpeed*(((float)(System.currentTimeMillis()-time1))/1000));
int newY = (int)(acceleration/2*Math.pow((((float)(System.currentTimeMillis()-time1))/1000), 2));
setLocation(initialX - newX, initialY + newY);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
public class Friend extends Actor{
boolean isRunning = false;
int runningSpeed;
public void act() {
if(!isRunning){
isRunning = true;
String str = JOptionPane.showInputDialog("Enter UFO's speed (m/s)");
runningSpeed = Integer.parseInt(str);
}
run();
}
boolean touchedBoarder = false;
boolean timeTaken = false;
long time1;
public void run(){
if(getX()>895)
touchedBoarder = false;
if(getX()<5)
touchedBoarder = true;
if(!timeTaken){
time1 = System.currentTimeMillis();
timeTaken = true;
}
if(touchedBoarder){
if(System.currentTimeMillis() - time1>40){
setLocation(getX()+runningSpeed/20, getY());
timeTaken = false;
}
}
else if(!touchedBoarder){
if(System.currentTimeMillis() - time1>40){
setLocation(getX()-runningSpeed/20, getY());
timeTaken = false;
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor{
public void act(){
throwBalloon();
}
boolean isThrowing = false;
boolean finalThrow = true;
public void throwBalloon(){
if(Greenfoot.isKeyDown("space")){
this.setImage("alien1.png");
Greenfoot.delay(10);
this.setImage("alien2.png");
isThrowing = true;
}
if(isThrowing && finalThrow){
isThrowing = false;
finalThrow = false;
Balloon balloon = new Balloon();
World world = getWorld();
world.addObject(balloon, this.getX(), this.getY());
Greenfoot.delay(5);
}
}
}