Initialize patrolMissileY to 0.

COSC 1336, Project 2: Space Invader

Objectives

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.

Hand-in Requirements

Submit thru Canvas.

•Project2.java

Introduction

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)

•change patrolX

•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.

COSC 1336, Project 2: Space Invader

Objectives

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.

Hand-in Requirements

Submit thru Canvas.

•Project2.java

Introduction

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)

•change patrolX

•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.

.

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.

Calculate the price of your order

Simple Order Process

Fill in the Order Form

Share all the assignment information. Including the instructions, provided reading materials, grading rubric, number of pages, the required formatting, deadline, and your academic level. Provide any information and announcements shared by the professor. Choose your preferred writer if you have one.

Get Your Order Assigned

Once we receive your order form, we will select the best writer from our pool of experts to fit your assignment.

Share More Data if Needed

You will receive a confirmation email when a writer has been assigned your task. The writer may contact you if they need any additional information or clarifications regarding your task

Let Our Essay Writer Do Their Job

Once you entrust us with your academic task, our skilled writers embark on creating your paper entirely from the ground up. Through rigorous research and unwavering commitment to your guidelines, our experts meticulously craft every aspect of your paper. Our process ensures that your essay is not only original but also aligned with your specific requirements, making certain that the final piece surpasses your expectations.

Quality Checks and Proofreading

Upon the completion of your paper, it undergoes a meticulous review by our dedicated Quality and Proofreading department. This crucial step ensures not only the originality of the content but also its alignment with the highest academic standards. Our seasoned experts conduct thorough checks, meticulously examining every facet of your paper, including grammar, structure, coherence, and proper citation. This comprehensive review process guarantees that the final product you receive not only meets our stringent quality benchmarks but also reflects your dedication to academic excellence.

Review and Download the Final Draft

If you find that any part of the paper does not meet the initial instructions, send it back to us with your feedback, and we will make the necessary adjustments.