Question: Please help having problems with coding in greenfoot. 1. Modify the constructor of the ButtonWorld class so that a single Ball object is placed at
Please help having problems with coding in greenfoot.
1. Modify the constructor of the ButtonWorld class so that a single Ball object is placed at location (500, 400) and ten Button objects are placed at random locations throughout the world.
code is below word class:
public class ButtonWorld extends World {
/** * Constructor for objects of class ButtonWorld. * */ public ButtonWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } }
Actor:
Button:
public class Button extends Actor { private GreenfootImage img1, img2; private GreenfootImage defaultImg; //public constructor public Button() { // modify the image sources // set img1 to steel-ball.png img1 = new GreenfootImage("button-red.png"); //set img2 to gold-ball.png img2 = new GreenfootImage("button-green.png"); // set defaultImage to img1 defaultImg = img1; } /** * Event1: Changes the location of this button approximately * 2% of the time. * Event2: Changes the location of this button approximately * 1% of the time. */ public void act() { if (Greenfoot.getRandomNumber(100) < 2) { changeLocation(); } else if(Greenfoot.getRandomNumber(100)<1) { changeImage(); } else { // please provide the implemenation of move() move(1); } }
/** * Sets the location of this Button object to a new (x, y) * coordinate selected at random. The x coordinate wil be in * the range 100 to 500 inclusive, and the y coordinate will * be the range 100 to 300 inclusive. */ public void changeLocation() { setLocation(Greenfoot.getRandomNumber(401) + 100, Greenfoot.getRandomNumber(201) + 100); } /** * Switches the default image source from the current image to the other image. */ public void changeImage() { if (defaultImg == img1) { defaultImg = img2; } else { defaultImg = img1; } } }
Ball:
public class Ball extends Actor { private int removedButtonCounts;// to count the number of button objects removed private GreenfootImage img1, img2; private GreenfootImage defaultImage; // create a default constructor to intitialize removedButtonCounts to 0 public Ball() { removedButtonCounts = 0; // set img1 to steel-ball.png img1 = new GreenfootImage("steel-ball.png"); // set img2 to gold-ball.png img2 = new GreenfootImage("gold-ball.png"); // set defaultImage to img1 defaultImage = img1; } /** * Responds to motion control from the arrow keys and removes * any buttons from the world when it intersects them. */ public void act() { handleKeyPress(); lookForButton(); checkToStop(); }
/** * Checks to see if the ball is "touching" an object of the Button class. * If it is, then the ball removes from the World a Button that it touches. * If the ball isn't touching an object of the Button class, then this * method does nothing. */ public void lookForButton() { if (isTouching(Button.class)) { removeTouching(Button.class); // increment removedButtonCounts to keep track of buttons removed removedButtonCounts ++; //change the defaultImage to gold-ball.png once 5 or more buttons are removed. if (removedButtonCounts >=5); defaultImage = img2; } }
/** * Handles input from the arrow keys. */ public void handleKeyPress() { checkLeftArrow(); checkRightArrow(); checkUpArrow(); checkDownArrow(); }
/** * Checks to see if the left arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves backward 5 units. If it is not being pressed, * this method does nothing. */ public void checkLeftArrow() { if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); } }
/** * Checks to see if the right arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkRightArrow() { if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); } }
/** * Checks to see if the up arrow key is being pressed. * If it is, then the ball sets its rotation angle to 270 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkUpArrow() { if (Greenfoot.isKeyDown("up")) { setRotation(270); move(5); } }
/** * Checks to see if the down arrow key is being pressed. * If it is, then the ball sets its rotation angle to 90 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkDownArrow() { if (Greenfoot.isKeyDown("down")) { setRotation(90); move(5); } } /** * Checks to see if all ten buttons have been removed. * In that case, stop the scenario */ public void checkToStop() { if (removedButtonCounts ==10) { // stop the scenario here. } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
