This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2020/10/11

Check collision before adding

1
2
3
4
5
Roshan123 Roshan123

2020/10/18

#
B.class.....
import greenfoot.*;
public class B extends Actor
{
    int t,count,speed=5;
    boolean upKeyDown;
    int movt;
    Counter counter;
    int health=150;
    int s[]={0,1,2};
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    static final int m=100;
    public B(Counter counter)
    {
      this.counter=counter;
      getImage().scale(60,60);
      setRotation(180);
      background = getImage();
    }
    public void act() 
    {
        magnet();
        sheild();
        speed();
        stone();
        die();
        moveANDturn();

    }
    protected void add(World w)
    {
     int i=0;    while(getOneIntersectingObject(null)!=null && i++<m)
     {
         setLocation((int)(Math.random()*w.getWidth()), (int)(Math.random()*w.getHeight()));
     
     }
     if(i>=m)
     {
         w.removeObject(this);
    }
    }
    public void magnet()
    {
       
     if(Greenfoot.isKeyDown("w") && isTouching(Magn_feild.class))
     {
         speed=+2;
     }else speed=5;
    }
    public void sheild()
    {
        if(isTouching(Crate.class))
        {
            int rand=Greenfoot.getRandomNumber(s.length);
            if(rand==0)
            getWorld().addObject(new BSheild(),getX(),getY() );
            if(rand==1)getWorld().addObject(new HealthB(),410,26 );
            if(rand==2)getWorld().addObject(new Score_inc_b(counter),getX(),getY());
        }
    }
    public void speed()
    {
        Actor Speed=getOneIntersectingObject(Speed.class);
        if(Greenfoot.isKeyDown("w") && Speed !=null)
        {
          speed=+16;
        }
        
    }
    public void stone()
    {
      Actor Stone=getOneIntersectingObject(Stone.class);
      Actor EnemyTank=getOneIntersectingObject(EnemyTank.class);
      Actor EnemyTank1=getOneIntersectingObject(EnemyTank1.class);
      Actor Sheild=getOneIntersectingObject(Sheild.class);
      Actor Tank=getOneIntersectingObject(Tank.class);
      if(Greenfoot.isKeyDown("w") && Stone       !=null
      || Greenfoot.isKeyDown("w") && EnemyTank   !=null 
      || Greenfoot.isKeyDown("w") && EnemyTank1  !=null 
      || Greenfoot.isKeyDown("w") && Sheild      !=null  
      || Greenfoot.isKeyDown("w") && Tank        !=null) 
      {
        speed=0;
      }
      if(upKeyDown != Greenfoot.isKeyDown("w") && Stone      !=null 
      || upKeyDown != Greenfoot.isKeyDown("w") && EnemyTank1 !=null
      || upKeyDown != Greenfoot.isKeyDown("w") && EnemyTank  !=null 
      || upKeyDown != Greenfoot.isKeyDown("w") && Sheild     !=null  
      || upKeyDown != Greenfoot.isKeyDown("w") && Tank       !=null) 
      {
        move(-15);
      }
        
    }   
    public void moveANDturn()
    {    
        if(Greenfoot.isKeyDown("w"))
        {
          movt++;
        }else {turn(t);movt=0;}
        if(movt>7)
        {
         move(speed);   
        }
        if(upKeyDown != Greenfoot.isKeyDown("w"))
        {
            upKeyDown= !upKeyDown;
            if(upKeyDown)
            {
            count+=1;
            Projectile1 projectile1=new Projectile1();
            getWorld().addObject(projectile1, getX(),getY());
            projectile1.setRotation(getRotation());
            }
        }
        if(count % 2==0)
        {
            t=4;
        }else t=-4;
    } 
    public void die()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(""+health,20,Color.WHITE, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth()-31), 
                        (image.getHeight()-text.getHeight()-20));
        setImage(image);
        if(isTouching(Projectile.class))
        {
         counter.tscore++;
        }
        Actor Projectile=getOneIntersectingObject(Projectile.class);
        if(Projectile !=null)
        {
         health-=50;
         getWorld().removeObject(Projectile);
        }
        if(health ==0 )
        {
            removeTouching(Tank.class);
            getWorld().removeObject(this);
        }
    }
} 
Roshan123 Roshan123

2020/10/18

