import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class BeeWorld here. * * @author (your name) * @version (a version number or a date) */ public class BeeWorld extends World { //declare variables int beeValue; int weatherValue; public static Flower flower; int dayValue; int honeyValue; Actor btnAddDay, btnHelp, btnEndSimulation; private static final String[] FLOWER_COLOR = { "pink", "purple", "yellow" }; /** * Constructor for objects of class BeeWorld. * */ public BeeWorld(int weatherVal) { //set background to main screen when button is pressed super(864, 540, 1); setPaintOrder(Buttons.class); //set nectar value Forager.nectarValue = 0; setBackground(new GreenfootImage("Background.png")); //run new day newDay(); //add the counter for the bee value showText("Bees: ", 810, 20); //declare weatherValue weatherValue = weatherVal; //set honey value honeyValue = 0; showText("Honey: " + honeyValue, 810, 110); } public void act() { if (Greenfoot.mouseClicked(btnAddDay)) { newDay(); } if (Greenfoot.mouseClicked(btnHelp)) { help(); } if(Greenfoot.mouseClicked(btnEndSimulation)) { endSimulation(); } } private void endSimulation() { Greenfoot.setWorld(new EndScreen()); } private void help() { //add help menu HelpMenu HelpMenu = new HelpMenu(); addObject (HelpMenu, getWidth(), getHeight()); HelpMenu.setLocation(650,200); //button to be able to remove help menu RemoveMenu RemoveMenu = new RemoveMenu(); addObject (RemoveMenu, getWidth(), getHeight()); RemoveMenu.setLocation(900,200); } public void runSimulation() { if(weatherValue >= -40 && weatherValue <= 15) { //if the weather value is between -40 and 15 then the low temp simulation will run lowTemperature(); } else if(weatherValue >= 16 && weatherValue <= 27) { //if the weather value is between 16 and 27 then the regular temp simulation will run regularTemperature(); } else if(weatherValue >= 28 && weatherValue <= 40) { //if the weather value is between 28 and 40 then the high temp simulation will run highTemperature(); } } public void lowTemperature() { //add random coloured flowers to the bee world int flowerColorNum = Greenfoot.getRandomNumber(FLOWER_COLOR.length); Actor flower = new Flower(FLOWER_COLOR[flowerColorNum]); addObject(new Flower(), 500, 500); addObject(new Flower(), 100, 300); addObject(new Flower(), 200, 400); addObject(new Flower(), 100, 100); addObject(new Flower(), 700, 400); addObject(new Flower(), 800, 200); //add bees to the bee world Worker Worker = new Worker (); Queen Queen = new Queen (); Forager Forager = new Forager (); addObject(new Queen(), 500, 250); addObject(new Forager(), 400, 150); addObject(new Forager(), 450, 200); addObject(new Forager(), 500, 220); addObject(new Forager(), 550, 250); //add the hive to the bee world Hive Hive = new Hive (); addObject(new Hive(), 650, 400); } public void regularTemperature() { //add random coloured flowers to the bee world int flowerColorNum = Greenfoot.getRandomNumber(FLOWER_COLOR.length); Actor flower = new Flower(FLOWER_COLOR[flowerColorNum]); addObject(new Flower(), 500, 500); addObject(new Flower(), 100, 300); addObject(new Flower(), 200, 400); addObject(new Flower(), 100, 100); addObject(new Flower(), 700, 400); addObject(new Flower(), 800, 200); addObject(new Flower(), 600, 100); addObject(new Flower(), 400, 700); //add bees to the bee world Worker Worker = new Worker (); Queen Queen = new Queen (); Forager Forager = new Forager (); addObject(new Queen(), 500, 250); addObject(new Forager(), 400, 150); addObject(new Forager(), 450, 200); addObject(new Forager(), 500, 220); addObject(new Forager(), 550, 250); addObject(new Forager(), 520, 175); addObject(new Queen(), 500, 250); //add the hive to the bee world Hive Hive = new Hive (); addObject(new Hive(), 650, 400); } public void highTemperature() { //add random coloured flowers to the bee world int flowerColorNum = Greenfoot.getRandomNumber(FLOWER_COLOR.length); Actor flower = new Flower(FLOWER_COLOR[flowerColorNum]); addObject(new Flower(), 500, 500); addObject(new Flower(), 100, 300); addObject(new Flower(), 200, 400); addObject(new Flower(), 100, 100); addObject(new Flower(), 700, 400); addObject(new Flower(), 800, 200); //add bees to the bee world Worker Worker = new Worker (); Queen Queen = new Queen (); Forager Forager = new Forager (); addObject(new Queen(), 500, 250); addObject(new Forager(), 400, 150); addObject(new Forager(), 450, 200); addObject(new Forager(), 500, 220); addObject(new Forager(), 550, 250); addObject(new Forager(), 520, 175); addObject(new Queen(), 500, 250); //add the hive to the bee world Hive Hive = new Hive (); addObject(new Hive(), 650, 400); } public void updateDay() { //update what day it is and run the next day when the day button is clicked dayValue = dayValue +1; showText("Day: " + dayValue, 810, 50); newDay(); } private void newDay() { //start new day //remove all objects List objects = getObjects(null); removeObjects(objects); //increase day value dayValue++; //increase honey value updateHoney(); //add buttons btnAddDay = new Buttons(); btnAddDay.setImage(new GreenfootImage("addDaybutton.png")); btnHelp = new Buttons(); addObject(btnAddDay, 820, 500); btnHelp.setImage(new GreenfootImage("helpButton.png")); addObject(btnHelp, 100, 190); btnEndSimulation = new Buttons(); btnEndSimulation.setImage(new GreenfootImage("EndSimulationbutton.png")); addObject(btnEndSimulation, 50, 500); //run simulation again runSimulation(); //random chance to see if bee dies //add bee every 5 days if (dayValue%5 == 0) { addObject(new Forager(), 550, 250); } } public void beeTouchingFlower(final Bee touchingBee, final Flower touchingFlower) { //when bee touches flower: //stop bee moving touchingBee.stop(); touchingFlower.removeObject(this); } public void updateHoney() { //take away from nectar value nectarValue = nectarValue - 2; //increase honey value honeyValue = honeyValue + 2; //add the counter for the honey value showText("Honey: " + honeyValue, 810, 110); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Forager here. * * @author (your name) * @version (a version number or a date) */ public class Forager extends Bee { public static int nectarValue; public Forager() { //set image of forager setImage(new GreenfootImage("foragerBee.png")); GreenfootImage image = getImage(); image.scale(image.getWidth() - 1350, image.getHeight() - 750); setImage(image); } public void act() { //walk randomly randomWalk(); findFlower(); } public void findFlower() { if (isTouching(Flower.class)) { removeTouching(Flower.class); nectarValue += 2; getWorld().showText("Nectar: " +nectarValue, 810, 80); } } }
mport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; /** * A pile of food. The pile consists initially of 100 crumbs of food. * * @author Michael Kolling * @version 1.1 */ public class Flower extends Actor { //create variable to be able to change colour of flowers public String colour; public Flower(String pigment) { //set image of flowers colour = pigment; GreenfootImage image = new GreenfootImage(colour+"Flower.png"); image.scale(image.getWidth() - 500, image.getHeight() - 500); setImage(image); } public void act() { //check if bee is touching flower beeTouchingFlower(); } public void beeTouchingFlower() { //check if bee is touching flower final touchingBee = this.getOneIntersectingObject(Bee.class); final touchingFlower = this.getOneIntersectingObject(Flower.class); if (touchingBee != null) { //run beeTouchingFlower in world class BeeWorld b = (BeeWorld) getWorld(); b.beeTouchingFlower(touchingBee); b.removeObject(this); } } public boolean touchingForager() { //check if touching bee then return it return isTouching(Forager.class); } }
mport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bee here. * * @author (your name) * @version (a version number or a date) */ public class Bee extends Actor { //maximum movement speed private static final int SPEED = 2; //current movement and offset private int coordX; private int coordY; public Bee() { //neutral movement coordX = 0; coordY = 0; } public void randomWalk() { //walk around randomly (random direction and speed) if (randomChance(15)) { coordX = adjustSpeed(coordX); coordY = adjustSpeed(coordY); } walk(); } public void walk() { //walk forward in the current direction with the current speed setLocation(getX() + coordX, getY() + coordY); setRotation((int) (180 * Math.atan2(coordY, coordX) / Math.PI)); } public int stop(int speed) { speed = 0; return capSpeed(speed); } public void goHome() { coordX = 0; coordY = 0; setLocation(getX() + coordX, getY() + coordY); setRotation((int) (180 * Math.atan2(coordY, coordX) / Math.PI)); } private int adjustSpeed(int speed) { //adjust speed randomly speed = speed + Greenfoot.getRandomNumber(2 * SPEED - 1) - SPEED + 1; return capSpeed(speed); } private int capSpeed(int speed) { //make sure the speed returned is in the range if (speed < -SPEED) return -SPEED; else if (speed > SPEED) return SPEED; else return speed; } private boolean randomChance(int percent) { return Greenfoot.getRandomNumber(100) < percent; } }