This site requires JavaScript, please enable it in your browser!
Greenfoot back
mattstg

mattstg

Welcome to my page

mattstg's collections

This user has no collections

Recent Comments

mattstg

2020/7/30

@danpost thank you!
mattstg

2020/7/29

How would I add a game over screen for when all of the cars have been destroyed and removed from the world by the bombs? Here is my code for the bombs: public class Bomb extends Actor { /** * Act - do whatever the Bomb wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (foundCar()) { destroyCar(); } setLocation(getX(),getY()+1); if (getY() > getWorld().getHeight() - getImage().getHeight() / 2) { getWorld().removeObject(this); } } public boolean foundCar() { Actor car = getOneObjectAtOffset(0, 0, Car.class); if(car != null) { return true; } else { return false; } } public void destroyCar() { Actor car = getOneObjectAtOffset(0, 0, Car.class); if(car != null) { getWorld().removeObject(car); Counter counter = ((Road)getWorld()).getCounter(); counter.bumpCount(1); } } } Here is my code for the cars: public class Car extends Actor { /** * Act - do whatever the Car wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("left")) { move(-4); } if (Greenfoot.isKeyDown("right")) { move(4); } } } Here is my code for the world: public class Road extends World { private Counter theCounter; /** * Constructor for objects of class Road. * */ public Road() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); setBackground("road.jpg"); theCounter = new Counter(); addObject(theCounter, 15, 15); prepare(); } public Counter getCounter() { return theCounter; } public void act() { if (Greenfoot.getRandomNumber(100) < 2) { Bomb bomb = new Bomb(); addObject(bomb, Greenfoot.getRandomNumber(getWidth()), bomb.getImage().getHeight() / 2); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Car car = new Car(); addObject(car,87,307); Car car2 = new Car(); addObject(car2,215,304); Car car3 = new Car(); addObject(car3,387,305); } } Here is my code for the counter: public class Counter extends Actor { private int totalCount = 0; public Counter() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); } public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); } }