Microsoft® XNA® Game Studio 4.0: Learn Programming Now!

Book description

Now you can build your own games for your Xbox 360, Windows Phone 7, or Windows-based PC—as you learn the underlying concepts for computer programming. Use this hands-on guide to dive straight into your first project—adding new tools and tricks to your arsenal as you go. No experience required!

  • Learn XNA and C# fundamentals—and increase the challenge with each chapter

  • Write code to create and control game behavior

  • Build your game’s display—from graphics and text to lighting and 3-D effects

  • Capture and cue sounds

  • Process input from keyboards and gamepads

  • Create features for one or multiple players

  • Tweak existing games—and invent totally new ones

  • Table of contents

    1. Dedication
    2. Acknowledgments
    3. Introduction
      1. Who This Book Is For
      2. System Requirements
      3. Code Samples
      4. Errata and Book Support
      5. We Want to Hear from You
      6. Stay in Touch
    4. 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. App Hub
            2. XNA Game Studio Connect
            3. XNA Game Studio Device Center
          4. Setting up a Windows Phone to run XNA games
            1. Selling Your Windows Phone Games in the Marketplace
            2. Connecting a Windows Phone 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 Windows Phone
          5. Running the Same XNA Game on Different Devices
            1. Creating a Copy of an XNA Project for Another Device
            2. Choosing Between Windows Phone and Emulator
        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
        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
    5. 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 Content 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
          6. Creating a RayGun
            1. The SoundEffectInstance Class
            2. Controlling a Sound Effect Instance
            3. Changing Instance Properties
            4. Sound Bugs from Your Younger Brother
            5. Checking for Null References
            6. The XACT Audio Tool
            7. 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
    6. 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. Windows Phone 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. Creating a Game Lobby
          4. 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
          5. Playing the Game
            1. Game Topology
            2. Creating the Server Behavior
            3. Creating the Client Behavior
          6. The Completed Game
        5. Conclusion
        6. Chapter Review Questions
    7. IV. Making Mobile Games for Windows Phone 7 with XNA
      1. 17. Motion-Sensitive Games
        1. Introduction
        2. The Accelerometer
          1. What Does the Accelerometer Actually Do?
        3. Acceleration and Physics
          1. Making Sense of Accelerometer Readings
        4. Creating a “Cheese Lander” Tipping Game
          1. Game World Objects in “Cheese Lander”
          2. Getting Access to the Accelerometer Class from XNA
          3. Using the Accelerometer in an XNA Game
          4. Starting the Accelerometer
          5. Using Accelerometer Values in a Game
            1. Improving Game Play by Adding Physics
          6. Using Vectors to Express Movement
          7. Adding Friction
            1. Controlling Sounds with Vectors
          8. Detecting Shaking
          9. A Quick Digression About Threads and Synchronization
        5. Conclusion
        6. Chapter Review Questions
      2. 18. Exploring Touch Input
        1. Introduction
        2. The Windows Phone Touch Screen
          1. Getting Touch Input
        3. Creating a Panic Button
          1. Reading Touch Events
          2. Touch Location Types
          3. Using the Location of a Touch
        4. Creating a Touch Drumpad
          1. Creating a soundPad Class for Each Drum Sound
          2. Storing soundPad Values in the Game
          3. Drawing the Soundpads
          4. Updating the Soundpads
          5. Making the Soundpads Flash
        5. Creating a Shuffleboard Game
          1. The PuckSprite Class
            1. Updating a Stationary Puck
            2. Dragging the Puck Across the Panel
            3. Puck Movement Across the Board
            4. Emulators and Real Devices
        6. Conclusion
        7. Chapter Review Questions
      3. 19. Mobile Game Development
        1. Introduction
        2. The Windows Phone
          1. The Windows Phone Marketplace
        3. Maximizing the Phone Battery Life in XNA Games
          1. Setting the Update Rate of a Game
        4. Dealing with Changes in Phone Orientation
          1. Selecting Orientations in an XNA Game
          2. Getting Messages When the Orientation Changes
        5. Using a Specific Display Size for Windows Phone Games
        6. Hiding the Windows Phone Status Bar
        7. Stopping the Screen Timeout from Turning Off Your Game
        8. Creating a Phone State Machine
          1. Games and States
            1. Creating a Simple Game State Machine
            2. Creating More Complex State Machines
        9. Handing Incoming Phone Calls
          1. Detecting Phone Calls
        10. A Game as a Windows Phone Application
          1. The Windows Phone Back and Start Buttons
          2. Starting New Programs with the Start Button
          3. Using Isolated Storage to Store Game State
            1. Storing Files in Isolated Storage
            2. Connecting Streams Together
            3. Saving the State of a Game
            4. Loading a Saved Game
            5. Exceptions in Number Reading
            6. Erasing Saved Files
            7. Creating a Well-Behaved Game
        11. Getting Your Games into the Marketplace
          1. The Windows Phone Marketplace
          2. Registering for the App Hub
          3. Using a Windows Phone Device
          4. Creating Games for Sale
        12. Conclusion
        13. 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
      17. Chapter 17
      18. Chapter 18
      19. Chapter 19
    9. B. About the Author
      1. Rob Miles
    10. Index
    11. About the Author
    12. Copyright

    Product information

    • Title: Microsoft® XNA® Game Studio 4.0: Learn Programming Now!
    • Author(s): Rob Miles
    • Release date: January 2011
    • Publisher(s): Microsoft Press
    • ISBN: 9780735659841