i wants to make an image which counts the seconds when the execution is start and remain until the world execution is stop..
import greenfoot.*;
public class Background extends World implements Runnable
{
boolean isDown = false;
int key = 0;
MouseInfo mouse;
Ball ball=new Ball();
Thread t=new Thread(this);
Message messagebox = new Message("");
static int k=0;// add enemy
static int p=1;//move ++
static int q=0;//improving speed
int i=0,n=0;//for endless loop
static int sec=0;
public Background()
{
super(1160, 600, 1);
prepare();
System.gc();
}
private void prepare()
{
addObject(ball, 604, 279);
}
public void act()
{
mouse = Greenfoot.getMouseInfo();
if(Greenfoot.mousePressed(null)){
isDown = true;
key = mouse.getButton();
t.start();
}
}
public void run()
{
String text = "Time:"; // or not
Message msgbox = new Message(); // getting the messagebox object
addObject(msgbox, getWidth()-1120,getHeight()-20);
try
{
for(i=0;i<=n;i++)
{
Thread.sleep(1500);
for(int f=0;f<=i;f++){
msgbox.setText(text+i+"s");
}
for(int l=0;l<=i;l++){
if(k==i && k==l)
{
addObject(new Enemy(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));//after 7 seconds new actor will be added
k=k+7;
}
for(int a=0;a<=l;a++)
if(q==i && q==a && q==l)
{
p++;//after 20 seconds enemy moving speed will increse
q=q+20;
}
}
n++;
}
}
catch(Exception e)
{
//System.out.println("Exception"+e);
}
}
}
import greenfoot.*;
public class Background extends World
{
public Background()
{
super(1160, 600, 1);
addObject(new Ball(), 604, 279);
}
public void act()
{
}
}// insert at line 5 private int actCounter; // add at line 14 actCounter++;
if (actCounter%55 == 0) updateTimeDisplay();
private void updateTimeDisplay()
{
}public class SimpleActor extends greenfoot.Actor {}private Actor saTime = new SimpleActor();
updateTimeDisplay(); addObject(saTime, 40, 580);
int time = actCounter/55;
GreenfootImage image = new GreenfootImage("Time: "+time, 24, null, null);
saTime.setImage(image);// replace this line in the act method
if (actCounter%55 == 0) updateTimeDisplay();
// with these
if (actCounter%55 == 0)
{
updateTimeDisplay();
}if (actCounter/55 == 7) addObject(new Enemy(), <x>, <y>); if (actCounter/55 == 20) increaseEnemySpeed();
import greenfoot.*;
public class Background extends World
{
boolean isDown = false;
int key = 0;
MouseInfo mouse;
Ball ball=new Ball();
Message messagebox = new Message("");
Message msgbox = new Message();
private int actCounter;
static int k=1;// add enemy
static int p=1;//move ++
static int q=1;//improving speed
private Actor m=new Message();
int time;
public Background()
{
super(1160, 600, 1);
addObject(m,40,580);
prepare();
System.gc();
}
private void prepare()
{
addObject(ball, 604, 279);
addObject(msgbox, getWidth()-1120,getHeight()-20);
}
public void act()
{
actCounter++;
String text="Time :"+time;
if(actCounter%55==0)
{
time=actCounter/55;
msgbox.setText(text);
addEnemy();
improvingSpeed();
}
}
public void addEnemy()
{
if(time==k)
{
addObject(new Enemy(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));//after 7 seconds new actor will be added
k=k+7;
}
}
public void improvingSpeed()
{
if(time==q)
{
p++;//after 20 seconds enemy moving speed will increse
q=q+20;
}
}
}
import greenfoot.*;
import java.awt.Color; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Message here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Message extends Actor
{
public Message()
{
updateImage("");
}
public Message(String text)
{
updateImage(text);
}
public void setText(String text)
{
updateImage(text);
}
private void updateImage(String text)
{
GreenfootImage textImage = new GreenfootImage(text, 20, Color.black, new Color(0, 0,0, 0));
GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
setImage(image);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Enemy extends Actor
{
/**
* Act - do whatever the Enemy wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Background b=new Background();
int s=b.p;
turn();
moveto(s);
turn(360);
}
public void turn()
{
if(isTouching(null)==true)
{
turn(Greenfoot.getRandomNumber(70)-50);
}
if(getX()<=10 ||getX()>=getWorld().getWidth()-10)
{
turn(Greenfoot.getRandomNumber(360)-90);
}
if(getY()<=10||getY()>=getWorld().getHeight()-10)
{
turn(Greenfoot.getRandomNumber(360)-90);
}
}
public void moveto(int s)
{
move(s);
}
}
Background b = (Background) getWorld();
if(isTouching(Enemy.class))
{
GreenfootImage image = new GreenfootImage("images/blast.png");
world=getWorld();
world.removeObject(this);
world.getBackground().drawImage(image,mouse.getX(),mouse.getY());
Greenfoot.stop();
}