CP1300 Assignment 2 – Pong!
Pong is one of the first computer games ever created back in 1972. We are making a customized version of Pong – not exactly the same as the original game-play. You are expected to make your implementation of Pong look and behave almost exactly like what is described here. You are free to define your own parameters for things like colours, fonts, the size and shape of the paddles, and the size of the ball. Note that the window should not be resizable!
Below is the initial state of our version of Pong. We see how our high scores are incorporated into the overall program. When the program is first executed, any previously saved high scores are loaded from a data file. When the program window is closed, the current list of high scores overwrites the high scores in the data file.
Next, if the “start match” button is pressed and the player names are not entered, then we should see a modal popup that indicates user error, something like:
But your version is expected to make this popup indicate which player name(s) need to be entered. The player names are non-empty strings of letters only. Putting a single space between letters is acceptable. The maximum length of a player name is 20 characters.
Once the player names are entered and the “start match” button is pressed, then the player names cannot be changed again until the “end match” button is pressed (i.e. the name fields are disabled). Notice also that the “start match” button becomes disabled until the “end match” button is pressed:
The game-play continues as follows:
• Pressing the space-bar starts the ball moving, it is placed in the centre of the window with a randomly chosen speed and direction
• The ball speed is independent in both directions!
• The left-side paddle can be controlled by pressing ‘q’ for moving up and ‘a’ for moving down
• The right-side paddle can be controlled by pressing ‘p’ for moving up and ‘l’ for moving down
• The paddles can be moved regardless if the ball is moving or not (while a match is active)
• When the ball hits the top or bottom edge of the window the ball’s direction is adjusted appropriately so that the ball remains completely within the bounds of the window and appears to “bounce” off the edge of the window
• If the ball reaches the left edge or right edge then it stops and the appropriate player’s score is incremented by one
• If the ball hits a paddle, then the ball “bounces” off that paddle such that the horizontal direction reverses and the ball’s speed has randomly changed
When the “end match” button is pressed, the player with the higher score has their score updated in the high scores table – as defined by the behaviour coded in Assignment 1. Notice that the high scores are now sorted into descending order of value. Also notice that the “start match” button and the player name fields become enabled, and the “end match” button becomes disabled, and that the Scores are reset to zero, but the player names are not cleared:
There are a number of other characteristics that you must incorporate into your program:
• Create a “test” package and a “software” package and use them appropriately
• Incorporate the source code for the Score class and HighScore class into your Netbeans project
• Adjust the Score class and HighScore class so that the methods don’t return true/false values, but rather they generate ScoreException objects and HighScoreException objects
• The Visual Shapes (Ball, Paddle, ScoreDisplay, …) should be derived types of Shape
o The Shape class
Must be an abstract class
It records speed, width, height, 2D position, and direction information
It defines an abstract method called draw(Graphics)
o You should adapt the code for Ball code from Assignment 1 for this purpose!
• The game-play must be controlled by a Field object (a modified version of Assignment 1 code)
o The field has a fixed size of width = 600 and height = 400
o The Field implements a frame-by-frame animation for game-play and display updates
o The Field reacts to user input from the keyboard
• Adjust the Score class so that it uses the Comparable interface – this will allow you to easily sort the scores in the high score table!
• The Shape class can throw ShapeException objects:
o The speed value can only be in the range 0 – 10
o The range of widths of shapes can only be 3 – 200
o The range of heights of shapes can only be 3 – 200
• Note: Your exception classes should be derived from RuntimeException only!
• Add appropriate tests for classes (e.g. Shape, Ball, etc.) if you think its necessary
• The Pong class is the main class for the program – use it to setup the JFrame and the Field
o Pong’s main() method must contain local variables for the JFrame and the Field objects!
Planning:
Write up the algorithms in pseudocode for methods like moveBall(), loadHighScores() and saveHighScores(). For each non-trivial method, you need separate pseudocode. You will find the “Guide to Good Pseudocode” on LearnJCU helpful. Please write your method pseudocode above the code for the non-trivial methods.
You may show this part of the assignment to your tutor during practical time (after finishing your prac work) to get comments or suggestions. You can only get help from your tutors in practical time after your prac work is finished, and only if you show your planning.
No help will be given on code without seeing your planning first.
Program Code:
You need to hand in a complete Netbeans Java Project containing eleven (11) files – Shape.java, Ball.java, Paddle.java, ScoreDisplay.java, Field.java, Pong.java (the main class), Score.java, HighScores.java, and TestHighScores.java. Please name the Project based on your first and last name. For example: JackBlack_Assig2Solution
Submission:
Submit a zipped copy of the Netbeans project by uploading it in LearnJCU.
Due:
The assignment is to be submitted before 9am Monday of Week 16. Submissions received after this date will incur late penalties as described in the subject guide.
Coding:
Part of the challenge (and assessment) for this assignment is for you to decide what programming constructs to use to solve the problem. Here are some guidelines:
• Use methods appropriately. Its useful to add a comment above each method that indicates what it does (e.g. “accessor method – gets the value for the score object”). Carefully think about the inputs and outputs for methods and ensure that they are useful and reusable, as discussed in class. There are accessor methods (get the value of member fields), mutator methods (set the value of member fields), helper methods (provide support to other methods or are otherwise not classified as an accessor or mutator)
• Anonymous inner classes are useful for handling button events
o To access a local variable inside an anonymous inner class, use final
• JFrame has a WindowListener interface for handling the window closing event
• The StringBuilder class is useful for creating a string from many smaller strings.
• The String class has many useful methods such as split(), format(), and so on
Marking Scheme
Take note of the following marking criteria so that you know what needs to be done for marks.
Don't just try and get your program to work, but instead do proper planning followed by careful coding. Make sure you give attention to what is rewarded here. You may notice that you don’t get a whole lot of marks for just getting it to work.
Requirement Marks Out Of
Algorithm – Pseudocode – for non-trivial methods
(clear, complete, well-formatted, consistent, accurate) 4
Program Execution
Tests verify the stability of Score class and HighScore class 1
Tests verify the stability of the Shape class 1
Tests verify the stability of the Field class 1
Additional tests provide more useful verification of the classes 2
Displaying a popup message box when player names are invalid 2
Creating an interesting “look-and-feel” for the paddles, ball, etc. 1
Similarity of tests to sample output (as per Assignment 1 style) 1
Quality of Code
Meaningful variable names 1
Appropriate use of methods (e.g. helper methods) 1
Code readability: formatting, indentation, line spacing, etc. 1
Useful, descriptive comments (good amount and quality) 2
Code Constructs
Using KeyEvents to achieve smooth paddle motion 2
The Pong class main() method has JFrame and Field local vars 2
Appropriate uses of anonymous inner class(es) 2
Appropriate background threading and synchronisation 2
Appropriate throwing of exceptions 2
Class Design
Appropriate UML class diagrams for all classes in the program 2
Deduction
Shape class is not abstract, Project submission was invalid, etc. -1
Total 30
|