
TimeBalls
By: a guest on
May 8th, 2012 | syntax:
Java | size: 2.14 KB | hits: 18 | expires: Never
This is the code i have now for the balls. I use secs.format(s.getElapsedTime()/1000.000 to count the elapsed time while game ran. How to make it that enemy 2 will start after elapsed time 4s?
Enemy1 enemy1 = new Enemy1();
e1 = new Thread(enemy1);
e1.start();
Enemy2 enemy2 = new Enemy2();
e2 = new Thread(enemy2);
e2.start();
}
class Enemy1 implements Runnable{
//each enemey runs in its own thread
public void run(){
while(true){
if(avoid.getRun()){
//move object randomly
x2 = x2+generator.nextInt(7);
y2 = y2+generator.nextInt(7);
if(x2>500 || y2>500){
int side = generator.nextInt(2);
//which side should the ball appear on
if(side==0)
{x2=0;
y2=generator.nextInt(470);}
if(side==1)
{x2=generator.nextInt(470);
y2=0;}}
x1 = avoid.getx1();
y1 = avoid.gety1();
if ((x1+ 30>x2 && x1-30<x2)&&(y1+ 30>y2 && y1-30<y2))
{ //calculate intersection for game over
GameOver();
} //draw the moved object
avoid.set2(x2,y2);
avoid.drawSurface.repaint();}
try{
//wait 10 milliseconds
Thread.sleep(10);}
catch(InterruptedException e) {
System.out.println("Interrupt Caught");
}}}}
class Enemy2 implements Runnable{
public void run(){
while(true){
if(avoid.getRun()){
//move object randomly (each object has different pattern)
if(generator.nextInt(2)==1)
x3 = x3+generator.nextInt(7);
else
x3 = x3-generator.nextInt(7);
y3 = y3-generator.nextInt(12);
if(y3 < -30){
x3=generator.nextInt(470);
y3=500;}
x1 = avoid.getx1();
y1 = avoid.gety1();
if ((x1+ 30>x3 && x1-30<x3)&&(y1+ 30>y3 && y1-30<y3))
{
GameOver();
}
avoid.set3(x3,y3);
avoid.drawSurface.repaint();}
try{
//wait 10 milliseconds
Thread.sleep(10);}
catch(InterruptedException e) {
System.out.println("Interrupt Caught");
}}}}