• 周三. 4 月 22nd, 2026

物嫩软件资讯网

软件资讯来物嫩

使用策略模式设计一个动作冒险类游戏

admin@wunen

3 月 29, 2025

使用策略模式设计一个动作冒险类游戏



背景


  • 动作冒险游戏:


    游戏中有一系列角色(Character),包括国王(King)、皇后(Queen)、骑士(Knight)、妖怪(Troll),这些角色可以在游戏中每一次使用一个武器(Weapon)来攻击对方,并且可以在运行时切换武器,为了增加游戏的乐趣,可以有的武器包括:匕首、宝剑、斧头、弓箭等等,可能以后又更时髦的武器出现。使用策略模式来设计。



ClassDiagram图



部分代码展示


Character.java

package RPGDemo;

public class Character {
   
	protected Weapon weapon;
	protected String name = "Character";
	protected int HP = 100;
	
	
	//构造方法
	public Character() {
   
		
	}
	
	public Character(String name) {
   
		this.name = name;
	}

	public Character(String name,Weapon weapon) {
   
		this.name = name;
		this.weapon = weapon;
	}
	
	public Character(String name, Weapon weapon,int HP) {
   
		this.name = name;
		this.weapon = weapon;
		this.HP = HP;
	}
	
	
	//普通方法
	public void setWeapon(Weapon weapon) {
   
		this.weapon = weapon;
	}
	
	public void setHP(int hp) {
   
		this.HP 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注