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(); } } }