CHAPTER 1

image

Introducing 2D Game Development in C#

C#, a modern object-oriented programming language from Microsoft, is one the easiest languages to develop applications with. When developing C# applications within the Microsoft Visual Studio Integrated Development Environment (IDE), programmers are especially empowered with near-transparent application programming interface (API) access, friendly editor-assisted code completion, and almost instantaneous compilations. For these reasons, C# is one the best programming languages for prototyping sophisticated ideas and for learning and experimenting with difficult concepts.

Developing games can be a challenging and lengthy process, partly due to the general programming knowledge and experience required to begin game development, and partly due to the steep learning curve associated with most graphics APIs, such as Microsoft Direct3D and OpenGL. The MonoGame Framework, an open source implementation of the popular XNA Framework discontinued by Microsoft, addresses these issues by creating a developer-friendly framework with a much shallower learning curve. This framework lets developers quickly learn the information needed to begin creating 2D games for many of the popular platforms, from machines running Microsoft (Windows 7 or 8), Apple (Mac OS X), or Linux operating systems to popular mobile devices (iOS, Android, or Windows Phone). The examples in this book are designed to run on Windows for easy demonstration, but the same core principles apply to 2D game development on any platform. So once you’ve mastered the basics, you’ll have MonoGame’s cross-platform support at your fingertips.

The MonoGame framework presents its interface in C#. Together with Microsoft Visual Studio or the MonoDevelop IDE, it becomes possible to focus on learning 2D game development concepts in C# and avoid being distracted by the peripheral requirements of computer graphics, input device interactions, or programming language nuances. For example, instead of working with separate graphical API and User Interface (UI) API for drawing and receiving input, MonoGame provides a straightforward way of drawing graphical objects to the application windows and a simple model for receiving player actions. These allow us to concentrate on the structures and logics for coordinating the drawing of gaming elements and interpreting the intentions of the players rather than being consumed by the details of converting information between the different APIs.

With the elegant C# programming language, we can take advantage of data abstraction and object inheritance in modeling game element behaviors while avoiding preoccupation with pointer manipulation or memory management. Best of all, with MonoGame being an open source project and C# being freely available for the general public, it is not only relatively straightforward, but also free to build games for multiple platforms. Since many vendors (e.g., Windows Marketplace, or Google play) encourage hobbyists to self-publish applications and games, it becomes possible to build, perfect, and eventually publish games in the marketplace!

This chapter first leads you through the steps of downloading, installing, and setting up the development environment: Visual Studio Express IDE, MonoGame framework, and XNB Builder. We then describe the coverage of 2D game development topics in the rest of this book, with brief discussions of related topics that are not covered and where interested readers can find additional information on them.

image Note  MonoDevelop is an IDE, while MonoGame framework, or MonoGame Library, is the library that we will be using throughout this book for building the games. We will be using Microsoft Visual Studio as the IDE for developing example projects.

If you wish to learn more about or brush up on the technologies discussed in this chapter, such as C#, MonoGame, XNA, DirectX, or OpenGL, see the “Technology References” section at the end of this chapter.

Downloading and Installing Development Tools

To use MonoGame and follow along with this book, you will need the Windows 7 or Windows 8 operating system and three additional pieces of software:

  • An IDE: We will be working with the Microsoft Visual Studio IDE. This is the software with which you will edit, compile, and run your games.
  • A game library software development kit (SDK): We will be working with the MonoGame V3.0 (or higher) SDK. With proper installation, we will be able to access MonoGame functions from the Microsoft Visual Studio IDE and build our games.

image Note  If you are working on an Apple Mac machine, you can download the MonoDevelop IDE instead (URL is provided at the end of this chapter). Due to the similarities in the two IDEs, you should be able to follow the examples in this book using MonoDevelop.

  • An asset builder: We will be using XNB Builder for converting formats of assets (images, audios, fonts) to those that are suitable for your games.

Download and install the IDE: Visual Studio Express

The first piece of software you need is the IDE. To develop in C# and MonoGame, you will need to use either a version of Visual Studio or MonoDevelop. The examples in this book use Visual Studio 2012 Express. Visual Studio Express is free to download, and you can find the latest versions, along with optional earlier versions such as Visual C# 2010 Express at the following link:

