Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my code and I have to make the following changes in java: import java.aw

ID: 3574099 • Letter: T

Question

This is my code and I have to make the following changes in java:

import java.awt.*;

public class Project3Part1 {
  
public static final int SLEEP_TIME = 50;
  
// keys
public static final int RIGHT_ARROW = 39;
public static final int LEFT_ARROW = 37;
public static final int UP_ARROW = 38;
  
// panel size
public static final int PANEL_WIDTH = 600;
public static final int PANEL_HEIGHT = 400;
  
// patrol ship
public static final int PATROL_Y = PANEL_HEIGHT-50;
public static final int PATROL_WIDTH = 30;
public static final int PATROL_HEIGHT = 18;
public static final int PATROL_MOVE_AMOUNT = 3;
public static final int PATROL_INITIAL_X = PANEL_WIDTH - PATROL_WIDTH - 5;
public static int patrolX;
  
// enemy ship
public static final int ENEMY_Y = 20;
public static final int ENEMY_WIDTH = 36;
public static final int ENEMY_HEIGHT = 21;
public static final int ENEMY_INITIAL_X = 5;
public static int enemyX;
  
// patrol missile
public static final int PATROL_MISSILE_LENGTH = 10;
public static final int PATROL_MISSILE_MOVE_AMOUNT = 5;
public static final int PATROL_MISSILE_AVAILABLE_Y = 0;
public static int patrolMissileX;
public static int patrolMissileY;
  
// colors
public static final Color BACKGROUND_COLOR = Color.WHITE;
public static final Color ENEMY_COLOR = Color.RED;
public static final Color ENEMY_DEAD_COLOR = Color.BLACK;
public static final Color PATROL_COLOR = Color.GREEN;
public static final Color PATROL_MISSILE_COLOR = Color.GREEN;
public static final Color HEADING_COLOR = Color.BLACK;
public static final Color MESSAGE_WIN_COLOR = Color.GREEN;
public static final Color MESSAGE_LOSE_COLOR = Color.RED;
public static final Color MESSAGE_SPACE_BAR_COLOR = Color.BLACK;
  
// messages
public static final String HEADING_MESSAGE = "Project3 Part 1 by NAME";
public static final String START_MESSAGE = "Push Space Bar to Start";
public static final String ENEMY_HIT_MESSAGE = "Enemy ship hit!";
public static final String ENEMY_GOT_AWAY_MESSAGE = "Enemy ship got away!";
  
// message positions
public static final int HEADING_X = 10;
public static final int HEADING_Y = 15;
public static final int MESSAGE_X = 10;
public static final int MESSAGE_Y = PANEL_HEIGHT - 10;
  
public static boolean running = false;
public static boolean hit = false;
  
// Main method for Project 3
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(PANEL_WIDTH, PANEL_HEIGHT);
Graphics g = panel.getGraphics( );
restart(g);
running = false;
showMessage(g,START_MESSAGE,MESSAGE_SPACE_BAR_COLOR);
startGame(panel, g);
}
  
// show a message at the bottom of the screen
public static void showMessage(Graphics g, String message, Color c) {
g.setColor(c);
g.drawString(message,MESSAGE_X,MESSAGE_Y);
}
  
// show a message at the top of the screen
public static void showHeading(Graphics g, String message, Color c) {
g.setColor(c);
g.drawString(message,HEADING_X,HEADING_Y);
}
  
// This method contains the main loop for the program
// The loop runs forever, sleeping after each iteration
public static void startGame(DrawingPanel panel, Graphics g) {
drawPatrol(g, Color.green);
while(true) {
moveEnemyShipAndDraw(g);
handleKeys(panel, g);
movePatrolMissileAndDraw(g);
if (detectHit())
hit = true;
if (hit)
showMessage(g,ENEMY_HIT_MESSAGE,MESSAGE_WIN_COLOR);
panel.sleep(SLEEP_TIME);
}
}
  
// redraw the screen and reset the game to the beginning
public static void restart(Graphics g) {
running = true;
hit = false;
g.setColor(BACKGROUND_COLOR);
g.fillRect(0,0,PANEL_WIDTH,PANEL_HEIGHT);
showHeading(g,HEADING_MESSAGE,HEADING_COLOR);
patrolMissileY = PATROL_MISSILE_AVAILABLE_Y;
enemyX = ENEMY_INITIAL_X;
patrolX = PATROL_INITIAL_X;
drawPatrol(g,PATROL_COLOR);
}
  
