所以我在C#中制作了一个简短的基于文本的游戏,我遇到了一个问题。
一位朋友帮我,并告诉我需要使用不同的课程,到目前为止,我有一个课程,主菜单和字符选择/课程选择/难度选择。
现在当我去写第二堂课的东西时(我希望这个班级包含实际的游戏)并且看起来像名字,班级等变量似乎不能延续到第二堂课。我已经尝试过谷歌搜索它,但还没有真正找到一个好方法来说出来(对不起,如果这被覆盖在某处,如果是这样请链接)。
以下是代码的pastebin链接(我尝试在此处发布,但是缩短所有行的时间太长了):http://pastebin.com/6CJiZMjX
谢谢!
答案 0 :(得分:1)
您的第一个问题与范围有关。
您在 local 范围内声明了诸如难度和名称之类的内容,也就是说它们只存在于您声明它们的方法中。
将它们移到方法之外的更高范围。
namespace ConsoleApplication3
{
class Initiation
{
public string name; // Here are your variables
public int diff; // Now they're accessible across the whole class
...
void characterCreation()
{
Console.WriteLine("Please enter your name: ");
name = Console.ReadLine();
Console.WriteLine("\nWelcome " + name + "!");
...
...
}
void difficultyMenu()
{
Console.WriteLine("Please select a difficulty:\n1. Easy\n2. Medium\n3. Hard");
diff = Convert.ToInt32(Console.ReadLine());
...
...
}
}
}
您的第二个问题是您的Game
课程需要了解此信息。一种简单的方法(但不是唯一的方法)是创建一个构造函数,其中包含一些可以为名称,类和难度设置的参数。
public class Game
{
public string name { get; set; } // Read about auto-properties
public int cclass { get; set; }
public int diff { get; set; }
// This is the constructor that assigns the values passed to this class
public Game(string n, int c, int d)
{
name = n;
cclass = c;
diff = d;
}
// Example of gameStart that shows the chosen values
public void gameStart()
{
Console.WriteLine("GAME STARTED:");
Console.WriteLine(string.Format("Name: {0}, Class: {1}, Difficulty: {2}", name, cclass, diff));
}
}
现在回到Main
,您需要实例化并致电Game.gameStart()
。
public void Main(string[] args)
{
... // All your existing code is here
...
// Create new instance of Game class and tell it the choices
var game = new Game(name, cclass, diff);
// Start the game
game.gameStart();
}
这是一个Dot Net小提琴 https://dotnetfiddle.net/4qqh0y
输出:
主菜单
1.玩游戏
2.退出
的 1 强>
请输入你的姓名:
Equalsk欢迎Equalsk!
请选择一个班级:
1.战士 - 使用近战武器,平均生命值造成的额外伤害 2.游侠 - 使用远程武器减少健康,加重伤害 3.圣骑士 - 低伤害,增加健康和护甲。
<强> 3 强>
你选择成为一名圣骑士,Equalsk! 请选择难度:
1.容易 2.中等 3.辛苦 的 1 强>
你选择了简单难度!游戏开始:
姓名:Equalsk,等级:3,难度:1
答案 1 :(得分:0)
像这样的东西。希望这会有所帮助。
//use this new class as a container to pass multiple items back to the calling procedure
public class Character {
public string Name { get; set; }
public int CharacterClass { get; set; }
public int Diff { get; set; }
}
public class Initiation //Pre-game stuff - Character name, class and game difficulty
{
static Character characterCreation() //changed return type to Character to return object of Class Character
{
Console.WriteLine("Please enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("\nWelcome " + name + "!");
Console.WriteLine("Please select a class:\n1. Warrior - Bonus damage with melee weapons, average health.\n2. Ranger - Reduced health, bonus damage with ranged weapons.\n3. Paladin - Low damage, bonus to health and armor.");
int cclass = Convert.ToInt32(Console.ReadLine());
switch (cclass)
{
case 1:
Console.WriteLine("You have chosen to be a Warrior, " + name + "!");
break;
case 2:
Console.WriteLine("You have chosen to be a Ranger, " + name + "!");
break;
case 3:
Console.WriteLine("You have chosen to be a Paladin, " + name + "!");
break;
default:
Console.WriteLine("Incorrect entry, please enter 1, 2 or 3!");
break;
}
var character = new Character(); //build up the return Object
character.Name = name;
character.CharacterClass = cclass;
character.Diff = Initiation.difficultyMenu();
return character;
}
static int difficultyMenu() //return type int so it returns the diff
{
do {
Console.WriteLine("Please select a difficulty:\n1. Easy\n2. Medium\n3. Hard");
int diff = Convert.ToInt32(Console.ReadLine());
switch (diff)
{
case 1:
Console.WriteLine("You have selected easy difficulty!");
break;
case 2:
Console.WriteLine("You have selected medium difficulty!");
break;
case 3:
Console.WriteLine("You have selected hard difficulty!");
break;
default:
Console.WriteLine("Incorrect entry! Please enter either 1, 2 or 3!");
break;
}
} while (diff<1 || diff >3) // loop so program keeps asking until suitable input
return diff;
}
static void Main(string[] args)
{
Character character;
Console.WriteLine("Main Menu");
Console.WriteLine("1. Play Game");
Console.WriteLine("2. Exit");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
character = characterCreation(); //created character is now returned
Game game = new Game();
game.gameStart(character);
break;
case 3:
Environment.Exit(0);
break;
default:
Console.WriteLine("Incorrect entry, please enter 1 or 2!");
break;
}
Console.ReadLine();
}
}
public class Game //Actual game
{
static void gameStart(Character character)
{
}
}
答案 2 :(得分:0)
也许是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Game
{
class Program
{
static void Main (string[] args)
{
IAppModule module = new MainMenuModule ();
while (true)
{
module = module.Run ();
Console.Clear ();
}
}
}
public class Character
{
public string Name;
public int Health, Armor, Damage;
public static Character Warrior => new Character { Name = "Warrior", Health = 600, Armor = 20, Damage = 80 };
public static Character Archer => new Character { Name = "Archer", Health = 250, Armor = 5, Damage = 35 };
public static Character Mage => new Character { Name = "Mage", Health = 90, Armor = 0, Damage = 150 };
}
public interface IAppModule
{
IAppModule Run ();
}
public class ExitGameModule : IAppModule
{
public IAppModule Run ()
{
Console.WriteLine ("Thanks for playing!");
Thread.Sleep (2000);
Environment.Exit (0);
return null;
}
}
public class MainMenuModule : IAppModule
{
public IAppModule Run ()
{
var menu = new Menu ("Main menu:");
menu.AddMenuEntry ("1", "start adventure", new SelectHeroModule ());
menu.AddMenuEntry ("q", "exit game", new ExitGameModule ());
return menu.Execute ();
}
}
public class SelectHeroModule : IAppModule
{
public IAppModule Run ()
{
var mm = new Menu ("Select Hero");
mm.AddMenuEntry ("1", "Paladin", new PlayModule (Character.Warrior));
mm.AddMenuEntry ("2", "Archer", new PlayModule (Character.Archer ));
mm.AddMenuEntry ("3", "Mage", new PlayModule (Character.Mage ));
mm.AddMenuEntry ("q", "Back to main menu", new MainMenuModule ());
return mm.Execute ();
}
}
public class PlayModule : IAppModule
{
private Character character;
public PlayModule (Character character)
{
this.character = character;
}
public IAppModule Run ()
{
Console.WriteLine ("You are playing: " + character.Name);
Console.WriteLine ("Health: " + character.Health);
Console.WriteLine ("Armor: " + character.Armor);
Console.WriteLine ("Damge: " + character.Damage);
// game logic here
Console.WriteLine ("\r\nPress any key to exit to menu.");
Console.ReadKey ();
return new MainMenuModule ();
}
}
// helper class for building text menus
public class Menu
{
private string title;
private List<MenuItem> items = new List<MenuItem> ();
public Menu (string title)
{
this.title = title;
}
public void AddMenuEntry (string cmd, string description, IAppModule module)
{
var item = new MenuItem
{
Command = cmd,
Description = description,
Module = module
};
items.Add (item);
}
public IAppModule Execute ()
{
Console.WriteLine ($"***** {title} *****\r\n");
foreach (var item in items)
{
Console.WriteLine ($"{item.Command} - {item.Description}");
}
var cmd = GetCommand ();
return items.First (x => x.Command == cmd).Module;
}
private string GetCommand ()
{
while (true)
{
Console.Write ("\r\nYour choice: ");
string cmd = Console.ReadLine ();
if (items.Any (x => x.Command == cmd)) return cmd;
Console.WriteLine ("Invalid choice. Try again.");
}
}
}
public class MenuItem
{
public string Command, Description;
public IAppModule Module;
}
}