Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help with Pong game

1. ## Need help with Pong game

I'm making my first GUI based game in Java using Slick2D, and I have hit a sort of wall, no pun intended . This might be a really simple question but honestly my mathematics are not amazing by no means and I'm more use to general stuff in Java than gaming math.

I have my ball moving on the screen and when it hit's a paddle, I can get it to change direction and to move up or down but I want to to change direction depending on where it hits. If it hits the lower half then it will go down, if it hits the upper half then it should go up. Any ideas on how I can do this? I have posted a segment of my code below showing what is currently happening, sorry if it's not the correct way to do these sort of mechanics D:

```                if(ballIcon.intersects(playerIcon)){
vecX = 2 * delta * 0.2f;
}else if(ballIcon.intersects(cpuIcon)){
vecX = -2 * delta * 0.2f;
}

if(ballIcon.intersects(side)){
vecY = 1;
}else if(ballIcon.intersects(sideBottom)){
vecY = -1;
}```

This code basically changes the direction of the ball. I'm using Slick2D shapes to detect collisions and these are invisible, but their x and y cords are equal to the images on the screen so they follow the images around.

2. ## Re: Need help with Pong game

I want to to change direction depending on where it hits. If it hits the lower half then it will go down, if it hits the upper half then it should go up
These variations are controlled by the changes in the object's x/y coordinates at each animation step, or frame. In order for the object to move only up or down (vertically), the y-coordinate is changed; to move only horizontally, the x-coordinate is changed. Often, movement at an angle from pure vertical or horizontal is desired, and that is accomplished by varying both the x- and y-coordinates with each frame. If at a 45-degree angle, x and y vary the same with each step. Simple reflection is relatively easy to explain:

If the object is travelling down and right: deltaX = +1 (right), deltaY = +1 (down)
And hits a vertical wall. Then the movement should 'reflect' to down and left: deltaX = -1 (left), deltaY = +1 (down).

So, the answer to your question is: add logic that detects the object's collision point with the wall and vary the change in x/y-coordinates with each frame based on the rules you've established.

3. ## Re: Need help with Pong game

Ahh I see now. I got it working which is great! Thanks . I am feeling slightly overwhelmed with all the game stuff right now so I'm thinking of maybe sticking with core Java for a few more months, increasing my understanding and giving it another shot. I have a better understanding in Java due to the pong game which is great as well

4. ## Re: Need help with Pong game

I'm thinking of maybe sticking with core Java for a few more months, increasing my understanding and giving it another shot.
Excellent idea. Most don't recognize that their basic skills need to be sharpened before taking on the advanced stuff (why wouldn't they?), get discouraged, and quit. That you recognize it and are willing to invest the time to build a better foundation is a good thing. Be patient, work hard, and progress in an orderly, planned way.

5. ## Re: Need help with Pong game

Thanks for the encouragement ! I'm starting University in September to study Software Engineering and I'm doing an extra year to help my math skills which I feel is my downfall. Hopefully I'll be much better by the time I start