#
import greenfoot.*;  
public class MyWorld extends World
{
    Counter counter=new Counter();
    public Tank mainTank=new Tank(counter);
    public B mainB=new B(counter);
    Help help = new Help();
    int tank,b,crate,bottimer,bottimer1;
    int r[]={680,120};
    int r2[]={120,480};
    int r3[]={680,120};
    int r4[]={120,480};
    public MyWorld()
    {    
        super(800, 600, 1);
        setPaintOrder(Help.class,Crate.class,EnemyCannon2.class,EnemyTank.class,EnemyCannon.class,
        EnemyTank1.class,BSheild.class,Sheild.class,Tank.class,B.class,
        Projectile.class,Projectile1.class,Magn_feild.class,Stone.class,Counter.class);
        
        addObject(counter, 410, 15);
        mainTank=new Tank(counter);
        addObject(mainTank, 680,120);

        mainB=new B(counter);
        addObject(mainB, 120,480);
        
        addObject(new EnemyTank(true,4), 268, 373);
        addObject(new EnemyCannon2(20), 268, 373);
        
        addObject(new EnemyTank1(true,4), 532, 227);
        addObject(new EnemyCannon(20), 532, 227);
        
        
        addObject(new Crate(), 196,300);
        addObject(new Crate(), 604,300);
        
        addObject(new Stone(), 300,300);
        addObject(new Stone(), 248,300);
        addObject(new Stone(), 196,353);
        addObject(new Stone(), 196,406);
        
        addObject(new Stone(), 500,300);
        addObject(new Stone(), 552,300);
        addObject(new Stone(), 604,248);
        addObject(new Stone(), 604,196);
        
        addObject(new Speed(), 607, 80);
        addObject(new Speed(), 195, 520);
        addObject(new Magn_feild(), 85, 400);
        addObject(new Magn_feild(), 715, 200);
        showText("PRESS 1 FOR HELP\nPRESS 0 TO CLOSE IT", 105, 25);
    }
    public void act()
    {
        showText("PRESS 1 FOR HELP\nPRESS 0 TO CLOSE IT", 105, 25);
        if(Greenfoot.isKeyDown("1"))
        {
            addObject(help, 400, 300);
        }
        if(Greenfoot.isKeyDown("0"))
        {
            removeObject(help);
        }
        
        int rand=Greenfoot.getRandomNumber(r.length);
        int rand2=Greenfoot.getRandomNumber(r2.length);
        int rand3=Greenfoot.getRandomNumber(r.length);
        int rand4=Greenfoot.getRandomNumber(r2.length);
        
        if(tank>0 && --tank ==0) addObject(new Tank(counter),r[rand],r2[rand2]);
        if(tank==0 && getObjects(Tank.class).isEmpty())tank =100;
        
        //if(b>0 && --b ==0) addObject(new B(counter),r[rand],r2[rand2]);
      //  if(b==0 && getObjects(B.class).isEmpty())b =99;
        //removed for the time bieng
        
        if(crate>0 && --crate ==0) {addObject(new Crate(),196,300); addObject(new Crate(),604,300);}
        if(crate==0 && getObjects(Crate.class).isEmpty())crate =150;
         
        if(bottimer>0 && --bottimer ==0) addObject(new EnemyTank(true,4),  268, 373);
        if(bottimer==0 && getObjects(EnemyTank.class).isEmpty())bottimer =100;
        
        if(bottimer>0 && --bottimer ==0) addObject(new EnemyCannon2(20),  268, 373);
        if(bottimer==0 && getObjects(EnemyCannon2.class).isEmpty())bottimer =10;
        
        if(bottimer1>0 && --bottimer1 ==0) addObject(new EnemyTank1(true,4), 532, 227);
        if(bottimer1==0 && getObjects(EnemyTank1.class).isEmpty())bottimer1 =100;
        
        if(bottimer1>0 && --bottimer1 ==0) addObject(new EnemyCannon(20), 532, 227);
        if(bottimer1==0 && getObjects(EnemyCannon.class).isEmpty())bottimer1 =10;
        
    }
    
}
danpost danpost

2020/10/18

#
I noticed lines 75 and 74 are commented out still. Which is red team (tank or b)?
Roshan123 Roshan123

2020/10/18

#
B
danpost danpost

2020/10/18

#
danpost wrote...
I noticed lines 75 and 74 are commented out still.
I meant lines 73 and 74 in MyWorld. Aren't these the lines that should restore red team into the world?
Roshan123 Roshan123

2020/10/18

#
Yes I removed it becaz it doesn't check collision before adding
danpost danpost

2020/10/18

#
Roshan123 wrote...
I removed it becaz it doesn't check collision before adding
The addedToWorld method should do that (in the actor, B, class). Your world only need add one as normal.
Roshan123 Roshan123

2020/10/18

#
if(b>0 && --b ==0) addObject(new B(counter),r,r2); if(b==0 && getObjects(B.class).isEmpty())b =99; Can u add code to check collision using this code Becaz i only want b to add randomly (only in place of r and r2) If r and r2 place is full then it will check collision and add again to the world
Roshan123 Roshan123

2020/10/18

#
danpost wrote...
Roshan123 wrote...
I removed it becaz it doesn't check collision before adding
The addedToWorld method should do that (in the actor, B, class). Your world only need add one as normal.
I don't understand And also reply it
Roshan123 wrote...
if(b>0 && --b ==0) addObject(new B(counter),r,r2); if(b==0 && getObjects(B.class).isEmpty())b =99; Can u add code to check collision using this code Becaz i only want b to add randomly (only in place of r and r2) If r and r2 place is full then it will check collision and add again to the world
danpost danpost

2020/10/18

#
Roshan123 wrote...
i only want b to add randomly (only in place of r and r2)
If you add actor at (r, r2), then the addedToWorld method as given by RcCookie will try to find some empty place for it ONLY IF it is touching another actor,
Roshan123 Roshan123

2020/10/18

#
Can u plz write the code
danpost danpost

2020/10/18

#
Roshan123 wrote...
Can u plz write the code
You already have it.
Roshan123 Roshan123

2020/10/18

#
I have the code but, RcCookie code is not adding randomly to the world when b is empty Plz tell me how will i fix it
danpost danpost

2020/10/18

#
At line 30 in B class, change "add" to "addedToWorld".
Roshan123 Roshan123

2020/10/18

#
danpost wrote...
At line 30 in B class, change "add" to "addedToWorld".
Now also its not working
There are more replies on the next page.
1
2
3
4
5