For all sections M, N, and O of ITEC 1630, the assignment must be submitted by Monday April 2 at 7pm.
Submission: Prepare a single zip file containing all the source files (.java files, NOT .class files) and the HTML file for running the applet. Submit this file as an attachment to an email sent to: email@example.com. Put in the subject line your section (M, N, or O), name, student number, and the words "Assignment 3". Please make sure that your section is entered correctly. For example, if your name is Jane Doe, and your section is M, your subject line will be "M Jane Doe 999999999 Assignment 3". Your section must be in capital case. Your submission will not reach your TA and will therefore NOT be marked if a wrong section code is used in the subject line. It is highly recommended that you CC yourself on this email. You can email the program any time before the deadline. A printout of the source files and HTML file with the following cover page:
ITEC 1630 Assignment 3
Section (M, N, or O):
Marking Template: (-10 marks if this template is not included)
__ 5 Marks – Style (variable naming, indentation, & Layout)
__ 5 Marks – Comments
__ 15 Marks – Proper use of multithreading (by inspection of source code)
__ 15 Marks – Planes appear, move horizontally at a constant speed, and disappear
__ 15 Marks – Bullets appear, move vertically at a constant speed, and disappear
__ 5 Marks – The gun appears
__ 5 Marks – Correct gun movement (left/right)
__ 15 Marks – Correct behavior when bullets hit planes
__ 5 Marks – Correct scoring
__ 5 Marks – Correct showing game over message
__ 10 Marks – Artistic impression
__ 100 Marks – Total
According to this template the maximum mark you can get for code that
does not compile is 25/100. If you do not provide a printout of your
code including this template, the maximum mark is 75/100.
is to be put in the drop box on the 3rd floor of TEL building before the deadline.
For this assignment, you are required to write a game, where the player has to navigate an anti-aircraft gun and fire bullets to shoot moving planes. The player gets certain scores if a plane is shot down successfully. There are a set number of bullets and planes. The game is over when there are either no more bullets or no more planes left.
Create your own version of the game as an applet, by implementing the functionalities described below. Your program must compile under Java 5.0, and run properly on the computers in TEL2003 under Internet Explorer, Firefox, or AppletViewer.
Applet: Write an HTML file named A3.html, where the size of the applet is specified. You should use the getWidth() and getHeight() methods of the Applet (or JApplet) class to determine the size of the applet in your program. Set the background color to something other than white.
Anti-aircraft Gun: The gun should be represented by a filled rectangle, and appear at the bottom of the applet. The user of the program can use left and right keys on the keyboard to control the movement of the gun. In order to learn how to program the use of the arrow keys, study the program AD.java, which allows the user to move a colored ball with the help of the arrow keys. Make sure the position of the gun is always within the boundaries of the applet.
Bullets: Bullets should be represented by (small) filled circles. The user of the program can press the space key to open fire. Each time the space key is pressed, a bullet should appear in the applet (assuming that there are still bullets left), moving vertically from the gun to the top of the applet at a constant speed. The bullet should disappear once it gets to the top of the applet.
Planes: Planes are also represented by filled circles. Choose a different color than those for the gun and the bullets. At each time interval, a plane appears from the left border of the applet at a random vertical position and moves horizontally toward the right border of the applet at a constant speed. Note that there can be more than one plane on the applet at the same time. The plane disappears when it reaches the right border of the applet.
Hits: When a bullet hits a plane, both of them should disappear, and the user scores 10. Display the current score, along with the number of bullets left, in the applet status bar.
Game Over: Since there are a set number of bullets and planes, the game is over when either bullets or planes run out. Update the status bar to indicate that game is over and show the final score.
A screenshot of the game (taken on a Mac) is here. The parameters of the assignment are open, and you can play with different parameter settings to make the game more interesting. The only restrictions are the following: (1) the number of bullets should not be greater than 30, and (2) the number of planes should not be greater than 20.
Tips & Traps:
1. By default, the keyboard focus is not on the applet when your Webpage containing the applet is loaded, and therefore the applet wonít respond to your keyboard input. You have to set the keyboard focus on the applet manually by clicking on it with your mouse.
2. Recommended structure of the program:
applet class (1) handling interactions with the user, (2) showing the different
objects (planes etc.) on the screen, (3) detecting the hits and updating the
score, (4) determining whether game is over.
You can handle the keyboard events here in this class. When the space key is processed, create bullet threads accordingly. Other keystrokes can be passed to a function in the Gun class for further processing (determining which key is pressed in order to navigate the gun accordingly). Alternatively, you can delegate the keyboard events handling entirely to the Gun class, similar to whatís done in the sample program AD.java. However, the former approach is likely to simplify your job and therefore recommended.
Use a Swing Timer to help create plane threads periodically. For information on Timers, please see the tutorial here or Section 11.6 of the textbook.
∑ A Plane class that extends the Thread class (or implements the Runnable interface, depending on your implementation). A planeís position and velocity can be tracked by its x and y coordinates. The planeís y coordinate does not change because it moves horizontally. Its velocity in the x direction can be tracked by having another variable representing the speed (think: use sleep() to control the speed). Try starting with speed = 5 pixels per sleep. You may adjust them later to make your game more playable. Update the position of the plane in the run() method.
∑ A Bullet class that extends the Thread class (or implements the Runnable interface, depending on your implementation). Similar to a plane, a bulletís position and velocity can be tracked by its x and y coordinates. Use sleep() to control the bulletís speed.
∑ A Gun class responsible for the movement of the gun.
3. Since there can be multiple planes on the screen at the same time, it is recommended to use an array in the applet class to keep track of them. You can initialize the array with the size equal to the maximum number of planes (which is a parameter of the game). Keep a counter of the number of plane threads generated so far. The game is over when the number of plane threads generated exceeds the maximum number of planes. Note that when a plane reaches the right border, it disappears and the corresponding thread should stop. The same for the bullets.
4. A bullet (represented by a circle) hits a plane (also represented by a circle) when the distance between their centers is less than the sum of their radii. Hint: The distance between points (x1, y1) and (x2, y2) is given by the square root of (x1-x2)2 + (y1-y2)2.
5. Thread synchronization (locking) is not required in this assignment.
6. To simplify your task, it is OK to use the deprecated method stop() of the Thread class to stop a thread.