我是一个比较爱玩游戏的人,比如王者荣耀。我常常在想,如果我可以自己创造一个英雄那该多好,现在我要开始以自己的想法来实现我的愿望,也来练习练习自己对于 java 对象的认识。
前期准备
我们要在 IDEA 上创建一个新的项目,创建好一个项目后,我们需要创建5个包,分别是英雄包,装备包,铭文包,野怪包,皮肤包,然后我们就可以一个一个的实现这些功能了。
实现我的英雄梦
①英雄基本属性实现
代码如下:
- package Hero;
- //英雄的基本属性
- public class Hero {
- public String name; //英雄的名字
- public String type; //英雄的职业
- public int blood; //英雄的基本血量
- public int phyattack; //英雄的基本物理攻击
- public int Magicattavk; //英雄的基本法术攻击
- public int phyfangyu; //英雄的基础防御
- public int attavknum; //普通伤害的值
- public int move; //移动速度
- public int money; //经济
- public Hero(String name, String type, int blood, int phyattack, int magicattavk, int phyfangyu,int attavknum,int move,int money) {
- this.name = name;
- this.type = type;
- this.blood = blood;
- this.phyattack = phyattack;
- this.Magicattavk = magicattavk;
- this.phyfangyu = phyfangyu;
- this.attavknum= attavknum;
- this.move= move;
- this.money= money;
- }
- public String getName() {
- return name;
- }
- public String getType() {
- return type;
- }
- public int getBlood() {
- return blood;
- }
- public int getPhyattack() {
- return phyattack;
- }
- public int getMagicattavk() {
- return Magicattavk;
- }
- public int getPhyfangyu() {
- return phyfangyu;
- }
- public int getAttavknum() {
- return attavknum;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setType(String type) {
- this.type = type;
- }
- public void setBlood(int blood) {
- this.blood = blood;
- }
- public void setPhyattack(int phyattack) {
- this.phyattack = phyattack;
- }
- public void setMagicattavk(int magicattavk) {
- Magicattavk = magicattavk;
- }
- public void setPhyfangyu(int phyfangyu) {
- this.phyfangyu = phyfangyu;
- }
- public void setAttavknum(int attavknum) {
- this.attavknum = attavknum;
- }
- //受到普通攻击
- public int bnormalAttack(int attavknum){
- if(this.phyfangyu-this.attavknum<=0){
- int res = this.phyfangyu-this.attavknum;
- return this.blood+res;
- }
- return blood-this.attavknum;
- }
- //普通攻击
- public int znormalAttack(){
- if(this.type=="射手"||this.type=="战士"||this.type=="刺客"){
- this.attavknum+=phyattack;
- }
- if(this.type=="法师"){
- this.attavknum+=Magicattavk;
- }
- return attavknum;
- }
- }
②装备包
装备太多了,我主要写一些我平时比较喜欢的几个装备,有兴趣的盆友可以自己练习多写几个。
鞋子:
- package zhuangbei;
- //抵抗之靴
- /**
- * 效果:减低敌方的攻击,也就是增加自己的物防
- */
- public class Shoe {
- public String name = "抵抗之靴";
- public int phyattack=200;
- public int mckattack=0;
- public int move = 200;//英雄移动速度+200;
- public Shoe() {
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPhyattack() {
- return phyattack;
- }
- public void setPhyattack(int phyattack) {
- this.phyattack = phyattack;
- }
- public int getMckattack() {
- return mckattack;
- }
- public void setMckattack(int mckattack) {
- this.mckattack = mckattack;
- }
- public int getMove() {
- return move;
- }
- public void setMove(int move) {
- this.move = move;
- }
- }
反甲:
- package zhuangbei;
- //反甲
- public class Fajia {
- public String name = "反甲";
- public int phyattack = 40;
- public int phyfanyu = 420;
- public double fashang = 0.2; //冷却时间
- public int getPhyattack() {
- return phyattack;
- }
- public void setPhyattack(int phyattack) {
- this.phyattack = phyattack;
- }
- public int getPhyfanyu() {
- return phyfanyu;
- }
- public void setPhyfanyu(int phyfanyu) {
- this.phyfanyu = phyfanyu;
- }
- public double getFashang() {
- return fashang;
- }
- public void setFashang(double fashang) {
- this.fashang = fashang;
- }
- }
暗影战斧:
- package zhuangbei;
- //暗影战斧
- /*
- *
- * */
- public class Anyinzhanfu {
- public String name = "暗影战斧";
- public int phyattack = 85; //加物理攻击85
- public int blood = 500; //生命值加500
- public int cool = 15; //冷却缩减15
- public int getPhyattack() {
- return phyattack;
- }
- public void setPhyattack(int phyattack) {
- this.phyattack = phyattack;
- }
- public int getBlood() {
- return blood;
- }
- public void setBlood(int blood) {
- this.blood = blood;
- }
- public int getCool() {
- return cool;
- }
- public void setCool(int cool) {
- this.cool = cool;
- }
- }
③没有经济来源怎么行?野怪实现
我主要写了野猪,小鸟,河蟹,小兵的类。
野猪:
- package yeguai;
- public class Pig {
- public int num = 80;
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- }
小鸟:
- package yeguai;
- public class Bird {
- public int num = 100;
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- }
河蟹:
- package yeguai;
- public class Heixie {
- public int num = 150;
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- }
小兵:
- package yeguai;
- public class Xiaobing {
- public int num = 150;
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- }
开始游戏啦
- import Hero.Hero;
- import yeguai.Bird;
- import yeguai.Heixie;
- import yeguai.Pig;
- import yeguai.Xiaobing;
- import zhuangbei.Anyinzhanfu;
- import zhuangbei.Fajia;
- import zhuangbei.Shoe;
- public class Deom {
- public static void main(String[] args) {
- Hero yase = new Hero("亚瑟","战士",1000,200,0,500,200,50,0);
- System.out.println(yase.name);
- System.out.println(yase.money);
- //开始发育(先产生野怪,小兵,河蟹的实例化对象)
- Pig pig1 = new Pig();
- Bird Bird = new Bird();
- Xiaobing xiaobing = new Xiaobing();
- Heixie heixie = new Heixie();
- for(int i =0;i<=10;i++){ //打了10轮
- yase.money+=pig1.getNum()+Bird.getNum()+xiaobing.getNum()+heixie.getNum();
- }
- System.out.println(yase.name+"的经济现在是"+yase.money);
- //开始创建装备类
- System.out.println("购买装备前:");
- System.out.println(yase.name+"当前的属性"+"生命值:"+yase.blood+"物理攻击:"+yase.phyattack+"法术攻击:"+yase.Magicattavk+
- " 物理防御:"+yase.phyfangyu+"移动速度:"+yase.move);
- if(yase.money>=200){
- Shoe shoe = new Shoe();
- System.out.println(yase.name+"购买了"+shoe.name);
- yase.phyfangyu+=shoe.getPhyattack();
- yase.move+=shoe.getMove();
- yase.money=yase.money-200;
- }
- if(yase.money>=1200){
- Fajia fajia = new Fajia();
- System.out.println(yase.name+"购买了"+fajia .name);
- yase.phyattack+=fajia.getPhyattack(); //攻击力+
- yase.phyfangyu+=fajia.getPhyfanyu(); //防御力+
- yase.money=yase.money-1200;
- }
- if(yase.money>=2000){
- Anyinzhanfu anyinzhanfu = new Anyinzhanfu();
- System.out.println(yase.name+"购买了"+anyinzhanfu.name);
- yase.phyattack+=anyinzhanfu.getPhyattack(); //攻击力+
- yase.blood+=anyinzhanfu.getBlood(); //生命力+
- yase.money=yase.money-2000;
- }
- System.out.println("购买装备后:");
- System.out.println(yase.name+"当前的属性"+"生命值:"+yase.blood+"物理攻击:"+yase.phyattack+"法术攻击:"+yase.Magicattavk+
- " 物理防御:"+yase.phyfangyu+"移动速度:"+yase.move);
- }
- }
我基本可以实现简单的创建一个英雄,实现刷野怪,购买装备的简单操作,后序我将实现英雄的三个技能和铭文的实现。
作者:沉默着忍受
编辑:陶家龙
出处:http://adkx.net/wkxf2