现在我们来写一个游戏
这个游戏类似于 赛尔号 或者是 洛克王国的那种双方人物 都有 4个技能 一个普通攻击 每个人物的技能都有所不同 使用技能需要指定的蓝量 普通攻击就是简答的攻击 打出的伤害值是角色本身伤害值的2倍~3倍之间且可以恢复50点蓝量 双方使用策略 进行对战 直到将敌方杀死 赢得胜利
创建人物类
人物类有一下的成员
字段
- 当前血量
- 最大血量
- 当前蓝量
- 最大蓝量
- 技能的名称与技能介绍组成的二维表数组
- 技能可用次数的数组
- 游玩玩家
方法
- 普通攻击
- 伤害
- 技能1
- 技能2
- 技能3
- 电脑攻击的规律
- 还有一些私有的临时字段 我这里就不介绍了 这些字段往往只是为了实现逻辑层面而存在的字段
-
其中我们可以给 血量 蓝量 建立一个访问器(就是属性的封装);
-
然后技能的话我们可以返回一个数组
· 数组下标为0的数值 代表技能的类型为 -1 就是技能释放了 这种技能一般是对自己打的 就像是自身血量 -20
为 0 就是打出去了 并且向对面打出去的 就像是对敌方的伤害
为 1 就是由于特质的原因 技能无法被释放 需要重新使用其他技能我们来写个例子 比如 某个人物的技能是这样子的:敌方受到自身攻击能力2倍伤害 , 自身回血 50
// this._hp 是 自身的当前血量
// set_hp 是自身血量的访问器(字段的封装属性)
// shangHai 是自身的攻击能力
public int[] SkillA(){
this.set_hp(this.get_hp() + 50); // 回血
int temp = this.shangHai * 2;
return {0,temp};
}
数组下标为1的数值代表技能的伤害值
然后我们在写个游戏类,在游戏类中分析技能的类型 对双方进行与伤害等值的血量扣除
咱先撸一个人物父类出来看看?
package game.chara;// 申明一下包
import java.util.Random;
public class CharaFather {
// 定义一个父类
public int _maxHp;// 最大的血量
public int _hp; // 当前的血量
public String _player; // 游玩玩家
public int _hitValue; // 伤害
public int _maxMp; // 最大的蓝量
public int _mp; // 当前人物的蓝量
public int _shangHai; // 人物的伤害
public int[] _skillCount; // 技能可用次数
public int _temp_mpHistory; // 私有字段 临时的蓝量记录
public String[][] _skillInfo;
public Random rand = new Random(); // 随机数模块
// region 访问器的设置
public int get_hp() {
return _hp;
}
public void set_hp(int _hp) {
if (_hp > this.get_hp()){
System.out.println(this._player+"获得了"+(_hp-this.get_hp())+"的 HP");
}else if(_hp < this.get_hp()){
System.out.println(this._player+"失去了"+(this.get_hp()-_hp)+"的 HP");
}
if (_hp > this._maxHp){
this._hp = this._maxHp;
// 如果设置的血量大于了最大的血量 那么就设置一个上限
}else {
this._hp = _hp;
}
}
public int get_mp() {
return _mp;
}
public void set_mp(int _mp) {
if (_mp > this.get_mp()){
System.out.println(this._player+"获得了"+(_hp-this.get_mp())+"的 MP");
}else if(_mp < this.get_mp()){
System.out.println(this._player+"失去了"+(this.get_mp()-_mp)+"的 MP");
}
if (_mp > this._maxMp){
this._mp = this._maxMp;
// 如果设置的蓝量大于了最大的血蓝 那么就设置一个上限
}else if(_mp < 0) {
System.out.print("蓝量不足!");
this._temp_mpHistory = this.get_mp();
this._mp = -1;
}else {
this._mp = _mp;
}
}
// endregion
public int[] SkillA(){
// 技能1
return new int[]{0,100};
}
public int[] SkillB(){
// 技能2
return new int[]{0,100};
}
public int[] SkillC(){
// 技能3
return new int[]{0,100};
}
public int[] Hit(){
// 普通攻击
int temp = rand.nextInt( this._hitValue*2- this._hitValue*3 + 1) + this._hitValue*2;
// 普通 攻击
this.set_mp(this.get_mp() + 50); // 蓝量回复50
return new int[]{0,temp};
}
public int[] AIHit(){
//电脑出招
return new int[]{0,100};
}
}
根据这个父类 我们创建一个战士类如何?
ppackage game.chara;
import java.util.Random;
/*
战士人物的简介
初始血量:40
初始蓝量:100
攻击力: 5
技能一:
名称:打击
消耗蓝量: 30
简介:攻击敌方 10 ~ 20 的随机伤害
技能二:
名称:护盾
消耗蓝量: 100
简介:最大血量增加10 血量回复30
技能三:
名称:舍命
消耗蓝量: 60
简介:自身失去10点血量 增加 3 点攻击力
AI 攻击的方法:
如果 可以使用2 技能 那么:
二技能
如果二技能没法使用:
继续
可以:
返回
如果 蓝量 > 60 且 血量 > 20:
三技能
如果三技能没法使用:
继续
可以:
返回
否则使用:
一技能
如果一技能没法使用:
继续
可以:
返回
使用普通攻击 并返回数组
*/
public class ZhanShi extends CharaFather{
// 创建一个战士类 继承于CharaFather类
public ZhanShi(){
// 战士的构造器
super();
this._skillCount = new int[]{99,1,99};
this._hp = 40;
this._maxHp = this._hp;
this._mp = 100;
this._maxMp = this._mp;
this._shangHai = 5;
this._skillInfo = new String[][]{
{"打击","消耗蓝量: 30\n攻击敌方 10 ~ 20 的随机伤害"},
{"护盾","消耗蓝量: 100\n 只能使用一次 最大血量增加10"},
{"舍命","消耗蓝量: 60\n自身失去10点血量 增加 3 点攻击力"}
};
}
public int[] SkillA(){
// 技能1 的重写
if (this._skillCount[0] >= 0) {
// 如果技能可用次数剩余大于0
this._skillCount[0]--;
this.set_mp(this.get_mp() - 50);
if (this.get_mp() == -1) {
// 出现蓝量不足的情况
this.set_mp(this._temp_mpHistory); // 使蓝量回复到使用技能之前的状态
return new int[]{1, 0};
// 返回技能失败的数组
} else {
// 技能得以释放的情况
int _tempValue = 10 + this.rand.nextInt(10); // 生成 10 - 20 的随机伤害
return new int[]{0, _tempValue};
// 发出技能
}
}else {
return new int[]{1,0};
}
}
public int[] SkillB() {
// 技能二的重10
if (this._skillCount[1] >= 0 ) {
this._skillCount[1]--;
this.set_mp(this.get_mp() - 100);
if (this.get_mp() == -1) {
this.set_mp(_temp_mpHistory);
return new int[]{1, 0};
} else {
this._maxHp += 80;
return new int[]{0, 0};
// 技能打出 但是没有伤害
}
}else{
return new int[]{1,0};
}
}
public int[] SkillC() {
if(this._skillCount[2]>=0) {
this.set_mp(this.get_mp() - 60);
if (this.get_mp() == -1) {
this.set_mp(this._temp_mpHistory);
return new int[]{1, 0};
} else {
if (this.get_hp() > 10) {
this.set_hp(this.get_hp() - 10);
this._shangHai += 3;
return new int[]{0, 0};
} else {
System.out.println("血量不足~");
return new int[]{1, 0};
}
}
}
else
{
return new int[]{1,0};
}
}
public int[] AIHit(){
if (this._skillCount[1] > 0){
int[] tempA = this.SkillB();
if (tempA[0] == 1){
// 技能释放失败
}else{
return tempA;
}
}
else if (this._skillCount[2] > 0 && this.get_mp() > 60 && this.get_hp() > 20){
int[] tempB = this.SkillC();
if (tempB[0] == 1) {
// 技能再次释放失败
}else {
return tempB;
}
}
else {
int[] tempC = this.SkillA();
if (tempC[0] == 1){
}else {
return tempC;
}
}
return this.Hit();
}
}
既然可以这样 那么你也来动手试试写吧
诗人的简介
初始血量:35
初始蓝量:80
攻击力: 10
- 技能一:
名称:歌颂
消耗蓝量: 20
简介:恢复自身1~10点血量- 技能二:
名称:护佑
消耗蓝量: 10
简介: 血量上限减少10 并且 回满血量 只能使用3次- 技能三:
名称:恩赐
消耗蓝量: 60
简介: 打出 10 ~ 12 点伤害 并增加攻击力 1- AI 攻击的方法:
if 血量上限 - 当前血量 > 10:
使用2技能()
if 2技能不能被打出:
pass # 继续
elif 向量上限 - 当前血量 > 20:
使用1技能()
if 1技能不能被打出:
使用3技能()
如果三技能不能被打出:
使用普通攻击()
这就是诗人代码
package game.chara;
import java.util.Random;
public class ShiRen extends CharaFather{
// 创建一个战士类 继承于CharaFather类
public ShiRen(){
// 战士的构造器
super();
this._skillCount = new int[]{99,3,99};
this._hp = 35;
this._maxHp = this._hp;
this._mp = 80;
this._maxMp = this._mp;
this._shangHai = 10;
this._skillInfo = new String[][]{
{"歌颂","消耗蓝量: 20\n回复自身 1~10"},
{"护盾","消耗蓝量: 10\n血量上限减少10 并且 回满血量 只能使用3次"},
{"恩赐","消耗蓝量: 60\n打出 10 ~ 12 点伤害 并增加攻击力 1"}
};
}
public int[] SkillA(){
// 技能1 的重写
if (this._skillCount[0] >= 0) {
// 如果技能可用次数剩余大于0
this._skillCount[0]--;
this.set_mp(this.get_mp() - 20);
if (this.get_mp() == -1) {
// 出现蓝量不足的情况
this.set_mp(this._temp_mpHistory); // 使蓝量回复到使用技能之前的状态
return new int[]{1, 0};
// 返回技能失败的数组
} else {
// 技能得以释放的情况
int randTemp = rand.nextInt(9) + 1;
this.set_hp(this.get_hp() + randTemp);
return new int[]{0,0};
// 发出技能
}
}else {
return new int[]{1,0};
}
}
public int[] SkillB() {
if (this._skillCount[1] >= 0 ) {
this._skillCount[1]--;
this.set_mp(this.get_mp() - 10);
if (this.get_mp() == -1) {
this.set_mp(_temp_mpHistory);
return new int[]{1, 0};
} else {
this._maxHp -= 10;
this.set_hp(_maxHp);
return new int[] {0,0};
}
}else{
return new int[]{1,0};
}
}
public int[] SkillC() {
if(this._skillCount[2]>=0) {
this.set_mp(this.get_mp() - 60);
if (this.get_mp() == -1) {
this.set_mp(this._temp_mpHistory);
return new int[]{1, 0};
} else {
int temp = rand.nextInt(2) + 10;
this._shangHai += 1;
return new int[]{0,temp};
}
}
else
{
return new int[]{1,0};
}
}
public int[] AIHit(){
if (this._maxHp - this.get_hp() > 10){
int[] tempA = this.SkillB();
if (tempA[0] != 1){
return tempA;
}
}
else if(this._maxHp - this.get_hp() > 20){
int[] tempB = this.SkillA();
if (tempB[0] == 1){
int[] tempC = this.SkillC();
if (tempC[0] == 1){
return this.Hit();
}
else {
return tempC;
}
}
else {
return tempB;
}
}
return this.Hit();
}
}
目前我们已经完成两个人物了,接下来我们开始写游戏运行类了(你可以自己再写一 俩个人物练练手)
创建游戏类
正常的游戏流程是:
根据这个流程图 我们就开始了游戏类 的开发
import game.chara.*;
import java.util.Random;
import java.util.Scanner;
public class gameMain {
CharaFather userChara;// 用户的角色 - 这里我们使用了多态 因为可能是多种数据类型的变量
CharaFather computerChara; // 电脑的角色
Scanner input = new Scanner(System.in); // 输入模块的实例
Random rand = new Random();
void ChooseChara(){
// region 玩家选择
String outText = "请输入你的人物编号 \n 1、战士 \n 2、诗人 \n输入数字确认角色:"; //使用\n可以模拟回车
System.out.print(outText);
String userChoose = input.nextLine(); // 等待用户的输入
if (userChoose.equals("1")){
// 选择了战士
this.userChara = new ZhanShi();
}else if(userChoose.equals("2")){
this.userChara = new ShiRen();
}
// endregion
// region 电脑选择
int computerRandomNumber = rand.nextInt(2); // 生成一个 0 - 2 不包括2的随机数
if (computerRandomNumber == 1){
this.computerChara = new ZhanShi();
System.out.println("电脑选择了战士");
}else{
this.computerChara = new ShiRen();
System.out.println("电脑选择了诗人");
}
// endregion
this.computerChara._player = "电脑";
this.userChara._player = "玩家";
}
void showInfo(){
// 提示字符串生成
String temp = "目前在场角色信息一览:\n玩家:\n hp:" + this.userChara.get_hp() +
"\n mp:" + this.userChara.get_mp() +"\n maxHp:"+ this.userChara._maxHp+"\n maxMp:"+ this.userChara._maxMp+
"\n电脑:\n hp:" + this.computerChara.get_hp() +"\n maxHp:"+ this.computerChara._maxHp+"\n maxMp:"+ this.computerChara._maxMp +
"\n mp:" + this.computerChara.get_mp() + "\n";
System.out.print(temp);
}
void userMkSkill(){
// 玩家释放技能的方法
showInfo();
for (int i = 0;i < userChara._skillInfo.length;i++){
for (int j = 0; j < userChara._skillInfo[i].length;j++){
System.out.print((i+1) + ":"+ userChara._skillInfo[i][j]+" ");
}
System.out.println("");
System.out.println("");
}
while(true) {
System.out.print("请输入你的技能编号:");
String temp = input.nextLine();
if (temp.equals("1")) {
// 使用技能1
int[] ret = userChara.SkillA();
if (ret[0] == 0){
// 对敌有效技能
this.computerChara.set_hp(this.computerChara.get_hp() - ret[1]);
break;
}else if(ret[0] == -1){
// 自身有效技能
this.userChara.set_hp(this.userChara.get_hp() - ret[1]);
}else{
System.out.print("技能使用无效请重新输入");
}
}else if(temp.equals("2")){
// 使用技能2
int[] ret = userChara.SkillB();
if (ret[0] == 0){
// 对敌有效技能
this.computerChara.set_hp(this.computerChara.get_hp() - ret[1]);
break;
}else if(ret[0] == -1){
// 自身有效技能
this.userChara.set_hp(this.userChara.get_hp() - ret[1]);
}else{
System.out.print("技能使用无效请重新输入");
}
}else if(temp.equals("3")){
// 使用技能2
int[] ret = userChara.SkillC();
if (ret[0] == 0){
// 对敌有效技能
this.computerChara.set_hp(this.computerChara.get_hp() - ret[1]);
break;
}else if(ret[0] == -1){
// 自身有效技能
this.userChara.set_hp(this.userChara.get_hp() - ret[1]);
}else{
System.out.print("技能使用无效请重新输入");
}
}else {
System.out.print("错误的技能编号~");
}
}
}
void comMKSkill(){
int[] ret = computerChara.AIHit();
if (ret[0] == -1){
this.computerChara.set_hp(this.computerChara.get_hp() - ret[1]);
}else{
this.userChara.set_hp(this.userChara.get_hp() - ret[1]);
}
}
void Game(){
while (userChara.get_hp() != 0 && computerChara.get_hp() != 0){
this.userMkSkill(); // 我方使用技能
this.comMKSkill(); // 敌方使用技能
}
}
public static void main(String[] args) {
gameMain t = new gameMain();
t.ChooseChara();
t.Game();
}
}