www.microsoft.com/visualstudio/downloads

All the examples in this book should work in any recent edition of Visual Studio Express, including the 2010 edition.

image Note  Through the Microsoft DreamSpark program, all full-time students have free access to the Professional version of the Visual Studio IDE. All that is required is a school e-mail account. Please refer to www.dreamspark.com/ for more details.

Figure 1-1 shows the page for initiating the installation of Microsoft Visual Studio 2010 Express.

9781430266044_Fig01-01.jpg

Figure 1-1. Landing page for Visual Studio 2010 Express download

Download and install the game SDK: MonoGame Framework

You will also need the MonoGame framework, which you can download from www.monogame.net/downloads. Figure 1-2 shows the download page at the time of writing. The version of MonoGame used in this book is V3.0.1 (released March 6, 2013), as indicated at the bottom of the screenshot.

9781430266044_Fig01-02.jpg

Figure 1-2. Download page at www.monogame.net

Upon clicking the MonoGame 3.0.1 link, you will be brought to a page similar to the one shown in Figure 1-3. The recommended download is MonoGame 3.0.1 for Visual Studio 2010/2012. Although all examples in this book are built with Visual Studio 2012 Express, they will work with Visual Studio 2010 Express just fine.

9781430266044_Fig01-03.jpg

Figure 1-3. The MonoGame website provides a recommended download link for the latest SDK

To install the MonoGame SDK:

  1. Double-click the downloaded installation file to see the following setup:

    9781430266044_unFig01-01.jpg

  2. Click Next to see the installation options. Make sure you check the OpenAL, Visual Studio 2010, and 2012 template options.

    9781430266044_unFig01-02.jpg

  3. Click Install. On successful completion you should see the following confirmation screen.

    9781430266044_unFig01-03.jpg

That’s it! MonoGame is now installed on your computer. The final step is to download and install the asset builder, XNB Builder.

Download and install the asset builder: XNB Builder

In order for MonoGame to work with images, audio clips, and fonts (generally referred to as assets of your games), you need to download the XNB Builder. This can be found at the following site, shown in Figure 1-4:

http://sourceforge.net/projects/xnbbuilder/

9781430266044_Fig01-04.jpg

Figure 1-4. Download page for XNB Builder

The download takes the form of a zip file, XNAFormatter.zip. This file should be unzipped into the XNAFormatter folder that contains the XNAFormatter.exe program. Make sure you remember the location of the XNAFormatter program. You will need to use this program to build art assets for your game into formats that can be loaded by MonoGame.

image Note  The XNAFormatter.exe program converts formats of images, audio files, and fonts into MonoGame’s internal representation, the XNB format. This conversion is referred to as Content Pipeline processing for MonoGame. It is expected that in the future the XNAFormatter.exe will be integrated into the MonoGame SDK and this extra step of installing the asset importer will become unnecessary.

In the next chapter, we will begin by building our first empty template project. In the meantime, let’s look at a brief overview of what we will cover in this book, and some recommendations on where to go to learn about other related topics in game development.

image Note  Interested readers can refer to the excellent article by Dean Ellis overviewing MonoGame: www.gamasutra.com/view/feature/192209/from_xna_to_monogame.php

What Is Covered in This Book?

Because this book is targeted toward individuals with some experience in software development, it jumps headlong into walking you through the technical details you need to know to create your own games from scratch. The overall goal is to guide you through this process as quickly and painlessly as possible while providing a solid foundation in the game development concepts used in the industry today.

Also note that game development is a catch-all term that actually includes many different disciplines. While some of these disciplines lie within the computer science and computer programming fields, there are several other areas of game development that are actually outside of the computer science and programming disciplines, including game design and asset creation (art). These areas are as crucial to the success of a game as the technical process. With that in mind, we’ll detail the areas of development that are and are not covered in this book.

