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

2017/3/16

Programming A.i. for Pong

1
2
Kronos Kronos

2017/3/16

#
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);
            }
        }
Super_Hippo Super_Hippo

2017/3/16

#
You shouldn't create a new Paddle every time Blues act method is executed. The Paddle should not create a new PongBall every time its act method is executed. These new objects are not the same as the ones which are in your world. Like always, there is not only one way of how to do it. That could be one:
private static int active = -1; //0=blue, 1=red
private int color = -1; //0=blue, 1=red

public Paddle(int c)
{
    color = c;
    //set blue or red image depending on c/color
}

public void act()
{
    if (active == color)
    {
        //check for key presses and move accordingly
    }
    else
    {
        //move automatically
    }
}

public static void setActive(int c)
{
    active = c;
}
//in your world, when creating the paddles
new Paddle(0) //for blue paddle
new Paddle(1) //for red paddle
//when you chose single player and want to be the blue one:
Paddle.setActive(0);
danpost danpost

2017/3/16

#
Nor should you create new PongBall objects every act cycle of the Paddles class.
Kronos Kronos

2017/3/17

#
I know that I shouldn't create new objects every act cycle, but how do i actually get the paddle to move by itself?
Super_Hippo Super_Hippo

2017/3/17

#
You need to get a reference to the Ball which is in play and then you compare its y-coordinate to the paddle's y-coordinate.
Kronos Kronos

2017/3/17

#
Would this work? Lines 11 -21 or lines 23-33
       
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);
            }
        }
Super_Hippo Super_Hippo

2017/3/17

#
More like that:
Actor pongBall = getWorld().getObjects(PongBall.class).get(0);
if (pongBall != null)
{
    int y=getY(), py=pongBall.getY();
    if (y<py-1) setLocation(getX(), getY()+2);
    else if (y>py+1) setLocation(getX(), getY()-2);
}
Kronos Kronos

2017/3/17

#
How do i reference the ball that is in play?
Nosson1459 Nosson1459

2017/3/17

#
Kronos wrote...
How do i reference the ball that is in play?
The first line in the code Hippo gave. It makes a list of all the PongBall Actors that are in the world and gets the first item (get(0)) in the list (there is only one).
Kronos Kronos

2017/3/17

#
It doesn't do anything. The paddle still won't move.
Super_Hippo Super_Hippo

2017/3/17

#
Show your entire paddle class. Did you create the Paddle as mentioned before?
Kronos Kronos

2017/3/18

#
With the code you gave earlier, it doesn't compile.
/**
 * Write a description of class Paddles here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Paddles extends Actor
{
    private static int active = -1; //0=blue, 1=red
    private int color = -1; //0=blue, 1=red
    GreenfootImage bluebat = GreenfootImage("Blue Bat.png");
    GreenfootImage redbat = GreenfootImage("Red Bat.png");
    public Paddles(int c)
    {
        color = c;
        //set blue or red image depending on c/color
    }
     
    public void act()
    {
        if (active == color == 0)
        {
            setImage(bluebat);
            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);
            }
        }
        else
        if (active == color == 1)
        {
            setImage(redbat);
            if (Greenfoot.isKeyDown("up")) // Pressing the w key will move the blue paddle up.
            {       
                setLocation(getX(),getY() - 10);
            }
            if (Greenfoot.isKeyDown("down")) // Pressing the s key will move the blue paddle down.
            {
                setLocation(getX(),getY() + 10);
            }
        }
        else
        {
            //move automatically
            Actor pongball = getWorld().getObjects(PongBall.class).get(0);
           if (pongball != null)
           {
                int y=getY(), py=pongball.getY();
                if (y<py-1) 
                {
                    setLocation(getX(), getY()+2);
                }
                else if (y>py+1)
                {
                    setLocation(getX(), getY()-2);
                }
           }
        }
    }
     
    public static void setActive(int c)
    {
        active = c;
    }
}
danpost danpost

2017/3/18

#
Lines 21 and 34 are each trying to compare a boolean value with an int value -- obviously, this will fail. I think they should be:
// line 21
if (active == 0 && color == 0)
// and line 34
if (active == 1 && color == 1)
Kronos Kronos

2017/3/18

#
It still doesn't work.
danpost danpost

2017/3/18

#
Kronos wrote...
It still doesn't work.
This does not tell us anything. What errors are you getting? Where are they located at? If not getting error, then how is it not running like you want. How does that differ from what you do want?
There are more replies on the next page.
1
2