Creating a Hero in King of Glory: Java OOP Tutorial (Part 1)
This article walks through building a simple hero system for the game King of Glory using Java, covering package setup, class definitions for heroes, equipment, and monsters, and a demo program that creates a hero, simulates earning gold, and purchases items to enhance attributes.
How a Hero Is Created in King of Glory? (Part 1)
(Object and class practice 1) I often wonder how to create my own hero in the game. This tutorial uses Java objects to model a hero, equipment, and monsters, and demonstrates basic gameplay mechanics.
1. Preparation
Create a new IDEA project and set up five packages: hero, equipment, rune, monster, and skin.
2. Implement My Hero Dream!
1. Hero Basic Attributes
package Hero;</code>
<code>// Hero basic attributes</code>
<code>public class Hero {</code>
<code> public String name; // hero name</code>
<code> public String type; // hero role</code>
<code> public int blood; // base health</code>
<code> public int phyattack; // base physical attack</code>
<code> public int Magicattavk; // base magic attack</code>
<code> public int phyfangyu; // base defense</code>
<code> public int attavknum; // normal damage value</code>
<code> public int move; // movement speed</code>
<code> public int money; // economy</code>
<code> public Hero(String name, String type, int blood, int phyattack, int magicattavk, int phyfangyu, int attavknum, int move, int money) {</code>
<code> this.name = name;</code>
<code> this.type = type;</code>
<code> this.blood = blood;</code>
<code> this.phyattack = phyattack;</code>
<code> this.Magicattavk = magicattavk;</code>
<code> this.phyfangyu = phyfangyu;</code>
<code> this.attavknum = attavknum;</code>
<code> this.move = move;</code>
<code> this.money = money;</code>
<code> }</code>
<code> public String getName() { return name; }</code>
<code> public String getType() { return type; }</code>
<code> public int getBlood() { return blood; }</code>
<code> public int getPhyattack() { return phyattack; }</code>
<code> public int getMagicattavk() { return Magicattavk; }</code>
<code> public int getPhyfangyu() { return phyfangyu; }</code>
<code> public int getAttavknum() { return attavknum; }</code>
<code> public void setName(String name) { this.name = name; }</code>
<code> public void setType(String type) { this.type = type; }</code>
<code> public void setBlood(int blood) { this.blood = blood; }</code>
<code> public void setPhyattack(int phyattack) { this.phyattack = phyattack; }</code>
<code> public void setMagicattavk(int magicattavk) { this.Magicattavk = magicattavk; }</code>
<code> public void setPhyfangyu(int phyfangyu) { this.phyfangyu = phyfangyu; }</code>
<code> public void setAttavknum(int attavknum) { this.attavknum = attavknum; }</code>
<code> // Normal attack handling</code>
<code> public int bnormalAttack(int attavknum) {</code>
<code> if (this.phyfangyu - this.attavknum <= 0) {</code>
<code> int res = this.phyfangyu - this.attavknum;</code>
<code> return this.blood + res;</code>
<code> }</code>
<code> return blood - this.attavknum;</code>
<code> }</code>
<code> public int znormalAttack() {</code>
<code> if (this.type == "射手" || this.type == "战士" || this.type == "刺客") {</code>
<code> this.attavknum += phyattack;</code>
<code> }</code>
<code> if (this.type == "法师") {</code>
<code> this.attavknum += Magicattavk;</code>
<code> }</code>
<code> return attavknum;</code>
<code> }</code>
<code>}2. Equipment Package
Below are examples of equipment classes such as shoes, armor, and a shadow axe.
package zhuangbei;</code>
<code>// Resistance Shoes</code>
<code>public class Shoe {</code>
<code> public String name = "抵抗之靴";</code>
<code> public int phyattack = 200;</code>
<code> public int mckattack = 0;</code>
<code> public int move = 200; // +200 movement speed</code>
<code> public Shoe() {}</code>
<code> public String getName() { return name; }</code>
<code> public void setName(String name) { this.name = name; }</code>
<code> public int getPhyattack() { return phyattack; }</code>
<code> public void setPhyattack(int phyattack) { this.phyattack = phyattack; }</code>
<code>}Similar classes are defined for armor (Fajia) and the shadow axe (Anyinzhanfu).
3. Monster Implementations
Simple monster classes (Pig, Bird, Heixie, Xiaobing) each expose a numeric value representing gold earned when defeated.
package yeguai;</code>
<code>public class Pig { public int num = 80; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }</code>
<code>public class Bird { public int num = 100; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }</code>
<code>public class Heixie { public int num = 150; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }</code>
<code>public class Xiaobing { public int num = 150; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }3. Start the Game
import Hero.Hero;</code>
<code>import yeguai.*;</code>
<code>import zhuangbei.*;</code>
<code>public class Deom {</code>
<code> public static void main(String[] args) {</code>
<code> Hero yase = new Hero("亚瑟", "战士", 1000, 200, 0, 500, 200, 50, 0);</code>
<code> System.out.println(yase.name);</code>
<code> System.out.println(yase.money);</code>
<code> // Simulate earning gold from monsters</code>
<code> Pig pig1 = new Pig();</code>
<code> Bird bird = new Bird();</code>
<code> Xiaobing xiaobing = new Xiaobing();</code>
<code> Heixie heixie = new Heixie();</code>
<code> for (int i = 0; i <= 10; i++) {</code>
<code> yase.money += pig1.getNum() + bird.getNum() + xiaobing.getNum() + heixie.getNum();</code>
<code> }</code>
<code> System.out.println(yase.name + "的经济现在是" + yase.money);
<code> // Purchase equipment if enough gold</code>
<code> if (yase.money >= 200) {</code>
<code> Shoe shoe = new Shoe();</code>
<code> System.out.println(yase.name + "购买了" + shoe.name);</code>
<code> yase.phyfangyu += shoe.getPhyattack();</code>
<code> yase.move += shoe.getMove();</code>
<code> yase.money -= 200;</code>
<code> }</code>
<code> if (yase.money >= 1200) {</code>
<code> Fajia fajia = new Fajia();</code>
<code> System.out.println(yase.name + "购买了" + fajia.name);</code>
<code> yase.phyattack += fajia.getPhyattack();</code>
<code> yase.phyfangyu += fajia.getPhyfanyu();</code>
<code> yase.money -= 1200;</code>
<code> }</code>
<code> if (yase.money >= 2000) {</code>
<code> Anyinzhanfu anyinzhanfu = new Anyinzhanfu();</code>
<code> System.out.println(yase.name + "购买了" + anyinzhanfu.name);</code>
<code> yase.phyattack += anyinzhanfu.getPhyattack();</code>
<code> yase.blood += anyinzhanfu.getBlood();</code>
<code> yase.money -= 2000;</code>
<code> }</code>
<code> System.out.println("购买装备后:");</code>
<code> System.out.println(yase.name + "当前的属性" + "生命值:" + yase.blood + "物理攻击:" + yase.phyattack + "法术攻击:" + yase.Magicattavk + " 物理防御:" + yase.phyfangyu + "移动速度:" + yase.move);
<code> }</code>
<code>}With this code you can create a hero, earn gold by defeating monsters, and buy equipment to improve attributes. Future parts will add hero skills and rune mechanics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
