ebook img

Learning C# by Programming Games PDF

450 Pages·2013·5.227 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 Learning C# by Programming Games

A E = m c 2 This eBook is downloaded from www.PlentyofeBooks.net ∑ PlentyofeBooks.net is a blog with an aim 1 of helping people, e specially students, who cannot afford to buy some costly books from the market. For more Free eBooks and educational material visit www.PlentyofeBooks.net Uploaded By $am$exy98 theBooks Arjan Egges Jeroen D. Fokker Mark H. Overmars Learning C# by Programming Games Learning C# by Programming Games Arjan Egges (cid:2) Jeroen D. Fokker (cid:2) Mark H. Overmars Learning C# by Programming Games ArjanEgges MarkH.Overmars Dept.ofInformationandComputing Dept.ofInformationandComputing Sciences Sciences UtrechtUniversity UtrechtUniversity Utrecht,TheNetherlands Utrecht,TheNetherlands JeroenD.Fokker Dept.ofInformationandComputing Sciences UtrechtUniversity Utrecht,TheNetherlands ISBN978-3-642-36579-9 ISBN978-3-642-36580-5(eBook) DOI10.1007/978-3-642-36580-5 SpringerHeidelbergNewYorkDordrechtLondon LibraryofCongressControlNumber:2013935694 ACMComputingClassification(1998): D.1,D.3,K.8,H.5 ©Springer-VerlagBerlinHeidelberg2013 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartof thematerialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation, broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionorinformation storageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilarmethodology nowknownorhereafterdeveloped.Exemptedfromthislegalreservationarebriefexcerptsinconnection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’slocation,initscurrentversion,andpermissionforusemustalwaysbeobtainedfromSpringer. PermissionsforusemaybeobtainedthroughRightsLinkattheCopyrightClearanceCenter.Violations areliabletoprosecutionundertherespectiveCopyrightLaw. Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant protectivelawsandregulationsandthereforefreeforgeneraluse. Whiletheadviceandinformationinthisbookarebelievedtobetrueandaccurateatthedateofpub- lication,neithertheauthorsnortheeditorsnorthepublishercanacceptanylegalresponsibilityforany errorsoromissionsthatmaybemade.Thepublishermakesnowarranty,expressorimplied,withrespect tothematerialcontainedherein. Coverdesign:CoverdesignbasedonagamespritedesignedbyHeinyReimes.Reusedbypermission. Printedonacid-freepaper SpringerispartofSpringerScience+BusinessMedia(www.springer.com) Preface Introduction If you are reading this, then we assume that you are interested in learning how to develop your own computer games. If you do not yet know how to program, don’t worry! Developing computer games is actually a perfect way to learn how to program in modern programming languages. This book will teach you how to programinC#withoutrequiringanypreviousprogrammingexperience.Itdoesso throughthecreationofcomputergames. Inour opinion,C#is thelanguageofchoicetolearnhowtoprogram.Thelan- guageisverywellstructuredandavoidssomeoftheproblemsofJava.Itisalsoa moremodernlanguage.MovingfromC#toC++,whichisstillthemostimportant programming language in the game industry, is relatively easy. Also, for C# there isanexcellentfreeprogrammingenvironmentavailable,VisualStudioC#Express Edition,whichisusedinthisbook. Contrarytomostprogrammingbooks,wedonotorganizethebookaccordingto programminglanguageparadigms,butinsteadweusethestructureandelementsof computer games as a framework. For instance, there are chapters on dealing with player input, game objects, game worlds, game states, levels, animation, physics, andintelligence. While reading this book, you will create four games. We have chosen different types of games to show the various aspects of game development.We start with a simple shooting game, we move on to puzzle games consisting of multiple levels, andweconcludethebookbydevelopingafull-fledgedplatformgamewithanima- tion,gamephysics,andintelligentenemies. Thisbookisnotacookbook.ThebookprovidesathoroughintroductiontoC# and object-oriented programming, organized by the structure of games. We intro- duce important aspects of programming in general, such as an overview of differ- entprogrammingparadigms,syntaxdiagrams,collections,exceptionhandling,and more.Wewillalsodiscussvariousaspectsofsoftwarearchitecturewithinacontext ofgamedevelopment.Bydoingthat,weproposeaframeworkformanaginglevels, gamestates,aswellasahierarchyofself-sufficientgameobjectsthattogetherform v vi Preface aninteractivegameworld.Furthermore,wewillshowanumberofcommonlyused techniques in games, such as drawing layers of sprites, rotating, scaling and ani- matingsprites,showingaheads-updisplay(HUD),dealingwithphysics,handling interactionbetweengameobjects,andcreatingnicevisualeffectssuchassnowor glitters. Throughoutthebook,youwillfindtextingrayboxes.Theseboxescontaintips andtricksfordesigningpartsofyourgame,suchasaddingtutoriallevels,butsome- timestheyalsodiscussaparticularprogrammingissue,suchasdealingwithstatic variablesordesigninggameenginecode. RequiredMaterialsand Tools Alongwiththisbook,wesupplyvariousmaterials.Alltheexampleprogramsused inthisbookareavailableasVisualStudioprojects,whichyoucanopen,edit,com- pile, and run yourself. Furthermore, we supply a set of game assets (sprites and sounds),whichareusedbyalltheexamples.Nexttotheexampleprograms,wealso providedetailedinstructiononhowtodownloadandinstallthenecessarytools.We havecreatedawebsitewhereyoucandownloadallthematerials.TheURLofthis websiteishttp://www.csharpprogramminggames.com. In order to develop computer games, a few tools need to be installed on your computer.ThemaintoolthatyouaregoingtoneedistheXNAGameStudiosoft- ware,incombinationwithadevelopmentenvironmentcalledVisualStudioExpress 2010,createdbyMicrosoft.Ontheaccompanyingwebsite,youcanfinddetailedin- structionsonhowtoobtainandinstallthesetools.TheVisualStudioExpress2010 environment is freely available and compatible with the latest XNA Game Studio version(4.0whenthisbookwasprinted). UsingthisBook asaBasisfora ProgrammingCourse Thisbookisgearedtowardbeingusedasabasisforagame-orientedprogramming course.Eachpartinthisbookisconcludedbyexercisesandchallenges.Solutions to the exercises are available through the accompanying website. The challenges are generally more complex programming exercises. These challenges can serve as practical assignments for students following the programming course. On the accompanyingwebsite,anumberofadditionalchallengesareavailablethatcanbe usedasabasisforpracticalassignmentsaswell. Byfollowingthestructureofthebookthroughoutthecourse,thestudentswillbe introducedtoallthemainaspectsofprogramminginanobject-orientedlanguage. Supplementarymaterialsfor organizingsucha courseareavailableontheaccom- panyingwebsite.Asamplescheduleofacourseconsistingof15sessionswiththree practicalassignmentsisgivenasfollows: Preface vii Topic Chapters Exercises,deadlines 1 Introduction 1,2 ExercisespartI 2 Gameloop,types,variables 3,4 ExercisespartI 3 Playerinput,if,booleans 5,6 ExercisespartI 4 Classes,methods,objects 7,8 ExercisespartII 5 for,while,randomness 8,9 HandinpracticalassignmentI 6 Inheritance 10,11 ExercisespartII 7 Collections,arrays,interfaces 12 ExercisespartIII 8 Grids,gameworlds 13,14,15 ExercisespartIII 9 Timeingames,recursion 16,17,18 ExercisespartIII 10 Spritesheets,gamestates 19,20,21 HandinpracticalassignmentII 11 Abstractclasses,switch,fileI/O 21,22 ExercisespartIV 12 Libraries,gamestructure 23,24,25 ExercisespartIV 13 Animation,gamephysics 26,27 ExercisespartV 14 Enemies,exceptions 28,29,30 ExercisespartV 15 Generalquestions all HandinpracticalassignmentIII Utrecht,TheNetherlands ArjanEgges JeroenD.Fokker MarkH.Overmars Acknowledgements Just as games are generally not developed by a single person but by a team, this book was a team effort as well. First of all, I would like to thank my co-authors JeroenandMarkfortheirinspiringideasandtheinterestingdiscussionswe’vehad whilewritingthisbook. Thisbookisbasedonreadingmaterialthatwashandedouttothe(many)students ofthegameprogrammingcourseatUtrechtUniversity.Theirfeedbackandcritical analysisofthematerialhasbeenofgreathelp,aswellastheirmotivationtowork withtheexamplegames.Iofficiallyapologizeforanyfrustrationthatoccurreddue toafewridiculouslydifficultlevelsoftheTickTickgame! Iwouldliketothankmycolleaguesfortheirideasandtheirinterestinthiswork. In particular, I would like to thank Cathy Ennis and Sybren Stüvel for providing corrections and for reading the text in detail. Their feedback has resulted in many improvementsofthetextandthesampleprograms. The sprites for all the example games in this book were designed by Heiny Reimes. It was a pleasure working with him on designing the example games and improvingthem. IwouldalsoliketothankRalfGerstnerfromSpringerfortakingthetimetoread themanuscriptandhelpingtomakethisbookareality. Finally,Iwouldliketothankmywife,Sterre,forhercontinuingsupport.Idedi- catethisworktoher. January2013 ArjanEgges ix Contents PartI GettingStarted 1 BuildingYourFirstGameApplication . . . . . . . . . . . . . . . . . 3 1.1 StructureofThisBook . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 GettingandInstallingtheTools . . . . . . . . . . . . . . . . . . . 4 1.3 CreatingYourFirstGameApplication . . . . . . . . . . . . . . . 4 1.4 RunningtheNewlyCreatedProject . . . . . . . . . . . . . . . . . 5 1.5 ProjectsandSolutions . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 RunningtheExamplesinThisBook . . . . . . . . . . . . . . . . 8 1.7 WhatYouHaveLearned . . . . . . . . . . . . . . . . . . . . . . . 9 2 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 ComputersandPrograms . . . . . . . . . . . . . . . . . . . . . . 12 2.2.1 ProcessorandMemory . . . . . . . . . . . . . . . . . . . 12 2.2.2 Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.3 ProgrammingLanguages . . . . . . . . . . . . . . . . . . . . . . 13 2.3.1 TheEarlyDays:ImperativeProgramming . . . . . . . . . 14 2.3.2 ProceduralProgramming:Imperative+Procedures. . . . . 15 2.3.3 Object-OrientedProgramming:Procedural+Objects . . . 15 2.3.4 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3.5 C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4 TranslatingaProgram . . . . . . . . . . . . . . . . . . . . . . . . 18 2.4.1 Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.4.2 Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.4.3 Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.4.4 Compiler+Interpreter . . . . . . . . . . . . . . . . . . . . 20 2.4.5 Compiler+Compiler . . . . . . . . . . . . . . . . . . . . 21 2.5 ProgrammingGames . . . . . . . . . . . . . . . . . . . . . . . . 22 2.5.1 GamesAreSpecialPrograms . . . . . . . . . . . . . . . . 22 2.5.2 GameEngines . . . . . . . . . . . . . . . . . . . . . . . . 22 xi

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.