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: Paddle Ball Game

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Paddle Ball Game

    I'm having problems with my code for my CS1302 class.
    80 pts - Add the ability to keep track of points (number of times the ball bounces off the paddle, times the number of balls on the screen) and lives (start with 5 or more, subtract when a ball hits the bottom of the screen).

    90 pts - Add a "game over" popup when the lives are gone (and stop the animation/timer), ask the user if they want to play again, and reset the score/lives/balls if so.

    100 pts - Add 3 sliders to the Applet Panel (using the Window Editor): one for paddle size, one for ball size, and one for speed.

    I'm stuck at the 80 points(keeping track of points per ball on the screen).

    So far, here are my 2 codes.
    import javax.swing.JApplet;
    public class PongApplet extends JApplet {
     
    	public PongApplet() {
    		this.getContentPane().add(new DotsReboundPanel());
    	}
     
     
    }


    //********************************************************************
    //  DotsReboundPanel.java       Author: Dr. Payne
    //
    //  Represents the primary panel for the Dots program.
    //********************************************************************
     
    import java.util.ArrayList;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;	// get the timer functionality
     
    public class DotsReboundPanel extends JPanel
    {
       private final int SIZE = 20;  // radius of each dot
     
       private Timer timer;	// timer for our animation
     
       private final int DELAY = 20;	// 50 fps, 20 ms delay
     
       private ArrayList<Point> pointList;
     
       private int[] xvel = new int[1000], yvel = new int[1000]; // velocity x, y
     
       private Color[] col = new Color[1000];
     
       private int paddlex, paddley, paddlew, paddleh;
     
       private int score=0, lives = 5;
     
          //-----------------------------------------------------------------
          //  Constructor: Sets up this panel to listen for mouse events.
          //-----------------------------------------------------------------
          public DotsReboundPanel()
          {
             pointList = new ArrayList<Point>();
     
             // initialize my velocity vectors
             for (int i=0; i < 1000; i++)
             {
             	xvel[i] = (int) (Math.random()*10-5);// random velocity between
             	yvel[i] = (int) (Math.random()*10-5);
             	col[i] = new Color(
             		(int)(Math.random()*255),
             		(int)(Math.random()*255),
             		(int)(Math.random()*255) );
             }
     
             timer = new Timer (DELAY, new DotTimeListener()); // create timer
     
             addMouseListener (new DotsListener());
             addMouseMotionListener (new DotsListener());
             setBackground(Color.black);
             setPreferredSize (new Dimension(300, 200));
     
     
     
             timer.start();	// start timer
          }
     
          //-----------------------------------------------------------------
          //  Draws all of the dots stored in the list.
          //-----------------------------------------------------------------
          public void paintComponent (Graphics page)
          {
             super.paintComponent(page);
             //paddlex = getWidth()/2; // starting paddle position
             paddley = getHeight()-75; 
             paddlew = getWidth()/8;
             paddleh = getHeight()/10;
     
    		 int i=0;
             for (Point spot : pointList)
             {
           		page.setColor (col[i]);
                page.fillOval (spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);
                i++;
     
             }
     
     
    		 page.setColor( Color.red );
             page.drawString ("Score: " + score*i, 5, 15);         
             page.drawString ("Lives:" + lives, 60, 15);
     
     
     
            i=0;
     		for (Point spot:pointList)
     		{
     			if ((spot.x > paddlex - paddlew/2 - SIZE) && 
     				(spot.x < paddlex + paddlew/2 + SIZE) &&
     				(spot.y >= paddley - SIZE) && 
     				(spot.y < paddley + SIZE) && 
     				(yvel[i] > 0))
     			{
     				yvel[i] = -(Math.abs(yvel[i]));
     				// add # of points to score
     				score++;
     			}
     			i++;
     		}
     
             // draw a pong paddle
             page.setColor(Color.white);
             page.fillRect(paddlex-paddlew/2, paddley, paddlew, paddleh);
          }
     
       //*****************************************************************
       //  Represents the listener for mouse events.
       //*****************************************************************
       private class DotsListener implements MouseListener, MouseMotionListener
       {
          //--------------------------------------------------------------
          //  Adds the current point to the list of points and redraws
          //  the panel whenever the mouse button is pressed.
          //--------------------------------------------------------------
          public void mousePressed (MouseEvent event)
          {
             pointList.add(event.getPoint());
             repaint();
          }
     
          //--------------------------------------------------------------
          //  Provide empty definitions for unused event methods.
          //--------------------------------------------------------------
          public void mouseClicked (MouseEvent event) {}
          public void mouseReleased (MouseEvent event) {}
          public void mouseEntered (MouseEvent event) {}
          public void mouseExited (MouseEvent event) {}
     
    	@Override
    	public void mouseDragged(MouseEvent arg0) {
     
     
    	}
     
    	@Override
    	public void mouseMoved(MouseEvent event) {
     
     
    		// move the paddle to the x location of the mouse
    		paddlex = event.getX();
     
    		int i=0;
    		for (Point spot:pointList)
    		{
    			if ((spot.x > paddlex - paddlew/2 - SIZE) && 
    				(spot.x < paddlex + paddlew/2 + SIZE) &&
    				(spot.y >= paddley - SIZE) && 
    				(spot.y < paddley + SIZE) && 
    				(yvel[i] > 0))
    			{
    				yvel[i] = -(Math.abs(yvel[i]));
    				// add # of points to score
    				score++;
    			}
    			i++;
    		}
     
    		repaint();
     
    	}
     
     
       }
     
     
          //*****************************************************************
       //  Represents the action listener for the timer.
       //*****************************************************************
       private class DotTimeListener implements ActionListener
       {
          //--------------------------------------------------------------
          //  Move the dots in the pointList
          //--------------------------------------------------------------
          public void actionPerformed (ActionEvent event)
          {
          	int i=0;
    		for(Point spot : pointList)
    		{
    			spot.x += xvel[i];
    			spot.y += yvel[i];
     
    			// bounce my dots around
    			if ((spot.x+SIZE)>getWidth() || spot.x<0)
    				xvel[i] = -xvel[i];
    			if (spot.y<0)
    				yvel[i] = -yvel[i];
    			if (spot.y - SIZE >getHeight())
    			{
    				yvel[i] = -yvel[i];
    				// lose a life
    				lives--;
    			}
     
    			i++;
     
    		}
     
             repaint();
     
     
          }
       }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,819
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Paddle Ball Game

    (keeping track of points per ball on the screen).
    What variable is used to keep track of the number of balls?
    When and where is that variable's value changed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paddle Ball Game

    i figured that part out. All i had to do was scores
    score+= pointList.size();
    . Now i'm just having problems with the 90 points(Game Over Popup).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,819
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Paddle Ball Game

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Paddle Ball Game

    Our class figured out the popup window and I'm going to try to add the sound when I get home but if I can't figure it out I'll be back to the forums lol.

Similar Threads

  1. Pong--paddle collision algorithm help
    By sora628 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 5th, 2013, 09:55 PM
  2. Has the ball hit my paddle?
    By JakkyD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 3rd, 2012, 02:16 PM
  3. [SOLVED] Pong: Ricochet off Paddle Issue
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 07:57 AM
  4. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 04:35 PM
  5. breakout paddle algorithm
    By Brain_Child in forum Algorithms & Recursion
    Replies: 0
    Last Post: December 30th, 2009, 05:24 AM