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

Report as inappropriate.

mattstg
mattstg presents ...

2020/7/28

Car vs. Bombs


1047 views / 18 in the last 7 days

Tags: game with-source

open in greenfoot
Your browser does not support the canvas tag.
A new version of this scenario was uploaded on 2020-07-29 16:34:17 UTC The issue with the game stopping before all the cars were destroyed has been fixed.
mattstgmattstg

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)); } }
danpostdanpost

2020/7/29

Start act method of Road class with: if (getObjects(Car.class).isEmpty()) { // game over code here } else
A new version of this scenario was uploaded on 2020-07-30 16:31:23 UTC A game over screen has been added.
mattstgmattstg

2020/7/30

@danpost thank you!

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.