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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with Pong game

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.
    Last edited by SauronWatchesYou; April 16th, 2014 at 04:56 AM. Reason: Spellings :P


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default 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. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default 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. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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

Similar Threads

  1. Ping Pong Game Problem with ball Drop and Catch
    By zille in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 26th, 2014, 06:55 AM
  2. How to defeat computer in a Java game? (Pong)
    By DJBENZ10 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 13th, 2012, 01:00 PM
  3. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04:11 PM
  4. [SOLVED] Pong: Ricochet off Paddle Issue
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:57 AM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM