note: The game are is between the x coordinate -6 and 6. I am assuming the moveE
ID: 3642322 • Letter: N
Question
note: The game are is between the x coordinate -6 and 6. I am assuming the moveEnemies() method goes in the EnemyShip class. I think a code might be needed for it to communicate with the Game class as well.
my code so far
import java.util.Scanner;
public class Game {
private EnemyShip enemy1;
private EnemyShip enemy2;
private EnemyShip enemy3;
private PlayerShip player;
public Game(int initialPosition) {
player = new PlayerShip(initialPosition);
enemy1 = new EnemyShip(0,1);
enemy2 = new EnemyShip(-4, 1);
enemy3 = new EnemyShip(2, -1);
}
public Game() {
int x,y;
Scanner input = new Scanner(System.in);
System.out.println("Enemy #1");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy1 = new EnemyShip(x,y);
System.out.println("Enemy #2");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy2 = new EnemyShip(x, y);
System.out.println("Enemy #3");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy3 = new EnemyShip(x, y);
}
public String toString() {
String ret = enemy1.toString() + enemy2.toString() + enemy3.toString() + player.toString();
return ret;
}
}
class Gun {
int x;
int power;
int points;
int bonus;
boolean justFired;
public Gun(int initialX, int initialPower) {
x = initialX;
power = initialPower;
points = 0;
justFired = false;
bonus = 1;
}
}
public class PlayerShip {
int x;
Gun gun1;
Gun gun2;
public PlayerShip(int initialX) {
x = initialX;
gun1 = new Gun(-1,5);
gun2 = new Gun(1,5);
}
public String toString() {
String ret = "Player[" + x + ", " + (gun1.points + gun2.points) + "pts]";
return ret;
}
}
public class EnemyShip {
int x;
int direction;
int life;
boolean justHit;
public EnemyShip(int initialX, int nDirection) {
x = initialX;
direction = nDirection;
life = 10;
justHit = false;
}
public String toString() {
String ret = "Enemy(" + x + ")";
if(life == 0) {
ret += "* ";
}
else {
ret += " ";
}
return ret;
}
}
Explanation / Answer
public class EnemyShip { int x; int direction; int life; boolean justHit; public EnemyShip(int initialX, int nDirection) { x = initialX; direction = nDirection; life = 10; justHit = false; } public String toString() { String ret = "Enemy(" + x + ")"; if(life == 0) { ret += "* "; } else { ret += " "; } return ret; } /** * This method is used to Move the position Automatically until the life remains. */ public void moveEnemy(){ while(life >0){ if( x == 6 || x == -6){ changeDirection(); } if(this.direction == 1){ x++; }else if(this.direction == -1){ x--; } } } /** * This method is used to Move the position for given no of steps until the life remains. */ public void moveEnemy(int noOfSteps){ if(life >0){ if( x == 6 || x == -6 ){ changeDirection(); } if(this.direction == 1){ x+=noOfSteps; }else if(this.direction == -1){ x-=noOfSteps; } } } /** *This method is used to change the direction of Ship. */ public void changeDirection(){ if(this.direction == 1){ this.direction =-1; }else if(this.direction == -1){ this.direction =1; } } } /** * Add the below code in 'Game' constructors after initializing the enemy classes. */ enemy1.moveEnemy(); enemy2.moveEnemy(); enemy3.moveEnemy(); /* OR */ enemy1.moveEnemy(1); enemy2.moveEnemy(1); enemy3.moveEnemy(1); /**/ /** *After adding the above code the Game Class look like */ import java.util.Scanner; public class Game { private EnemyShip enemy1; private EnemyShip enemy2; private EnemyShip enemy3; private PlayerShip player; public Game(int initialPosition) { player = new PlayerShip(initialPosition); enemy1 = new EnemyShip(0,1); enemy2 = new EnemyShip(-4, 1); enemy3 = new EnemyShip(2, -1); //New lines of Code enemy1.moveEnemy(); enemy2.moveEnemy(); enemy3.moveEnemy(); } public Game() { int x,y; Scanner input = new Scanner(System.in); System.out.println("Enemy #1"); System.out.print(" - Inital x Position: "); x = input.nextInt(); System.out.print(" - Inital velocity: "); y = input.nextInt(); enemy1 = new EnemyShip(x,y); System.out.println("Enemy #2"); System.out.print(" - Inital x Position: "); x = input.nextInt(); System.out.print(" - Inital velocity: "); y = input.nextInt(); enemy2 = new EnemyShip(x, y); System.out.println("Enemy #3"); System.out.print(" - Inital x Position: "); x = input.nextInt(); System.out.print(" - Inital velocity: "); y = input.nextInt(); enemy3 = new EnemyShip(x, y); //New lines of Code enemy1.moveEnemy(); enemy2.moveEnemy(); enemy3.moveEnemy(); } public String toString() { String ret = enemy1.toString() + enemy2.toString() + enemy3.toString() + player.toString(); return ret; } } /** * For more interaction you can add 2 public static final fields X_END and X_START to the Gmae Class and access them in changeDirection method of EnemyShip class. * */ /** * Please write to me for More help. */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.