COSC 1336, Project 2: Space Invader
This is one of three major programming projects this semester. You may collaborate on this project, but the code must be yours. While you may ask for assistance in debugging, this project should be ENTIRELY your own work. Objectives include:
•Call and write methods with parameters and return values.
•Use the DrawingPanel, Graphics, and Color classes.
•Use for loops and if statements to control assignments.
Submit thru Canvas.
In this project you will write a program that could be the basis for a simple version of the space invaders video game. This project will involve graphics and animation. (See BouncingBall.java in Canvas->Files This project has several parts and should be started early.
Make sure your program and each method is preceded by a descriptive comment.
Part 1: Setup
Start a new project in Dr. Java. Load BouncingBall.java into Dr. Java and change the name of the class to Project2.
Also get a copy of DrawingPanel.java from Canvas->File and put it into the same directory as Project2.java.
•Change drawCircles to startGame.
•In startGame, change loop variable ‘I’ to ‘time’ and make the time go up to 1000 instead of 90.
•Remove everything from the body of the for loop except for the sleep method. •In the main method, before the call to startGame, print the following on the screen in black at position x = 10 and y = 15:
Project 2 by YOURNAME.
Replace YOURNAME with your full name.
Part 2: The Patrol ship
Create class constants PATROL_Y initialized to 250 and PATROL_SIZE initialized to 20.
For example, put public static final int PATROL_Y = 250; after the class header: public class Project2.
Make a (non-constant) class variable called patrolX initialized to 270.
For example, put public static int patrolX = 270; after the class constants.
Write the method:
public static void drawPatrol(Graphics g, Color c); that fills a square at x = patrolX, y = PATROL_Y, with side PATROL_SIZE in the given color. Do a Google for “Java Graphics fillRect to find a graphics method.
Call drawPatrol(g, Color.GREEN) before the for loop statement. Get it working.
Part 3: The enemy ship
Make class constants ENEMY_Y and ENEMY_SIZE initialized to 20 and 30, respectively.
Make a class variable enemyX initialized to 0.
The enemy ship will be a red square with the given position and size.
Write the method: public static void moveEnemyShipAndDraw(Graphics g); that draws the enemy ship first in white, then increment enemyX by 1, and then draws the ship again in red. Put a call to this method inside the for loop.
Part 4: Moving the patrol ship
Make integer constants RIGHT_ARROW = 39, LEFT_ARROW = 37, and UP_ARROW = 38.
Write the method:
public static void handleKeys(DrawingPanel panel, Graphics g);
This will call panel.getKeyCode() that returns a code for a key. It returns 0 if no key has been pushed, and will return one of the above constants if the corresponding arrow key has been pushed.
•The method handleKeys should do nothing if the returned key code is 0.
•If the key code is RIGHT_ARROW or LEFT_ARROW, move the patrol ship to the right or left by 3 pixels, but do not let any part of the patrol ship move off the screen.
To move the patrol ship:
•draw the patrol ship in white (to erase the old one)
•draw the patrol ship in green
Call the method handleKeys in the loop. You should be able to move the patrol ship by pushing the left and right arrows on the keyboard.
Before using the arrow keys, make sure your drawing window is the active window by clicking the mouse in this window.
Part 5: Shooting
The patrol ship will shoot a missile when the up arrow key is pushed.
Make class variables patrolMissileX and patrolMissileY.
Initialize patrolMissileY to 0.
Make a contant PATROL_MISSILE_LENGTH initialized to 10.
Write a method: public static void movePatrolMissileAndDraw(Graphics g); that moves the missile and draws it similar to moveEnemyShipAndDraw:
•Do not draw anything if patrolMissileY is 0.
•Otherwise, the missile as a vertical line with the stated length and x-position.
•PatrolMissileY is the top of the line.
•Draw the missile in white, move the missile up 5 pixels, and draw it again in black.
•If the patrolMissileY is 0 or negative, do not draw the missile in black, but set patrolMissileY to 0.
Call this method before the sleep in the loop.
When the up arrow key is pushed and patrolMissileY is 0, set patrolMissileX to be the center of the patrol ship and set the top of the missile so that its bottom is one pixel above the top of the patrol ship (PATROL_Y).
You should only be able to fire one missile at a time and the up arrow should not do anything if a missile is still displayed.
Part 6: Detecting a hit
Make a boolean class variable, hit, initialized to false; Write the method: public static boolean detectHit(); that returns true if the current missile has hit the enemy ship and false otherwise.
There is a hit if the top of the missile is inside the enemy ship.
For this to happen two thing must be true:
•the x value of the top of the missile must be between the left and right sides of the enemy
•the y value of the top of the missile must be between the top and bottom of the enemy.
At the end of the loop in startGame, set hit to true if detectHit() returns true. Modify moveEnemyShipAndDraw so that if hit is true, the enemy ship is drawn in black and does not move. In this case, display the message:
Enemy ship hit!
in green on a line below the patrol ship.
If the enemy ship moves off the screen or time runs out, Display the message:
Enemy ship got away!
in red on a line below the patrol ship.
COSC 1336, Project 2: Space Invader