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

2013/9/2

Single Player Pong

1
2
RedCreeper RedCreeper

2013/9/2

#
I have made a two player pong game, but i want to make it able to be a single player but i am having trouble with the "computer's" paddle. I can make the paddle follow the ball easily but the way i was doing it made it impossible to win. I have uploaded Pong.
JetLennit JetLennit

2013/9/2

#
So, you want it to be possible to win?
RedCreeper RedCreeper

2013/9/2

#
Yes
danpost danpost

2013/9/2

#
Here are some things you can do: (1) change the rate of movement of the paddle (ex. count acts and every third one, do not move) (2) add a varying delay to change direction (like reaction time) (ex. if needing to change and random chance 19 in 20, do not move or change direction that act cycle) (3) only follow the ball when it is on the computers side of the world
RedCreeper RedCreeper

2013/9/3

#
Thank you danpost. I have uploaded new version of pong but there are some glitches so if anyone can help me fix them they would be a big help. Some of the ones i know are when on single player if you score on the computer the ball goes crazy, then on multiplayer if player 2 scores the ball starts moving even before the player launches it. There are more but it is easier if you play it and find out.
danpost danpost

2013/9/3

#
If you check the 'Include source code' checkbox during upload or post some of the problematic code here, someone might be able to help.
RedCreeper RedCreeper

2013/9/3

#
Sorry i though i did. i have re posted it
danpost danpost

2013/9/3

#
I believe the excessive use of 'static' fields may be one culprit in some of the unwanted behavior. For example, if you took 'static' off the following Ball class field declaration, 'public static boolean stuck = true;', it would stop the ball from bouncing off the wall when it should be 'stuck' to the paddle. It is going to take some time to sort through your code. However, I did also notice the excessive use of non-static fields (for example: you can tell which paddle is which by its return value of 'getX()'; so by that, we know what keys control it). You have multiple methods that do the same exact thing (see 'haveBall' and 'gotBall' in the Paddle class); this really does not impair your project -- just excessive code. Getting back to 'static' fields: unless the value needs to be held between resets of the scenario, they probably should not be 'static'. An example might be if you were counting games won by Player 1 and Player 2. Resetting the scenario will not reset those 'static' fields (only re-compilation will reset those values). There are other reasons for using 'static' fields, but care must be taken in using them. 'static final' fields are a different story. Because their values do not change, they do not in any way affect the behavior of the scenario.
danpost danpost

2013/9/3

#
I also noticed you have multiple fields among multiple classes that are used to hold the same value which can complicate things. In your Table class, you have fields for 'player1score' and 'player2score' whose values are also kept in the Counter objects 'score' and 'score2'. Since you already have references to the Counter objects in the Table class, you have no need for the 'int' fields to hold the scores. You can use 'score.getValue()' and 'score2.getValue()' to return the scores.
RedCreeper RedCreeper

2013/9/3

#
Thank You again danpost. i did't know that the static field was never reset. I have fixed all of the errors i know of right now. Also it is possible to win against the computer, but i was wondering how to make the computer's paddle move more smoothly. I think that if i can make it so that the computer moves the paddle in increments instead of having it jump to the location of the ball.
danpost danpost

2013/9/3

#
OK, by playing your updated scenario, I see you tried by have to computer paddle move only when the ball is on its side. We will have to work on smoothing the movement out, however. Also. when the game is over, the final scoreboard is repeatedly being created and a multitude of scoreboard objects end up in the world. Two ways to prevent this are (1) stop the scenario after placing the scoreboard in the world; and (2) do not place a scoreboard in the world if there already is one in the world.
RedCreeper RedCreeper

2013/9/3

#
I will fix that thank you danpost
RedCreeper RedCreeper

2013/9/3

#
Also there a way that if the player hits the ball strait the computer will hit if no matter what
RedCreeper RedCreeper

2013/9/4

#
i have made the paddle more smooth moving but it still isn't as smooth as i would like, also there is an glich that if you launch the ball far away from the computer's paddle or if the ball picks up to much speed the computer's paddle can't even reach the ball in time here is the code i use to control where the paddle moves to, also the full game has been uploaded again with this change. This code also makes the computer's paddle launch its ball incorrectly.
if(Table.singlePlayer == true && returnPlayer() == 2) {
             if(Ball.X < 350 && Greenfoot.getRandomNumber(100) < 80 && Greenfoot.getRandomNumber(100) < 80) {
                if(Ball.Y > 250 && Ball.Y != getY()) {
                    moveSideways(5);
                }
                else if(Ball.Y != getY()) {
                    moveSideways(-5);
                }
            }
        }
Old code
if(Table.singlePlayer == true && returnPlayer() == 2) {
             if(Ball.X < 350 && Greenfoot.getRandomNumber(100) < 80 && Greenfoot.getRandomNumber(100) < 80) {
                if(Ball.Y > 250) {
                    setLocation(getX(), Ball.Y + 5);
                }
                else {
                    setLocation(getX(), Ball.Y - 5);
                }
            }
        }
danpost danpost

2013/9/4

#
To ensure smoother paddle movement, only use setLocation(getX(), getY()+n), where 'n' is 5, 0, or -5. This way the paddle has no choice but to move only 5 pixels at a time (if it moves at all). If computer controlled, use the difference between the ball's y-coordinate and the paddle's y-coordinate to determine which way to move (if the values are different. The following is possible code you could use after ensuring it is a computer controlled paddle
// is ball in world
if (!getWorld().getObjects(Ball.class).isEmpty())
{
    // get reference to ball
    Actor ball = (Actor)getWorld().getObjects(Ball.class);
    // is ball on the half of the playfield this paddle is on
    if (Math.abs(ball.getX()-getX()) < 335)
    {
        // get vertical direction to move (dy = { -1, 0, 1 })
        int dy = (int)Math.signum((ball.getY()-getY())/3);
        // make move in direction determined (or remain in place)
        setLocation(getX(), getY()+5*dy);
    }
}
There are more replies on the next page.
1
2