Friday, November 8, 2013

Modifying Pong to Speed Up

This is an example of making the ball speed up when you hit it with the paddle each time:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is the Ball for our Pong game.
 * @Robert Griffith 
 * @11/05/2013
 */
public class Ball extends Actor
{
    double x = 320; // (getWorld().getWidth())/2;  // Half my world width
    double y = 240; // (getWorld().getHeight())/2; // Half my world height
    double yDirection;
    double xDirection;
    double ballSpeedStart = 2.000;
    double ballSpeed = ballSpeedStart;
    double speedIncrease = 0.001;
    int score1 = 0;
    int score2 = 0;
    int start = 0;

    // Cycle through the main program.
    public void act() 
    {
        if (start == 0){ startup(); }
        paddleBounce(); // Test for paddle bounce
        edgeBounce(); // Test for edge bounce
        x = x + xDirection;
        y = y + yDirection;
        int x1 = (int) x;
        int y1 = (int) y;
        setLocation(x1, y1);
    } 
    // Set the direction and speed of the Ball -- randomly at first.
    public void startup(){  
        start++;
        if ( Greenfoot.getRandomNumber(100) < 50 ) 
        { yDirection = -1*ballSpeedStart; } 
        else { yDirection = ballSpeedStart; }
        if ( Greenfoot.getRandomNumber(100) < 50 ) 
        { xDirection = -1*ballSpeedStart; } 
        else { xDirection = ballSpeedStart; }
    }
    // If we reach the edge of the world, bounce.
    public void edgeBounce()
    {
        if (atWorldEdge())
        {
            Greenfoot.playSound("4389__noisecollector__pongblipf-3.wav");
            if (x > 640) // Give Player1 Score & Launch Another Ball
            { 
                score1++;
                x = (getWorld().getWidth())/2;
                y = (getWorld().getHeight())/2;
                ballSpeed = ballSpeedStart;
                start = 0;
            }
            if (x < 0) // Give Player2 Score & Launch Another Ball
            { 
                score2++;
                x = (getWorld().getWidth())/2;
                y = (getWorld().getHeight())/2;
                ballSpeed = ballSpeedStart;
                start = 0;
            }
            if (y < 0 || y > getWorld().getHeight()) {yDirection = yDirection * -1;} // Bounce off roof or floor.
        }
    }
    // If the ball hits either paddle, reverse the direction of the ball.
    public boolean paddleBounce()
    {
        if (canSee(Paddle1.class))
        {
            Greenfoot.playSound("4391__noisecollector__pongblipf-5.wav");
            ballSpeed = ballSpeed + speedIncrease;
            xDirection = xDirection - ballSpeed;
            xDirection = (xDirection) * (-1);

        }
        if (canSee(Paddle2.class))
        {
            Greenfoot.playSound("4391__noisecollector__pongblipf-5.wav");
            ballSpeed = ballSpeed + speedIncrease;
            xDirection = xDirection + ballSpeed;
            xDirection = (xDirection) * (-1);
        }
        return false;
    }
    // Check to see if we can see an object.
    public boolean canSee(Class clss)  
    {  
        Actor actor = getOneObjectAtOffset(0, 0, clss);  
        return actor != null;          
    }  
    // Test if we are close to one of the edges of the world. Return true if we are.
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    // Holds score1 variable to be called by the Scores class for display.
    public int getScore1()
    {return score1;}  
    // Holds score2 variable to be called by the Scores class for display.
    public int getScore2() // The Scores class reads this for display
    {return score2;}  
}

No comments:

Post a Comment