admin管理员组

文章数量:1566223

文字小冒险游戏

  • 简介
  • 效果图
  • 知识点简介
  • 源代码
    • EnemyInterface.java:
    • RoleInterface.java:
    • EquAttInterface.java:
    • EquDefInterface.java:
    • Enemy.java:
    • Role.java:
    • EquipmentAttack.java:
    • EquipmentDefend.java:
    • GameFrame.java

简介

花了半天帮数媒的同学写的小游戏,比较简单,但是拓展性还是很强的,以后有机会可以慢慢拓展。

效果图

由于只是帮同学做的一个比较简单的小项目,所以界面比较简单。
其他帮同学写的作业:英汉字典、牧场物语游戏

游戏运行界面:一个简单的游戏机外形,显示屏上写着几个字。

点击确定,进入创建角色界面,弹框提示输入角色姓名

角色创建成功,进入初始化界面,由于是简单项目,初始地点固定,并且整个地图只有五个地点。

点击【捡起装备】,【查看装备】,【查看属性】

移动刷出怪物,点击【攻击敌人】,战斗胜利:

战斗失败,角色死亡!

知识点简介

由于他们专业需要简单介绍用到的知识点,我就随便写了些。

  1. 利用JavaFx来制作界面,通过ActionEvent添加了事件,实现了上下左右的移动按钮,确定按钮,查看属性,查看装备,捡起装备,攻击敌人按钮,通过TextArea来显示活动记录,通过Alert类实现了弹框。

  2. 面向对象实现模型构造,体现了封装继承多态的性质,代码拥有良好的拓展性。
    (1)建立了角色类(Role.java)实现了角色接口(RoleInterface.java),角色有的属性是生命(hp),攻击力(harm),防御力(defence),拥有的武器装备(equipment_attack),拥有的防御装备(equipment_defend),类具有的方法是死亡die(),attack(enemy)以及实现了封装性的一些get()与set()方法等。
    (2)建立了敌人类(Enemy.java)实现了敌人接口(RoleInterface.java),敌人有的属性是生命(hp),攻击力(harm),防御力(defence),类具有的方法是死亡die()以及实现了封装性的一些get()与set()方法等。
    (3)建立了武器装备类(EquipmentAttack.java)实现了武器装备接口(EquAttInterface.java),类拥有的属性是攻击力(harm)以及实现了封装性的一些get()与set()方法等。
    (4)建立了防具装备类(EquipmentDefend.java)实现了防具装备接口(EquDefInterface.java),类拥有的属性是防御力(defence)以及实现了封装性的一些get()与set()方法等。

  3. 利用Random类和数组存储敌人的姓名实现了随机生成敌人,并且随机赋予属性(攻击力,防御力)。

  4. 所有代码利用了顺序、选择、循环三大基本语句实现。

  5. 角色与敌人的属性数值(生命,攻击力,防御力)使用浮点数表示。

源代码

4个接口类:

  • EnemyInterface.java
  • RoleInterface.java
  • EquAttInterface.java
  • EquDefInterface.java

4个实现类:

  • Enemy.java
  • Role.java
  • EquipmentAttack.java
  • EquipmentDefend.java

1个界面类:

  • GameFrame.java

EnemyInterface.java:

/*
 * 敌人接口
 */
public interface EnemyInterface {
	String name = "怪兽";
	float attack = 0;
	float defend = 0;
	void attack(); // 攻击方式
	void defend(); // 防御
}

RoleInterface.java:

/*
 * 角色接口
 */
public interface RoleInterface {
	String name = "角色";
	float attack = 0;
	float defend = 0;
	void attack();  // 攻击
	void defend();	// 防御
}

EquAttInterface.java:

/*
 * 武器接口
 */
public interface EquAttInterface {
	String name = "武器";	// 名字
	float harm = 0;	// 攻击
	Role owner = null;	// 持有者
}

EquDefInterface.java:

/*
 * 防具接口
 */
public interface EquDefInterface {
	String name = "防具";	// 名字
	float defend = 0;	// 攻击
	Role owner = null;	// 持有者
}

