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

2015/12/6

i need help, make police cars try to ram you to the side of the road

AngelusNeko13 AngelusNeko13

2015/12/6

#
okay guys i really need your help, im doing the finishing touches of Street Racer X, i dont know how to make police cars try to ram you to the sie of the road while driving the same speed as you, also i already made a class to make them able to crash into traffic and the side of the road, what im saying is there a way to make police class chase you while bumping into you try not to hit anything but also you can bump into them? i have the vehicle parent class made, i just need to know how to make a subclass do this, also can you give me and example naming the class police? heres my coding for the parent class of the traffic and bumpercar class
import greenfoot.*;
import java.util.List;


public class Vehicle extends Actor
{

    public int speed = 8;

    private int dir = -1;

    public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png");

    public boolean alive = true;

    protected void addedToWorld(World w)
    {
        super.addedToWorld(w);
        if ( Greenfoot.getRandomNumber(2) == 0 ) dir = 1;
        else dir = -1;
    }

    public void dodge()
    {

        List<Vehicle> cars = getWorld().getObjects(Vehicle.class);

        if ( !cars.isEmpty() )
        {
            Vehicle dodgeme = null;
            int minTime = 99999999;

            for ( Vehicle v : cars )
            {
                if ( v != this && v.getY() < getY() && v.speed > this.speed && v.getX() >= getX()-20 && v.getX() <= getX()+20  )
                {
                    int time = ( getY() - v.getY() - 20 ) / ( v.speed - this.speed );
                    if ( time < minTime )
                    {
                        dodgeme = v;
                        minTime = time;
                    }
                }
            }
            if ( dodgeme != null )
            {
                int dx = dodgeme.getX() - getX() + 21;
                int time = ( getY() - dodgeme.getY() - 20 ) / ( dodgeme.speed - this.speed );
                int s = 1;
                if ( time > 0 && dx > time ) s = dx/time + 1;
                setLocation(getX()+dir*s, getY());
                return;
            }
        }
    }

    public void move()
    {
        if ( alive ) setLocation(getX(), getY()+speed );
        else setLocation(getX(), getY()+Car.speed );
    }

    public void hitBottom()
    {
        if (getY()>=getWorld().getHeight()-1)
        {
            getWorld().removeObject(this);
            World myWorld = getWorld();
            CarWorld carworld = (CarWorld)myWorld;
            Counter counter = carworld.getCounter();
            counter.addScore4();
        }   
    }
    
   

    public void act() 
    {
        if ( alive )
        {
            Actor b = this.getOneIntersectingObject(Vehicle.class);
            if( b != null)
            {
                Greenfoot.playSound("Crash.wav");

            
                //getWorld().removeObject(this);
                setImage(crashed);
                alive = false;
             World myWorld = getWorld();
             CarWorld carworld = (CarWorld)myWorld;
             Counter counter = carworld.getCounter();
             counter.addScore2();

                return;
            }
            Actor c = this.getOneIntersectingObject(Tree.class);
            if( c != null)
            {
                Greenfoot.playSound("Explosion.wav");
            

            
                //getWorld().removeObject(this);
                setImage(crashed);
                alive = false;
                 World myWorld = getWorld();
             CarWorld carworld = (CarWorld)myWorld;
             Counter counter = carworld.getCounter();
             counter.addScore3();
                return;

            }
        }
        if ( alive ) dodge();
      move();
     
      hitBottom();
       
      

    }
}
and this is the bumpercar class here:
import greenfoot.*;


public class BumperCar extends Vehicle
{   
    public void act()
    {
        
        Actor player = getOneIntersectingObject(Car.class);
        if ( player != null )
        {
            if ( getX() < player.getX() )  setLocation(getX()-30, getY());
             else if ( getX() > player.getX() ) setLocation(getX()+30, getY());
            
            if ( getY() < player.getY() ) setLocation(getX(), getY()-5);
            else if ( getY() > player.getY() ) setLocation(getX(), getY()+20);
           
        }
        
        
        super.act();
    }
    
    
}
heeeelp please :(
You need to login to post a reply.