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

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

本来想把它移植到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 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

java冒险游戏_Java冒险小游戏相关推荐

  1. java走棋_Java五子棋小游戏(控制台纯Ai算法)

    Java五子棋小游戏(控制台纯Ai算法) 继续之前的那个五子棋程序 修复了一些已知的小Bug 这里是之前的五子棋程序 原文链接 修复了一些算法缺陷 本次增加了AI算法 可以人机对战 也可以Ai对Ai看 ...

  2. java太阳系_Java太阳系小游戏分析和源代码

    Java太阳系小游戏分析和源代码 -20150809 近期看了面向对象的一些知识.然后跟着老师的解说做了一个太阳系各行星绕太阳转的小游戏,来练习巩固一下近期学的知识: 用到知识点:类的继承.方法的重载 ...

  3. java 桌球_Java桌球小游戏

    版本一.出现窗口 package cn.xjion.game; /** * 出现窗口 * @author xjion * */ import java.awt.*; import javax.swin ...

  4. java简单通讯录的实现02person类_用java实现简单的小游戏(你一定玩过)

    用java实现简单的小游戏(你一定玩过) 对于java初学者来说,通过一些学习小游戏来对swing学习以及对java基础的学习是一个好的方法,同时也给学习带来了很多的乐趣,接下来就给大家分享一个jav ...

  5. java实现简单窗体小游戏----球球大作战

    java实现简单窗体小游戏----球球大作战 需求分析 1.分析小球的属性: ​ 坐标.大小.颜色.方向.速度 2.抽象类:Ball ​ 设计类:BallMain-创建窗体 ​ BallJPanel- ...

  6. main java game,playgame 一个JAVA编写的飞行小游戏,有基本完整的 框架,适合初学者参照学习 Other s 其他 238万源代码下载- www.pudn.com...

    文件名称: playgame下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 7050 KB 上传时间: 2013-06-06 下载次数: 3 提 供 者: Lyq ...

  7. 简易贪吃蛇小游戏java版_用GUI实现java版贪吃蛇小游戏

    本文实例为大家分享了java版贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 项目结构 新建一个JFrame窗口,作为程序入口 public class GameStart{ public stat ...

  8. java演练 猜奇偶小游戏开发 DB游戏必输的设计

    java演练 猜奇偶小游戏开发 DB游戏必输的设计 阶段一,视频 https://www.ixigua.com/6870390946270446088?logTag=J_BVJOm_LIpQ-hWYY ...

  9. java2048小游戏源码及解析_200行java代码实现2048小游戏

    本文实例为大家分享了java实现2048小游戏的具体代码,供大家参考,具体内容如下 效果图: 游戏介绍: 1.2048是一款益智类小游戏,刚开始随机出现两个数字,可以上下左右控制数字的移动. 2.当选 ...

最新文章

  1. 各种基本的排序算法在Object-C实现
  2. C语言程序模拟银行输入密码,模拟银行输入密码--源码
  3. “面试不败计划”:集合、日期、异常、序列化、其他知识点
  4. Shell入门(十)之echo
  5. 判断字符串相等能否用==
  6. SpringCloud 从菜鸟到大牛之五 统一配置中心 Spring Cloud Config
  7. lua调用shell 脚本
  8. 关于@media不生效的问题和meta总结
  9. linux 汇编语言ldreq,请教一个中断句柄的问题 - ARM技术论坛-ARM嵌入式论坛-人气最火爆ARM学习论坛 - 21ic电子技术开发论坛...
  10. Mac工具PullTube如何在下载列表中创建重复项
  11. python求最值_Python应用:python求极值点(波峰波谷)
  12. 本科毕业论文多久能写完 计算机,论文初稿几天可以写完-你觉得十天时间能不能写完一篇本科毕业论文?为什么?...
  13. 网易邮箱发送失败服务器连接失败,网易邮件发送不出去MI:SFQ错误
  14. MPI大漩涡(单纯的floyd)
  15. [iOS] Windows 使用IPSW文件升级iOS 13 beta
  16. 策略模式Java实现
  17. mysql怎么集合查询_MySql集合查询
  18. oracle ora 31644,dmp文件损坏导致ORA-39014 ORA-39029 ORA-31693错误
  19. 毕业论文评审意见范例
  20. Debian安装和配置chrony服务器

热门文章

  1. Android 操作系统中的内存回收
  2. PRIME TIME官方教程笔记(静态时序分析)(二)
  3. 【Windows服务删除“服务“】
  4. bitdefender比特梵德全方位安全杀毒软件-----完整保护您的计算机
  5. 解剖常用软件程序都用什么语言开发
  6. 软件外包平台有哪些?
  7. 越狱第三季第8集最新预告版
  8. 实训第五天:播放器现目的实现
  9. 班级纪念册php源码,我们的班级毕业纪念册个人主页怎么做好
  10. WPF动画设计2—卷轴动画