ebook img

Introduction to Programming through - Andrews University PDF

400 Pages·2009·9.76 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 Introduction to Programming through - Andrews University

PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by Microsoft Corporation All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2009932322 A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation ofcfi e or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to [email protected]. Microsoft, Microsoft Press, DirectX, Halo, Halo Wars, MS, MSDN, Visual C#, Visual Studio, Windows, Windows Live, Windows Media, Windows Vista, Xbox, Xbox 360, Xbox LIVE, XNA and Zune are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are cfi titious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Ben Ryan Developmental Editor: Devon Musgrave Project Editor: Valerie Woolley Editorial Production: S4Carlisle Publishing Services Technical Reviewer: Kurt Meyer; Technical Review services provided by Content Master, a member of CM Group, Ltd. Cover: Tom Draper Design Body Part No. X16-03277 To Mary, David, and Jenny. Table of Contents Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Part I Getting Started 1 Computers, C#, XNA, and You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Learning to Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Becoming a Great Programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 How the Book Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 C# and XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Installing the Development Environment and the XNA Framework . . . . . 6 Setting Up a PC to Run XNA Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Setting Up an Xbox 360 to Run XNA Games . . . . . . . . . . . . . . . . . . . . . . . . 7 Writing Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Creating Your First Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Running Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Stopping a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Storing Games on the Xbox 360 or Zune . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Running the Same XNA Game on Different Devices . . . . . . . . . . . . . . . . . 15 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 Programs, Data, and Pretty Colors . . . . . . . . . . . . . . . . . . . . . . . . . 21 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Making a Game Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Statements in the Draw Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Working with Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Storing Color Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Setting a Color Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Controlling Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Games and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Classes as Offices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Game World Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Storing Data in Computer Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Drawing by Using Our Color Intensity Variables . . . . . . . . . . . . . . . . . . . . 32 v vi Table of Contents Updating Our Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Memory Overflow and Data Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Making a Proper Mood Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Making Decisions in Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Completed Mood Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Finding Program Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3 Getting Player Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Reading a Gamepad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Gamepads and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Finding a Gamepad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Testing the Gamepad Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Zune Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Using the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Stopping the Game with the Escape Key . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using a Gamepad and a Keyboard at the Same Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Adding Vibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Controlling the Vibration of a Gamepad . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Testing Intensity Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Program Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Part II Images, Sound, and Text 4 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Resources and Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Getting Some Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Content Management Using XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Working with Content Using XNA Game Studio . . . . . . . . . . . . . . . . . . . . 68 XNA Game Studio Solutions and Projects . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Adding Resources to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Using Resources in a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Loading XNA Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Positioning Your Game Sprite on the Screen . . . . . . . . . . . . . . . . . . . . . . . 76 Table of Contents vii Sprite Drawing with SpriteBatch.................................79 Filling the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5 Writing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Text and Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Text as a Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Creating the XNA Clock Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Adding a Font Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Loading a Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Drawing with a Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Changing the Font Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Getting the Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Making a Prettier Clock with 3-D Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Drawing Multiple Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Repeating Statements with a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Other Loop Constructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Fun with for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Creating Fake 3-D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Creating Shadows Using Transparent Colors . . . . . . . . . . . . . . . . . . . . . . 101 Drawing Images with Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6 Creating a Multi-Player Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Creating the Button-Bash Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Level and Edge Detectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Constructing the Complete Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Adding Test Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 7 Playing Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Adding Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Creating the Drum Pad Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Capturing Sounds with Audacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Storing Sounds in Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 viii Table of Contents Using Sounds in an XNA Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Playing Background Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 8 Creating a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Making Another Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Reaction Timer Bug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Finding Winners Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Using Data in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Scanning an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Using an Array as a Lookup Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Displaying the Winner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 9 Reading Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Using the Keyboard in XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Creating the Message Board Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Registering Key Presses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 The Keys Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Working with Arrays, Objects, and References . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Values and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Arrays as Offices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Say Hello to the Garbage Collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Using References and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Why Do We Have References and Values? . . . . . . . . . . . . . . . . . . . . . . . . 149 References and GetPressedKeys ................................149 Displaying Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Detecting Key Presses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Decoding Key Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Using the Shift Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Editing the Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Table of Contents ix Part III Writing Proper Games 10 Using C# Methods to Solve Problems . . . . . . . . . . . . . . . . . . . . . 163 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Playing with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Zooming In on an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Creating a Zoom-Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Updating the Drawing Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Creating a Method to Calculate Percentages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Returning Nothing Using void .................................171 Debugging C# Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Hitting a Breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Using Floating-Point Numbers in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 The Compiler and C# Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Compilers and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Expression Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Stopping the Zoom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Zooming from the Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 11 A Game as a C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Creating Game Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Projects, Resources, and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 XNA Game Studio Solutions and Projects . . . . . . . . . . . . . . . . . . . . . . . . . 191 The Program .cs File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Renaming the Game1 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Creating Game Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Sprites in Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Managing the Size of Game Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Moving Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Bouncing the Cheese . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Dealing with Display Overscan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 x Table of Contents 12 Games, Objects, and State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Adding Bread to Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Using a Structure to Hold Sprite Information . . . . . . . . . . . . . . . . . . . . . . 212 Using the Gamepad Thumbsticks to Control Movement . . . . . . . . . . . . 214 Improving Programs Using Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Handling Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Making the Cheese Bounce off the Bat . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Strange Bounce Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Strange Edge Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Adding Tomato Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Tomato Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 13 Making a Complete Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Making a Finished Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Adding Scores to a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Adding Survival . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Adding Progression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Improving Code Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Refactoring by Creating Methods from Code . . . . . . . . . . . . . . . . . . . . . . 236 Refactoring by Changing Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Creating Code Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Creating Useful Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Adding a Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Adding a Title Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Games and State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Using the State Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Building a State Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Chapter Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 14 Classes, Objects, and Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Design with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 An Object Refresher Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Cohesion and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Coupling Between Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

Description:
Programming lets you bring your ideas to life, and with C# and XNA expertise you can learn a lot about how games work and even create totally new ones of your own .
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.