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

2015/3/1

I need help with car game.

david12 david12

2015/3/1

#
I am trying to get my vehicle class cars to reduce and show up one car per lane instead of 1000 on the screen every time i move right and left of my main car. How can i do it. This is what i have.
in my world subclass 
 public void act()
    {
        
        if (countDown>0)
        {
            countDown--;
        }else 
        {
           
            
            int random=(int)(Math.random()* 7);
            if (random ==0 )  lane = 51;
            if (random ==1 )  lane = 115;
            if (random ==2)  lane = 189;
            if (random ==3)  lane = 256;
            if (random ==4)  lane = 328;
            if (random ==5)  lane = 398;
            if (random ==6)  lane =  480;
            if (random ==7)  lane = 550;
            
            
            Vehicle v1 = new Vehicle();
           
             addObject(v1, lane,7);
             
            
            countDown = (int)(Math.random()* 5);
            
        }

        
    }
and in my Vehicle class i have  
public class Vehicle extends Actor
{
    private int speed;
    private int count;
   public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(7, 8, clss);
        return actor != null;        
    }

     public void hit(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    
    public Vehicle()
    {
        speed = (int)(Math.random()* 10 );
    
        
    }
    /**
     * Act - do whatever the Vehicle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.mov
        
        {
            moveDown(); 
        }  
        if(getY()>=getWorld().getHeight()-2)
        {

            getWorld().removeObject(this);

        }
        count++;
    }
    public void moveDown()
    {
        setLocation(getX(),getY()+speed);
    }
    
    
}

danpost danpost

2015/3/1

#
Line 28 in your world subclass above is what is setting the rate of production of vehicles. Using 'Math.random()*5', the average value would be two. That means you will produce on average one car every other act cycle. At normal scenario speeds of between 50 and 60 cycles per second, you would be creating 25 to 30 cars per second, on average. Change the '5' to a much larger value. You can also add a constant value to it (for example: 'Math.random()*60+25').
You need to login to post a reply.