Welcome to Dream.In.Code
Become a Java Expert!

Join 149,506 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,335 people online right now. Registration is fast and FREE... Join Now!




My input is not working with my pong game.

 
Reply to this topicStart new topic

My input is not working with my pong game.

Plutonic
15 Jul, 2007 - 02:11 AM
Post #1

New D.I.C Head
*

Joined: 15 Jul, 2007
Posts: 9


My Contributions
Hi, I'm about 3/4 of the way through programming my first pong game. So far I've gotten the paddle at the top (controlled by the computer) to follow the ball around. The only thing I'm having trouble with is using input to control the paddle at the bottom. I've tried a variety of methods but so far none have worked.

What is currently happening is that the botom paddle which is supposed to eb controlled by me is moving to the left automatically.

My code is broken up into two bits. A class called "BouncyBall" and a class called "pong."

Bouncyballs code is as follows:

CODE


   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;
   import java.awt.geom.*;
   import java.applet.AudioClip;
   import java.net.*;
    
    //create the class

    public class BouncyBall extends JPanel implements Runnable, KeyListener {
    
   // Set up variables
    
      Image ball;  
      char key;
      AudioClip slap;
      boolean right = false;
      boolean left = false;
      int direction = 0;
      int ballHeight = 30;
      int ballWidth = 30;
      int xPosition = 10;
      int yPosition = 2;
      int xMove = 1;
      int yMove = 2;
      float p1xPosition = 200;
      float p1xMove = 0;
      float compxPosition = 400;
      float compxMove = 1;
      int height;
      Thread Ball;
      boolean lol = false;
  
   //Contructer method, sets up Thread called Ball and image called ball.
  
       public BouncyBall() {
         super();
         Toolkit kit = Toolkit.getDefaultToolkit();
         ball = kit.getImage("ball.gif");
         Ball = new Thread(this);
         Ball.start();
      }
       
       public void keyTyped (KeyEvent e) {
      
      }
       
       public void keyPressed (KeyEvent evt) {
         if (evt.getKeyCode() == KeyEvent.VK_LEFT)
            right = true;
         if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
            left = true;
      }
       
       public void keyReleased (KeyEvent evt) {
         if (evt.getKeyCode() == KeyEvent.VK_LEFT)
            left = false;
         if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
            right = false;
      
      }
          
   // Paints the necessary graphics for the program.
  
       public void paintComponent(Graphics comp) {
         Graphics2D comp2D = (Graphics2D) comp;
         comp2D.setColor(Color.white);
         comp2D.fillRect(0, 0, getSize().width, getSize().height);
         height = getSize().height - ballHeight;
         comp2D.setColor(Color.black);
         BasicStroke pen = new BasicStroke(12F);
         comp2D.setStroke(pen);
         Line2D.Float ln1 = new Line2D.Float(p1xPosition, (getSize().height - 20F), (p1xPosition + (getSize().width / 10F)), (getSize().height - 20F));
         comp2D.draw(ln1);
         Line2D.Float ln2 = new Line2D.Float(compxPosition, 20F, (compxPosition + (getSize().width / 10F)), 20F);
         comp2D.draw(ln2);
         if (yPosition == -1)
            yPosition = height - 20;
         if (ball != null) {
            comp2D.drawImage(ball,
               (int) xPosition,
               (int) yPosition,
               this);
         }
      
         if ( right = true )
            direction = 1;
         else {
            direction = 0; }
          
         if ( left = true ) {
            direction = 2;
         }
         else {
            direction = 0; }
           
         switch(direction) {
            case 1: p1xPosition += 1;
               break;
            case 2: p1xPosition -= 1;
               break;
         }
                  
          
      }
       
       //Thread creation
       
       public void run(){
         Thread thisThread = Thread.currentThread();
         while (Ball == thisThread) {
        
         //Ball movement
            xPosition += xMove;
            if (xPosition > (getSize().width - ballWidth)) {
                
               xMove *= -1;
            }
            if (xPosition < 1) {
                
               xMove *= -1;
            }
            yPosition += yMove;
            if(yPosition < 1) {
                
               yMove *= -1;
            }
                 
            if(yPosition > (getSize().height - ballHeight)) {
                
               yMove *= -1;
            }
                 
             //Computer Movement    
            compxPosition += compxMove;
            if (compxPosition < xPosition)
               compxMove = 1;
            if (compxPosition < 1)
               compxMove = 1;
                 
            if (compxPosition > xPosition)
               compxMove = -1;
            if ((compxPosition + (getSize().width / 10)) > (getSize().width))
               compxMove = -1;
                 
             //Tests to see whether the ball is touching the pads.
             
             
                 
             
             //Attempted player movement
             
             
                                
            p1xPosition += p1xMove;
            
            repaint();        //Repaints graphics
                 
            try {
               Thread.sleep(16);        //frame pause 16 miliseconds
            }
                catch (InterruptedException e) { }
         }
      }
   }



