日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA回合制pk游戏

發(fā)布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA回合制pk游戏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

創(chuàng)建玩家類

/** 玩家類* 屬性:名字,類型,血量,防御,攻擊* 行為:自我介紹,pk*/ public class Player {//封裝:將屬性設(shè)為private,提供公共的get和set方法間接訪問,提升安全性private String name;//名字private String type;//職業(yè)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);//隨機數(shù)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);//隨機數(shù)0~9if (sj == 3 || sj == 9) {//如果隨機的是3或9表示暴擊System.out.println(this.name+"被暴擊了"+harm*sj);this.setLife(this.life-harm*sj);//我方掉血~ sj=3或9 等于數(shù)字幾就暴擊幾倍}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();//調(diào)用物品掉落的方法break;//有人倒下,停止戰(zhàn)斗}if (p.life <= 0) {System.out.println(p.name+"被ko了");p.explosiveEquipment();//調(diào)用物品掉落得方法break;//有人倒下,停止戰(zhàn)斗}//線程休眠try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}//爆裝備public void explosiveEquipment(){//裝備庫String[] arr={"屠龍寶刀","方天畫戟","五雷轟頂技能書","滅世套裝","大還丹","10W金幣"};//隨機數(shù)數(shù)組元素的下標: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) {//創(chuàng)建玩家對象01Player p1 = new Player();p1.setName("龍傲天");//名字p1.setType("戰(zhàn)士");//職業(yè)p1.setLife(20000);//血量p1.setDefense(600);//防御p1.setAttack(400);//攻擊//創(chuàng)建玩家對象02Player p2 = new Player();p2.setName("趙日天");//名字p2.setType("法師");//職業(yè)p2.setLife(10000);//血量p2.setDefense(200);//防御p2.setAttack(800);//攻擊//開始PKp1.pk(p2);} }

總結(jié)

以上是生活随笔為你收集整理的JAVA回合制pk游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。