[英]How to create a multi-dimensional array and have a property attached to a class determine where values are in the array? C#
我正在嘗試創建多維數組,作為基於文本的RPG游戲的一種2D地圖。 我想創建一個多維數組,例如5x5。 該數組將填充0,以表示對象的空間空白。 其他數字將表示地圖上的player
, enemy
, npc
, skill-object
(例如結界桌等),鏈接到地圖的doors
和chests
。 它們都將具有不同的值來表示存在的對象。 如果player
試圖走進chest
,它將搶奪chest
。 然后將chest
重置為0來表示空白,讓player
走到那里並表示沒有更多箱子。 如果player
試圖越過enemy
,它將進行戰斗(由單獨的類和職能進行處理)。 走過代表一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.
}
}
首先,您可以像這樣定義2d地圖數組:
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討論您將嘗試做的許多事情。 我建議您到那里戳一下,看看是否有什么對您有用。
最后,您的代碼具有完全硬編碼技能的skill []技能,實際上並不是大多數程序員處理它的方式。 我會回應喬恩·斯基特(Jon Skeet)的評論,即您應該研究集合。
祝您好運,我想您會在項目中玩得開心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.