Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

Lab 3: Ski Ball
Due: 11:59 PM, Monday, February 9, 2015

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. Only one submission per group is needed.

There are a number of lab questions and a practice program in addition to the SkiBall programming assignment in this lab. Please refer to the "Submission Guidelines" on the course home page and syllabus for the requirements for each of these items. You are strongly encouraged to complete the programs and questions in the order in which they appear in this document.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab3" (no spaces) for the project.

Create a document where you will record your answers to the lab questions. If you use plain text, call it "lab3.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab3.pdf" before you submit it.

Working solutions to all programs can be downloaded here.

Practice Questions and Programs

Start by taking another look at the ColorfulSunset example.

Question 1: What color is the sun when it starts, and what color is it when the color stops changing? Explain briefly. (2 points)

We see here that in addition to using the predefined color constants from the java.awt.Color class, using fixed custom colors (as in the Basketball2 example), and using completely random colors (as in the MoreColorfulSpirograph and elsewhere), we can compute colors.

Practice Program: As an example of this, write a program GreyscaleMouseDroppings that works much like the MouseDroppings example. It should still draw 10×10 circles each time a mouse move event is handled, but each circle drawn should be centered on the mouse location rather than having its upper left corner at the mouse location, and they should be drawn in shades of grey, with brighter shades as the mouse is moved further from the origin at the upper left. (6 points)

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



Get the centering done first - that shouldn't be too hard.

Picking the greyscale colors might be less obvious, but is also not too bad. The greyscale colors are those whose red, green, and blue components are all equal. Black is (0,0,0) and white is (255,255,255), and other shades are the values in between. We'll use the distance from the origin. You can use either the standard Euclidean distance formula (square root of the difference in x, squared, plus the difference in y, squared), or the Chebyshev distance (the larger of the distance in x and the distance in y).

Tips to help you out:

Question 2: How would you change your program so circle at the origin would be white, while those at a distance of over 255 are black, with appropriate shading in between? Do not change your program, just describe how you would do it. (2 points)

SkiBall

Now, it's on to SkiBall. Check out the game below or by downloading the compiled solution project.

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



As you can see, the basic idea is that you launch balls into the target area by dragging the ball below the foul line. The ball's trajectory is determined by where you drag it before releasing. Points are awarded for having the ball "land" in one of the targets.

Design Questions

To get started, you will answer a series of questions that will constitute a written design for this program. You should read the remainder of this document before completing these questions.

Question 3: Create a labeled drawing of the layout for your graphical objects on the screen, with all important locations and sizes labeled. Include a list of named constants you will define and their values. Your objects need not be exactly the same size as the provided sample solution, but the basic structure should be similar. You should assume a canvas size of 400 ×800. (5 points)

Question 4: List the methods you will need to provide to support the necessary functionality. (2 points)

Question 5: List the instance variables you will need to keep track of graphical objects and other information. (3 points)

Question 6: Give an English language or "pseudeocode" description of what each method does. If you do this well, these will become many of the comments in your methods. (8 points)

Do not start coding before completing this design. You are encouraged to send your responses to these in by email for approval before you begin coding. 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.

Detailed Requirements

Enforce an appropriate canvas size in your program by including the line

  setSize(400, 800);

(or better yet, the same line but with appropriate named constants for the numbers) as the first line in your begin method.

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 "sling" 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 (and remains attached to the center of the ball). You should think of the line as a slingshot 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" to its final resting place. This can happen in a single move or moveTo - there is no animation necessary (though it would be fun to add once we've talked about that). 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) and your foul line's y-coordinate is 500. This should place the start end of the launcher 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 the mouse release position and 400 (= 4 * 100) units above its that position. Thus its final coordinates will be (225,200), 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. 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.

Limit the number of shots in a game to 10.

Your game's scoreboard should include three labels. The first indicates the score from the last ball (but should read "Press and drag below the line to play!" or something similar before the first shot). The second should be the running score. The third indicates the number of shots remaining (but should read "Game over" after the last shot). Select a larger font and be sure to center your labels on the canvas (and keep them centered as the text within changes).

Finally, make your game more colorful:

Tips and Hints

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, up to a total of 8 points.

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

Additional bonus points are available for other advanced functionality of your choice. Possibilities include adding more color, generating some random messages, and creating a more interesting playing area (perhaps some small 100-point targets, or a bonus for consecutive balls hitting certain targets). Come up with your other ideas!

Submitting

Before 11:59 PM, Monday, February 9, 2015, submit your lab for grading. There are three things you need to do to complete the submission: (i) Copy your file with the answers to the lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab3". (iii) Demonstrate the execution of your programs for your instructor. (2 business day grace period for demos).

Grading

This assignment is worth 80 points, which are distributed as follows:

> FeatureValueScore
Lab Question 1 2
GreyscaleMouseDroppings centering 2
GreyscaleMouseDroppings colors 4
Lab Question 2 2
SkiBall Design
Lab Question 3 (drawing) 5
Lab Question 4 (method list) 2
Lab Question 5 (instance variables) 3
Lab Question 6 (method psuedocode) 8
SkiBall Correctness
basic graphical layout 6
sling line drawn correctly on press 3
sling line follows mouse during dragging 3
ball stays centered on mouse point during dragging 3
sling line and ball drawn only when press below foul line 2
ball launched to correct position 4
scores awarded properly for one shot 4
scoreboard message correctness and centering 4
game ends after 10 shots 2
red shading during dragging for launch 2
target area colors and balls that land colored with darker shade 3
SkiBall Style
Appropriate comments 4
Good variable names 2
Appropriate variable declarations 3
Good use of constants 4
Appropriate formatting 1
Does not generate new objects unnecessarily 2
SkiBall Extra Credit (up to 8 total)
Different messages for shooting off canvas or backwards 2

Total

80