0

我正在尝试创建一个多维数组作为基于文本的 RPG 游戏的一种 2D 地图。我想创建一个多维数组,例如 5x5。该数组将用 0 填充以表示对象的空间无效。其他数字将代表player地图上的 、 an enemy、 an npc、 a skill-object(如附魔台等)、doors链接到地图的 和chests。它们都有不同的值来表示那里的对象。如果 aplayer试图走进 a chest,它反而会掠夺chest. 然后chest将重置为 0 以表示空白空间,让player步行到那里并表示没有更多的胸部。如果player尝试走过enemy,它将改为进行战斗(由单独的类和函数处理)。遍历代表 a 的数字door将链接到代表另一个地图的另一个多维数组。

我想知道我需要在player类中放置什么样的属性来处理他在地图上的位置。然后,一个函数将更改该值以在地图上移动他。为地图创建一个类是个好主意吗?我会将对象的值存储在那里还是存储在这些对象的各个类中?我该怎么办?感谢您的帮助。作为参考,这是我的播放器类(小心,它很长......):

public class Player
{
    public string   name        { get; set; }        //Name of Player
    public int      level       { get; set; }        //Player's combat level         (average of combat-related skills' levels)
    public int      health      { get; set; }        //Player's health               (player dies when this value reaches 0)
    public int      health_max  { get; set; }        //Player's maximum health
    public int      stamina     { get; set; }        //Player's stamina              (used for power attacks, slowly recharges on own)
    public int      stamina_max { get; set; }        //Player's maxiumum stamina
    public int      fatigue     { get; set; }        //Player's fatigue rate         (player cannot fight or run while fatigue is 100)
    public int      hunger      { get; set; }        //Player's hunger level         (player becomes weaker as hunger increases)
    public int      style       { get; set; }        //Player's fighting style       (refer to styles.txt)
    //THIEVERY SKILLS - LOCKPICKING / LOCK_P, LUCK / LUCK, PICKPOCKETING / PICK_P, SNEAKING / SNEAK
    skill LOCK_P    = new skill();      //Ability to pick locks to open locked doors or chests. Trained by unlocking locks successfully. Higher level, better locks.
    skill LUCK      = new skill();      //The higher the level, the more lucky. Loot from enemies, dungeons, and chests are better. Higher level, higher crit chance. Leveled up 3 times every time your overall thievery levels up.
    skill PICK_P    = new skill();      //Ability to steal from NPCs' pockets without turning them against you. Trained by sucessfully stealing items.
    skill SNEAK     = new skill();      //Ability to move unseen. 25% of your sneak level boosts your pickpocketing level. 100% crit chance with an attack from sneaking. Trained by sneaking / escaping combat.
    //COMBAT SKILLS - MELEE / MELEE, SORCERY / SOR, MAGICKA / MAGICKA, ARCHERY / ARCHERY, HEAVY ARMOR / H_ARM, LIGHT ARMOR / L_ARM
    skill MELEE     = new skill();      //Ability to fight better with melee weapons in combat. Trained by dealing damage with melee weapons.
    skill SOR       = new skill();      //Ability to fight better with sorcerey and spells in combat. Trained by dealing damage with spells.
    skill MAGICKA   = new skill();      //Affects how many spells you can cast before you must regenerate your magicka pool. Trained by casting spells.
    skill ARCHERY   = new skill();      //Ability to fight better with ranged weapons, like bows, in combat. Trained by dealing damage with ranged weapons.
    skill H_ARM     = new skill();      //Affects how effective wearing heavy armor, like metal armor, is. Trained by taking damage while wearing heavy armor.
    skill L_ARM     = new skill();      //Affects how effective wearing light armor, like leather armor, is. Trained by taking damage while wearing light armor.
    //CRAFTSHIP SKILLS - SMITHING / SMITH, CRAFTING / CRAFT, ENCHANTMENT / ENCH, HERBLORE / HERB, FLETCHING / FLETCH
    skill SMITH     = new skill();      //Ability to create heavy armor and forge melee weapons. Trained by creating mentioned items.
    skill CRAFT     = new skill();      //Ability to create jewlery to be enchanted. Trained by creating jewlery.
    skill ENCH      = new skill();      //Ability to enchant items so that they give stat boosts to the wearer. Trained by enchanting items.
    skill HERB      = new skill();      //Ability to create potions from collected materials and plants. Trained by creating potions.
    skill FLETCH    = new skill();      //Ability to create bows, arrows, and crossbow stocks. Trained by creating mentioned items.
    //MISC. SKILLS - AGILITY / AGILITY, MINING / MINING, WOODCUTTING / WOOD_C, COOKING / COOK, SLAYER / SLAY
    skill AGILITY   = new skill();      //Ability to pass obstacles. Trained by passing obstacles.
    skill MINING    = new skill();      //Ability to mine ore from ore veins to be used in smithing. Trained by mining ore.
    skill WOOD_C    = new skill();      //Ability to cut wood from trees and vines to be used in fletching. Trained by cutting wood.
    skill COOK      = new skill();      //Ability to cook food to feed hunger / heal health.
    skill SLAY      = new skill();      //The knowledge of how to slay advanced monsters using special equipment. Trained by completeing tasks.

