I've been trying to make a game where a random equation is made and four random answers are displayed to select from with one of them being the correct answer and the last three being incorrect. I've been successful in making the random equation display as well as it's answer, but I have been stumped on getting the incorrect answers to display. They are supposed to detect whether the correct answer is in their spot or not. If it is than nothing happens, if it isn't than a random incorrect answer is generated to take the place. Everything I've tried so far hasn't worked and I've looked all over the place to figure something out.
Here is my code:
The world is Easy
Here is the Actor: EquationLabel
Here is the Actor: SumLabel
Here is the Actor: AnswerButton1
Please help me if you can, I really am stuck and have tried to get this to work.
public class Easy extends World
{
/**
* Constructor for objects of class Easy.
*
*/
public Easy()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
EquationLabel.reset();
AnswerButton1.reset();
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
KayakEasy kayakeasy = new KayakEasy();
addObject(kayakeasy, 300, 215);
EquationLabel equationlabel = new EquationLabel();
addObject(equationlabel, 332, 50);
}
}
public class EquationLabel extends Actor
{
/**
* Here to keep the random number generator from constantly making new
* numbers. It is supposed to be one random number at a time.
*/
protected static int num1 = Greenfoot.getRandomNumber(15);
protected static int num2 = Greenfoot.getRandomNumber(15);
protected static int sum1 = (num1 + num2);
protected static int var = Greenfoot.getRandomNumber(100);
/**
* Allows for a different number to be generated every time the world is reset.
* What I plan to do is make it so every time the question is answer the numbers reset,
* but I'm not there yet.
*/
public static void reset()
{
num1 = Greenfoot.getRandomNumber(15);
num2 = Greenfoot.getRandomNumber(15);
sum1 = (num1 + num2);
var = Greenfoot.getRandomNumber(100);
}
/**
* Allows the equation to display in the world senario.
*/
public EquationLabel()
{
GreenfootImage img = new GreenfootImage (100, 30);
img.drawString (num1 + " + " + num2 + " =", 2, 20);
setImage (img);
}
/**
* Act - do whatever the Question_AnswerDisplay wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
sumPlacement();
}
/**
* This puts the SumLabel in one of four different spots each with a 25%
* chance of being selected.
*/
public void sumPlacement()
{
if (var < 25)
{
getWorld().addObject(new SumLabel(), 190, 150);
}
if ((var > 25) & (var < 50))
{
getWorld().addObject(new SumLabel(), 290, 150);
}
if ((var > 50) & (var < 75))
{
getWorld().addObject(new SumLabel(), 390, 150);
}
if ((var > 75) & (var < 100))
{
getWorld().addObject(new SumLabel(), 490, 150);
}
}
/**
* This was put in place to test if the sumPlacement method worked properly.
*/
public void test()
{
if (Greenfoot.isKeyDown("a"))
{
sumPlacement();
}
}
}
public class SumLabel extends EquationLabel
{
/**
* Allows the SumLabel to be displayed in the world senario.
*/
public SumLabel()
{
GreenfootImage img = new GreenfootImage (100, 30);
img.drawString ("" + sum1,2,20);
setImage (img);
}
/**
* Act - do whatever the SumLabel wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
setImage("Correct.png");
}
}
}
public class AnswerButton1 extends EquationLabel
{
/**
* Here to keep random number generator from constanly making new
* numbers. It is supposed to be one random number at a time.
*/
protected static int subnum = Greenfoot.getRandomNumber(30);
/**
* Allows for a different number to be generated every time the world is reset.
* What I plan to do is make it so every time the question is answer the numbers reset,
* but I'm not there yet.
*/
public static void reset()
{
subnum = Greenfoot.getRandomNumber(30);
}
/**
* Allows AnswerButton1 to display in the world senario.
*/
public AnswerButton1()
{
GreenfootImage img2 = new GreenfootImage (100,200);
img2.drawString ("" + subnum, 2, 20);
setImage (img2);
}
/**
* Act - do whatever the AnswerButton1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*
* What I'm trying to do it make it detect whether or the the SumLabel is in it's
* spot. If it is than it does nothing, but if the SumLabel is not in it's spot
* then it is supposed to generate a random number to take the place of an incorrect
* answer.
*/
public void act()
{
if (intersects((SumLabel)getWorld().getObjects(SumLabel.class).get(0)))
{
int subnum = Greenfoot.getRandomNumber(30);
if (Greenfoot.mouseClicked(this))
{
setImage("Incorrect.png");
}
}
}
}
