JAVA回合制pk游戏
生活随笔
收集整理的這篇文章主要介紹了
JAVA回合制pk游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建玩家類
/** 玩家類* 屬性:名字,類型,血量,防御,攻擊* 行為:自我介紹,pk*/ public class Player {//封裝:將屬性設為private,提供公共的get和set方法間接訪問,提升安全性private String name;//名字private String type;//職業private int life;//生命值private int defense;//防御private int attack;//攻擊//自我介紹public void say(){System.out.println("我叫"+name+",是一個"+type+",生命值"+life+",防御"+defense+",攻擊"+attack);}/** pk的方式,和另一個玩家pk;* @param p* */public void pk(Player p){//定義一個標記,0表示我方進攻,1表示敵方進攻int flag = 0;//默認我方先進攻//回合制pk,直到一方死亡while (true){//每次都顯示剩余的生命值this.say();p.say();if (flag == 0) {//我方攻擊:敵方生命值-(我方攻擊力-敵方防御力)int harm = this.attack-p.defense;//得到傷害int sj = (int) Math.round(Math.random()*(2-1)+1);//隨機數0~2if (sj == 2) {System.out.println(p.name+"被暴擊了"+harm*2);//暴擊2倍p.setLife(p.life-harm*sj);//敵人掉血~sj=2}else {System.out.println(p.name+"掉血"+harm);p.setLife(p.life-harm);//敵人掉血}//改變進攻方flag = 1;}else {//敵方攻擊:我方生命值-(敵方攻擊力-我方防御力)int harm = p.attack-this.defense;int sj = (int) Math.round(Math.random()*(10-1)+1);//隨機數0~9if (sj == 3 || sj == 9) {//如果隨機的是3或9表示暴擊System.out.println(this.name+"被暴擊了"+harm*sj);this.setLife(this.life-harm*sj);//我方掉血~ sj=3或9 等于數字幾就暴擊幾倍}else {System.out.println(this.name+"掉血了"+harm);this.setLife(this.life-harm);//我方掉血}//改變進攻方flag = 0;}//判別血量if (this.life <= 0) {System.out.println(this.name+"被ko了");this.explosiveEquipment();//調用物品掉落的方法break;//有人倒下,停止戰斗}if (p.life <= 0) {System.out.println(p.name+"被ko了");p.explosiveEquipment();//調用物品掉落得方法break;//有人倒下,停止戰斗}//線程休眠try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}//爆裝備public void explosiveEquipment(){//裝備庫String[] arr={"屠龍寶刀","方天畫戟","五雷轟頂技能書","滅世套裝","大還丹","10W金幣"};//隨機數數組元素的下標:0~arr.length-1int sj = (int) Math.round(Math.random()*((arr.length-1)-0)+0);System.out.println("爆了["+arr[sj]+"]!!!");}public String getName() {return name;}public void setName(String name) {this.name = name;}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 int getDefense() {return defense;}public void setDefense(int defense) {this.defense = defense;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;} }測試類
public class PlayerTest {public static void main(String[] args) {//創建玩家對象01Player p1 = new Player();p1.setName("龍傲天");//名字p1.setType("戰士");//職業p1.setLife(20000);//血量p1.setDefense(600);//防御p1.setAttack(400);//攻擊//創建玩家對象02Player p2 = new Player();p2.setName("趙日天");//名字p2.setType("法師");//職業p2.setLife(10000);//血量p2.setDefense(200);//防御p2.setAttack(800);//攻擊//開始PKp1.pk(p2);} }總結
以上是生活随笔為你收集整理的JAVA回合制pk游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C与C++中的extern与static
- 下一篇: android选择头像弹窗,Androi