Creation of Classes
In a single file named game.java, create an Enemy class, Player class, and gameObject class. 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 method that allows the player to move around.
gameObject SuperClass | |
Variables | Description |
static char World[][] = new char[41][21]; | This is the character array that will store what is at each XY location., It is static so there is only one World for all game objects. |
int Xpos, Ypos | XY location of the game object. ,The top left of the screen will be 11. |
int HP | Hitpoints of the game object it is alive as long as HP > 0 |
int Attack | Attack rating of the game object – the higher the number, the more damage it does |
int Armor (optional) | Armor rating of the game object – the higher the number, the less damage it takes when attacked |
char Avatar | The character that will be displayed when the World is printed |
Methods | Description |
PrintWorld() | This will print the World to the screen. Example code: for (int y=1; y<=20; y++) { for (int x=1; x<=40; x++) { System.out.print(World[x][y]); // optionally put a space after each element if (x < 40) System.out.print(” “); } System.out.println(); } |
MoveRight() | Move the game object to the right. Here’s some example code: if (World[Xpos+1][Ypos] == ‘ ‘) { World[Xpos][Ypos] = ‘ ‘; Xpos++; World[Xpos][Ypos] = Avatar; } |
MoveLeft() | Move the game object to the left. |
MoveUp() | Move the game object to the up. |
MoveDown() | Move the gameobject to the down. |
Enemy SubClass (inherits from gameObject) | |
Variable Names | Variable Description |
String Type | Type of enemy such as “Orc” or “Troll” |
int Speed (optional) | The speed of the enemy. You could have some enemies move 2 spaces per turn instead of 1. |
Methods Names | Methods Description |
Enemy() | Constructor that takes 1 parameter – Type
You can set the Enemy’s Xpos,Ypos to a random location In the constructor, you will set these things based on the Race: HP, Attack, Armor, Speed, Avatar. For example: |
Player SubClass (inherits from gameObject) | |
Variable Names | Variable Description |
String Name | Name of the player. Creation of Classes |
int Gold (optional) | The amount of gold the player has collected. |
Methods Names | Methods Description |
Player() | Constructor that takes 2 parameters – Name, Avatar
Since there will only be 1 player, this constructor will only be called once. Therefore you can initialize the World here. Here’s some example things you could do: // set entire world to spaces // put the player into the world after filling it with spaces // line perimeter of world with trees ‘@’ // draw a lake at a random location ~ |
Below is an example screen print showing the player, Orcs, Trolls, armor and weapons.Creation of Classes
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ K $ @ @ @ @ % O O @ @ O @ @ ~ ~ ~ T @ @ ~ ~ ~ O O @ @ ~ ~ ~ O O @ @ + @ @ @ @ $ @ @ @ @ @ @ + @ @ O O % @ @ @ @ $ @ @ $ T @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Enter your command:
|
The main program will be rather simple since many things are handled in the classes.
import java.util.*;
public class game
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String Choice = “”;
// creating the player will initialize the world
Player P = new Player(“Kirk”,’K’);
// create some enemies in random locations
Enemy E1 = new Enemy(“Dragon”);
while (!Choice.equals(“q”))
{
P.PrintWorld();
System.out.println(“Enter your command: “);
Choice = in.nextLine();
// call move methods – you can use the standard gaming directions – a,s,d,w
if (Choice.equals(“a”))
P.MoveLeft();