Every program has a starting point and ours will begin with the creation of the Windows Forms project which will be the basis of our game and will provide a way to immediately test the code in the chapters to come. In this chapter, we also look at the basics of C# and .NET.
This post is part of a series on creating a roguelike game in C#. For the latest chapters and more information, please visit the Official Project Page on ComeauSoftware.com. The current code for this project is also available on Github.
The original Rogue game was created as a console application that used a C programming library to control the character placement on the screen. At the time before Windows, there was no such thing as keeping multiple apps open and switching between them. In this course, I’ll be creating a roguelike game as a Windows Forms application in C#. This means it can be run in Windows but minimized or moved around the screen if necessary … like when your boss walks in the room. (The old game had a “supervisor key” that would pop up a fake DOS prompt.)
NOTE: I am creating this application as a Windows Forms application (WinForms) rather than WPF or another newer interface design simply because it’s quick and easy for the sake of a C# demo and for those who are relatively new to the language. There are many conflicting opinions on just how “dead” WinForms is as a design option but Microsoft still has plans for it in .NET 7.0 so there must still be some life there. There are also a lot of Windows Forms apps out there to support so it’s a good option to start with.
I’ll keep the same look and feel of the original game at its core but I’ll add some button controls to provide extra features like directional controls and maybe some details about the monster hit points and “dice rolls” that the game always maintained in the background.
Understanding C# and .NET
There is a lot of backstory when it comes to the C# language and the .NET framework that supports it (now simply known as .NET). I could write entire chapters explaining the deeper, internal aspects of both but much of the detail might be lost on you at this point and it wouldn’t make it any easier for you to start coding.
C# was initially developed as one of the languages under the .NET Framework, also released in 2000. This is a system of software libraries and tools that support the development and operation of applications in multiple languages including C#, Visual Basic and F#. The simplest way to think of it is as a software environment that sits between the operating system and the language you’re using and supplies many of the necessary rules and functions that you use to create your programs. When the users run your programs, the framework again steps in to support their operation by supplying a runtime which will translate the language your program is using to commands the operating system can understand.
I mentioned in an earlier chapter that I started out with Visual Basic, also called VB.NET, which is also part of .NET and I was able to quickly become productive with C# because the the two languages share common features thanks to their common framework. It’s even possible to use both languages in the same project.
In 2016, Microsoft released a replacement for the .NET framework, simply called .NET. This newer version is cross-platform and it is also free and open source, meaning that the source code is open for study and inspection by other developers.
If you would like to know more about C# and .NET before you start coding, check the end of the chapter for links that will point you in the right direction. I will definitely share more information in later chapters.
When you open Visual Studio 2022, it comes up to a screen where you can open an existing project or create a new one.
- Select Create a new project.
- On the next screen, select C#. Windows and Desktop from the drop-downs at the top of the screen.
- Select Windows Forms App from the project templates (not the one marked (.NET Framework).
The next screen accepts project and solution names. Visual studio can maintain multiple projects of different types and even different languages under one solution. Later in the course, you’ll probably see a demonstration of this. For now, choose a name for your solution and project and a Location to save it. You can always copy the entire solution somewhere else if necessary and open it from there.
The next screen asks you to select a version of the .NET framework to write the program for. This determines what programming features are available but for a program of this type, it won’t make much of a difference.
I recommend using the Long Term Support version. As of this writing, that’s .NET 6.0 so that’s the one I’m using. If another version is marked as LTS when you’re reading this, that’s fine. Click Create.
After a few moments, Visual Studio will create your new project and display the main form in the IDE for you to work with. The image below shows the beginning screen with the sections that you should be familiar with numbered.
- The Toolbox contains all the controls that you can use on Windows forms like buttons, text boxes, dropdowns and more. Usually, you can just drag one of these over to the form to create a new instance of the control.
- The Solution Explorer shows all the organization of your solution with associated projects. It’s loosely based on the file structure of the directory where your solution is created.
- The Properties panel will show you the properties and events that can be set for whatever form or other object is selected in ..
- The main work area shows your form or the code behind it, depending on the view that you’ve selected.
- The bottom of the screen initially shows tabs for the Error List, Output console and some code metrics. These tabs and all the other areas can be changed; the Visual Studio IDE offers an impressive collection of information windows that you can select from based on the kind of work that you need to do.
All the sections of the IDE can actually be moved around and changed as needed. Sections can be closed by clicking on the X in their top-right corner and other sections can be added from the View menu at the top of the screen. For now, I’d recommend leaving the IDE organized as it is. I’ll get more into some of the other windows later.
After you’ve created your first project or two and have gotten comfortable working with Visual Studio, it really is a good idea to read a little about the history of C# and .NET. Don’t worry if you don’t understand a lot of the more intricate concepts involved; there’s plenty of time to absorb those as you go. Just remain aware that there’s a lot going on under the hood and you’ll probably notice that every so often, something else clicks with you as you continue exploring.
Microsoft’s own pages offer helpful overviews of these technologies.