The code for pong is...

CODE

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class Pong extends JFrame {
    public Pong() {
        super("Bounce!");
        setSize(550, 450);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BouncyBall boing = new BouncyBall();
        Container pane = getContentPane();
        pane.add(boing);
          addKeyListener(boing);
        setContentPane(pane);
        setVisible(true);
          setFocusable(true);
    }

    public static void main(String[] arguments) {
        Pong frame = new Pong();
    }
}



User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: My Input Is Not Working With My Pong Game.
15 Jul, 2007 - 10:24 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, here is the working code, and let me tell you what was the problem.
The problem was in BouncyBall class
this part here (your code)
QUOTE
if ( right = true )
direction = 1;
else {
direction = 0; }

if ( left = true ) {
direction = 2;
}
else {
direction = 0; }

switch(direction) {
case 1: p1xPosition += 1;
break;
case 2: p1xPosition -= 1;
break;
}


And I changed it into this code(my code)
CODE
if ( right == true )
            direction = 2;
         else  if ( left == true ) {
            direction = 1;
        
            }
          
        
         else {
            direction = 0; }
          
         switch(direction) {
            case 1: p1xPosition += 1;
               break;
            case 2: p1xPosition -= 1;
               break;
         }
                  
          
      }

Check it out you'll see the difference right away.
This is the entire code:




CODE
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;
   import java.awt.geom.*;
   import java.applet.AudioClip;
   import java.net.*;
    
    //create the class

    public class BouncyBall extends JPanel implements Runnable, KeyListener {
    
   // Set up variables
    
      Image ball;  
      char key;
      AudioClip slap;
      boolean right = false;
      boolean left = false;
      int direction = 0;
      int ballHeight = 30;
      int ballWidth = 30;
      int xPosition = 10;
      int yPosition = 2;
      int xMove = 1;
      int yMove = 2;
      float p1xPosition = 200;
      float p1xMove = 0;
      float compxPosition = 400;
      float compxMove = 1;
      int height;
      Thread Ball;
      boolean lol = false;
  
   //Contructer method, sets up Thread called Ball and image called ball.
  
       public BouncyBall() {
         super();
         Toolkit kit = Toolkit.getDefaultToolkit();
         ball = kit.getImage("ball.gif");
         Ball = new Thread(this);
         Ball.start();
      }
      
       public void keyTyped (KeyEvent e) {
      
      }
      
       public void keyPressed (KeyEvent evt) {
         if (evt.getKeyCode() == KeyEvent.VK_LEFT)
            right = true;
         if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
            left = true;
      }
      
       public void keyReleased (KeyEvent evt) {
         if (evt.getKeyCode() == KeyEvent.VK_LEFT)
            left = false;
         if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
            right = false;
      
      }
          
   // Paints the necessary graphics for the program.
  
       public void paintComponent(Graphics comp) {
         Graphics2D comp2D = (Graphics2D) comp;
         comp2D.setColor(Color.white);
         comp2D.fillRect(0, 0, getSize().width, getSize().height);
         height = getSize().height - ballHeight;
         comp2D.setColor(Color.black);
         BasicStroke pen = new BasicStroke(12F);
         comp2D.setStroke(pen);
         Line2D.Float ln1 = new Line2D.Float(p1xPosition, (getSize().height - 20F), (p1xPosition + (getSize().width / 10F)), (getSize().height - 20F));
         comp2D.draw(ln1);
         Line2D.Float ln2 = new Line2D.Float(compxPosition, 20F, (compxPosition + (getSize().width / 10F)), 20F);
         comp2D.draw(ln2);
         if (yPosition == -1)
            yPosition = height - 20;
         if (ball != null) {
            comp2D.drawImage(ball,
               (int) xPosition,
               (int) yPosition,
               this);
         }
      
         if ( right == true )
            direction = 2;
         else  if ( left == true ) {
            direction = 1;
        
            }
          
        
         else {
            direction = 0; }
          
         switch(direction) {
            case 1: p1xPosition += 1;
               break;
            case 2: p1xPosition -= 1;
               break;
         }
                  
          
      }
      
       //Thread creation
      
       public void run(){
         Thread thisThread = Thread.currentThread();
         while (Ball == thisThread) {
        
         //Ball movement
            xPosition += xMove;
            if (xPosition > (getSize().width - ballWidth)) {
                
               xMove *= -1;
            }
            if (xPosition < 1) {
                
               xMove *= -1;
            }
            yPosition += yMove;
            if(yPosition < 1) {
                
               yMove *= -1;
            }
                
            if(yPosition > (getSize().height - ballHeight)) {
                
               yMove *= -1;
            }
                
             //Computer Movement    
            compxPosition += compxMove;
            if (compxPosition < xPosition)
               compxMove = 1;
            if (compxPosition < 1)
               compxMove = 1;
                
            if (compxPosition > xPosition)
               compxMove = -1;
            if ((compxPosition + (getSize().width / 10)) > (getSize().width))
               compxMove = -1;
                
             //Tests to see whether the ball is touching the pads.
            
            
                
            
             //Attempted player movement
            
            
                                
            p1xPosition += p1xMove;
            
            repaint();        //Repaints graphics
                
            try {
               Thread.sleep(16);        //frame pause 16 miliseconds
            }
                catch (InterruptedException e) { }
         }
      }
   }

