Hello, I am trying to develop a text based Java game. (my VERY basic main and player class are at the bottom of this post, no more than 20minutes of coding)
Here is what I do not understand:
How does the game flow?
So the game runs and the menu is displayed, but how do I progress the game? I know it’s done through do while loops of if the game is running true/false but I don’t know much further than that! Here is what I am trying to achieve:
Example
Game Loads
Menu Loads
User enters: 1
Welcome Message
Ask user name
Take user name input
Create player object from name input
Code is executed which determines whether a random event happens or not (i.e the player gets 20exp bonus)
If no random event happens:
Player goes to the gym where I will do calculations, give them exp for reps (which are randomly calculated)
After that he goes home
Next day, [random event][gym][home]
How would I program this flow? I’ve seen examples of people shoving everything into the main method but I don’t want to do this.
Any tips? How could I get the program to flow like that?
Hope I’ve explained this well, I am struggling to get my head around these concepts!
Here is my code so far:
Main Class
import java.util.*;
public class Game implements Runnable{
/*
* VARIABLES
*/
public boolean running;
public void start() {
running = true;
new Thread(this).start();
}
public void stop() {
running = false;
}
public void run() {
menu();
}
public static void main(String args[]) {
Game game = new Game();
game.start();
}
public static void menu() {
System.out.println("<--------------------------------------------------------------->");
System.out.println("\t\tWelcome to the Adventure");
System.out.println("\t\t[1] Start Game");
System.out.println("\t\t[2] Credits");
System.out.println("\t\t[3] Exit");
System.out.println("<--------------------------------------------------------------->");
}
}
Player Class
import java.util.*;
public class player extends Game{
/*
* Player attributes:
* Name
* Weight
* Morale
* Money
*/
private String name;
private int weight;
private int morale;
private int money;
private int exp;
public player(String sName, int iWeight, int iMorale, int iMoney, int iExp) {
name = sName;
weight = iWeight;
morale = iMorale;
money = iMoney;
exp = iExp;
}
//GET AND SET NAME
public void setName(String sName) {
name = sName;
}
public String getName() {
return name;
}
//GET AND SET WEIGHT
public void setWeight(int iWeight) {
weight = iWeight;
}
public int getWeight() {
return weight;
}
//GET AND SET MORALE
public void setMorale(int iMorale) {
morale = iMorale;
}
public int getMorale() {
return morale;
}
//GET AND SET MONEY
public void setMoney(int iMoney) {
money = iMoney;
}
public int getMoney() {
return money;
}
//GET AND SET EXP
public void setExp(int iExp) {
exp = iExp;
}
public int getExp() {
return exp;
}
}