java 最簡單的文字類遊戲
阿新 • • 發佈:2018-11-28
本來想把它移植到JAVA GUI文字遊戲裡,貌似空餘時間越來越少,只能提前發出來了
JAVA GUI文字遊戲 應該是會開發下去。。。歡迎關注點贊評論啥的
先上兩張圖
直接上程式碼吧
Enemy 怪
public class Enemy { private String type;// 名稱 private int life;// 生命值 private boolean isLive;// 是否活著 private int attack;// 攻擊力 private int defend;// 防禦力 private int maxLife;//最大生命 private int miss;// 躲避係數 // 受傷 public void injured(Player h) { int n = GameUtil.getNumber(1,101); if (n < miss) { System.out.println("[#]"+"沒打到"); kill(h);// 還擊 return; } System.out.println("[#]"+type + ":受傷"); // 生命值減少 int loseLife = GameUtil.getLoseLife(h.getAttack(), defend); // int loseLife=g.getLoseLife(h.attack, defend); // int loseLife=h.attack-defend; life -= loseLife;// life=life-30; // 判斷生命值是否小於0 if (life < 0) { life = 0; dead(h);// 死亡 } else { show();// 顯示狀態 kill(h);// 還擊 } } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getLife() { return life; } public void setLife(int life) { this.life = life; } public boolean isLive() { return isLive; } public void setLive(boolean isLive) { this.isLive = isLive; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getDefend() { return defend; } public void setDefend(int defend) { this.defend = defend; } public int getMaxLife() { return maxLife; } public void setMaxLife(int maxLife) { this.maxLife = maxLife; } public int getMiss() { return miss; } public void setMiss(int miss) { this.miss = miss; } // 還擊 public void kill(Player h) { System.out.println("[#]"+type + ":反擊" + type + "還擊" + h.getName()); h.injured(this); } // 死亡 public void dead(Player h) { isLive = false; System.out.println("[#]" + type + "掛了"); // 呼叫hunter中新增經驗值的方法 h.addExp(maxLife); } // 顯示狀態 public void show() { System.out.println("[#]"+type + "生命值是:" + life + "是否活著" + isLive); } }
Game 遊戲主程式
import java.util.ArrayList; import java.util.List; public class Game { Player h; List<Enemy> enemys = new ArrayList<Enemy>(); public Game(String name, String weapon) { h = new Player(name, weapon); enemys.add(new Monster(1)); enemys.add(new Monster(2)); enemys.add(new Monster(3)); enemys.add(new Vampire(1)); enemys.add(new Vampire(2)); enemys.add(new Vampire(4)); enemys.add(new Vampire(1)); } public void start() { // 死迴圈 實現遊戲自動打 while (true) { // 生成一個隨機數 0-5 int ran = GameUtil.getNumber(0, enemys.size()); h.fight(enemys.get(ran)); // 玩家死亡 遊戲結束 if (!h.isLive()) { System.out.println("恢復一下"); h.setLife(100); h.setLive(true); // end(); // break; } // 如果當前對手是死亡 if (!enemys.get(ran).isLive()) { // 將此對手移除集合 enemys.remove(enemys.get(ran)); } // 判斷集合大小 如果小於等於0 證明所有的對手都死亡了 if (enemys.size() <= 0) { end(); break; } try { System.out.println("-----------正在尋找對手--------------"); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void end() { System.out.println("Game Over!!!"); } }
GameUtil 遊戲方法
public class GameUtil {
//求減少生命值的方法 方法用static 修飾 ,呼叫時 類名.方法名
public static int getLoseLife(int attack,int defend){
return attack-defend;
}
//求a-b之間隨機數方法
public static int getNumber(int a,int b){
//求任意兩個數之間的隨機數(int)
return (int)(Math.random()*(b-a)+a);
}
}
Monster 小怪列表
public class Monster extends Enemy{
public Monster(int t) {
switch (t) {
case 1:
this.setType("青銅小怪");
this.setLife(70);
this.setAttack(5);
this.setDefend(2);
this.setMiss(20);
break;
case 2:
this.setType("白銀小怪");
this.setLife(80);
this.setAttack(8);
this.setDefend(4);
this.setMiss(30);
break;
case 3:
this.setType("黃金小怪");
this.setLife(90);
this.setAttack(10);
this.setDefend(6);
this.setMiss(60);
break;
case 4:
this.setType("鉑金小怪");
this.setLife(100);
this.setAttack(15);
this.setDefend(10);
this.setMiss(70);
break;
case 5:
this.setType("鑽石小怪");
this.setLife(110);
this.setAttack(28);
this.setDefend(12);
this.setMiss(70);
break;
default:
System.out.println("輸入錯誤");
break;
}
this.setLive(true);
this.setMaxLife(this.getLife());
//maxLife=life;
}
}
Player 玩家
//玩家
public class Player {
private String name;
private String weapon;// 武器
private int life;// 生命值
private boolean isLive;// 是否活著
private int attack;// 攻擊力
private int defend;// 防禦力
private int exp;// 經驗值
private int level;// 等級
private int maxLife;// 最大生命值
private int miss;// 躲避係數
int times;
public Player() {
// TODO Auto-generated constructor stub
}
// 為name 和武器賦值 並且初始化
public Player(String name, String weapon) {
this.name = name;
this.weapon = weapon;
life = 200;
isLive = true;
attack = 60;
defend = 3;
level = 1;
exp = 0;
maxLife = life;
miss = 60;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefend() {
return defend;
}
public void setDefend(int defend) {
this.defend = defend;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getMaxLife() {
return maxLife;
}
public void setMaxLife(int maxLife) {
this.maxLife = maxLife;
}
public int getMiss() {
return miss;
}
public void setMiss(int miss) {
this.miss = miss;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
// 打Vampire
public void fight(Enemy e) {
// 在打之前判斷一下小怪和玩家是否存活 如果一方死亡 那麼程式終止
if (!isLive || !e.isLive()) {
return;// return 表示結束 方法
}
System.out.println("[-]" + name + "揮舞著" + weapon + "殺向" + e.getType());
// m必須受傷
e.injured(this);// this 表示當前物件
}
// 受傷Vampire
public void injured(Vampire v) {
int n = GameUtil.getNumber(1, 101);
if (n < miss) {
System.out.println("[-]" + "躲避成功");
show();
return;
}
System.out.println("[-]" + name + ": 掉血了");
// 減少的生命是動態的值
int loseLife = GameUtil.getLoseLife(v.getAttack(), defend);
// int loseLife=m.attack-defend;
life -= loseLife;
if (life < 0) {
life = 0;
dead();
}
show();
// 當玩家受傷後 吸血
v.getBlood(loseLife);
}
// 受傷Monster
public void injured(Enemy e) {
int n = GameUtil.getNumber(1, 101);
if (n < miss) {
System.out.println("[-]" + "閃躲成功");
show();
return;
}
System.out.println("[-]" + name + ":掉血");
// 減少的生命是動態的值
int loseLife = GameUtil.getLoseLife(e.getAttack(), defend);
// int loseLife=m.attack-defend;
life -= loseLife;
if (life < 0) {
life = 0;
dead();
}
show();
}
// 死亡
public void dead() {
System.out.println("[-]" + name + "你掛了");
isLive = false;
}
// 顯示狀態
public void show() {
System.out.println(name + "生命值是:" + life + "\n攻擊力" + attack + "防禦力:" + defend);
}
// 升級方法
public void addLevel() {
attack += 3;
defend += 3;
level++;
// 滿血
life = maxLife;
System.out.println("[-]" + "升級成功!當前的等級是:" + level);
show();
}
// 增加經驗方法 當對手死亡的時候 玩家增加經驗 經驗值=對手的生命值
public void addExp(int life) {
exp += life;// 加的經驗值=對手的生命值
// 判斷當前經驗值是否滿足此等級升級所需的經驗值
int needExp = 0;
for (int i = 1; i < level; i++) {
needExp += i * 50;
}
if (exp > needExp) {
addLevel();
}
}
}
Vampire 吸血類怪
public class Vampire extends Enemy {
private int blood;// 吸血係數
public Vampire(int t) {
switch (t) {
case 1:
this.setType("青銅吸血鬼");
this.setLife(70);
this.setAttack(5);
this.setDefend(2);
this.setMiss(20);
break;
case 2:
this.setType("白銀吸血鬼");
this.setLife(80);
this.setAttack(8);
this.setDefend(4);
this.setMiss(30);
break;
case 3:
this.setType("黃金吸血鬼");
this.setLife(90);
this.setAttack(10);
this.setDefend(6);
this.setMiss(60);
break;
case 4:
this.setType("鉑金吸血鬼");
this.setLife(100);
this.setAttack(15);
this.setDefend(10);
this.setMiss(70);
break;
default:
System.out.println("輸入錯誤");
break;
}
this.setLive(true);
this.setMaxLife(this.getLife());
// maxLife=life;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
// 吸血
public void getBlood(int loseLife) {
System.out.println("[+]" + this.getType() + ":吸血成功!");
// 吸玩家失去的生命力*blood/100;
int getBlood = loseLife * blood / 100;
this.setLife(this.getLife() + getBlood);
// life+=getBlood;
if (this.getLife() >= this.getMaxLife()) {
this.setLife(this.getMaxLife());
// life=maxLife;
}
}
}
TestGame 最最重要的main方法所在類
import java.util.Scanner;
public class TestGame {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("請輸入玩家姓名:");
String name = s.next();
System.out.println("請輸入玩家武器:");
String w = s.next();
Game g = new Game(name, w);
System.out.println("是否開始遊戲?[Y][N]");
String f = s.next();
if (f.equals("Y") || f.equals("y")) {
g.start();
} else {
System.out.println("結束");
}
}
}