java冒险游戏_Java冒险小游戏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_40176716/article/details/83153653

本来想把它移植到JAVA GUI文字游戏里,貌似空余时间越来越少,只能提前发出来了

JAVA GUI文字游戏 应该是会开发下去。。。欢迎关注点赞评论啥的

先上两张图

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

开始

结束

直接上代码吧

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 enemys = new ArrayList();

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("结束");

}

}

}

————————————————

版权声明:本文为CSDN博主「莫言情难忘」的原创文章,遵循 CC 4.0 BY-SA

版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_40176716/article/details/83153653

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值