代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
模拟RPG格斗
作者:
Lxi
/ 发布于
2014/9/10
/
275
单纯的模拟,无图无声。心血来潮想实现一下,没有完全符合要求,随便看看吧
package com.lxi; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Random; abstract class Person { int val; double coldTime; int waitTime; int fight; int chanceHit; int chanceDefense; abstract void hit(Person p); abstract int defense(Person p); } class DH extends Person { public DH() { val = 600; coldTime = 1.0; fight = 30; chanceHit = 3; chanceDefense = 3; waitTime = 0; } Random rand = new Random(); boolean hitFlag = false; boolean defenseFlag = false; public void hit(Person p) { if (rand.nextInt(10) < chanceHit) { int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { System.out.println(this.getClass().getSimpleName() + "胜出!"); System.exit(0); } val = val + hurt; if (val > 600) val = 600; hitFlag = true; } else { int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { System.out.println(this.getClass().getSimpleName() + "胜出!"); System.exit(0); } } System.out.println(this.getClass().getSimpleName() + "攻击" + p.getClass().getSimpleName() + "," + this.getClass().getSimpleName() + (this.hitFlag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getClass().getSimpleName() + (this.defenseFlag ? "发动防御技能 " : "未发动防御技能 ") + this.getClass().getSimpleName() + ":" + this.val + "," + p.getClass().getSimpleName() + ":" + p.val); hitFlag = false; defenseFlag = false; } public int defense(Person p) { if (rand.nextInt(10) < chanceDefense) { defenseFlag = true; return 0; } else { return p.fight; } } } class BM extends Person { public BM() { val = 650; coldTime = 1.5; fight = 40; chanceHit = 3; chanceDefense = 3; waitTime = 0; } int count = 0; int temp = 40; boolean hitFlag = false; boolean defenseFlag = false; Random rand = new Random(); public void hit(Person p) { if (rand.nextInt(10) < chanceHit) { fight = fight * 2; hitFlag = true; } int hurt = p.defense(this); p.val = p.val - hurt; fight = temp; if (p.val <= 0) { System.out.println(this.getClass().getSimpleName() + "胜出!"); System.exit(0); } System.out.println(this.getClass().getSimpleName() + "攻击" + p.getClass().getSimpleName() + "," + this.getClass().getSimpleName() + (this.hitFlag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getClass().getSimpleName() + (this.defenseFlag ? "发动防御技能 " : "未发动防御技能 ") + this.getClass().getSimpleName() + ":" + this.val + "," + p.getClass().getSimpleName() + ":" + p.val); hitFlag = false; defenseFlag = false; } public int defense(Person p) { if (rand.nextInt(10) < chanceDefense) { if (count != 0) { p.val = p.val - p.fight; count++; defenseFlag = true; if (p.val <= 0) { System.out.println(this.getClass().getSimpleName() + "胜出!"); System.exit(0); } } } return p.fight; } } class MK extends Person { public MK() { val = 700; coldTime = 2.5; fight = 50; chanceDefense = 6; chanceHit = 3; waitTime = 0; } boolean hitFlag = false; boolean defenseFlag = false; Random rand = new Random(); public void hit(Person p) { if (rand.nextInt(10) < chanceHit) { p.waitTime = 3; hitFlag = true; } int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { System.out.println(this.getClass().getSimpleName() + "胜出!"); System.exit(0); } System.out.println(this.getClass().getSimpleName() + "攻击" + p.getClass().getSimpleName() + "," + this.getClass().getSimpleName() + (this.hitFlag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getClass().getSimpleName() + (this.defenseFlag ? "发动防御技能 " : "未发动防御技能 ") + this.getClass().getSimpleName() + ":" + this.val + "," + p.getClass().getSimpleName() + ":" + p.val); hitFlag = false; defenseFlag = false; } public int defense(Person p) { if (rand.nextInt(10) < chanceDefense) { defenseFlag = true; return p.fight / 2; } return p.fight; } } public class Rpg { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { System.out.println("在这里输入两个人物进行PK,以英文逗号分隔: [BM,DH,MK]"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Class<Person> c1; Class<Person> c2; try { String temp = br.readLine(); String[] str = temp.split(","); if (str.length != 2) { throw new Exception("输入格式有误,按默认PK"); } c1 = (Class<Person>) Class.forName("com.lxi." + str[0].toUpperCase()); c2 = (Class<Person>) Class.forName("com.lxi." + str[1].toUpperCase()); } catch (Exception e) { // TODO Auto-generated catch block c1 = (Class<Person>) Class.forName("com.lxi.BM"); c2 = (Class<Person>) Class.forName("com.lxi.DH"); } try { Person p1 = c1.newInstance(); Person p2 = c2.newInstance(); long time = System.currentTimeMillis(); long nextTime1 = (long) (time + p1.coldTime); long nextTime2 = (long) (time + p2.coldTime); System.out.println("---游戏开始---"); while (true) { long currenTime = System.currentTimeMillis(); if (nextTime1 < currenTime) { p1.hit(p2); nextTime1 += p1.coldTime + p1.waitTime; p1.waitTime = 0; } if (nextTime2 < currenTime) { p2.hit(p1); nextTime2 += p2.coldTime + p2.waitTime; p2.waitTime = 0; } } } catch (ClassCastException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
试试其它关键字
RPG
格斗
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Lxi
贡献的其它代码
(
1
)
.
模拟RPG格斗
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3