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

2021/1/25

Make everytime game starts random car start

DatPaul DatPaul

2021/1/25

#
Hi I am making a school assignment (due tonight) and I made a small game with four cars 2 red and 2 blue now everytime I start up the game I want to make a random car start so first time it can be red and second time i start the game it can be blue how can I do this? tyia
Super_Hippo Super_Hippo

2021/1/25

#
Try something like this:
if (Greenfoot.getRandomNumber(2) == 0)
{
    //red starts
}
else
{
    //blue starts
}
danpost danpost

2021/1/25

#
Or:
Car car = new Car();
String color = "0"+(Greenfoot.getRandomNumber(2) +1);
car.setImage("car"+color+".png");
or something along this line:
Car randomCar = Greenfoot.getRandomNumber(2) == 0 ? new BlueCar() : new RedCar();
The best way would depend on how you create your red and blue cars. That is why it always helps to provide related class codes (and describe your classes structure)
You need to login to post a reply.