    public void set_all_skills()        //Function to set values for all skills. Called once at beginning of game.
    {
        string[] names = { "Thievery", "Combat", "Craftship", "Misc" };     //Group names for skills
        skill[] skills = { LOCK_P, LUCK, PICK_P, SNEAK, MELEE, SOR, MAGICKA, ARCHERY, H_ARM, L_ARM, SMITH, CRAFT, ENCH, HERB, FLETCH, AGILITY, MINING, WOOD_C, COOK, SLAY };    //Array of all the player's skills
        for (int i = 0; i < 20; i++)    { skills[i].set_level(1); }       //For loop to set each level at 1 (for base values).
        int counter = 0, name = 0;                                        //Creates variables for the while loop to set tag names.
        while (counter < 20)                                              //While loop to set tag names of each skill.
        {
            if (counter < 4)                        { skills[counter].set_tag(names[0]); }      //First 4 skills are given the first tag
            if (counter > 4 && counter < 11)        { skills[counter].set_tag(names[1]); }      //Next 6 skills are given the second tag
            if (counter > 11 && counter < 16)       { skills[counter].set_tag(names[2]); }      //Next 5 skills are given the third tag
            if (counter > 16 && counter < 21)       { skills[counter].set_tag(names[3]); }      //Last 5 skills are given the last tag
            counter++;                                                                          //Increment the counter by 1.
        }
    }

    //Health / Stamina Alteration Functions
    public void     take_blunt_damage(int dmg)            { this.health -= dmg; }           //Decrement player's health by the value of `dmg`
    public void     take_weak_damage(int dmg, int weak)   { this.health -= dmg + weak; }  //Take this dmg if the player is weak to the enemy's type of attack
    public void     take_strong_damage(int dmg, int str)  { this.health -= dmg - str; }   //Take this dmg if the player is strong to the enemy's type of attack
    public void     heal(int x)                           { this.health += x; }           //Increment player's health by `x` amount
    public void     heal()                                { this.health = health_max; }   //Fully heal the player
    public void     die()                                 { this.health = 0; }            //Kill the player by setting health to 0
    public void     dec_stamina(int x)                    { this.stamina -= x; }          //Decrement player's stamina by `x` amount
    public void     fill_stamina(int x)                   { this.stamina += x; }          //Increment player's stamina by `x` amount
    public void     fill_stamina()                        { this.stamina = stamina_max; } //Fully fill the player's stamina
    //Stat Alteration
    public void     lvl_stat(int x, skill s)    { s.level_up_x(x); }            //Level up skill `s` by `x` levels
    public void     lvl_stat(skill s)           { s.level_up(); }               //Level up skill `s` once
}

对于那些想要它作为参考的人,这是我的skill课程:

 public class skill
{
    public string       tag                     { get; set; }               //Tags a sub-skill to a major-skill
    public int          level                   { get; set; }               //Level of the skill    (average of sub-skills' levels)
    public int          XP                      { get; set; }               //XP towards leveling up
    public int[]        XP_to_level_up          = { 50, 120, 200, 350, 420, 500, 650, 720, 800, 950, 1020, 2000, 3500, 4200, 5000, 6500, 7200, 8000, 9500, 10200, 11000, 12500, 13200, 14000, 15500 };               //XP needed to level up
    public void         set_tag(string tag)     { this.tag = tag; }                     //Sets the tag of the skill
    public void         set_level(int x)        { this.level = x; }                     //Set this level to int x
    public bool         level_up()              { this.level++; return true; }          //Increment this level by 1
    public bool         level_up_x(int x)       { this.level += x; return true; }       //Increment this level by int x
    public void         add_XP(int x)           { this.XP += x; }                       //Add XP towards the next level
    public int          calc_where_on_array(int level, int[] XP_to_level_up)            //Calculate what int on the array based on your level
    {
        return XP_to_level_up[level];
    }
    public bool         check_XP(int XP, int where_on_array, int[] XP_to_level_up)      //Check if the current XP you have can level you up
    {
        if (XP >= XP_to_level_up[where_on_array])       //If current XP is greater than what is needed to level up...
        { 
            this.level_up();                            //... level this skill up.
            return true;                                //Return true, to show that you successfully leveled up.
        }
        else { return false; }                          //If you cannot level up, return false.
    }
}
4

1 回答 1

1

首先,您可以像这样定义二维地图数组:

int[,] map = new int[5, 5];

有关更多信息,请参阅MSDN 上的多维数组。是的,我建议将地图放入课程中,尽管如果您只是在学习这些东西,我猜想在短期内尽您所能将它拼凑起来!

其次,我认为你的球员可以有 X、Y 的位置。所以保持你上面的风格,也许把它添加到你的播放器类中:

public int      x_pos { get; set; }
public int      y_pos { get; set; }

然后你可以使用一个函数来更新他的位置,然后说 map[player.x_pos, player.y_pos] = 1,或者“player”的任何代码。

第三,考虑地图上的一个正方形可能有熔岩。一个怪物站在那里。或者某个方格是浅水区,玩家就站在那里。你打算如何处理在一个方格上有多个东西?您当前的设计有一些限制。可能会很荒谬:也许你有一个方块,上面施了黑暗咒语,上面飘着一团毒气,地上还有10个金块,上面是光滑的大理石,上面有一些苔藓,还有那里睡着一个妖精。

如果我正确理解您的项目,您将尝试做的许多事情将在Rogue Basin讨论。我建议你在那里四处逛逛,看看有什么对你有用的。

最后,你的代码具有充满硬编码技能的技能 [] 技能并不是大多数程序员真正处理它的方式。我会回应 Jon Skeet 的评论,即您应该研究收藏品。

祝你好运,我想你会在你的项目中玩得开心。

于 2013-07-23T22:11:56.257 回答