// draw the patrol ship in a given color
public static void drawPatrol(Graphics g, Color c) {
g.setColor(c);
g.fillRect(patrolX, PATROL_Y, PATROL_WIDTH, PATROL_HEIGHT);
}
  
// draw the enemy ship in a given color
public static void drawEnemy(Graphics g, Color c) {
g.setColor(c);
g.fillRect(enemyX, ENEMY_Y, ENEMY_WIDTH, ENEMY_HEIGHT);
}
  
// move the enemy ship and redraw it, erasing the old position
public static void moveEnemyShipAndDraw(Graphics g) {
if (hit) {
drawEnemy(g,ENEMY_DEAD_COLOR);
return;
}
if (running) {
drawEnemy(g,BACKGROUND_COLOR);
enemyX++;
}
drawEnemy(g,ENEMY_COLOR);
if (enemyX > PANEL_WIDTH)
showMessage(g,ENEMY_GOT_AWAY_MESSAGE,MESSAGE_LOSE_COLOR);
}
  
// handle the space bar and arrow keys
public static void handleKeys(DrawingPanel panel, Graphics g) {
int keyCode = panel.getKeyCode();
if (keyCode == 0)
return;
if (keyCode == ' ')
restart(g);
if (keyCode == LEFT_ARROW)
movePatrol(g,-PATROL_MOVE_AMOUNT);
if (keyCode == RIGHT_ARROW)
movePatrol(g,PATROL_MOVE_AMOUNT);
if ((keyCode == UP_ARROW) && (patrolMissileY == 0)) {
patrolMissileX = patrolX + PATROL_WIDTH/2;
patrolMissileY = PATROL_Y - PATROL_MISSILE_LENGTH - 1;
}
}
  
// move the patrol ship by a given amount, erasing the ship at the old position
public static void movePatrol(Graphics g, int delta) {
drawPatrol(g, BACKGROUND_COLOR);
patrolX += delta;
if (patrolX < -PATROL_WIDTH/2)
patrolX = -PATROL_WIDTH/2;
if (patrolX >= PANEL_WIDTH - PATROL_WIDTH/2)
patrolX = PANEL_WIDTH - PATROL_WIDTH/2 - 1;
drawPatrol(g, PATROL_COLOR);
}
  
// draw the missile with (x,y) at the top of the missile
public static void drawMissile(Graphics g, int x, int y, Color c) {
g.setColor(c);
g.drawLine(x,y,x,y+PATROL_MISSILE_LENGTH);
}
  
// move the patrol missile, erasing the old one
public static void movePatrolMissileAndDraw(Graphics g) {
if (patrolMissileY == PATROL_MISSILE_AVAILABLE_Y)
return;
drawMissile(g,patrolMissileX,patrolMissileY,BACKGROUND_COLOR);
patrolMissileY -= PATROL_MISSILE_MOVE_AMOUNT;
g.setColor(PATROL_MISSILE_COLOR);
if (patrolMissileY <= 0)
patrolMissileY = PATROL_MISSILE_AVAILABLE_Y;
else
drawMissile(g,patrolMissileX,patrolMissileY,PATROL_MISSILE_COLOR);
}
  
// return true if the missile intersects the enemy ship
public static boolean detectHit() {
return patrolMissileX >= enemyX && patrolMissileX <= enemyX + ENEMY_WIDTH &&
patrolMissileY >= ENEMY_Y && patrolMissileY <= ENEMY_Y + ENEMY_HEIGHT;
}
  
}

Part 1:Random motion of the enemy ship:

Change moveEnemyShipAndDraw so that the ship can randomly change directions. The possible directions are moving left, moving right, or stationary. At each step, With a probability of .02 change the direction to one of the other two values with equal probability. Use a random number generator declared similar to: public static Random random = new Random(); Do not let any part of the enemy ship move off the screen, change the direction of move if necessary. Remove the test to see of the enemy s hip gets away, since it cannot. The only way for the game to end is if the enemy ship is hit.

Details: You can perform an action with probability .02 by executing: if (random.nextDouble() < .02) ... Define a static integer variable enemyMoveAmount that can have the values -1, 0, or 1. At each step, move the enemy ship by this amount. After the probability check, change the value of enemyMoveAmount to one of the other two values. You can use random.nextInt(2) to choose one of two random values and use this with a number of if statements to decide what value to change enemyMoveAmount to. If you are clever, you and write a formula (without if statements) that computes the new value of enemyMoveAmount from the old value and the value returned by random.nextInt(2).

