Computer Science 120
Introduction to Programming

Spring 2012, Siena College

Lab 3: SkiBall
Due: the start of your next lab session

Some of you have probably played the carnival game SkeeBall. This is a game where you roll a ball down an alley and over a "skee-jump" so that the ball flies in the air for a few feet before landing in the target area. In the target area are several circular containers into which the ball can fall. Generally getting the ball into smaller containers results in more points being scored.

For this lab, you are to write a program that is somewhat simpler. We will call it "SkiBall" to reflect the greater simplicity and to avoid infringing on anyone's copyright.

You may work alone or with a partner on this lab.

The Approach

This lab is broken into two parts: one that implements the basic functionality of a playable SkiBall game, and one that adds some bells and whistles to make the game look nicer.

As with any programming task, you should approach the problem in a step-wise fashion. Begin by drawing the basic objects on the screen. Then, add bits of functionality to the program, one at a time, testing as you go.

Design Document

To get started, you will develop a written design for this program, similar to the one you developed for the laundry lab. Before you write anything down, read this entire lab description carefully. Then develop a design that includes:

Do not start coding before completing this design and having it approved by your instructor. It is often more productive to design a program away from the computer, and you should get into the habit of working in this way. If you arrive at the keyboard with a written idea of how to approach the problem, you will be able to make progress much more quickly than you would otherwise.

To help you get started, we have provided a starter BlueJ project with the class header and some method stubs. It should provide a default window size of 400x700, which is necessary for a good SkiBall experience.

If your program window appears at a different size, you can set the default canvas size in BlueJ's preferences under the "Extensions" tab in the "Objectdraw invoker" section.

Part 1: Basic SkiBall

A working solution for part 1 will appear below. Click inside the applet to interact with it.

The diameters of the target circles should be 25, 50, 100, and 150, while the center of the concentric circles should be at (200,200). The foul line should be drawn with a y-coordinate of 500. Again, the window should be 400 pixels wide and 700 pixels high.

The idea is for the player to shoot the ball into one of the target circles to earn points. The top small circle is worth 50 points. The small middle circle is worth 30 points. The circle around that is worth 20 points, while the biggest circle is worth 10 points. Obviously the player should get the highest score of all the circles it is contained in. Thus if the ball goes into the small middle circle then the score should be 30 points, even though it is also contained in the two circles containing that one. You can enforce this in your program by being careful about the order in which you check to see which circle(s) contain the center of the ball.

When the player presses the mouse button down somewhere behind (below, on the screen) the foul line, a ball is drawn on the foul line, centered at the same x coordinate as the mouse position (i.e., the ball is drawn directly above the current mouse position). A line is drawn from the center of the ball to the current mouse position. When the mouse is dragged, the end point of the line follows (the start remains attached to the ball, which does not move). You should think of the line as a slingshot or pool cue that will be used to determine in what direction and what distance the ball will move when released. Recall that Line objects have setStart and setEnd methods to help you accomplish this variant on dragging.

When the mouse is released, the ball is "shot" from its position on the foul line to its final resting place. This can happen in a single move or moveTo - there is no animation necessary. To determine the final resting place of the ball, compute the difference in the x coordinates between the start and end position of the line. Do the same for the y coordinates. The final position of the ball is determined by moving it 4 times as far in the opposite direction in each of the x and y coordinates. (Suggestion: define a named constant for that "4" so you could experiment with other "power levels" for the launch by changing just that one named constant definition).

For example, suppose the mouse is initially pressed at (300,650). This should place the ball on the foul line at (300,500). Then suppose that the mouse is at (325,600) when the button is released. The difference in the x-coordinates is 25, while the difference in y-coordinates is 100. The final resting place of the ball will then be 100 (= 4 * 25) units to the left of its starting position and 400 (= 4 * 100) units above its starting position. Thus its final coordinates will be (200,100), a point along the straight line showing on the screen when the ball is released. The score earned by a ball is determined by where its center is after the shot.

You may leave the balls on the screen after they are shot. Always indicate the score obtained from the last shot. No totals are required in part 1. You need not worry about handling the cases where someone shoots the ball off the court or backwards. Just place the ball where it lands based on the computation described above.

Note: you may find the translate method of the Location class to be helpful in several places in this program. For example, if you have a Location called ballPos that contains the coordinates of the upper left corner of a FilledOval called ball, you can change the coordinates stored in ballPos to be the center of the ball with the statement:

      ballPos.translate(ball.getWidth()/2, ball.getHeight()/2);

Part 2: Fancier SkiBall

Part 2 is worth just a small percentage of this lab's points, but it will make the game look nicer and be more playable.

A working solution for part 2 will appear below. Click inside the applet to interact with it.

Here are the enhancements you should make for part 2:

Extra Credit Opportunities

You should always focus first on getting the lab working, writing good comments, and using good style in your programming. But if you have done all that and would like to do more, here is an extra credit opportunity.

For two bonus points, if the player shoots the ball backwards or completely off the canvas, display a different (perhaps sarcastic) message.

Up to 3 additional bonus points are available for other advanced functionality of your choice.

Submitting Your Work

Before the start of your next lab session, submit your Java program for grading. There are three things you need to do to complete the submission: (i) place a copy of your Java program into your csis120/hw folder under hw5, (ii) print a copy of your program and hand it to your lab instructor, and (iii) demonstrate the execution of your program for your instructor.

Don't forget to check your programs for compliance with the Style Guide for CSIS 120 Programs


This lab is worth 100 points, which are distributed as follows:

> FeatureValueScore
Initial Design
Sufficient detail to convey the basic design of Part 1 8
Includes picture of layout with coordinates 4
Style, Design, and Efficiency
Appropriate comments 6
Good variable names 4
Appropriate variable declarations 6
Good use of constants 8
Appropriate formatting 2
Does not generate new objects unnecessarily 4
Good overall design and methods 6
Drawing initial playing board 12
Ball drawn on the line when the mouse is pressed 6
"Cue stick" line drawn and updated to aim ball 6
Ball is launched to correct location 7
Scores awarded and displayed properly for one shot 8
Enhanced scoreboard (part 2) 6
Game over after 10 shots (part 2) 4
Enhanced playing board (part 2) 3
Extra Credit
Different message for shooting off the canvas 2
Other advanced functionality 3
Total 100