User is offlineProfile CardPM
+Quote Post

ravensrock86
RE: My Input Is Not Working With My Pong Game.
15 Jul, 2007 - 03:05 PM
Post #3

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
Just curious, but where do you run this program?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: My Input Is Not Working With My Pong Game.
15 Jul, 2007 - 04:37 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I don't know if that question was for me but I'm gonna answer.
And that answer would be a question:
What do you mean by where do I run it?
User is offlineProfile CardPM
+Quote Post

Plutonic
RE: My Input Is Not Working With My Pong Game.
15 Jul, 2007 - 06:48 PM
Post #5

New D.I.C Head
*

Joined: 15 Jul, 2007
Posts: 9


My Contributions
Wow thanks alot! I've tried asking the same question in different forums but usually I'd get no reply.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: My Input Is Not Working With My Pong Game.
15 Jul, 2007 - 06:53 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
It was my pleasure that I can help a fellow member.
See ya around biggrin.gif
User is offlineProfile CardPM
+Quote Post

ravensrock86
RE: My Input Is Not Working With My Pong Game.
16 Jul, 2007 - 12:50 PM
Post #7

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(PennyBoki @ 15 Jul, 2007 - 05:37 PM) *

I don't know if that question was for me but I'm gonna answer.
And that answer would be a question:
What do you mean by where do I run it?


It was meant for anybody really. I mean like do you run it in Eclipse? Command Prompt? Something else?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: My Input Is Not Working With My Pong Game.
16 Jul, 2007 - 02:56 PM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(ravensrock86 @ 16 Jul, 2007 - 01:50 PM) *

QUOTE(PennyBoki @ 15 Jul, 2007 - 05:37 PM) *

I don't know if that question was for me but I'm gonna answer.
And that answer would be a question:
What do you mean by where do I run it?


It was meant for anybody really. I mean like do you run it in Eclipse? Command Prompt? Something else?



So you open the .java file where the main is, with the program you use for compiling and running in my case JCreator.
In this case Pong.java is the file where the main is.
So I click on the Run button in my JCreator and there you go.

Was this the answer you were looking for?
User is offlineProfile CardPM
+Quote Post

ravensrock86
RE: My Input Is Not Working With My Pong Game.
17 Jul, 2007 - 07:29 AM
Post #9

New D.I.C Head
*

Joined: 11 Jul, 2007
Posts: 21


My Contributions
QUOTE(PennyBoki @ 16 Jul, 2007 - 03:56 PM) *

QUOTE(ravensrock86 @ 16 Jul, 2007 - 01:50 PM) *

QUOTE(PennyBoki @ 15 Jul, 2007 - 05:37 PM) *

I don't know if that question was for me but I'm gonna answer.
And that answer would be a question:
What do you mean by where do I run it?


It was meant for anybody really. I mean like do you run it in Eclipse? Command Prompt? Something else?



So you open the .java file where the main is, with the program you use for compiling and running in my case JCreator.
In this case Pong.java is the file where the main is.
So I click on the Run button in my JCreator and there you go.

Was this the answer you were looking for?



Ya it was thanks. The program looked really cool, so I wanted to see it run.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: My Input Is Not Working With My Pong Game.
17 Jul, 2007 - 08:14 AM
Post #10

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
no prob.
User is offlineProfile CardPM
+Quote Post

battlecry78
RE: My Input Is Not Working With My Pong Game.
15 Feb, 2008 - 10:29 AM
Post #11

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 1

hi i am a 11 grade currently in a java programming class and i was going to run your code on jcreator and i got iot to work adn everything but a ball does not show up i am not very good at htis so i was wondering if you could help me with this?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month