Can I use C# to make an RPG game? Is it possible? I really want one for my forum members. Thanks
Read Ad-free Manga, Manhwa, and Manhua at https://trilliux.me!
void Main()
{
int state = 0;
string command = "";
while (command != "Quit")
{
if (command == "Look" && state == 0)
{
// Explore the dungeon.
DoExploreDungeon();
}
else if (command == "Attack" && state == 0)
{
// There's nothing to attack when you're just exploring!
DoNothingToAttack();
}
else if (command == "Look" && state == 1)
{
// You can't explore when a monster is attacking you!
DoMonsterFreeHit();
}
else if (command == "Attack" && state == 1)
{
// Attack the monster.
DoAttackEvilMonster();
}
}
}
void Main()
{
RPGController rpg = new RPGController();
string command = "";
while (command != "Quit")
{
if (command == "Look")
{
rpg.Explore();
}
else if (command == "Attack")
{
rpg.Battle();
}
}
}
public interface IState
{
int Explore();
int Battle(int level);
}
public class ExploreState : IState
{
private RPGController context;
public ExploreState(RPGController context)
{
this.context = context;
}
#region IState Members
public int Explore()
{
Console.WriteLine("You look around for something to kill.");
int ran = RandomGenerator.GetRandomNumber(5);
if (ran == 0)
{
Console.WriteLine("A monster approaches! Prepare for battle!");
context.SetState(context.GetBattleState());
}
else if (ran == 1)
{
Console.WriteLine("You find a golden jewel behind a tree!");
return 2;
}
return 0;
}
public int Battle(int level)
{
Console.WriteLine("You find nothing to attack.");
return 0;
}
#endregion
}
public class BattleState : IState
{
private RPGController context;
private int rounds = 0;
public BattleState(RPGController context)
{
this.context = context;
}
#region IState Members
public int Explore()
{
Console.WriteLine("You'd love to, but see, there's this big ugly monster in front of you!");
return 0;
}
public int Battle(int level)
{
Console.Write("You try to slay the monster.. ");
rounds++;
System.Threading.Thread.Sleep(1000);
int maxRan = 10 - level;
if (maxRan < 1)
{
maxRan = 1;
}
int ran = RandomGenerator.GetRandomNumber(maxRan);
if (ran == 0)
{
Console.WriteLine("he's dead!");
context.SetState(context.GetExploreState());
int tempRounds = rounds;
rounds = 0;
return tempRounds;
}
else
{
Console.WriteLine("but fail.");
}
if (rounds >= 9)
{
Console.WriteLine("You panic and run away in fear.");
context.SetState(context.GetExploreState());
rounds = 0;
}
return 0;
}
#endregion
}
public class RPGController
{
private IState exploreState;
private IState battleState;
private IState state;
private int level = 1;
public RPGController()
{
exploreState = new ExploreState(this);
battleState = new BattleState(this);
state = exploreState;
}
public int Explore()
{
return state.Explore();
}
public int Battle()
{
return state.Battle(level);
}
public void SetState(IState state)
{
this.state = state;
}
public void SetLevel(int level)
{
this.level = level;
}
public int GetLevel()
{
return level;
}
public IState GetExploreState()
{
return exploreState;
}
public IState GetBattleState()
{
return battleState;
}
}
Similar topics
Permissions in this forum:
You cannot reply to topics in this forum
|
|