Enemy.java:

/*
 * 敌人类
 */
public class Enemy implements EnemyInterface {
	private String name;	// 名字
	private float hp = (new Random().nextInt(10))+1; 		// 血量,至少1滴血
	private float harm = new Random().nextInt(10);		// 伤害
	private float defence = new Random().nextInt(10);	// 防御力
	boolean isLive;	// 是否活着
	// 敌人创建
	public Enemy(String name, float hp, float harm, float defence){
		this.name = name;
		this.hp = hp;
		this.harm = harm;
		this.defence = defence;
		isLive = true;	// 敌人活着
		System.out.println("创建了一名新怪兽!");
	}
	public Enemy(String name){ // 默认属性
		this.name = name;
		isLive = true;	// 敌人活着
		System.out.println("创建了一名新怪兽!");
	}
	// 怪物死亡
	public void die(){
		this.hp = 0; 	// 生命归零
		isLive = false; // 变为死亡状态
		System.out.println(name + "死亡!");
	}
	public void attack(Role role){
		if ((harm - role.getDefence() > 0)) { // 本方伤害比对方防御高
			System.out.println(name + "对" + role.getName() + "造成了" + (harm-role.getDefence()) + "点伤害");
			if (role.getHp() > (harm-role.getDefence())) { // 攻击不致命
				role.setHp(role.getHp() - (harm-role.getDefence())); // 扣血
				System.out.println(role.getName() + "剩余" + role.getHp() + "滴血");
			}else{ // 攻击致命
				role.die();
			}
		}
	}
	public void attack() {
		System.out.println("怪物进行了攻击");
	}
	public void defend() {
		System.out.println("怪物进行了防御");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getDefence() {
		return defence;
	}
	public void setDefence(float defence) {
		this.defence = defence;
	}
	public float getHp() {
		return hp;
	}
	public void setHp(float hp) {
		this.hp = hp;
	}
	public float getHarm() {
		return harm;
	}
	public void setHarm(float harm) {
		this.harm = harm;
	}
}

Role.java:

/*
 * 角色类
 */
public class Role implements RoleInterface{
	String name;	// 名字
	float hp; 		// 血量
	float harm;		// 伤害
	float defence;	// 防御力
	EquipmentAttack equipment_attack;	// 武器装备
	EquipmentDefend equipment_defend;	// 防御装备
	boolean isLive;	// 生存还是死亡
	// 角色创建
	public Role(String name, float hp, float harm, float defence){
		this.name = name;
		this.hp = hp;
		this.harm = harm;
		this.defence = defence;
		isLive = true;
	}
	public Role(String name){ // 默认属性
		this.name = name;
		this.hp = 10;
		this.harm = 5;
		this.defence = 5;
		System.out.println("创建了一名新角色!");
		isLive = true;
		getAttr();
	}
	// 攻击敌人
	public boolean attack(Enemy enemy){
		if (!enemy.isLive) { 
			System.out.println(enemy.getName() + "已经死亡!");
			return false; 
		}
		int count = 0; // 防止陷入死循环
		while(true){
			if(count == 10){
				break;
			}
			if(getHp() <= 0){ // 我方败北
				System.out.println(name + "被" + enemy.getName() + "击败了!");
				die();
				return false;
			}
			if ((harm - enemy.getDefence() > 0)) { // 本方伤害比对方防御高
				System.out.println(name + "对" + enemy.getName() + "造成了" + (harm-enemy.getDefence()) + "点伤害");
				if (enemy.getHp() > (harm-enemy.getDefence())) { // 攻击不致命
					enemy.setHp(enemy.getHp() - (harm-enemy.getDefence())); // 扣血
					setHp(enemy.getHarm()-getDefence()>0?enemy.getHarm()-getDefence():0);
					System.out.println(enemy.getName() + "剩余" + enemy.getHp() + "滴血");
				}else{ // 攻击致命
					enemy.die();
					return true;
				}
			}
			count++;
		}
		return false;
	}
	// 携带武器
	public void setEquipment_attack(EquipmentAttack equipment_attack) {
		this.equipment_attack = equipment_attack;
		this.harm += equipment_attack.harm;
		System.out.println(name + "拿起一把" + equipment_attack.name);
	}
	// 穿戴防具
	public void setEquipment_defend(EquipmentDefend equipment_defend) {
		this.equipment_defend = equipment_defend;
		this.defence += equipment_defend.defence;
		System.out.println(name + "穿上一件" + equipment_defend.name);
	}
	// 查看属性
	public StringBuilder getAttr(){
		StringBuilder stringBuilder = new StringBuilder();
		// stringBuilder.append(name + "的名字为:" + name + "\n");
		stringBuilder.append(name + "的生命为:" + hp + "\n");
		stringBuilder.append(name + "的攻击为:" + harm + "\n");
		stringBuilder.append(name + "的防御为:" + defence + "\n");
		if (equipment_attack == null) {
			stringBuilder.append(name + "的武器为:无\n");
		}else{
			stringBuilder.append(name + "的武器为:" + equipment_attack.name + "\n");
		}
		if (equipment_defend == null) {
			stringBuilder.append(name + "的防具为:无" + "\n");
		}else{
			stringBuilder.append(name + "的防具为:" + equipment_defend.name + "\n");
		}
		return stringBuilder;
	}
	public void showAttr(){
		// System.out.println(name + "的名字为:" + name);
		System.out.println(name + "的生命为:" + hp);
		System.out.println(name + "的攻击为:" + harm);
		System.out.println(name + "的防御为:" + defence);
		if (equipment_attack == null) {
			System.out.println(name + "的武器为:无");
		}else{
			System.out.println(name + "的武器为:" + equipment_attack.name);
		}
		if (equipment_defend == null) {
			System.out.println(name + "的防具为:无");
		}else{
			System.out.println(name + "的防具为:" + equipment_defend.name);
		}
	}
	public void die(){ // 死亡
		this.hp = 0;
		isLive = false;
		System.out.println(name + "死亡");
	}
	@Override
	public void attack() {
		System.out.println("角色进行了攻击");
	}
	@Override
	public void defend() {
		System.out.println("角色进行了防御");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public EquipmentAttack getEquipment_attack() {
		return equipment_attack;
	}
	public EquipmentDefend getEquipment_defend() {
		return equipment_defend;
	}
	public float getHarm() {
		return harm;
	}
	public void setHarm(float harm) {
		this.harm = harm;
	}
	public float getDefence() {
		return defence;
	}
	public void setDefence(float defence) {
		this.defence = defence;
	}
	public float getHp() {
		return hp;
	}
	public void setHp(float hp) {
		this.hp = hp;
	}
}

EquipmentAttack.java:

/*
 * 武器装备类
 */
public class EquipmentAttack implements EquAttInterface{
	String name;	// 武器名字
	float harm = 0;	// 伤害
	Role owner;		// 武器的主人
	public EquipmentAttack(String name) {
		this.name = name;
		System.out.println("创建了一把" + name + ",攻击力为:" + harm);
	}
	public EquipmentAttack(String name, float harm){
		this.name = name;
		this.harm = harm;
		System.out.println("创建了一把" + name + ",攻击力为:" + harm);
	}
	private void showHarm(){
		System.out.println(name + "的伤害为:" + harm);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getHarm() {
		return harm;
	}
	public void setHarm(float harm) {
		this.harm = harm;
	}
}

EquipmentDefend.java:

/*
 * 防具装备类
 */
public class EquipmentDefend implements EquDefInterface {
	String name;	// 装备名字
	float defence = 0;	// 防御力
	public EquipmentDefend(String name) {
		this.name = name;
		System.out.println("创建了一件" + name + ",防御力为:" + defence);
	}
	public EquipmentDefend(String name, float defence){
		this.name = name;
		this.defence = defence;
		System.out.println("创建了一件" + name + ",防御力为:" + defence);
	}
	private void showDefence(){
		System.out.println(name + "的防御力为:" + defence);
	}
	public String getName() {
		return name;
	}
	public void setDefence(float defence) {
		this.defence = defence;
	}
	public float getDefence() {
		return defence;
	}
}

GameFrame.java

GameFrame.java:
import java.util.Optional;
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class GameFrame extends javafx.application.Application {
	static boolean isStart = false;	// 是否已经开始
	static boolean isHaveEnemy = false;		// 是否存在敌人
	static boolean isEquipExist = false; 	// 是否存在装备
	static boolean isCenter;	// 为简化游戏,游戏场景很少
	static boolean isNorth;		// 为简化游戏,游戏场景很少
	static boolean isSouth;		// 为简化游戏,游戏场景很少
	static boolean isWest;		// 为简化游戏,游戏场景很少
	static boolean isEast;		// 为简化游戏,游戏场景很少
	static int count = 0;		// 走路次数过多则死亡
	Button buttonUp, buttonDown, buttonLeft, buttonRight; // 上下左右按钮
	Button buttonEquip, buttonShow, buttonGetEquip, buttonAttack; // 右边按钮
	static TextArea gameArea;	// 游戏区域
	static Role role = null;
	static Enemy enemy = null;
	static EquipmentAttack sword = null;	// 武器
	static EquipmentDefend cloth = null;  	// 装备
	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane rootPane = new BorderPane(); // 边界布局面板
		rootPane.setPadding(new Insets(10)); // 边缘内侧空白距离
		gameArea = new TextArea();
		gameArea.setEditable(false);	// 不可修改
		gameArea.setPrefSize(800, 350); // 游戏画面大小
		gameArea.setStyle("-fx-background-color:red;-fx-text-fill:blue;");
		gameArea.setWrapText(true);	// 自动换行
		/*****设定进入游戏时的界面*****/
		// gameArea.setText("*********************************\n");
		Font font = new Font("Cambria", 45);
		gameArea.setFont(font);
		gameArea.appendText("\n\t 欢迎来到奇怪的小冒险游戏\n");
		gameArea.appendText("\n\t  请点击确认按钮开始游戏!");
		/************************/
		rootPane.setTop(gameArea);
		
		// 控制按钮
		GridPane controlPane = new GridPane();
		controlPane.setAlignment(Pos.CENTER);
		controlPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		controlPane.setHgap(5.5);
		controlPane.setVgap(5.5);
		
		int helpRowNumNum = 4;
		int buttonWidth = 60;	// 按钮的宽
		int buttonHeight = 50;	// 按钮的高
		buttonUp = new Button("上");
		buttonUp.setPrefSize(buttonWidth, buttonHeight);
		controlPane.add(buttonUp, helpRowNumNum+2, 0);
		// controlPane.setTop(buttonUp);
		buttonDown = new Button("下");
		buttonDown.setPrefSize(buttonWidth, buttonHeight);
		controlPane.add(buttonDown, helpRowNumNum+2, 2);
		// controlPane.setBottom(buttonDown);
		buttonLeft = new Button("左");
		buttonLeft.setPrefSize(buttonWidth, buttonHeight);
		controlPane.add(buttonLeft, helpRowNumNum+1, 1);
		// controlPane.setLeft(buttonLeft);
		buttonRight = new Button("右");
		buttonRight.setPrefSize(buttonWidth, buttonHeight);
		controlPane.add(buttonRight, helpRowNumNum+3, 1);
		// controlPane.setRight(buttonRight);
		Button buttonEnter = new Button("确定");
		buttonEnter.setPrefSize(buttonWidth, buttonHeight);
		controlPane.add(buttonEnter, helpRowNumNum+2, 1);
		
		rootPane.setLeft(controlPane);
		
		// 面板按钮
		GridPane attrPane = new GridPane();
		attrPane.setAlignment(Pos.CENTER);
		attrPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		attrPane.setHgap(5.5);
		attrPane.setVgap(5.5);
	
		int helpRowNumNum2 = 14;
		int buttonWidth2 = 110;
		int buttonHeight2 = 50;
		buttonShow = new Button("查看属性");
		buttonShow.setPrefSize(buttonWidth2, buttonHeight2);
		attrPane.add(buttonShow, 0+helpRowNumNum2, 1);
		buttonEquip = new Button("查看装备");
		buttonEquip.setPrefSize(buttonWidth2, buttonHeight2);
		attrPane.add(buttonEquip, 3+helpRowNumNum2, 1);
		buttonGetEquip = new Button("捡起装备");
		buttonGetEquip.setPrefSize(buttonWidth2, buttonHeight2);
		attrPane.add(buttonGetEquip, 0+helpRowNumNum2, 3);
		buttonAttack = new Button("攻击敌人");
		buttonAttack.setPrefSize(buttonWidth2, buttonHeight2);
		attrPane.add(buttonAttack, 3+helpRowNumNum2, 3);

		rootPane.setCenter(attrPane);
		
		/****************控制事件****************/
		// 点击攻击敌人
		buttonAttack.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(!isHaveEnemy){	// 没有敌人
					gameArea.appendText("\n这里没有敌人呀!\n");
				}else{
					if(role.attack(enemy)){
						gameArea.appendText("\n" + role.getName() + "击败了" + enemy.getName() + "!\n");
					}else{
						gameArea.appendText("\n" + role.getName() + "败给了" + enemy.getName() + "!\n");
						
						Alert alert = new Alert(AlertType.ERROR);
						alert.setTitle("死亡提示");
						alert.setHeaderText(null);
						alert.setContentText(role.getName() + "已经死亡!");
						Optional<ButtonType> result = alert.showAndWait();
						if (result.get()!=null) {
							System.exit(0);
						}
					};
				}
			}
		});
		// 点击捡起装备
		buttonGetEquip.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(!isStart){
					return;
				}
				if (isCenter && isEquipExist) { // 有装备存在
					role.setEquipment_attack(sword);
					StringBuilder stringBuilder = new StringBuilder();
					role.setEquipment_defend(cloth);
					stringBuilder.append(role.getName() + "拿起了一把" + sword.getName() + "。(攻击力+" + sword.getHarm() + ")\n");
					stringBuilder.append(role.getName() + "穿上了一件" + cloth.getName() + "。(防御力+" + cloth.getDefence() + ")\n");
					gameArea.appendText(stringBuilder+"");
					isEquipExist = false;
				}else{	// 没有装备
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append("没有装备可以捡起来。\n");
					gameArea.appendText(stringBuilder+"");
				}
			}
		});
		// 点击确定开始游戏
		buttonEnter.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				startGame(); // 开始游戏
			}
		});
		// 点击查看属性
		buttonShow.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				viewAttr(role);	// 
			}
		});
		// 点击查看装备
		 buttonEquip.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				viewEquip(role); // 查看装备
			}
		});
		// 按钮-上
		 buttonUp.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(!isStart){
					return;
				}
				produceEnemy();	// 生成敌人
				checkCount(); // 检验步数
				StringBuilder describe = new StringBuilder();
				if (isSouth) {	// 如果在南方
					describe.append("\n\n【中】你来到一座黑色城楼之前,城楼上刻着三个大字:酆都城。往南不远处有一座桥,桥上鬼影幢幢,但是却听不到半点声音,"
							+ "往北走进城楼只见一片黑漆漆的,只有少许暗黑色的火光若隐若现地闪烁着。");
					// gameArea.setText(describe+"");
					gameArea.appendText(describe+"");
					isCenter = false;
					isSouth = false;
				}else if (isCenter) {
					describe.append("\n\n【北】这里是鬼门大道,你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的。往南看去你勉强可以分辨出一个城楼的模样,"
							+ "往北隐约可以看到几盏灯笼,在浓雾中显得格外的诡异。路的两旁各有一栋木造的建筑,门都是半开的,但你看不清楚里面有什么。");
					// gameArea.setText(describe+"");
					gameArea.appendText(describe+"");
					isNorth = true;	// 来到北方
					isCenter = false; // 离开中央
				}else{
					count++;
					describe.append("\n\n你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的......");
					// gameArea.setText(describe+"");
					gameArea.appendText(describe+"");
				}
				checkEnemy();
			}
		});
		// 按钮-下
		 buttonDown.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					if(!isStart){
						return;
					}
					produceEnemy();	// 生成敌人
					checkCount(); // 检验步数
					StringBuilder describe = new StringBuilder();
					if(isNorth){ // 如果在北方
						describe.append("\n\n【中】你来到一座黑色城楼之前,城楼上刻着三个大字:酆都城。往南不远处有一座桥,桥上鬼影幢幢,但是却听不到半点声音,"
								+ "往北走进城楼只见一片黑漆漆的,只有少许暗黑色的火光若隐若现地闪烁着。");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isCenter = true;	// 来到中央
						isNorth = false;	// 离开北方
					}else if(isCenter) { // 如果在中央
						describe.append("\n\n【南】这里就是著名的阴间入口[鬼门关],在你的面前矗立着一座高大的黑色城楼,许多亡魂正哭哭啼啼地列队前进,"
								+ "因为一进鬼门关就无法再回阳间了。");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isSouth = true; 	// 来到南方
						isCenter = false;	// 离开中央
					}else{
						count++;
						describe.append("\n\n你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的......");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
					}
					checkEnemy();
				}
			});
		// 按钮-左
		 buttonLeft.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					if(!isStart){
						return;
					}
					produceEnemy();	// 生成敌人
					checkCount(); // 检验步数
					StringBuilder describe = new StringBuilder();
					if(isEast){ // 如果在东方
						describe.append("\n\n【中】你来到一座黑色城楼之前,城楼上刻着三个大字:酆都城。往南不远处有一座桥,桥上鬼影幢幢,但是却听不到半点声音,"
								+ "往北走进城楼只见一片黑漆漆的,只有少许暗黑色的火光若隐若现地闪烁着。");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isEast = false;
						isCenter = true;
					}else if(isCenter) { // 如果在中央
						describe.append("\n\n【西】这里是一家小店,你一进来就发现屋里异常的温暖,墙角壁炉里微弱的火光将你的影子投射在对面的墙上。"
								+ "几个人影围在炉旁不知在讨论些什么。屋里有几张木桌,椅,墙上挂了几幅字画,一切看来非常的祥和宁静,"
								+ "你几乎忘了自己身在何处。");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isWest = true;
						isCenter = false;
					}else{
						count++;
						describe.append("\n\n你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的......");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
					}
					checkEnemy();
				}
			});
		// 按钮-右
		 buttonRight.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					if(!isStart){
						return;
					}
					produceEnemy();	// 生成敌人
					checkCount(); // 检验步数
					StringBuilder describe = new StringBuilder();
					if(isWest){ // 如果在西方
						describe.append("\n\n【中】你来到一座黑色城楼之前,城楼上刻着三个大字:酆都城。往南不远处有一座桥,桥上鬼影幢幢,但是却听不到半点声音,"
								+ "往北走进城楼只见一片黑漆漆的,只有少许暗黑色的火光若隐若现地闪烁着。");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isCenter = true;
						isWest = false;
					}else if(isCenter) {
						describe.append("\n\n【东】你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的......");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
						isCenter = false;
						isEast = true;
					}else {
						count++;
						describe.append("\n\n你走在一条阴森森的路上,浓浓的雾环绕在你的四周,好像永远都不会散去似的......");
						// gameArea.setText(describe+"");
						gameArea.appendText(describe+"");
					}
					checkEnemy();
				}
			});
		/****************控制事件****************/
		
		Scene scene = new Scene(rootPane, 800, 550); // 游戏机整体大小
		primaryStage.setTitle("小冒险游戏");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	// 游戏开始
	public static void startGame(){
		if(isStart){
			return;
		}
		Font font = new Font("Cambria", 15);
		gameArea.setFont(font);
		gameArea.setText("\n\n\t\t\t   游戏开始");
		gameArea.setStyle("-fx-background-color:red;-fx-text-fill:green;");
	
		TextInputDialog dialog = new TextInputDialog("勇者");
		dialog.setTitle("姓名输入");
		dialog.setHeaderText(null);
		dialog.setContentText("请输入角色的名字:");
		Optional<String> result = dialog.showAndWait();
		String name = "";
		if (result.isPresent()){
			name = result.get(); // 获取输入的名字
		}else{
			return;
		}
		
		role = new Role(name);
		isStart = true;	// 创建完角色则游戏开始
		isCenter = true; // 在中央
		String describe = "你创建了一个角色 -----" + role.getName() + "\n";
		describe += role.getAttr();
		describe += "\n" + role.getName() + "揉揉眼睛,迷惘的望着这个陌生的世界。\n";
		// describe += "\n这里是精灵庭院,茂密美丽的丛林和可爱的居民使得这里如同世外桃源般幽静祥和,然而在树影的深处,亦有危机和阴谋在暗处酝酿。\n";
		describe += "\n你来到一座黑色城楼之前,城楼上刻着三个大字:酆都城。往南不远处有一座桥,桥上鬼影幢幢,但是却听不到半点声音,"
				+ "往北走进城楼只见一片黑漆漆的,只有少许暗黑色的火光若隐若现地闪烁着。\n";
		
		sword = new EquipmentAttack("长剑",5);
		cloth = new EquipmentDefend("布衣",3);
		describe += "地上有一把" + sword.getName() + "(攻击力为:" +sword.getHarm()+ ")。\n";
		describe += "地上有一件" + cloth.getName() + "(防御力为:" + cloth.getDefence() +")。\n";
		isEquipExist = true;

		// describe += "(请按方向键移动)\n";
		gameArea.setText(describe);
	}
	// 查看属性
	public static void viewAttr(Role role){
		if (!isStart) {
			return;
		}
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(role.getName() + "的生命为:" + role.getHp() + "\n");
		stringBuilder.append(role.getName() + "的攻击为:" + role.getHarm() + "\n");
		stringBuilder.append(role.getName() + "的防御为:" + role.getDefence() + "\n");
		gameArea.appendText("\n" + stringBuilder);
	}
	// 查看装备
	public static void viewEquip(Role role){
		if (!isStart) {
			return;
		}
		produceEnemy();	// 生成敌人
		StringBuilder stringBuilder = new StringBuilder();
		if (role.getEquipment_attack() == null) {
			stringBuilder.append(role.name + "的武器为:无\n");
		}else{
			stringBuilder.append(role.name + "的武器为:" + role.getEquipment_attack().name + "\n");
		}
		if (role.getEquipment_defend() == null) {
			stringBuilder.append(role.name + "的防具为:无" + "\n");
		}else{
			stringBuilder.append(role.name + "的防具为:" + role.getEquipment_defend().name + "\n");
		}
		gameArea.appendText("\n" + stringBuilder);
	}
	// 检查寻路次数,次数 > 5 次直接死亡
	public static void checkCount(){
		System.out.println("距离死亡:" + (5-count));
		if (count > 5) {
			Alert alert = new Alert(AlertType.ERROR);
			alert.setTitle("死亡提示");
			alert.setHeaderText(null);
			alert.setContentText(role.getName() + "已经死亡!");
			Optional<ButtonType> result = alert.showAndWait();
			if (result.get()!=null) {
				System.exit(0);
			}
		}
	}
	// 检查是否有人敌人
	public static void checkEnemy(){
		if (isHaveEnemy) {
			gameArea.appendText("\n" + enemy.getName() + "(生命:" + enemy.getHp() +", 攻击:"+enemy.getHarm()+", 防御"+enemy.getDefence() + ")正在盯着你看!\n");
		}else{
			gameArea.appendText("\n这里没有敌人存在!");
		}
	}
	static String[] enemyName = {"魔王", "小兵", "恶魔护卫", "神秘人"};
	// 生成敌人
	public static void produceEnemy(){
		if (new Random().nextInt(7) == 5) {
			enemy = new Enemy(enemyName[new Random().nextInt(3)]); // 产生随机数
			isHaveEnemy = true;		// 存在敌人
		}
	}
	public static void main(String[] args) {
		Application.launch(args);	// 启动独立的JavaFx程序
	}
}

本文标签: 冒险游戏文字项目JavaFX