Game Development 9 min read

Creating a Hero in a Java Game: Step-by-Step Tutorial (Part 1)

This article walks through building a simple Java-based game prototype by creating hero, equipment, and monster classes, demonstrating object-oriented concepts, and showing a main program that simulates hero creation, resource gathering, and equipment purchase.

Java Captain
Java Captain
Java Captain
Creating a Hero in a Java Game: Step-by-Step Tutorial (Part 1)

The author, a gaming enthusiast, uses this tutorial to practice Java object‑oriented programming by designing a basic hero for a game similar to "King of Glory".

First, a new IntelliJ IDEA project is created and five packages are set up: Hero, zhuangbei (equipment), yeguai (monsters), and others for future extensions.

Hero basic attributes are defined in the Hero class, which includes fields such as name, type, blood, physical attack, magic attack, defense, movement speed, and economy. Getter and setter methods, as well as attack logic, are provided.

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;
    }
    // getters and setters omitted for brevity
    //受到普通攻击
    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;
    }
}

The equipment package contains three sample items:

Shoe (Resist Boots) increases physical defense and movement speed.

package zhuangbei;
//抵抗之靴
public class Shoe {
    public String name = "抵抗之靴";
    public int phyattack = 200;
    public int mckattack = 0;
    public int move = 200; //英雄移动速度+200
    // getters and setters omitted
}

Fajia (Anti‑Armor) provides physical attack and a large defense boost.

package zhuangbei;
//反甲
public class Fajia {
    public String name = "反甲";
    public int phyattack = 40;
    public int phyfanyu = 420;
    public double fashang = 0.2; //冷却时间
    // getters and setters omitted
}

Anyinzhanfu (Shadow Battle Axe) adds physical attack, health, and cooldown reduction.

package zhuangbei;
//暗影战斧
public class Anyinzhanfu {
    public String name = "暗影战斧";
    public int phyattack = 85; //加物理攻击85
    public int blood = 500;   //生命值加500
    public int cool = 15;    //冷却缩减15
    // getters and setters omitted
}

The monster package defines simple classes for Pig, Bird, Heixie (river crab), and Xiaobing (minion), each exposing a num field representing the gold earned when defeated.

package yeguai;
public class Pig { public int num = 80; }
public class Bird { public int num = 100; }
public class Heixie { public int num = 150; }
public class Xiaobing { public int num = 150; }

Finally, the Deom class ties everything together: it creates a hero instance, simulates ten rounds of monster farming to accumulate gold, checks the hero’s wealth, purchases equipment when affordable, updates the hero’s attributes accordingly, and prints the hero’s status before and after purchases.

import Hero.Hero;
import yeguai.*;
import zhuangbei.*;

public class Deom {
    public static void main(String[] args) {
        Hero yase = new Hero("亚瑟", "战士", 1000, 200, 0, 500, 200, 50, 0);
        // simulate farming
        Pig pig1 = new Pig();
        Bird bird = new Bird();
        Xiaobing xiaobing = new Xiaobing();
        Heixie heixie = new Heixie();
        for (int i = 0; i <= 10; i++) {
            yase.money += pig1.num + bird.num + xiaobing.num + heixie.num;
        }
        // purchase equipment
        if (yase.money >= 200) {
            Shoe shoe = new Shoe();
            yase.phyfangyu += shoe.getPhyattack();
            yase.move += shoe.getMove();
            yase.money -= 200;
        }
        if (yase.money >= 1200) {
            Fajia fajia = new Fajia();
            yase.phyattack += fajia.getPhyattack();
            yase.phyfangyu += fajia.getPhyfanyu();
            yase.money -= 1200;
        }
        if (yase.money >= 2000) {
            Anyinzhanfu anyinzhanfu = new Anyinzhanfu();
            yase.phyattack += anyinzhanfu.getPhyattack();
            yase.blood += anyinzhanfu.getBlood();
            yase.money -= 2000;
        }
        // output final stats (omitted for brevity)
    }
}

The author concludes that the prototype successfully demonstrates hero creation, monster farming, and equipment purchase, and promises future additions such as hero skills and rune systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGame DevelopmentCode ExampleTutorialObject-OrientedEquipmentHero
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.