here is my code for task 1 to 3 need help with moving player task import java.ut
ID: 3642249 • Letter: H
Question
here is my code for task 1 to 3 need help with moving player task
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 void movePlayer() {
Scanner in = new Scanner(System.in);
System.out.print("move: ");
String input = in.next();
if(input.toLowerCase().charAt(0) == 'l') {
if(x > -6) {
x = x - 1;
}
}
if(input.toLowerCase().charAt(0) == 'r') {
if(x < 6) {
x = x + 1;
}
}
}
public PlayerShip(int initialX) {
x = initialX;
gun1 = new Gun(x - 1, 5);
gun2 = new Gun(x + 1, 5);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.