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

2019/3/23

Help with Ping pong game

Melissa Melissa

2019/3/23

#
Hello, I am working on a Ping Pong game for school but i don't know how to change the angle when teh ball touches the left or the right side of the paddle. Can someone help me?
Melissa Melissa

2019/3/23

#
Right now i got this
public class speler2 extends spelers
{
    /**
     * Act - do whatever the PingpongRood wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        bewegen();
    }    
    public void bewegen()
    {
        if (Greenfoot.isKeyDown("W")) {
            setLocation(getX(), getY() - 3);
        }
        if (Greenfoot.isKeyDown("S")) {
            setLocation(getX(), getY() + 3);
        
        }
    }
}
public class Bal extends Actor
{
    public int balX=8;
    public int balY=8;
    /**
     * Act - do whatever the Bal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        balBeweging();
        raaktSpelers();
    }    
    public void balBeweging()
    {
        setLocation(getX()+balX,getY());
        if(getY()<5 || getY()>551)
        {
            balY = balY*-1;
        }
    }
    public void raaktSpelers()
    {
        Actor spelers = getOneIntersectingObject (spelers.class);
        if(spelers != null)
        {
            balX = balX * -1;
        }
    }
}
You need to login to post a reply.