Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 05:56:27 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsC# Console RPG Tutorial
Pages: [1]
Print
Author Topic: C# Console RPG Tutorial  (Read 4161 times)
_glitch
Guest
« on: February 05, 2017, 12:23:43 AM »

This tutorial series is for the entry-level game programmer who wants to start with something easy.
You will see that the code is not the most efficient, but it's easy to understand.
So let's start!

Part 1: setting up the console window

In this part we will create the console window, change it's size etc. to make it ready for our game output.

But at first the basics of the C# console:
Code:
Console.Write("text"); //writes "text"
Console.WriteLine("text"); //writes "text" and adds the newline command ("\n") at the end
Console.ReadKey(); //waits for the user to press a key
Console.ReadLine(); //waits for user input and the [Enter] key

These commands are the most important ones.

Now let's create the console application. I am using Visual Studio 2015 with version 4.6.1 of the .NET Framework.

To create a new project follow these steps:
File ---> New ---> Project ---> Console Application
Or: Ctrl + Shift + N ---> Console Application
Now enter the name of your project and press Enter

After it has loaded you should see a bit of code. Change it to
Code:
using System;

namespace Tutorial //change "Tutorial" to your namespace
{
    public static class Program
    {
        static void Main()
        {
        }
    }
}

Now we will write the method that changes the console window to our preferences.

Code:
using System;

namespace Tutorial //change "Tutorial" to your namespace
{
    public static class Program
    {
        static bool IsInitialized = false;
        public static long ViewportWidth, ViewportHeight;

        static void Main()
        {
            if (IsInitialized.Equals(false))
                Initialize();

            Console.ReadKey();
        }

        static void Initialize()
        {
            Console.Title = "Fun Game";
            Console.SetWindowSize(140, 40);
            Console.SetBufferSize(140, 40);
            ViewportWidth = Console.WindowWidth;
            ViewportHeight = Console.WindowHeight;
            IsInitialized = true;
        }
    }
}

If you now press F5, you should see the console window with the size and title you set.

This was part 1; in the next part we will create useful custom console commands.
« Last Edit: February 05, 2017, 01:41:12 AM by _glitch » Logged
_glitch
Guest
« Reply #1 on: February 08, 2017, 11:15:20 AM »

Part 2: creating methods for better formatting

The following methods will make the centering, filling etc more easy. I put them in a class named XConsole. Make sure the class is public, static and in the same namespace like your main class (Program.cs)
The first method is for centering the text in the console window.

Code:
public static void CenterText(string Text)
{
    Console.WriteLine(new string(' ', (Program.ViewportWidth - Text.Length) / 2));
    Console.WriteLine(Text);
}

Use it in this way:
Code:
XConsole.CenterText("Text to center");

The following will add the desired amount of blank lines to the output.

Code:
public static void AddBlankLines(long Count)
{
    for (int i = 0; i < Count; i++)
        Console.WriteLine();
}

This method will fill the complete line with the same character.

Code:
public static void FillLine(char Char)
{
    string CompleteText = string.Empty;

    while (CompleteText.Length < Program.ViewportWidth)
        CompleteText += Char;
   
    Console.Write(CompleteText);
}

Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic