Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

I have to make a game in java where I have to make a ball bouncing game and I have no clue at all what I'm doing. Here is the source code so far: /** * An animated game where user drops a ball and * tries to hit a moving target. * @author your name * @version */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.imageio.*; import java.io.*; public class BallGame extends Canvas implements MouseListener, ActionListener { final int BALL_SPEED = 10; // speed of ball and goal final int GOAL_SPEED = 1; final int GOAL_WIDTH = 60; // size of rectangle for goal int ballX; // x,y location of ball int ballY; boolean isBall = false; int goalX = 0; // position of goal rectangle int score = 0; // user's score //static Image submarine; // option to use jpeg image for goal static Image submarine; public static void main(String[] args) { // try { // submarine = ImageIO.read(new File("submarine.jpg")); // } catch (Exception e) { // System.out.println("unable to read file"); // System.exit(0); // terminate program // } try{ submarine = ImageIO.read(new File("submarine.jpg")); } catch (Exception e) { System.out.println("unable to read file"); System.exit(0); } JFrame window = new JFrame(); window.setSize( 600, 600); window.setTitle("Drop Ball Game"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BallGame canvas = new BallGame(); window.add(canvas); canvas.addMouseListener(canvas); // create timer event 30 times per second Timer t = new Timer(1000/30, canvas); t.start(); window.setVisible(true); } public void paint(Graphics g){ // clear window g.clearRect(0, 0, getWidth(), getHeight()); // draw the goal // g.drawRect( ... ); g.drawRect(0,0, getWidth()/40, getHeight()/10); // draw the line for the drop zone g.drawLine(0, getHeight()/5, getWidth(), getHeight()/5); // if there is a ball draw it // if (isBall) ..... // display score String scoreString = Integer.toString(score); g.drawString("Score: "+scoreString, 40, 20); } /** * This method is called by the timer and is responsible * for updating position of ball and moving target * @param ActionEvent is not used by this game */ public void actionPerformed(ActionEvent e){ // this method does the animation. // change the (x,y) position of the target goal // if goal is at right side, then reset to left side of window // change the (x,y) position of the ball if there is a ball // did ball hit the target? // if yes, update score, reset the target // if ball is at bottom of window, then remove the ball // by assigning false to isBall. repaint(); } /** * mouse method called when user presses mouse button. * If mouse is in window above the line, and there is no * ball, create a ball object positioned at location of mouse. * @param MouseEvent contains the (x,y) location of mouse */ public void mousePressed(MouseEvent e){ int mx = e.getX(); // x coordinate of mouse int my = e.getY(); // y coordinate of mouse // if y position of mouse is above the line, and isBall // is false, then create a ball. Otherwise do nothing. repaint(); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} }

OpenStudy (rsmith6559):

Start by thinking about what you need to do to accomplish this. What objects are involved? What high level, broad things need to be done to do this? Break those down into smaller steps and continue breaking those steps down until they get to where the code kind of jumps out at you.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!