我想制作一款基于文本的游戏,一开始您可以选择角色应具有的颜色。我到处搜索,但似乎找不到任何东西! 这是我的代码(自我4个小时前启动以来,它可能是垃圾) 每当我有主角讲话时,我都会写“ Player();”。 Console.WriteLine(“ XXXXXX”);
之前的一行using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Adventure_game
{
class Program
{
static void Main(string[] args)
{
//Start
menuspeaker();
Console.WriteLine("Welcome to -- (by: Patrick Sørensen)");
Thread.Sleep(2000);
Console.WriteLine("Note: You can escape the anytime by pressing ESC, but be careful you can not save and any progress will be lost!");
Thread.Sleep(8000);
Console.WriteLine("First of all select your color!");
Thread.Sleep(2000);
Console.WriteLine("Type 1 for Blue");
Thread.Sleep(1000);
Console.WriteLine("Type 2 for White");
Thread.Sleep(1000);
Console.WriteLine("Type 3 for Green");
Thread.Sleep(1000);
Console.WriteLine("Type 4 for Red");
Thread.Sleep(1000);
Console.WriteLine("Type 5 for Yellow");
Thread.Sleep(1000);
Console.WriteLine("Type 6 for Purple");
ConsoleKeyInfo keyInfo = Console.ReadKey();
Thread.Sleep(2000);
if (keyInfo.KeyChar == '1')
{
var Player = Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("");
Console.WriteLine("You have choosen BLUE!");
}
if (keyInfo.KeyChar == '2')
{
var Player = Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
Console.WriteLine("You have choosen WHITE!");
}
if (keyInfo.KeyChar == '3')
{
var Player = Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("");
Console.WriteLine("You have choosen GREEN!");
}
if (keyInfo.KeyChar == '4')
{
var Player = Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("");
Console.WriteLine("You have choosen RED!");
}
if (keyInfo.KeyChar == '5')
{
var Player = Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("");
Console.WriteLine("You have choosen YELLOW!");
}
if (keyInfo.KeyChar == '6')
{
var Player = Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("");
Console.WriteLine("You have choosen PURPLE!");
}
menuspeaker();
Console.WriteLine("Write Ready when you are ready to begin");
menuanswer();
string readyString = Console.ReadLine();
//Ready command
if (readyString.ToLower() == "ready")
{
Thread.Sleep(2000);
}
//Navn
menuspeaker();
Console.WriteLine("Before we begin the story, tell me, what is your name?");
menuanswer();
string nameString = Console.ReadLine();
Thread.Sleep(1000);
menuspeaker();
Console.WriteLine(""+nameString+"?");
Thread.Sleep(2000);
Console.WriteLine("What a wounderfull name!");
Thread.Sleep(2000);
Console.WriteLine("Okay, now we can begin with our adventure!");
Thread.Sleep(500);
Console.WriteLine("Please wait");
Thread.Sleep(5000);
Console.Clear();
//Start på spillet
Fred();
Thread.Sleep(2000);
Console.WriteLine("Fred: Hurry up "+nameString+" the king is waiting!");
Thread.Sleep(5000);
//The Player will speak here vv
Console.WriteLine(""+nameString+": Why are we in such a hurry? What has happend?");
Thread.Sleep(5000);
Fred();
Console.WriteLine("Fred: I don't know, when the alarm went off the guards requested you in the throne room");
Thread.Sleep(5000);
menuspeaker();
Console.WriteLine("Fred and "+nameString+" entered the the big throne room and on the throne sat the King nervous");
Thread.Sleep(6000);
King();
Console.WriteLine("King: I have been waiting quite a while for you " + nameString + " and Fred, how come");
Thread.Sleep(5000);
Fred();
Console.WriteLine("Fred: I had a hard time finding " + nameString + ", why did the alarm go off?");
Thread.Sleep(4000);
King();
Console.WriteLine("King: The princess have been taken by a Big monster!");
Thread.Sleep(2000);
Console.WriteLine("And i want you Fred and " + nameString + " go find the Monsters");
}
static void menuspeaker()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
}
static void menuanswer()
{
Console.ForegroundColor = ConsoleColor.White;
}
static void Fred()
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
static void Player()
{
}
static void King()
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
static void Monster()
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
}
}
}
答案 0 :(得分:4)
我认为您需要做的是在程序开始时将颜色链接到播放器,并在整个程序中记住这些颜色。然后,每当特定玩家说话时,您就设置控制台的颜色,说话后再将其设置回去。
因此,假设您有一个像这样的简单类Player
:
public class Player
{
public string Name { get; set; }
public ConsoleColor Color { get; set; }
public void Speak(string speech)
{
Console.ForegroundColor = Color;
Console.WriteLine(speech);
Console.ForegroundColor = ConsoleColor.White;
}
}
现在,每个播放器都有一个Name
和一个ConsoleColor
关联,并且每当调用Speak()
时,控制台将设置为该播放器的颜色,编写语音,然后将其重新设置变成白色(假设白色是默认颜色)。
现在在您的主程序中,您可能可以要求输入Name
和Color
。
static void Main(string[] args)
{
var player = new Player();
Console.Write("Enter Name : ");
var name = Console.ReadLine();
player.Name = name;
Console.WriteLine("First of all select your color!");
Console.WriteLine("Type 1 for Blue");
Console.WriteLine("Type 2 for Green");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.KeyChar == '1')
{
player.Color = ConsoleColor.Blue;
}
if (keyInfo.KeyChar == '2')
{
player.Color = ConsoleColor.Green;
}
player.Speak(string.Format("You have chosen {0}", player.Color.ToString()));
Console.ReadLine();
}
现在您可以扩展这个想法,并在游戏开始时询问许多玩家的姓名和颜色。然后,将它们保存到List<Player>
,并在整个过程中使用它们。
您甚至可以将从用户处获取的信息包括到Player
类中,这样它就可以使代码更整洁,更容易出错。您将在Player
类中包含此方法:
public void GetPlayerInfo()
{
Console.Write("Enter Name : ");
Name = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Pick a Color : ");
Console.WriteLine("1 : Red");
Console.WriteLine("2 : Blue");
Console.WriteLine("3 : Green");
var key = Console.ReadLine();
switch (key)
{
case "1":
Color = ConsoleColor.Red;
break;
case "2":
Color = ConsoleColor.Blue;
break;
case "3":
Color = ConsoleColor.Green;
break;
default:
Color = ConsoleColor.White;
break;
}
Console.WriteLine();
}
然后在最主要的工作就是调用方法:
static void Main(string[] args)
{
var player1 = new Player();
player1.GetPlayerInfo();
player1.Speak(string.Format("You have chosen {0}", player1.Color.ToString()));
var player2 = new Player();
player2.GetPlayerInfo();
player2.Speak(string.Format("You have chosen {0}", player2.Color.ToString()));
Console.ReadLine();
}