Computer Science 120
Introduction to Programming

Spring 2011, Siena College

Lab 2: 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 copyright problems.

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

The Approach

Once again, 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 last week's lab, 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.

Lab Preparation

To make our limited lab time as productive as possible, please do the following before coming to your lab meeting:

  1. Read the lab description carefully.
  2. Prepare a written design for your program, similar to the one you developed for the laundry lab.

    As part of the design, you should also draw the layout for your graphical objects on the screen.

  3. Do not start coding before lab. 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 come to lab 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.

Part 1: Basic SkiBall

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



The idea is for the player to shoot the ball into one of the circles to obtain a score. 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.

When the player presses the mouse button down somewhere behind 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.

When the mouse is released, the ball moves from the foul line to its final resting place in a single move - there is no animation necessary. To determine the final resting place of the ball, measure 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. (Hint: define a named constant for that "4" so you could experiment with other "power levels" for the launch).

For example, suppose the ball is sitting on the foul line at (300,500) and the mouse is at (325,600) when the mouse 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.

The diameters of the 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.

Part 2: Fancier SkiBall

Part 2 is worth just a few 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 one bonus point, if the player shoots the ball backwards or completely off the canvas, display a different (perhaps sarcastic) message.

A second bonus point is available for other advanced functionality of your choice.

You should also feel free to extend or embellish your programs, but always be sure to have the basic assignment working properly beforehand.

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 hw3, (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.