Results 1 to 5 of 5

Thread: Help with pong

  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Help with pong

    Hello every one I could really use some help with this assignment I have to create a pong game for class I have the paddles at least one paddle functioning correctly but I can not get the ball to show up any more and when I do it stays up in the corner.I need the ball to bounce off the wall.Well I am sure you guys know pong works.I am not looking for anyone to write my code for me I known you guys probly always get people wanting you write there programs for them I have a copy of the game written by some one else but I dont want to look at it I need to be abe to do this my self I just need a few pointers or some one to point out my mistakes thank alot.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    /**
     * Created by IntelliJ IDEA.
     * User: root
     * Date: 7/5/12
     * Time: 9:12 AM
     * To change this template use File | Settings | File Templates.
     */
    public class Board extends JFrame {
      JLabel paddle1 = new JLabel();
        JLabel paddle2 = new JLabel();
        JPanel panel1 = new JPanel();
        Ball ball = new Ball();
        Timer timer = new Timer(20,new timerEventHandler());
        int paddle1Y = 240;
        int paddle2Y = 250;
        int xPoint =0;
        int yPoint =0;
        Board()
        {
            timer.start();
            setSize(800, 600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
            setLayout(null);
            paddle1.setOpaque(true);
            paddle2.setOpaque(true);
            paddle1.setBackground(Color.RED);
            paddle2.setBackground(Color.BLUE);
            panel1.setLayout(null);
            panel1.setBounds(50,50,400,400);
            panel1.add(paddle2);
            panel1.add(paddle1);
            paddle1.setBounds(770,240,10,50);
            paddle2.setBounds(20,240,10,50);
            this.add(panel1);
            panel1.add(ball);
            panel1.setBounds(0,0,800,600);
            panel1.setBackground(Color.BLACK);
            addKeyListener(new keyEventHandler());
    
        }
    
       class timerEventHandler implements ActionListener
       {
            public void actionPerformed(ActionEvent actionEvent)
            {
                if(paddle2.getY() < paddle1.getY())
                    paddle2Y = paddle2Y + 10;
                else if(paddle2.getY() > paddle1.getY())
                    paddle2Y = paddle2Y - 10;
            
                paddle2.setBounds(paddle2.getX(),paddle2Y,paddle2.getWidth(),paddle2.getHeight());
    
                if (ballHitsWall())
                {
                    ball.setBounds(ball.getX()- panel1.getHeight(),ball.getY(),15,15);   //update ball position
                    repaint();
                }
                if(ballHitsPaddle())
                {
                    ball.setBounds(ball.getX()+ball.getXSpeed(),ball.getY()+ball.getYSpeed(),15,15);   //update ball position
                    repaint();
                }
            }
           public boolean ballHitsPaddle()
           {
               if(ball.getX() == paddle1.getX())
               return true;
    
               if (ball.getX() == paddle2.getX())
                   return true;
    
               else
                   return false;
           }
           public boolean ballHitsWall()
           {
               if(ball.getX() >= panel1.getHeight())
                   return true;
    
               else if (ball.getX() <= panel1.getHeight())
                   return true;
    
               else
                   return false;
           }
       }
       class keyEventHandler implements KeyListener
       {
    
           @Override
           public void keyTyped(KeyEvent keyEvent) 
           {
    
           }
           @Override
           public void keyPressed(KeyEvent keyEvent) 
           {
               if(keyEvent.getKeyCode()== 38){
                   paddle1Y = paddle1Y - 10;
    
               }
               if(keyEvent.getKeyCode()== 40) {
                   paddle1Y = paddle1Y + 10;
               }
                   paddle1.setBounds(paddle1.getX(),paddle1Y,paddle1.getWidth(),paddle1.getHeight());
                   repaint();
    
           }
           @Override
           public void keyReleased(KeyEvent keyEvent) 
           {
               
           }
       }
    }
     import javax.swing.*;
     import java.awt.*;
     import java.util.*;
     /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/12/12
     * Time: 1:18 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Ball extends JPanel{
        private Color  _color;
        private int _radius;
        private int[] speed = new int[2];
         
        private double _direction; // number of degrees from positive x-axis counterclockwise;
        public Ball()
        {
            _color = Color.yellow;
            _radius = 5;
            _direction = Math.random() * 360;
            this.setSize(getRadius() * 2,getRadius()*2);
    
        }
        public Ball(Color color, int radius, double speed, double direction, int xPoint, int yPoint)
        {
            _color = color;
            _radius = radius;
            _direction = direction;
        }
        private Color getColor()
        {
           
            return  Color.WHITE;
        }
        public int getXSpeed()
        {
            return speed[0];
        }
        public  int getYSpeed()
        {
            return speed[1];
        }
        public int getRadius()
        {
            return _radius;
        }
        public double getDirection()
        {
            return _direction;
        }
        public void NewSpeed()
        {
            speed[0] = (int)(Math.random()*15 +5);
            speed[1] = (int)(Math.random()*15 +5);
            if((Math.random() < 0.5))
              speed[0] = -speed[0];
            if((Math.random() < 0.5))
                speed[1] = -speed[1];
        }
        protected void paintComponent(Graphics graphics)
        {
            graphics.setColor(getColor());
            graphics.fillOval(0, 0, getRadius() * 2, getRadius() * 2);
            graphics.setColor(Color.BLACK);
            graphics.drawOval(0, 0, getRadius()*2, getRadius()*2);
        }
     }
    import javax.swing.*;
    import java.awt.*;
    /**
     * Created by IntelliJ IDEA.
     * User: root
     * Date: 7/5/12
     * Time: 9:12 AM
     * To change this template use File | Settings | File Templates.
     */
    public class Application {
        public static void main(String[] args)
        {
            Board board = new Board();
        }
    }
    Last edited by aortell24; 07-05-2012 at 11:31 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with pong

    I need the ball to bounce off the wall.
    Just glanced at the code, but I don't see anywhere that the speed of the ball is set or adjusted. "Bouncing" - off either the wall or a paddle - is just the velocity of the ball changing.

    -----

    The code should be cleaned up so that standard Java coding conventions are followed (begin classes with an UpperCaseLetter and variables and methods with aLowerCaseLetter.) And remove stuff that isn't being used - like the method that randomises the ball's speed - until there is a use for it.

    One approach might be to give the ball a method (or 4) that effect a bounce: ie update the ball's velocity given a collision with a vertical or horizontal thing (wall or paddle) from left or right. Call this method when a collision is detected, and use the ball's speed to update its position.

    [Edit] I see that a ball's speed is a property of the Ball class, but its position is not. Personally I would put both these "kinematic" properties in the Ball class.
    Last edited by pbrockway2; 07-06-2012 at 12:17 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Help with pong

    Moved from Advanced Java.

    If you think your homework is 'advanced' you're terribly misinformed about the complexity of programming.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Help with pong

    I apologize I am still very new to this.

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Help with pong

    Here is what I have come up with for the ball hits the wall and ball hits the paddle as far as the bounce goes should I return the points of the ball's current postion ?This is what I am confused about.Thanks.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    /**
     * Created by IntelliJ IDEA.
     * User: root
     * Date: 7/5/12
     * Time: 9:12 AM
     * To change this template use File | Settings | File Templates.
     */
    public class Board extends JFrame {
      JLabel paddle1 = new JLabel();
        JLabel paddle2 = new JLabel();
        JPanel panel1 = new JPanel();
        Ball ball = new Ball();
        Timer timer = new Timer(20,new timerEventHandler());
        int paddle1Y = 240;
        int paddle2Y = 250;
        int xPoint =0;
        int yPoint =0;
        Board()
        {
            timer.start();
            setSize(800, 600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
            setLayout(null);
            paddle1.setOpaque(true);
            paddle2.setOpaque(true);
            paddle1.setBackground(Color.RED);
            paddle2.setBackground(Color.BLUE);
            panel1.setLayout(null);
            panel1.setBounds(50,50,400,400);
            panel1.add(paddle2);
            panel1.add(paddle1);
            paddle1.setBounds(770,240,10,50);
            paddle2.setBounds(20,240,10,50);
            this.add(panel1);
            panel1.add(ball);
            panel1.setBounds(0,0,800,600);
            panel1.setBackground(Color.BLACK);
            addKeyListener(new keyEventHandler());
    
        }
        public boolean ballHitsWall()
        {
            if(ball.getY() <= panel1.getHeight())
                return true;
    
            else if (ball.getY() >= panel1.getY())
                return true;
            else
                return false;
        }
        public boolean ballHitsPaddle1()
        {
            if(ball.getX() == paddle1.getX())
                return true;
            else if(ball.getY() == paddle1.getY())
                return true;
            else
                return false;
        }
         public boolean ballHitsPaddle2()
        {
            if(ball.getX() == paddle2.getX())
                return true;
            else if(ball.getY() == paddle2.getY())
                return true;
            else
                return false;
        }
        class timerEventHandler implements ActionListener
       {
            public void actionPerformed(ActionEvent actionEvent)
            {
                if(paddle2.getY() < paddle1.getY())
                    paddle2Y = paddle2Y + 10;
                else if(paddle2.getY() > paddle1.getY())
                    paddle2Y = paddle2Y - 10;
            
                paddle2.setBounds(paddle2.getX(),paddle2Y,paddle2.getWidth(),paddle2.getHeight());
    
                if(ballHitsPaddle1())
                {    
                    ball.NewSpeed();
                }   
                ball.setBounds(ball.getX()+ball.getXSpeed(),ball.getY()+ball.getYSpeed(),15,15);   //update ball position
                repaint();
    
            }
    
       }
       class keyEventHandler implements KeyListener
       {
    
           @Override
           public void keyTyped(KeyEvent keyEvent) 
           {
    
           }
           @Override
           public void keyPressed(KeyEvent keyEvent) 
           {
               if(keyEvent.getKeyCode()== 38){
                   paddle1Y = paddle1Y - 10;
    
               }
               if(keyEvent.getKeyCode()== 40) {
                   paddle1Y = paddle1Y + 10;
               }
                   paddle1.setBounds(paddle1.getX(),paddle1Y,paddle1.getWidth(),paddle1.getHeight());
                   repaint();
    
           }
           @Override
           public void keyReleased(KeyEvent keyEvent) 
           {
               
           }
           
       }
    }

Similar Threads

  1. Pong help
    By jsobel in forum New To Java
    Replies: 3
    Last Post: 04-03-2012, 03:33 PM
  2. And another pong problem!
    By ResidentBiscuit in forum New To Java
    Replies: 1
    Last Post: 11-18-2011, 03:07 AM
  3. ping pong
    By ahmed_ijsh in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-01-2010, 12:56 PM
  4. ping pong
    By ahmed_ijsh in forum Networking
    Replies: 0
    Last Post: 10-31-2010, 03:11 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •