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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

玩家类pk小游戏

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 玩家类pk小游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?玩家類

package com.hp.mxdx;import java.util.Random;/*** 面向對象* 玩家類* 屬性:名字、類型、生命值、防御值、攻擊力* 方法:自我介紹、PK* */ public class Demo1 {//封裝:把屬性設為私有private,提供公共的get和set方法間接訪問,提高安全性private String name; //姓名private String type; //類型:展示、法師private int life; //生命值private int defense; //防御值private int attack; //攻擊力private double baoji; //暴擊/*** 描述自己的屬性* @return*/public void say(){System.out.println("我叫"+name+",是一個"+type+",生命值高達"+life+",防御值"+defense+",攻擊力"+attack);}/*** 我方開啟的戰斗* @return P 敵對玩家*/public void pk(Demo1 demo1) {//回合制,直到一方死亡停止int flag = 0;//默認我方先攻擊while (true) {//顯示一下當前戰斗人員的信息this.say();demo1.say();Random a = new Random();//定義一個標記,0我方攻擊 1敵方攻擊if (flag == 0) {int x = a.nextInt(1);if (x != 2){int b = a.nextInt(4);if(b!=0 && b!=1){this.setAttack(this.attack*b);System.out.println("龍傲天"+"[暴擊!]"+b);}}//戰斗 我方攻擊力-敵方防御力=傷害值int harm = this.attack - demo1.defense;//讓敵方生命值-傷害值demo1.setLife(demo1.life -= harm);System.out.println(demo1.name + "掉血" + harm);flag = 1;//改變標記,轉換攻擊角色} else {//戰斗 敵方攻擊力-我方防御力=傷害值int x = a.nextInt(1);if (x != 2){int b = a.nextInt(4);if(b!=0 && b!=1){demo1.setAttack(demo1.attack*b);System.out.println("趙日天"+"[暴擊!]"+b);}}int harm = demo1.attack - this.defense;this.setLife(this.life -= harm);System.out.println(this.name + "掉血" + harm);//讓我方生命值-傷害值flag = 0; //改變標記,轉換攻擊角色}//有血量<=0,戰斗結束if (this.life <= 0){System.out.println(demo1.name+"打敗了"+this.name);int x = a.nextInt(3);if (x == 2){System.out.println("趙日天獲取SSS級大杖");}else {System.out.println("獲取1000金幣");}break;//結束循環}if (demo1.life <= 0){System.out.println(this.name+"打敗了"+demo1.name);int x = a.nextInt(3);if (x == 2){System.out.println("龍傲天獲取SSS級大劍");}else {System.out.println("獲取1000金幣");}break;//結束循環}//線程休眠try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}//構造器(建議如果定義了有參的構造器,一定定義一個無參構造器)public Demo1(String name, String type, int life, int defense, int attack) {this.name = name;this.type = type;this.life = life;this.defense = defense;this.attack = attack;}public Demo1(double baoji) {this.baoji = baoji;}//無參構造器public Demo1() {}//get和set方法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 double getBaoji() {return baoji;}public void setBaoji(double baoji) {this.baoji = baoji;} }

實現類

package com.hp.mxdx;/*** 測試玩家類* 創建玩家對象PK*/ public class Test {public static void main(String[] args) {//創建兩個玩家Demo1 demo1 = new Demo1("龍傲天", "戰士", 1000, 55, 80 );Demo1 demo2 = new Demo1("趙日天", "法師", 1000, 48, 95);//戰斗demo1.pk(demo2);} }

總結

以上是生活随笔為你收集整理的玩家类pk小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。