The following is an overview of the book and the topics we will cover:

  • Installation guide
  • Conceptual framework behind MonoGame
  • Basic architecture of a 2D game engine using the MonoGame Framework (Chapter 2):
  • Draw and update cycles
  • Assets and resource-loading models
  • Game object abstraction and encapsulation
  • Game state implementation
  • Drawing coordinate systems
  • 2D camera abstraction
  • Working with randomness
  • Points, vectors, dot product, cross product, and their support for implementing speed, velocity, direction, collision responses
  • Finite-state machines
  • Game complexity and linear collections
  • Pixel-accurate collisions
  • Semiautonomous behaviors (controlled gradual turning, homing in, chasing, and following)
  • 2D camera manipulation
  • Sprite sheets and sprite animations
  • Particle systems
  • Fonts and audio effects
  • Design of a complete 2D game
  • Enumerate functionality of all gaming elements
  • Define C# classes implementing gaming element functionalities
  • Synthesize all elements into a final fun game

This list represents fundamentals of technical knowledge required for developing videogames. By working through the activities in this book, you will gain a vital foundation in the technical building blocks that underlie all great games, from Pac-Man to Angry Birds! However, there are many other aspects to building successful video games. If you are serious about game developmen,t you will also need to consider some of the topics discussed in the next section.

What Is Not Covered in This Book?

The following list describes some areas of development that are not covered in the book:

  • Game design : This is the phase of brainstorming and creating the components and parts that comprise the game. In general, game design should be documented before the process of building the game begins; however, there are many different approaches to creating the game design document itself. Some people prefer to flesh out all the mechanics the game will have in a single large document, while others prefer to create a concise concept document of a few pages and then begin prototyping before completely fleshing out the mechanics.
  • Game development life cycle : There are many approaches to the game development process. If you’re starting out as a hobbyist, you won’t need to worry too much about your development life cycle, and you can follow something like the simplified process shown in Chapter 9. Professional game development shops, on the other hand, need to manage not only their technical know-how, but also the team and their various talents, the schedule, testing etc. There are many life cycle processes in use today; one commonly used for software projects is agile development.
  • Game architecture planning : This is about deciding how the game will be structured, including what objects the game will include, and how they will interact with one another. This phase relies heavily on knowledge of object-oriented programming. The game-building exercise in the last chapter, where we enumerate all the functionality of gaming elements and define how they should interact, gives a simplified peek into this area.
  • Asset creation : This is the creation and preparation of assets (art, video, and audio) for the game. Asset creation includes everything from the process of creating sprite sheets to properly sizing images. In this book assets to all games are provided so that you do not have to be concerned with the creation of these while learning your way around game development.

If you’re interested in some of these other areas of the game development process, or wish to dive more deeply into the technical aspects of creating 2D or 3D games, a plethora of resources exist. The authors have found the following books on game design and the development process to be very helpful and informative:

  • Game Design Workshop: A Playcentric Approach to Creating Innovative Games, by Tracy Fullerton (CRC Press, 2008)
  • Fundamentals of Game Design (2nd Edition), by Ernest Adams (New Riders, 2009)
  • The Art of Game Design, by Jesse Schell (CRC Press, 2008)

While the step–by-step tutorials from this book cover many of the foundational concepts in typical 2D game engines, the preceding areas can help you with the design and implementation of fun games. Together, these form a concrete foundation for building simple 2D games. In order to begin to understand and appreciate the intimate details of popular games like the Halo series, you need technical knowledge from standard computer science and computer engineering undergraduate curricula, including computer graphics, classical mechanics, artificial intelligence, networking, databases, human-computer interaction, software engineering, and so on. In addition, many of the advanced autonomous behaviors in games utilize concepts from linear algebra.

Very importantly, to be a successful game developer, you would need to know how to—and love to—work with people. Videogame creation is by nature a group effort. Artists, programmers, storytellers, managers, etc., must all work together in putting together a compelling and fun system. We should always remember that the modern blockbuster videogame titles, like the Halo series, are built by hundreds of full-time professionals. As aspiring indie developers, it is important to scope and design our projects accordingly. The technical concepts and knowledge you will learn from this book are important and great foundational first steps on your journey to becoming a successful game developer!

Technology References

The following list offers several links for obtaining additional information on topics related to this book.

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

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