SWE 2410, Lab 6: Observing Coconuts

In this lab and the next you will implement a simple game in which coconuts fall on a beach and a crab shoots them with its laser eyes. You will use the Observer Pattern in your implementation. This is a group lab.

The crab sits on the beach and moves right and left. Coconuts are introduced at the top and fall downwards. The intended rules for the game are that the crab would shoot lasers straight up at coconuts, destroying them. The laser beam should move slowly enough the user can see it move and the crab can move to the side to escape being hit if the laser misses. You might add a random probability for the laser destroying the coconut. The number of coconuts destroyed by a laser beam are counted, as are the number of coconuts reaching the beach. If a coconut hits the crab, no more coconuts are generated. The game ends after the crab is destroyed and all remaining coconuts have hit the beach. The game starts when the user presses the space bar. If the game is already running, pressing the space bar pauses it, and then pressing it again resumes game play. The up arrow key must fire the laser, but you can add actions for other keys as well.

You are being given an initial implementation of the game, either through GitHub Classroom or coconuts.zip. You may change most of the elements you are given, but preserve the following:

There are likely other things you need to know about the provided code; ask!

Design

Additional Elements

Your instructor may provide small amounts of extra credit for additional features. See Canvas.

There is a coconut tree picture in the images folder. Feel free to add it to the game screen along with other elements. Important: keep the images in the images folder to minimize problems for your instructor when they try to run your code.

Getting Started

Start by getting the provided code to run. You will probably need to add a run configuration:

  1. Open the Run menu and select *Edit Configurations…

  2. Click on *Add new run configuration… and select Application

  3. Click on the browse button at the far right of the box titled Main class, wait a second for options to update, and select Main of coconuts. This will fill in coconuts.Main for the build and run entry.

  4. Click on Modify Options and select Build VM Options

  5. In the VM Options box enter

        --module-path "C:\Program Files\Java\javafx-sdk-24\lib" --add-modules=javafx.controls,javafx.fxml
    

    You may need to update the path to the FX library to match your machine.

  6. Build and run.

Review the code and note how GameController sets up a Timeline object that calls theGame.advanceOneTick() after moving the coconuts. Review both tryDropCoconut and advanceOneTick in OhCoconutsGameManager. Note how objects are removed from the game and how the game ends. Then read the documentation for HitEvent and HittableIslandObject. This should give you enough information to see how crabs and coconuts move. Possible next steps include adding code to terminate the game when the crab is hit, then removing coconuts when they are hit by the laser.

Once you have objects moving around the screen, introduce the score board. This contains a subtle element: changing the score at run time typically introduces Java exceptions. This is because of the fact that a Java GUI is “multi-threaded”: multiple pieces of code are executing at the same time. The fix is to do the same as for the WeatherStation example introduced in class: if method xxx in a class needs to update the text on the screen, call it by

    Platform.runLater(this::xxx);

See TemperatureDisplay in the WeatherStation code.

Submission

You will have two weeks to work on this lab. Your instructor may specify deliverables in the first week. In the second week, you will need to submit a Minimal Solution Diagram showing how you applied the pattern, discussion about your experiences with the pattern, and screen shots showing your game in various stages. As for previous labs, do not use a reverse-engineer tool to create the diagram; it must be drawn by hand using Enterprise Architect.

To help you get started with the minimal solution diagram, we have created the EA file island.qea. You may modify this file however you like, including moving classes, changing assocations and generalizations, adding new classes, and removing classes you do not need. Consistent with the requirements for mimimal solution diagrams, the diagram properties have been set so type information is not shown.

Frequent Problems and Questions