Part 2: The enemy fights back

At each step have the enemy fire a missile it it is available and the initial space bar has been pushed. Like the patrol ship, the enemy ship can only have one active missile at a time. The enemy missile should have length length 5 and be red. If the patrol ship is hit, turn it black and do not allow it to move or fire. Also stop the firing of the enemy ship and print a message at the bottom of the screen indicating that the patrol ship has been hit. At this point, your project should look like this. Fewer Details Modify detectHit so that it returns true if either ship is hit. The implementation of the enemy missile is similar to the patrol missile, but it moves in the opposite direction. Have detectHit return true if either ship is hit. You can use a separate variable such as partrolHit to determine which ship was hit.

Explanation / Answer

import java.awt.*;

public class Test {

public static final int PATROL_Y = 250;

public static final int PATROL_SIZE = 20;

public static int patrolX = 270;

public static final int ENEMY_Y = 20;

public static final int ENEMY_SIZE = 30;

public static int enemyX = 0;

public static final int RIGHT_ARROW = 39;

public static final int LEFT_ARROW = 37;

public static final int UP_ARROW = 38;

public static int patrolMissileX = 0;

public static int patrolMissileY = 0;

public static final int PATROL_MISSILE_LENGTH = 10;

public static void main(String[] args) {

    Drawingpanel = new DrawingPanel(300, 300);

    Graphics g = panel.getGraphics( );

    g.drawString("Project 2 by Garrett Griffin", 10, 15);

    startGame(panel, g);

}

public static void drawPatrol(Graphics g, Color c){

    g.setColor(Color.GREEN);

    g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

}

public static void moveEnemyShipAndDraw(Graphics g) {

      g.setColor(Color.WHITE);

      g.fillRect(enemyX, ENEMY_Y, ENEMY_SIZE, ENEMY_SIZE);

      enemyX+=1;

      g.setColor(Color.RED);

      g.fillRect(enemyX, ENEMY_Y, ENEMY_SIZE, ENEMY_SIZE);

      if(enemyX>300) {

        g.setColor(Color.RED);

        g.drawString("The enemy ship got away!", 10, 290);

      }

    }

public static void handleKeys(Drawingpanel, Graphics g)

{

   

    int i = panel.getKeyCode();

  

    if (RIGHT_ARROW==i && patrolX>=280) {

      g.setColor(Color.WHITE);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      patrolX-=3;

      g.setColor(Color.GREEN);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

    } else if (LEFT_ARROW==i && patrolX<=0) {

      g.setColor(Color.WHITE);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      patrolX+=3;

      g.setColor(Color.GREEN);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      //if RA, move right 3 pixels

    } else if (RIGHT_ARROW==i) {

      g.setColor(Color.WHITE);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      patrolX+=3;

      g.setColor(Color.GREEN);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      //if LA, move left 3 pixels

    } else if(LEFT_ARROW==i) {

      g.setColor(Color.WHITE);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

      patrolX-=3;

      g.setColor(Color.GREEN);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

    } else if(i==0) {

      g.setColor(Color.GREEN);

      g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);

    }

}

public static void movePatrolMissileAndDraw(Drawingpanel, Graphics g) {

    int i = panel.getKeyCode();

    if(UP_ARROW==i && patrolMissileY>0){

      patrolMissileX=patrolX;

      patrolMissileY=PATROL_Y;

      g.setColor(Color.WHITE);

      g.drawLine(patrolMissileX, patrolMissileY, PATROL_MISSILE_LENGTH, PATROL_MISSILE_LENGTH);

      patrolMissileY+=5;

      g.setColor(Color.BLACK);

      g.drawLine(patrolMissileX, patrolMissileY, PATROL_MISSILE_LENGTH, PATROL_MISSILE_LENGTH);

    }

}

public static void startGame(Drawingpanel, Graphics g) {

    int x = 0;

    int y = 270;

    int deltaX = 1;

    int deltaY = -3;

    drawPatrol(g, Color.green);

    for (int time = 0; time <= 1000; time++) {

      moveEnemyShipAndDraw(g);

      handleKeys(panel, g);

      movePatrolMissileAndDraw(panel, g);

      panel.sleep(50);

    }

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote