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
Super_Hippo Super_Hippo

2017/3/18

#
Try this:
private static int active = -1; //0=blue, 1=red
private int color = -1; //0=blue, 1=red
private GreenfootImage bluebat = GreenfootImage("Blue Bat.png");
private GreenfootImage redbat = GreenfootImage("Red Bat.png");
private String movingKeys = new String[2];

public Paddle(int c)
{
    color = c;
    if (c==0)
    {
        setImage("bluebat.png");
        movingKeys[0] = "w";
        movingKeys[1] = "s";
    }
    else
    {
        setImage("redbat.png");
        movingKeys[0] = "up";
        movingKeys[1] = "down";
    }
}


public void act()
{
    if (active == color)
    {
        if (Greenfoot.isKeyDown(movingKeys[0])) setLocation(getX(), getY()-10);
        if (Greenfoot.isKeyDown(movingKeys[1])) setLocation(getX(), getY()+10);
    }
    else
    {
        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);
        }
    }
}
You need to login to post a reply.
1
2