Introduction

Imagine this scenario: You create a hunter character with random stats and hit points and enter the dungeon to begin exploring for treasure. You stumble upon a zombie pacing near a pile of gear and gold! You attack the zombie and it strikes back! You roll 1D20 and score a critical hit! The 20 point die plus dexterity, against the zombie’s armor class, nets a guaranteed hit. Your damage roll is 1D8 plus your strength and weapon modifier, which is 1D4, more than enough to kill the monster! It falls to the ground motionless. Lying next to the unmoving zombie is leather armor that you pick up and equip, giving your hunter a huge boost in defense points. You also pick up some gold coins and gain experience points. Now you dream of finding a better bow... Off to the right, you spot a whole group of undead monsters that are surely guarding loot you could use... and you head toward them to battle!

You will learn to build a game with every feature just described! This book will teach you how to create your own fully functional role-playing game (RPG) using Visual C#. You will learn step-by-step how to construct each part of the game engine using Windows Forms and GDI+, including a tiled scroller, game editors, and scripting. If you think role-playing games are fun to play, wait until you start working on one of your own design! Constructing an RPG is more fun than playing one, because you are in complete control over the game world, and you can let your imagination loose to create adventures for others to enjoy. However, it is not easy! Just as your game character must gain experience and level up, so must you level up by reading each chapter to learn new skills!

Before you can get to the point where you are able to design an adventure and build an RPG with Visual C#, you will need to learn the basics of RPG game mechanics. My goal with this book is to teach you just what you need to know in order to make this happen. You will learn how to construct a dungeon-based RPG called the Dungeon Crawler. When you have finished this book, you will have learned several new skills including game programming and tools programming. In addition, you will have complete creative control over how the game operates, and will be able to make many games from the same code and tools.

Pacing and Experience

This book is aimed toward the serious RPG fan with discussions about to-hit and damage rolls during combat, and RPG character creation with random stats and character classes and so forth. This is not just a pure programming book, it’s very much about creating an RPG, while C# programming comes second in the list of priorities. The primary purpose of this book is to help you to have fun learning about game programming. Typing in long source code listings out of a book is not fun, so I don’t ask you to do that in this book. Instead, you will learn by studying the short examples in each chapter. There is no memorization required here, as I’m a firm believer that repetition and practice is the best way to learn, not theory and memorization. The Dungeon Crawler game is built from one chapter to the next, with new features and gameplay elements added in each new chapter. You will learn to create this game completely from scratch in a very short amount of time.

The finished game in the last chapter includes all the source code to make your own game with the features described above, including: combat with monsters; talking with NPCs; picking up treasure and gold; creating items with the custom item editor; picking up items; managing the player’s inventory bag; equipping gear with buffs (stat modifiers) such as armor and weapons; creating NPCs with the character editor; rolling new player characters; saving and loading the game; and creating the dungeon with a custom level editor. Whew! That sounds like a lot of work, but we make all of this happen in the pages of this book, and you will have a playable, customizable RPG game system by the time you reach the last chapter.

Prerequisites

The goal of this book is to teach you how to create an RPG. You will most definitely benefit from a solid grasp of the C# language. If you are a C# beginner, you may struggle with the source code in this book. But, all of the projects are available in the downloadable resources, so you can still enjoy learning while running the working examples if that is your desire. Programming an RPG is a serious challenge, but if you pay attention and study the examples, you’ll be able to do it! The Dungeon Crawler game is large and complex, and it’s very hard to program your own RPG from scratch, but that is exactly what we do! I’ll try to explain it one step at a time, but there are some programming matters that you will just need to know in advance. If you ever feel lost, then a good primer on the C# language will be helpful. All of the game editors for the RPG are also covered in the book. We cover the key code for the editors but don’t list all of the source code because that would take up 500 pages!

Visual C# 2008 and the .NET Framework

This book supports Visual C# 2008 and requires the .NET Framework 2.0. Although the project files are slightly different, the code will compile without issues under Visual C# 2010 as well. If you have 2010 you will still be able to run all of the code in the book, but you will have to convert the projects to the 2010 format (an automatic process performed by Visual Studio when you open a 2008 project). I have left the projects in the 2008 format so that more readers will be able to open the projects with either version. You cannot open 2010 projects using 2008, but the opposite is true.

I recommend that you download the free version of Visual C# Express Edition. You can find it at http://www.microsoft.com/express/downloads. Since web links change frequently, I recommend you instead search Google for “Visual C# Express download.” There are links to the 2008 version as well, but 2010 is now the officially supported current version so go ahead and use that version if you want. Just note that the 2008 projects will need to be converted when you open them in 2010. This should not pose a problem, it’s just one additional step, and it allows us to support both versions.

There is one caveat: the LuaInterface library requires the .NET Framework 2.0, so when it comes to the script examples in Chapter 15, you may need to create a new Visual C# 2010 project and then import the sources to it, in order for LuaInterface to work correctly. As an option, an advanced reader may recompile the LuaInterface library with Visual C# 2010 and then use the later .NET Framework. If you feel confused about this issue, please visit the author’s web forum at www.jharbour.com/forum with any questions. This book was never intended to be complicated—we can thank Microsoft for that.

Managed DirectX?

This book does not use the .NET version of DirectX (called Managed DirectX). Instead, this book focuses on Windows Forms programming with the Windows GDI+ (Graphics Device Interface). Managed DirectX is no longer supported by Microsoft and does not work with modern 64-bit versions of Windows. Not to worry, though, because the demos in this book run at 100+ FPS on even an older PC using just GDI+ code.

Contacting the Author

My website is located at http://www.jharbour.com. It has information about this book that you may find useful. This site also features an online forum where you can pose questions and keep up to date with the latest discussions with other programmers about Visual C# and the Dungeon Crawler. If you have any problems working through this book, visit the site to find answers.

Book Contents

The book is divided into three major parts.

Part I, Dungeon Prerequisites,” includes five chapters that form the foundation of the role-playing game that is developed in the book. These chapters cover subjects like Windows Forms, bitmaps, sprite animation, user input, collision detection, and sound effects.

Part II, Building the Dungeon,” includes four chapters devoted to building the game engine components needed to manage and render dungeon levels. The core of this rendering system is a tiled scroller and a level editor.

Part III, Exploring the Dungeon,” includes six chapters that develop all of the gameplay components of the engine that make the Dungeon Crawler game truly playable. This part offers additional game editors and classes that make it possible to fight monsters, pick up treasure, manage the player’s inventory and equipped gear, gain experience and level up, and talk with NPCs.

Conventions Used in This Book

Source code is presented in fixed-width font for easy readability.

//This is what source code will look like in the text
public void Hello()
{
  Console.WriteLine("Hello World");
}

The following styles are used in this book to highlight portions of text that are important. You will find definition, hint, and tip boxes here and there throughout the book.

Definition

Definitions provide additional details about a key word or subject.

Hint

Hints offer guidance and suggestions on what to do or not do in a given situation.

Tip

Tips give additional information about the current subject being covered.

Companion Website Downloads

You may download the companion website files from www.courseptr.com/downloads. Please note that you will be redirected to our Cengage Learning site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.12.230