You are previewing Microsoft® XNA® Game Studio 3.0: Learn Programming Now!, 2nd Edition.
O'Reilly logo
Microsoft® XNA® Game Studio 3.0: Learn Programming Now!, 2nd Edition

Book Description

Your hands-on introduction to computer programming with Microsoft Visual C#® 2005 and XNA Game Studio 3.0, the Xbox 360® development tool. Get entertaining, easy-to-follow instructions for customizing Xbox 360 games—and creating games of your own.

Table of Contents

  1. Microsoft® XNA® Game Studio 3.0: Learn Programming Now!
  2. Dedication
  3. Acknowledgments
  4. Introduction
    1. Who This Book Is For
    2. System Requirements
    3. Code Samples
    4. Support for This Book
      1. Find Additional Content Online
    5. Questions and Comments
  5. I. Getting Started
    1. 1. Computers, C#, XNA, and You
      1. Introduction
        1. Learning to Program
        2. Becoming a Great Programmer
      2. How the Book Works
      3. C# and XNA
      4. Getting Started
        1. Installing the Development Environment and the XNA Framework
        2. Setting Up a PC to Run XNA Games
        3. Setting Up an Xbox 360 to Run XNA Games
          1. XNA Creators Club
          2. XNA Game Studio Connect
          3. XNA Game Studio Device Center
          4. Connecting a Zune to a Windows PC
      5. Writing Your First Program
        1. Creating Your First Project
        2. Running Your First Program
        3. Stopping a Program
        4. Storing Games on the Xbox 360 or Zune
        5. Running the Same XNA Game on Different Devices
          1. Creating a Copy of an XNA Project for Another Device
          2. Selecting Your Deployment Targets
      6. Conclusion
      7. Chapter Review Questions
    2. 2. Programs, Data, and Pretty Colors
      1. Introduction
      2. Making a Game Program
        1. Statements in the Draw Method
      3. Working with Colors
        1. Storing Color Values
        2. Setting a Color Value
      4. Controlling Color
        1. Games and Classes
          1. Classes and Behaviors
          2. Drawing and Updating in Games
          3. Sharing Game World Data Between Draw and Update
        2. Classes as Offices
        3. Game World Data
        4. Storing Data in Computer Memory
        5. Drawing by Using Our Color Intensity Variables
        6. Updating Our Colors
        7. Memory Overflow and Data Values
        8. Making a Proper Mood Light
        9. Making Decisions in Your Program
          1. Creating Conditional Statements
          2. Adding an Else Part
          3. Testing Values
        10. The Completed Mood Light
          1. A Proper Funky Mood Light
        11. Finding Program Bugs
      5. Conclusion
      6. Chapter Review Questions
    3. 3. Getting Player Input
      1. Introduction
      2. Reading a Gamepad
        1. Gamepads and Classes
        2. Finding a Gamepad
        3. Testing the Gamepad Status
        4. Zune Buttons
      3. Using the Keyboard
        1. Stopping the Game with the Escape Key
        2. Using a Gamepad and a Keyboard at the Same Time
      4. Adding Vibration
        1. Controlling the Vibration of a Gamepad
        2. Testing Intensity Values
      5. Program Bugs
      6. Conclusion
      7. Chapter Review Questions
  6. II. Images, Sound, and Text
    1. 4. Displaying Images
      1. Introduction
      2. Resources and Content
        1. Getting Some Pictures
        2. Content Management Using XNA
        3. Working with Content Using XNA Game Studio
        4. XNA Game Studio Solutions and Projects
        5. Adding Resources to a Project
          1. Adding Links to Resources
          2. The XNA Content Pipeline
      3. Using Resources in a Game
        1. Loading XNA Textures
        2. Positioning Your Game Sprite on the Screen
        3. Sprite Drawing with SpriteBatch
        4. Filling the Screen
          1. Intellisense
      4. Conclusion
      5. Chapter Review Questions
    2. 5. Writing Text
      1. Introduction
      2. Text and Computers
        1. Text as a Resource
        2. Creating the XNA Clock Project
        3. Adding a Font Resource
          1. The XML File Format
        4. Loading a Font
        5. Drawing with a Font
        6. Changing the Font Properties
      3. Getting the Date and Time
      4. Making a Prettier Clock with 3-D Text
        1. Drawing Multiple Text Strings
        2. Repeating Statements with a for Loop
        3. Other Loop Constructions
        4. Fun with for Loops
      5. Creating Fake 3-D
        1. Creating Shadows Using Transparent Colors
        2. Drawing Images with Transparency
      6. Conclusion
      7. Chapter Review Questions
    3. 6. Creating a Multi-Player Game
      1. Introduction
        1. Creating the Button-Bash Game
          1. Button-Bash Game Data
          2. Starting the Button-Bash Game
          3. Displaying the Button-Bash Count Value
          4. Counting Button Presses
          5. Detecting Changes in the Button Position
        2. Level and Edge Detectors
        3. Constructing the Complete Game
          1. Code Design
        4. Adding Test Code
      2. Conclusion
      3. Chapter Review Questions
    4. 7. Playing Sounds
      1. Adding Sound
        1. Creating the Drum Pad Project
        2. Capturing Sounds with Audacity
          1. Sounds and File Types
        3. Storing Sounds in Your Project
        4. Using Sounds in an XNA Program
          1. Playing Sounds
        5. Playing Background Music
          1. Controlling Sound Playback
          2. Controlling the Sound Playback
          3. Starting the Sound Playback
          4. Pausing the Sound Playback
          5. Making a Sound Repeat
          6. Playing Songs Using the MediaPlayer Class
      2. Conclusion
      3. Chapter Review Questions
    5. 8. Creating a Timer
      1. Making Another Game
        1. Reaction Timer Bug
      2. Finding Winners Using Arrays
        1. Creating an Array
        2. Using Data in an Array
        3. Scanning an Array
        4. Using an Array as a Lookup Table
        5. Displaying the Winner
      3. Conclusion
      4. Chapter Review Questions
    6. 9. Reading Text Input
      1. Using the Keyboard in XNA
        1. Creating the Message Board Project
        2. Registering Key Presses
          1. Detecting When Keys Are Pressed
        3. The Keys Type
        4. Enumerated Types
      2. Working with Arrays, Objects, and References
        1. Values and References
        2. Arrays as Offices
        3. Say Hello to the Garbage Collector
        4. Using References and Values
        5. Why Do We Have References and Values?
        6. References and GetPressedKeys
      3. Displaying Keys
        1. Detecting Key Presses
          1. Using break to Improve Performance
        2. Decoding Key Characters
        3. Using the Shift Keys
        4. Editing the Text
      4. Conclusion
      5. Chapter Review Questions
  7. III. Writing Proper Games
    1. 10. Using C# Methods to Solve Problems
      1. Introduction
      2. Playing with Images
        1. Zooming In on an Image
      3. Creating a Zoom-Out
        1. Updating the Drawing Rectangle
        2. Creating a Method to Calculate Percentages
          1. Putting a Method into Your Game Class
          2. Calling a Method
        3. Returning Nothing Using void
          1. Creating a getPercentage Method
          2. Testing a Method
          3. Designing Tests for getPercentage
          4. Creating the getPercentage Method Body
        4. Debugging C# Programs
          1. Debugging with Breakpoints
        5. Hitting a Breakpoint
          1. Viewing Variables
        6. Using Floating-Point Numbers in C#
        7. The Compiler and C# Types
        8. Compilers and Casting
        9. Expression Types
        10. Stopping the Zoom
        11. Zooming from the Center
      4. Conclusion
      5. Chapter Review Questions
    2. 11. A Game as a C# Program
      1. Introduction
      2. Creating Game Graphics
      3. Projects, Resources, and Classes
        1. XNA Game Studio Solutions and Projects
          1. Projects and Folders
          2. The Game Program Files
        2. The Program.cs File
          1. Namespaces and Programs
          2. Static Classes and Methods
          3. Making Methods Static
          4. Main Method Parameters
          5. The C# Using Statement
          6. The Main Method in an XNA Game
        3. Renaming the Game1 Class
      4. Creating Game Objects
        1. Sprites in Games
        2. Managing the Size of Game Sprites
          1. Working with Floating-Point Values and Integers
          2. Double Precision Floating-Point Values
          3. Drawing and Aspect Ratios
          4. Sprite Sizing
        3. Moving Sprites
        4. Bouncing the Cheese
        5. Dealing with Display Overscan
      5. Conclusion
      6. Chapter Review Questions
    3. 12. Games, Objects, and State
      1. Introduction
      2. Adding Bread to Your Game
        1. Using a Structure to Hold Sprite Information
        2. Using the Gamepad Thumbsticks to Control Movement
        3. Improving Programs Using Methods
          1. Value and Reference Parameters
        4. Handling Collisions
        5. Making the Cheese Bounce off the Bat
        6. Strange Bounce Behavior
        7. Strange Edge Behavior
          1. Debugging a Running Program
      3. Adding Tomato Targets
        1. Zune Image Sizes
        2. Tomato Collisions
          1. Controlling Sprite Visibility
          2. Setting the Initial Visibility State
          3. Using the Visible Field When Drawing
      4. Conclusion
      5. Chapter Review Questions
    4. 13. Making a Complete Game
      1. Introduction
      2. Making a Finished Game
        1. Adding Scores to a Game
          1. Drawing Text in the Game
        2. Adding Survival
        3. Adding Progression
      3. Improving Code Design
        1. Refactoring by Creating Methods from Code
        2. Refactoring by Changing Identifiers
        3. Creating Code Regions
        4. Creating Useful Comments
      4. Adding a Background
      5. Adding a Title Screen
        1. Games and State
        2. Using the State Values
        3. Building a State Machine
      6. Conclusion
      7. Chapter Review Questions
    5. 14. Classes, Objects, and Games
      1. Introduction
      2. Design with Objects
        1. An Object Refresher Course
        2. Cohesion and Objects
          1. Creating a Cohesive BatSpriteStruct
          2. Protecting Data Inside Objects
          3. BatSpriteStruct Behaviors
          4. Objects and Encapsulation
        3. Coupling Between Objects
          1. Creating a Link Between the Bread Bat and the Cheese Ball
        4. Designing Object Interactions
          1. Objects and Messages
          2. Messages and Organization
        5. Container Objects
        6. Background and Title Screen Objects
      3. Classes and Structures
        1. Creating and Using a Structure
        2. Creating and Using an Instance of a Class
      4. References
        1. Multiple References to an Instance
        2. No References to an Instance
        3. Why Bother with References?
      5. Value and Reference Types
        1. Should Our Game Objects Be Classes or Structures?
          1. Game Objects Should Be Structures Managed by Value
          2. Game Objects Should Be Classes Managed by Reference
      6. Creating a Sprite Class Hierarchy
        1. The BaseSprite Class
          1. Using the BaseSprite to Store the Background
        2. Extending the BaseSprite to Produce a TitleSprite
          1. Overriding Methods from a Parent Class
        3. Building a Class Hierarchy
          1. Using Protected Members from a Parent Class
      7. Adding a Deadly Pepper
        1. Creating a DeadlySprite Class
          1. Drawing the Deadly Pepper Sprite
          2. Setting Up the Deadly Pepper Sprite
          3. Updating the Deadly Pepper Sprite
      8. Conclusion
      9. Chapter Review Questions
    6. 15. Creating Game Components
      1. Introduction
      2. Objects and Abstraction
        1. Creating an Abstract Class in C#
        2. Extending an Abstract Class
        3. Designing with Abstract Classes
        4. References to Abstract Parent Classes
      3. Constructing Class Instances
        1. Constructors in Structures
        2. Constructors in Class Hierarchies
          1. Constructors in Child Classes
      4. Adding 100 Killer Tangerines
        1. Creating a KillerSprite Class
        2. Positioning the KillerSprites Using Random Numbers
          1. Pseudorandom Numbers
          2. The .NET Random Class
          3. Creating a Static Random Generator for the KillerSprite
          4. Getting a Random Sequence of Numbers
        3. Using Lists of References
          1. Creating a List Collection
          2. Adding Items to a List
          3. Accessing List Elements
          4. Working Through List Elements Using foreach
          5. Setting Up the Game Sprites
          6. Extra List Features
      5. Adding Artificial Intelligence
        1. Chasing the Bread Bat
          1. Using Properties to Read the Bread Bat Position
          2. Creating the KillerSprite-Chasing AI
          3. Hitting the Killer Sprite Tangerines
      6. Adding Game Sounds
      7. From Objects to Components
        1. C# Interfaces
        2. Creating an Interface
        3. Implementing an Interface
        4. References to Interfaces
        5. Linking Bread, Cheese, and Tomatoes
        6. Designing with Interfaces
      8. Conclusion
      9. Chapter Review Questions
    7. 16. Creating Multi-Player Networked Games
      1. Introduction
      2. Networks and Computers
        1. Starting with the Signal
        2. Building Up to Packets
        3. Addressing Messages
        4. Routing
        5. Calls and Datagrams
        6. Networks and Protocols
      3. Xbox Live
        1. Gamertags and Xbox Live
        2. System Link and XNA
      4. Bread and Cheese Pong
        1. Managing Gamer Profiles in XNA
          1. Creating a Profile
          2. Automatic Sign-In
        2. Ensuring a Gamer Is Signed In for Network Play
        3. Zunes and Gamer Profiles
        4. Creating a Game Lobby
        5. Network Games and State
          1. Starting at the Title Screen
          2. Signing In Players
          3. Selecting the Role of Host
          4. Displaying the Contents of the Lobby
          5. Selecting the Role of Player
          6. Waiting for a Host
        6. Playing the Game
          1. Game Topology
          2. Creating the Server Behavior
          3. Creating the Client Behavior
        7. The Completed Game
        8. Zune Network Games
      5. Conclusion
      6. Making Games for Fun
      7. verysillygames.com
      8. Chapter Review Questions
  8. A. Answers to the Chapter Review Questions
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
  9. Glossary
  10. B. About the Author
    1. Rob Miles
  11. Index
  12. About the Author
  13. Copyright