java写一个二人PK游戏,给两个人初始血量,每次二人随即互殴,每次掉的血在一定范围内随机import java.util.Random;
public class Game {
public static void main(String[] args) {
System.out.println("比赛开始");
Person p1 = new Person("刘邦", 100, 10);
Person p2 = new Person("项羽", 100, 10);
while (p1.getBlood()0p2.getBlood()0) {
Random random = new Random();
if (random.nextInt(100)50) {
p1.beat(p2);
p2.beat(p1);
} else {
p2.beat(p1);
p1.beat(p2);
}
}
p1.printState();
p2.printState();
System.out.println("game over!");
}
}
class Person {
private int blood;// 初始血量
private int range;// 掉血范围
private String name;// 姓名
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
// 构造函数
public Person(String name, int blood, int range) {
this.name = name;
this.blood = blood;
this.range = range;
}
// 击打
public void beat(Person person) {
int bleeding = person.hurt();
System.out
.println(String.format("%s打了%s一耳光,%s流了%s滴血 。", this.name, person.getName(), person.getName(), bleeding));
}
// 被打,受伤
public int hurt() {
Random random = new Random();
int bleeding = random.nextInt(this.range)1;
if (bleedingthis.blood) {
bleeding = this.blood;
}
this.blood -= bleeding;
return bleeding;
}
// 输出状态
public void printState() {
if (this.blood == 0) {
System.out.println(String.format("%s被打死了", this.name));
} else {
System.out.println(String.format("%s还剩%s滴血", this.name, this.blood));
}
}
}
结果(随机):
比赛开始
刘邦打了项羽一耳光,项羽流了6滴血 。
项羽打了刘邦一耳光,刘邦流了4滴血 。
刘邦打了项羽一耳光,项羽流了8滴血 。
项羽打了刘邦一耳光,刘邦流了2滴血 。
刘邦打了项羽一耳光,项羽流了9滴血 。
项羽打了刘邦一耳光,刘邦流了7滴血 。
项羽打了刘邦一耳光,刘邦流了8滴血 。
刘邦打了项羽一耳光,项羽流了9滴血 。
刘邦打了项羽一耳光,项羽流了1滴血 。
项羽打了刘邦一耳光,刘邦流了10滴血 。
刘邦打了项羽一耳光,项羽流了2滴血 。
项羽打了刘邦一耳光,刘邦流了7滴血 。
项羽打了刘邦一耳光,刘邦流了7滴血 。
刘邦打了项羽一耳光,项羽流了2滴血 。
刘邦打了项羽一耳光,项羽流了1滴血 。
项羽打了刘邦一耳光,刘邦流了7滴血 。
刘邦打了项羽一耳光,项羽流了10滴血 。
项羽打了刘邦一耳光,刘邦流了9滴血 。
项羽打了刘邦一耳光,刘邦流了5滴血 。
刘邦打了项羽一耳光,项羽流了1滴血 。
刘邦打了项羽一耳光,项羽流了5滴血 。
项羽打了刘邦一耳光,刘邦流了8滴血 。
刘邦打了项羽一耳光,项羽流了9滴血 。
项羽打了刘邦一耳光,刘邦流了6滴血 。
项羽打了刘邦一耳光,刘邦流了5滴血 。
刘邦打了项羽一耳光,项羽流了6滴血 。
项羽打了刘邦一耳光,刘邦流了5滴血 。
刘邦打了项羽一耳光,项羽流了6滴血 。
项羽打了刘邦一耳光,刘邦流了4滴血 。
刘邦打了项羽一耳光,项羽流了7滴血 。
刘邦打了项羽一耳光,项羽流了4滴血 。
项羽打了刘邦一耳光,刘邦流了6滴血 。
刘邦被打死了
项羽还剩14滴血
game over!
全手写,望采纳,谢谢!
求一个简单RPG游戏的代码,JAva编写的package com.lxi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Rpg {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
System.out.println("在这里输入两个人物进行PK,以英文逗号分隔JAVA英雄血战代码: [BMJAVA英雄血战代码 , DH,MK]");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ClassPerson c1;
ClassPerson c2;
try {
String temp = br.readLine();
String[] str = temp.split(",");
if (str.length != 2) {
throw new Exception("输入格式有误 , 按默认PK");
}
c1 = (ClassPerson) Class.forName("com.lxi."
str[0].toUpperCase());
c2 = (ClassPerson) Class.forName("com.lxi."
str[1].toUpperCase());
} catch (Exception e) {
// TODO Auto-generated catch block
c1 = (ClassPerson) Class.forName("com.lxi.BM");
c2 = (ClassPerson) Class.forName("com.lxi.DH");
}
try {
Person p1 = c1.newInstance();
Person p2 = c2.newInstance();
long time = System.currentTimeMillis();
long nextTime1 = (long) (timep1.coldTime*1000); //
long nextTime2 = (long) (timep2.coldTime*1000); //发动攻击JAVA英雄血战代码的时间
System.out.println("---游戏开始---");
while (true) {
long currenTime = System.currentTimeMillis();
if (nextTime1currenTime) {//时间到则发动攻击
p1.hit(p2);
nextTime1= p1.coldTime*1000p1.waitTime*1000; //下次攻击时间=冷却时间 被晕眩时间
p1.waitTime = 0;//回合结束,重置被晕眩时间为0
}
if (nextTime2currenTime) {
p2.hit(p1);
nextTime2= p2.coldTime*1000p2.waitTime*1000;
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();
}
}
}
package com.lxi;
import java.util.Random;
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;//攻击力,值同fight
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;
}
【JAVA英雄血战代码 英雄血战英雄代码】
boolean hitFlag = false;
boolean defenseFlag = false;
Random rand = new Random();
public void hit(Person p) {
if (rand.nextInt(10)chanceHit) {
p.waitTime = 3;//使对方晕眩3s
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;
}
}
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;//表示30%的概率
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 = valhurt;
if (val600)
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;
}
}
}
求一个简单的JAVA游戏代码,100行左右,谢谢!import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {" ", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i4; i) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i5; i) {
g.drawLine( (int) (center_xradius * Math.sin(Math.toRadians(degree72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i))),
(int) (center_xradius * Math.sin(Math.toRadians(degree72 * i144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == " ") {
if(radius200)
radius= 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
JAVA英雄血战代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于英雄血战英雄代码、JAVA英雄血战代码的信息别忘了在本站进行查找喔 。
推荐阅读
- java求大数的模逆代码,java实现大数乘法
- 华为录播服务器8800,华为录播服务器rse
- 台式机装了驱动怎么装显卡,台式驱动安装
- linux命令大全进程 linux常用进程管理命令
- 怎么安装系统自带的.net,怎么安装系统自带的word
- 如何将pdf导入word文档,如何把pdf倒入word
- 虚拟空间主机游戏推荐,虚拟空间主机游戏推荐手游
- linuxxen命令 linux命令 !
- jquery手机端上拉刷新,jquery实现上拉加载