Objective: You will write a Java graphics program that will play a game where the user scores points by dropping a ball and hitting a moving target.
1. Download the BallGame starter code and create a class in Bluej.
2. Implement the paint(Graphics g) method that will draw the game area.
a. Draw a rectangle (suggested size 40 wide by 10 high) near the bottom of the window. The rectangle will move, so the exact (x,y) location of the rectangle will be variables that will change over time. goalX will start at 0 but will change over time. goalY should be calculated to be (height of window 25 pixels). You can find the size of the window using the getWidth() and getHeight() methods. Remember that a user can change the size of the window while the program is running.
b. Draw a horizontal line across the window about 1/5 of the way down the window. To drop a ball, the user must click the mouse above the line.
c. Write the users score somewhere near the top of the window using the g.drawString method. See image.
3. To get the target to move.
a. Add the following method to your class. This method will be called 30 times each second and will do the animation by changing the x coordinate of the rectangle.
public void actionPerformed(ActionEvent e){
goalX = goalX + GOAL_SPEED;
if (goalX >= getWidth()){
goalX=-20;
}
repaint();
}
This code assumes that you have defined a class member variable named goalX that contains the x coordinate of the goal rectangle. And that you have a named constant GOAL_SPEED that is the number of pixels the goal rectangle will move every 1/30th of a second. When the goal rectangle reaches the right side of the window, it will reappear on the left side of the window. A class member variable is a variable that is declared inside the class but NOT inside any method. A class member variable can be referenced from any method in the class.
4. Run your program and make sure that the horizontal line, the score and the target rectangle appear. The rectangle should move across the screen, disappear when it gets to the right side of the window and reappear at the left side. Dont go to the next step until you have this working.
5. Implementing the ball drop.
a. Define a variable with name isBall of type boolean that will be used to indicate whether a ball exists (isBall is true). Define 2 integer variables for the X and Y coordinates of the ball. These 3 variables should be class member variables . Add a mouse method to your class as follows
public void mousePressed(MouseEvent e){
int mX = e.getX();
// x coordinate of mouse
int mY = e.getY();
// y coordinate of mouse
// write code that implements
// if mY is above the horizontal line
// then make isBall = true ,
// set ballX to mX and ballY to mY.
repaint();
}
b. Update the paint method so that if isBall is true, then draw a small filled circle at the location specified by the variables ballX and ballY.
c. Test your program. The target should move. When the user clicks below the line nothing happens. When the user clicks above the line a ball will appear but it will not drop.
d. To get the ball to drop, update the actionPerformed method to increment the y coordinate of the ball variable. Do this only if isBall is true. Check the y coordinate of the ball to see if the ball has reached the bottom of the window. If the ball has reached the bottom, then set isBall to false to make the ball disappear. Also check if the ball has hit the goal (the x coordinate of the ball is inside the rectangle) What Java condition expression using the x coordinate of the ball, the x coordinate of the goal and the width of the goal can perform this test? If the ball is inside the goal add points to the score, otherwise, no points are awarded.
6. Advanced features use your own ideas or any of the ideas listed below.
a. Use the up and down keys to change the difficulty of the game. You will have to implement the keyUp() and keyDown() methods in your class. To make the game easier, you can draw the horizontal line lower to the bottom of the window, you can increase the width of the rectangle, and you can make the rectangle move more slowly. Award points based on the degree of difficulty of the game.
b. When the goal rectangle gets to the edge of the window, have it reverse direction rather than disappear on the right and reappear on the left.
c. Use jpeg images rather than a rectangle and circle for goal and ball.
1. What is the purpose of the isBall flag variable?
2. What action does the user perform that causes isBall to become true?
3. When does isBall become false?
4. While the program is running, change the size of the window.
a. Does the goal stay near the bottom of the window?
b. Does the x position of the goal change when you resize the window?
5. What advanced feature (if any) did you implement in your program?
6. Copy and paste the code for your actionPerformed method here.