I have trying over and over to get the ai working for my pong game. The game is going to have both a single player and multiplayer option. If the single player option is selected, then the user picks the color they want and click on its button. E.g. The blue button should change the value of the boolean playerBlueControlled to true. So the player is the blue paddle and the computer is the red paddle.
However, no matter what i do, the paddles are only responding to key presses from the user.
Please help.
boolean playerBlueControlled = false;
/**
* Act - do whatever the Blue wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeypress(); // This statement calls the checkKeyPress method.
}
}
/**
* Check whether a control key on the keyboard has been pressed.
* If it has, react accordingly.
*/
public void checkKeypress()
{
Paddles paddles = new Paddles();
if(playerBlueControlled = false)
{
paddles.act();
}
else
if(playerBlueControlled = true)
{
if (Greenfoot.isKeyDown("w")) // Pressing the w key will move the blue paddle up.
{
setLocation(getX(),getY() - 10);
}
if (Greenfoot.isKeyDown("s")) // Pressing the s key will move the blue paddle down.
{
setLocation(getX(),getY() + 10);
}
}static int dif = 1;
/**
* Act - do whatever the Paddles wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
//the below code is to be used for the AI if multiplayer mode is not selected.
PongBall pongBall = new PongBall();
// setLocation(getX(),getY() - 5);
// if(getY() > pongBall.getY())
// {
// setLocation(getX(),getY() - 10);
// }
// else
// if(getY() > pongBall.getY())
// {
// setLocation(getX(),getY() + 10);
// }
for(int i=0; i<dif; i++) {
if(getY()< pongBall.getY())
{
setLocation(getX(), getY()+ 1);
}
else
if(getY()> pongBall.getY())
{
setLocation(getX(), getY()- 1);
}
}

