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 7 of 7

Thread: Java n00b

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java n00b

    Hey,

    I'm trying to learn java. I've made a simple applet, kind of pongish. I have two problems with it. One is manual control of the paddles. Trying to do keylisteners makes my brain hurt. The second problem is with the paddles. If I comment out the movement of the paddles the balls 'bounce' off them just fine. But when i reimplement the movement the balls miss the top and bottom of the paddles. I'll attach the .java and .html files. Test is the html, ballapp is the .java. The extensions had to be changed to attach them.

    Please don't make fun.
    Attached Files Attached Files


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java n00b

    Ok, here is a good way to use keylisteners in a game:

    Say you wanted W to move the left paddle up, and S to move it down. So, every time the keypressed method is executed, we would want to compare to VK_W and VK_S (static identifiers with KeyEvent). However, if you only allowed the paddles to move whenenver keyPressed is called, you'll notice that the paddle will move a bit, stop, and then re-start moving, possibly going faster. This is due to keyboard delay and acceleration from the OS. To avoid this, we'll set a flag every time a key is pressed, and reset it when the key is released.
    boolean lpmoveup = false;
    boolean lpmovedown = false;
    public void keyPressed (KeyEvent e)
        {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_W)
            {
                lpmoveup = true;
            }
            else if (key == KeyEvent.VK_S)
            {
                lpmovedown = true;
            }
        }
     
        public void lpMove ()
        {
     
            if (lpposy <= ubound && lpmoveup)
            {
                return;
            }
            if (lpposy >= bbound && lpmovedown)
            {
                return;
            }
            if (lpmoveup)
            {
                lpposy--;
            }
            if (lpmovedown)
            {
                lpposy++;
            }
            this.repaint();
        }
     
        public void keyReleased (KeyEvent e)
        {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_W)
            {
                lpmoveup = false;
            }
            else if (key == KeyEvent.VK_S)
            {
                lpmovedown = false;
            }
        }
    All you'll need after this is to call lpmove if lpmovedown or lpmoveup is true in your run method.

    I couldn't figure out why the top of the paddle wasn't detecting the collision correctly, could you explain what your collision detection code is doing, and where it is located?

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Campos (July 22nd, 2009)

  4. #3
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java n00b

    Thanks for the reply.

    See all those If statements? Thats where I did the collision detection.
    if (posx == lpredge)
         {
              if (posy >= lpuedge)
                      {
    	   if (posy <= lpbedge)
    	           {
    		ohspeed = -ohspeed;
    	            }
    	}
    }
    This is for detecting the right edge of the left paddle. In here the posx is the x coord of the ball, if its equal to the left paddle right edge it goes to the next if which checks to see if the y coord of the ball is >= the upper coord of the left paddle and then goes to the next if which checks to see if the y coord of the ball is <= the bottom edge of the left paddle. If the x coord of the ball is equal to the x coord of the paddle AND is between the top coord of the paddle and the bottom coord of the paddle, it reverses horizontal direction.
    All of the sides use the same concept. if the Y coord of the ball is equal to the top of the paddle and the x coord of the ball is between the x coords of the outsides of the paddle then it reverses vertical direction.

    Now to try and figure out how to implement the code you posted into this thing.

  5. #4
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java n00b

    Figured out the Top/Bottom collision problem. The ball and the paddle were moving vertically at the same speed, so once they met, the ball would reverse direction, but since the speed was the same the next draw they would still be equal, so the vertical speed would reverse again propelling the ball into the paddle making the y coords of the ball not equal to the y coords of the paddle so it wouldn't reverse again. whew. Had to increase the thread sleep rate to 300 and do drawStrings of the coords of the paddles and balls in order to see what was happening.

    Would like to come up with a way to slow/stop the paddle or increase the ball speed for a few cycles then return everything to normal until the next collision on the top/bottom of the paddles.

  6. #5
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java n00b

    To HelloWorld922

    I added the rest of the code you posted in what i feel is the correct spot. To call lpmove() from the run method i used the following:
    if (lpmovedown = true || lpmoveup == true)
         {
              lpmove();
         }

    But when i try to compile i get cannot find symol, method lpmove(). I'm guessing i'm not calling the method correctly. If you can help me out that'd be awesome. Also can you explain the part of the lpmove code:
    if (lpposy <= ubound && lpmoveup)
    					{
    						return;
    					}
    				if (lpposy >= bbound && lpmovedown)
    					{
    						return;

    I think i understand enough of the rest of it.

    Thanks

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java n00b

    it's lpMove (capital M)
    That is the correct code, and you did put it in the right place. The second fragment is to not move the paddle if it's at one of the bounds (don't move up at the top bound, don't move down at the lower bound)

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    Campos (July 22nd, 2009)

  9. #7
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java n00b

    Can't believe i missed the capital, thats like the first thing they tell you in big bold letters