Create an Enemy class, Player class, and GameObject class for your game.java. Th
ID: 3920495 • Letter: C
Question
Create an Enemy class, Player class, and GameObject class for your game.java. The Enemy and Player (sub) classes should inherit from the GameObject (super) class. Create a player and a few enemies. Create the basic movements (left/right/up/down) for the player. Develop a menu in the main program that allows the player to move around.
GameObject SuperClass Description Variables static char World[] [ ] = new char [41] [21] ; This is the character array that will store what is at each X,Y location. It is static so there is int Xpo3, Ypos char Avatar Methods PrintWorld() only one World for all objects X,Y location of the game object. The top left of the screen will be 1,1 The character that will be displayed on the screen Description This will print the World to the screen. Example code for (int y=1; y?=20; y++) for {int x=1; xExplanation / Answer
// Creates a class GameObject
public class GameObject
{
// Creates an matrix with 41 rows and 21 columns for the world
static char World[][] = new char[41][21];
// To store x and y position
int Xpos, Ypos;
// To store the avatar character
char Avatar;
// Method to display the world
void PrintWorld()
{
int x;
// Loops till number of columns
for(int y = 1; y <= 20; y++)
{
// Loops till number of rows
for(x = 1; x <= 40; x++)
// Displays the current position character
System.out.print(World[x][y]);
// Optionally put a space
// Checks if column index is less than 40 then display space
if(x < 40)
System.out.print(" ");
// Display new line character
System.out.println();
}// End of for loop
}// End of method
// Method to move right position
void MoveRight()
{
// Checks if current x next position and y position is empty
if(World[Xpos+1][Ypos] == ' ')
{
// Sets the current x and y position to empty
World[Xpos][Ypos] = ' ';
// Increases the x position by one
Xpos++;
// Sets the Avatar character at current x and y position
World[Xpos][Ypos] = Avatar;
}// End of if condition
}// End of method
// Method to move left position
void MoveLeft()
{
// Checks if current x previous position and y position is empty
if(World[Xpos-1][Ypos] == ' ')
{
// Sets the current x and y position to empty
World[Xpos][Ypos] = ' ';
// Decreases the x position by one
Xpos--;
// Sets the Avatar character at current x and y position
World[Xpos][Ypos] = Avatar;
}// End of if condition
}// End of method
// Method to move up ward direction
void MoveUp()
{
// Checks if current x position and y previous position is empty
if(World[Xpos][Ypos-1] == ' ')
{
// Sets the current x and y position to empty
World[Xpos][Ypos] = ' ';
// Decreases the y position by one
Ypos--;
// Sets the Avatar character at current x and y position
World[Xpos][Ypos] = Avatar;
}// End of if condition
}// End of method
// Method to move down ward direction
void MoveDown()
{
// Checks if current x position and y next position is empty
if(World[Xpos][Ypos+1] == ' ')
{
// Sets the current x and y position to empty
World[Xpos][Ypos] = ' ';
// Increases the y position by one
Ypos++;
// Sets the Avatar character at current x and y position
World[Xpos][Ypos] = Avatar;
}// End of if condition
}// End of method
}// End of class
---------------------------------------------------------------------------
// Creates a class Enemy derived from GameObject
public class Enemy extends GameObject
{
// Instance variables to store data
String Race;
int HP;
int Attack;
int Armo;
int Speed;
// Parameterized constructor definition
Enemy(String Race)
{
// Generates random x and y position
Xpos = (int)(Math.random()*30)+4;
Ypos = (int)(Math.random()*10)+2;
// Sets the enemy to current x and y position
World[Xpos][Ypos] = Race.charAt(0);
// Checks if enemy is "Orc" then sets the default values to instance variables
if(Race.equals("Orc"))
{
HP = 50;
Attack = 5;
Armo = 20;
Speed = 1;
Avatar = 'O';
}// End of if condition
}// End of parameterized constructor
}// End of class
---------------------------------------------------------------------
// Creates a class Player derived from GameObject
public class Player extends GameObject
{
// Instance variables to store data
int HP;
int Attack;
int Armor;
String Name;
int Gold;
// Defines parameterized constructor
Player(String Name, char Avatar)
{
// Sets the Avatar name
this.Avatar = Avatar;
// Set entire world to spaces
for(int x = 1; x <= 40; x++)
for(int y = 1; y <= 20; y++)
World[x][y] = ' ';
// don't forget to put the player into the world
Xpos = 2;
Ypos = 2;
World[2][2] = Avatar;
// Line parameter of world with trees @
for(int x = 1; x <= 40; x++)
{
World[x][1] = '@';
World[x][20] = '@';
}
for(int y = 1; y <= 20; y++)
{
World[1][y] = '@';
World[40][y] = '@';
}
// Draw a lake at a random location
int a = (int)(Math.random()*30)+4;
int b = (int)(Math.random()*10)+2;
World[a][b] = '~'; World[a+1][b] = '~'; World[a+2][b] = '~';
World[a][b+1] = '~'; World[a+1][b+1] = '~'; World[a+2][b+1] = '~';
World[a][b+2] = '~'; World[a+1][b+2] = '~'; World[a+2][b+2] = '~';
}// End of parameterized constructor
}// End of class
----------------------------------------------------------------------
import java.util.*;
// Driver class game definition
public class game
{
// main method definition
public static void main(String[] args)
{
// Scanner class object created
Scanner in = new Scanner(System.in);
// To store user choice
String Choice = "";
// creating the player will initialize the world
Player Kirk = new Player("Kirk", 'K');
// create some enemies here in random locations
Enemy Orc = new Enemy("Orc");
// Loops till user choice is not "Q"
while (!Choice.equals("q"))
{
// Calls the method to display world
Kirk.PrintWorld();
// Displays menu
System.out.println("a - Move Left d - Move Right w - Move Up s - Move Down ");
// Accepts user choice
System.out.println("Enter your command: ");
Choice = in.nextLine();
// Checks if choice is "a" calls the method MoveLeft() to move left
if (Choice.equals("a"))
Kirk.MoveLeft();
// Checks if choice is "d" calls the method MoveRight() to move right
if (Choice.equals("d"))
Kirk.MoveRight();
// Checks if choice is "w" calls the method MoveUp() to move up
if (Choice.equals("w"))
Kirk.MoveUp();
// Checks if choice is "s" calls the method MoveDown() to move down
if (Choice.equals("s"))
Kirk.MoveDown();
}// End of while loop
}// End of main method
}// End of driver class
Sample Output:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@K @
@ @
@ @
@ O ~~~ @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter your command:
a
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@K @
@ @
@ @
@ O ~~~ @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter your command:
d
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ K @
@ @
@ @
@ O ~~~ @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @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 @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter your command:
s
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ @
@ @
@ K O ~~~ @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter your command:
w
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ @
@ K @
@ O ~~~ @
@ ~~~ @
@ ~~~ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter your command:
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.