ebook img

Learn 2D Game Development with C#: For iOS, Android, Windows Phone, Playstation Mobile and More PDF

285 Pages·2016·7.04 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn 2D Game Development with C#: For iOS, Android, Windows Phone, Playstation Mobile and More

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Authors ���������������������������������������������������������������������������������������������������������������xv About the Technical Reviewer ������������������������������������������������������������������������������������������xvii Acknowledgments �������������������������������������������������������������������������������������������������������������xix Introduction �����������������������������������������������������������������������������������������������������������������������xxi ■ Chapter 1: Introducing 2D Game Development in C# ���������������������������������������������������������1 ■ Chapter 2: Getting to Know the MonoGame Framework �������������������������������������������������11 ■ Chapter 3: 2D Graphics, Coordinates, and Game State ����������������������������������������������������41 ■ Chapter 4: Getting Things Moving �����������������������������������������������������������������������������������87 ■ Chapter 5: Pixel-accurate collisions������������������������������������������������������������������������������123 ■ Chapter 6: Game object states and Semiautonomous Behaviors ����������������������������������147 ■ Chapter 7: Sprites, Camera, Action! ������������������������������������������������������������������������������183 ■ Chapter 8: Particle Systems ������������������������������������������������������������������������������������������213 ■ Chapter 9: Building Your First 2D Game �����������������������������������������������������������������������227 Index ���������������������������������������������������������������������������������������������������������������������������������261 v Introduction Welcome to Learn 2D Game Development with C#. Because you have picked up this book, you are likely interested in creating your own games with the C# programming language. This book teaches you how to develop 2D games with C# and MonoGame by giving you background and conceptual information so you can play, examine, and develop 2D games. This book identifies and presents relevant concepts from software engineering, computer graphics, mathematics, physics, and game development—all in the context of building 2D games. The projects you’ll develop in this book are based on MonoGame, the open source implementation of of the popular XNA Framework discontinued by Microsoft. The presentations are tightly integrated with the analysis and development of source code; you’ll spend much of the book building gamelike concept projects that demonstrate game principles and components. By building on concepts introduced early on, the book leads you on a journey through which you will master the basic concepts behind game development while simultaneously gaining hands-on experience developing simple but working 2D games. By the end of the book, you will be familiar with the implementation details of 2D games, and you should feel competent in implementing commonly encountered 2D game behaviors using MonoGame. Who should read this book This book is targeted toward programmers who are familiar with basic object-oriented programming concepts and have a basic to intermediate knowledge of an object-oriented programming language like C# or Java. For example, if you are a student who has taken a few introductory programming courses, an experienced developer who is new to games and graphics programming, or a self-taught programming enthusiast, you will be able to follow the concepts and code presented in this book with little trouble. If you’re new to programming in general, it is suggested that you first become comfortable with the C# programming language before tackling the content provided in this book. Assumptions You should be experienced with programming in an object-oriented programming language, such as C# or Java. The examples in this book were created with the assumption that readers understand data encapsulation and inheritance. In addition, you should also be familiar with basic data structures such as linked lists and dictionaries and be comfortable working with the fundamentals of algebra and geometry, particularly linear equations and coordinate systems. Who should not read this book This book is not designed to teach readers how to program, nor does it attempt to explain intricate details of C# or MonoGame. If you have no prior experience developing software with an object-oriented `programming language, you will probably find the examples in this book difficult to follow. xxi ■ IntroduCtIon On the other hand, if you have an extensive background in game development for other platforms and with other programming languages, the content here will be too basic; this is a book intended for developers without 2D game development experience. Organization of this book This book divides the process of building 2D games into essential topic areas: tools, graphics, special effects, math and physics, and logic and behavior. These topics are organized into chapters. Each topic area (chapter) is then subdivided into essential concepts; for example, concepts related to computer graphics include coordinate spaces and camera abstraction. The book introduces each concept via a gamelike example organized as a section in a chapter. Each has an associated step-by-step project workflow. In this way, each section in the book corresponds to a single project or concept. The first section begins with a simple project that you will build from scratch. Throughout the text, each subsequent section builds upon the sections that precede it. While this makes it a bit difficult to skip around in the book, it will give you practical experience and a solid understanding of how the different concepts relate to one another. In addition, rather than always working with new and minimalistic projects, you gain experience with building larger and more interesting projects. The projects themselves start with simple concepts, such as creating objects and moving them across the screen, but quickly move to more complex concepts, such as implementing pixel-accurate collision detection and working with user-defined coordinate systems. In this way, while the concepts are presented in simple 2D gamelike examples, by the end of the book, your code base for the projects will include all the essential concepts covered. That final code base, which you will have developed incrementally over the course of the entire book, serves as a great platform on which you can begin building your own 2D games. This is exactly what the very last chapter of the book does, leading you from conceptualization, to design, to implementation of a not-so-simple casual 2D game. Finding your best starting point in this book As a reader, there are several ways for you to follow along with this book. The first and most obvious is to enter the code into your project as you follow through each step in the book. From a learning perspective, this is probably the most effective way to absorb the information presented; however, we understand that it may not be the most realistic, due to the amount of code or debugging that approach may require. To help ameliorate this, each of the sections and projects in this book has two corresponding source code folders: the starter project in a 1.Starting folder, and the completed project in a 2.Completed folder. You can see an example of this structure in Figure 1. The starter projects allow you to follow along in the corresponding section by entering the code as you encounter it in the book, while the completed project lets you run and see the project in its completed state. xxii ■ IntroduCtIon Figure 1. The folder structure for the book projects We recommend that you refer to the completed project when you begin a new section. Doing so lets you preview the current section’s project, giving you a clear idea of the end goal, and letting you see what the project is trying to achieve. You may also find the completed project code useful when you have problems while building the code yourself, because you can compare your code with the completed project’s code during difficult debugging situations. ■ Note We have found the WinMerge program (http://winmerge.org/) to be an excellent tool for comparing source code files and folder. Finally, after completing a project, we recommend that you compare the behavior of your implementation with the completed-project implementation provided. By doing so, you can observe whether your code is behaving as expected. Conventions and features in this book This book presents information using conventions designed to make the information readable and easy to follow: • Each example is built on top of prior ones; however, complete source code is provided for each exercise, so you can use that to skip sections you don’t need. • A screen shot and an outlined overview of the steps involved precede each step-by-step procedure, so that you will know what to expect. • When necessary, we provide relevant background information before beginning the analysis of how to implement the concept. For example, the book discusses differences between pixel and user-defined spaces before analyzing how to implement a user-defined coordinate system. xxiii ■ IntroduCtIon •• SSoouurrccee ccooddee aannaallyyssiiss iiss ddiivviiddeedd iinnttoo ddiissttiinncctt sstteeppss,, wwhheerree eeaacchh sstteepp ccoonnttrriibbuutteess ttoo tthhee eevveennttuuaall iimmpplleemmeennttaattiioonn ooff tthhee ccoonncceepptt.. •• IItteemmss tthhaatt yyoouu sshhoouulldd ttyyppee ((eexxcceeppttiinngg ssoouurrccee ccooddee)) aappppeeaarr iinn bboolldd tteexxtt.. •• PPrrooggrraammmmiinngg--rreellaatteedd iitteemmss,, ssuucchh aass ccllaassss nnaammeess,, vvaarriiaabbllee nnaammeess,, nnaammeessppaacceess,, aanndd ssoo oonn,, aappppeeaarr iinn iittaalliiccss.. •• WWhheenn yyoouu nneeeedd ttoo pprreessss ttwwoo kkeeyyss aatt oonnccee,, ssuucchh aass hhoollddiinngg tthhee CCoonnttrrooll kkeeyy wwhhiillee pprreessssiinngg tthhee AA kkeeyy,, tthhiiss bbooookk sshhoowwss tthhee ttwwoo kkeeyyss sseeppaarraatteedd bbyy aa pplluuss ((++)) ssiiggnn——ffoorr eexxaammppllee,, CCttrrll++AA.. SSyysstteemm rreeqquuiirreemmeennttss YYoouu wwiillll nneeeedd tthhee ffoolllloowwiinngg hhaarrddwwaarree aanndd ssooffttwwaarree iinn oorrddeerr ttoo ffoollllooww tthhee eexxaammpplleess iinn tthhiiss bbooookk:: •• WWiinnddoowwss 77 oorr hhiigghheerr •• MMiiccrroossoofftt VViissuuaall SSttuuddiioo 22001100 oorr hhiigghheerr,, aannyy eeddiittiioonn •• MMoonnooGGaammee VV33..00 oorr hhiigghheerr •• AA ccoommppuutteerr tthhaatt hhaass aa 11..66 GGHHzz oorr ffaasstteerr pprroocceessssoorr ((22 GGHHzz iiss rreeccoommmmeennddeedd)) •• MMoorree tthhaann 11 GGBB ooff RRAAMM •• 11 GGBB ooff aavvaaiillaabbllee hhaarrdd ddiisskk ssppaaccee •• AA MMiiccrroossoofftt DDiirreeccttXX 99––ccaappaabbllee vviiddeeoo ccaarrdd tthhaatt ssuuppppoorrttss aatt lleeaasstt OOppeennGGLL 33 aanndd ccaann rruunn aatt aa rreessoolluuttiioonn ooff 11,,002244××776688 oorr hhiigghheerr •• AAnn IInntteerrnneett ccoonnnneeccttiioonn,, ffoorr ddoowwnnllooaaddiinngg ssooffttwwaarree oorr cchhaapptteerr eexxaammpplleess AAllll ooff tthhee pprroojjeeccttss iinn tthhiiss bbooookk wweerree bbuuiilltt ttoo uussee eeiitthheerr yyoouurr kkeeyybbooaarrdd oorr aa MMiiccrroossoofftt XXbbooxx 336600 ccoonnttrroolllleerr ffoorr WWiinnddoowwss.. YYoouu sshhoouulldd bbee aabbllee ttoo wwoorrkk wwiitthh aannyy wwiirreedd XXbbooxx 336600 ccoonnttrroolllleerr bbyy pplluuggggiinngg iitt iinnttoo aa UUSSBB ppoorrtt oonn yyoouurr ccoommppuutteerr;; hhoowweevveerr,, wwiirreelleessss XXbbooxx 336600 ccoonnttrroolllleerrss rreeqquuiirree aann XXbbooxx 336600 wwiirreelleessss rreecceeiivveerr ffoorr WWiinnddoowwss ttoo ffuunnccttiioonn oonn tthhee PPCC.. IIff yyoouu iinnsstteeaadd wwaanntt ttoo uussee aa mmoouussee wwiitthh yyoouurr WWiinnddoowwss--bbaasseedd pprroojjeeccttss,, yyoouu wwiillll nneeeedd ttoo mmaakkee ssoommee ccooddee mmooddiifificcaattiioonnss.. ThThee ttyyppeess ooff cchhaannggeess yyoouu’’llll nneeeedd ttoo mmaakkee aarree aaddddrreesssseedd nneeaarr tthhee eenndd ooff tthhee DDrraaww aanndd CCoonnttrrooll pprroojjeecctt iinn CChhaapptteerr 22.. YYoouu ccaann fifinndd ddeettaaiillss oonn hhooww ttoo ddoowwnnllooaadd,, iinnssttaallll,, aanndd ccoonnfifigguurree VViissuuaall SSttuuddiioo aanndd MMoonnooGGaammee iinn CChhaapptteerr 11,, ““IInnttrroodduucciinngg 22DD GGaammee DDeevveellooppmmeenntt iinn CC##..”” CCooddee ssaammpplleess WWiitthh tthhee eexxcceeppttiioonn ooff tthhee fifirrsstt cchhaapptteerr,, aallll tthhee cchhaapptteerrss iinn tthhiiss bbooookk iinncclluuddee eexxaammpplleess tthhaatt lleett yyoouu iinntteerraaccttiivveellyy eexxppeerriimmeenntt wwiitthh aanndd lleeaarrnn tthhee nneeww mmaatteerriiaallss.. YYoouu ccaann ddoowwnnllooaadd aallll tthhee ccooddee ffoorr aallll tthhee pprroojjeeccttss,, iinncclluuddiinngg tthhee aassssoocciiaatteedd aasssseettss ((iimmaaggeess,, aauuddiioo cclliippss,, oorr ffoonnttss)) iinn bbootthh tthheeiirr pprree--eexxaammppllee aanndd ccoommpplleetteedd ssttaatteess ffrroomm tthhee ffoolllloowwiinngg ppaaggee:: hhttttpp::////wwwwww..aapprreessss..ccoomm//99778811443300226666004444 FFoollllooww tthhee iinnssttrruuccttiioonnss ttoo ddoowwnnllooaadd tthhee ssoouurrccee ccooddee fifillee llaabbeelleedd 99778811443300226666004444..zziipp.. TToo iinnssttaallll tthhee ccooddee ssaammpplleess,, uunnzziipp tthhee ssoouurrccee ccooddee fifillee.. YYoouu sshhoouulldd sseeee tthhee ffoollddeerr ssttrruuccttuurree sshhoowwnn pprreevviioouussllyy iinn FFiigguurree 11.. AAss ddeessccrriibbeedd pprreevviioouussllyy,, tthhee ssttaarrtteerr pprroojjeecctt iiss iinn tthhee 11..SSttaarrttiinngg ffoollddeerr,, aanndd tthhee ccoorrrreessppoonnddiinngg ccoommpplleetteedd pprroojjeecctt iiss iinn tthhee 22..CCoommpplleetteedd ffoollddeerr.. WWiitthh VViissuuaall SSttuuddiioo pprrooppeerrllyy iinnssttaalllleedd,, yyoouu ccaann ddoouubbllee--cclliicckk tthhee ccoorrrreessppoonnddiinngg ssoolluuttiioonn ((..ssllnn)) fifillee ttoo bbeeggiinn wwoorrkkiinngg wwiitthh aannyy ooff tthhee pprroovviiddeedd pprroojjeeccttss.. xxiv Chapter 1 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. 1 Chapter 1 ■ IntroduCIng 2d game development In C# ■ 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. ■ 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. ■ 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. 2 Chapter 1 ■ IntroduCIng 2d game development In C# 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. 3

Description:
Apress, 2013. — 292 p. — ISBN-10: 143026604X, ISBN-13: 978-1-4302-6605-1На англ. языке.2D games are hugely popular across a wide range of platforms and the ideal place to start if you're new to game development. With Learn 2D Game Development with C#, you'll learn your way around the
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.