Adding Some Game Logic

Now that you have a class that will create a moving enemy, you need to add some game logic. As with nearly any game, there will be an element of randomness in this game, so you'll need to add a random number generator. Remember that you always want to have only one random number generator that you use throughout your entire game. If you have multiple random number generator variables, there's a possibility that some of the variables might end up with the same random seeds, in which...

Adding a Splash Screen Game Component

Before we jump in and code your splash screen component, let's step back and look at how this is going to work. Your Gamel class is going to manage different game states. Several states are possible starting the game, playing the game, pausing the game between levels, and showing a game-over screen at the end of the game. To help you manage these states, create an enum in your Game1 class that you will use to track changes from state to state during gameplay. Add the following lines of code at...

Supporting Gamepad Input

The mouse is currently the only way to move the camera and without support for the Xbox 360 Gamepad the camera will be immobile - which really won't increase the entertainment value of your game. To fix the Camera class, add support for the game-pad instead of the mouse anywhere that you see mouse support code being used. You can choose to surround the GamePad code with a preprocessor directive to have it compile only in the Xbox 360 project, but you might as well leave it in there for the...

Creating a Chasing Sprite

As mentioned previously, when it comes to computer-controlled objects, the goal of any game is to make those objects appear intelligent to the point where a user may not be able to tell the difference between an object controlled by a human and an object controlled by a computer. We clearly aren't even close to that. The automated sprites you've added do nothing more than move forward in a straight line. While you've done some great work on your SpriteManager, we haven't discussed how to do...

Safari Books Online

When you see a Safari Books Online icon on the cover of your lf JPI favorite technology book, that means the book is available online B ks amine through the O'Reilly Network Safari Bookshelf. Safari offers a solution that's better than e-books. It's a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http safari.oreilly.com.

Components of a Moving 3D Camera

In this chapter, you'll be starting with the code you finished in Chapter 10. Open that project for use throughout the rest of this chapter. In Chapters 9 and 10 we discussed setting up a 3D camera and the basic components that make up a camera in XNA 3D. You created a camera GameComponent, which you added to your solutions in previous chapters to enable you to see the triangles and spaceship models you've drawn. As a quick review, the camera is made up of two different matrices the projection...

Adding Biohazard Bombs of Insanity

Let's modify this game to make things a little more interesting. Rather than just having one player chase another player around the screen, we'll let the chased player drop biohazard bombs every 5 seconds, which will cut the movement speed of the chasing sprite by 50 for 5 seconds. That should spice things up a bit. First, you'll need to add a few more resources. Add the hazardhit.wav and hazardplant. wav files to your Content Audio folder in Visual Studio (the files are located with the source...

Creating a Custom Vertex

To create our particle engine, you'll be working with the 3D game code that you have been building so far in the 3D section of this book. Open the solution you finished with at the end of Chapter 12 and use it for the exercises throughout this chapter. Prior to DirectX 8, particle effects were typically created by drawing a rectangle using two triangles and then applying a texture to that rectangle. With DirectX 8, an important performance advancement was made through the introduction of...

Adding an Xbox 360 Device

To deploy a project to an Xbox 360, you need to let your PC know about the Xbox 360 machine. First, hook up your Xbox 360 to your PC's network with a LAN cable. (The Xbox 360 also has a wireless network adapter that you can purchase to connect your Xbox 360 to a wireless network.) When you add your Xbox 360 as a device, your PC will require a connection key, which is generated by your Xbox 360. To get a connection key, you'll need to have at least a Silver Xbox LIVE membership (which is free),...

Rotations in a First Person Camera

Yaw, pitch, and roll apply not only to objects, but to cameras as well In the classes in which I've taught XNA, one of the things that has traditionally been difficult for some students to understand is the fact that yaw, pitch, and roll rotations when applied to objects or cameras that move and rotate in 3D don't necessarily correspond to rotations around the X-, Y-, and Z-axes. For example, picture the camera you currently have in your game. The camera sits on the Z-axis and...

Drawing 2D Text

This chapter builds on the code that you finished writing in Chapter 6. Open that game project and use it throughout this chapter. First, you'll need to add an integer variable representing a sprite's score value to the Sprite base class (note the addition of the public get accessor as well, via auto-implemented properties) public int scoreValue get protected set Modify both constructors in the Sprite class to receive an integer value for the score value for that sprite. The first constructor...

Adding a PowerUp

You've done well here you've built your first 3D game, complete with scoring and increasingly difficult levels, and packed with a ton of fun Before we end this chapter, though, let's do one more thing. Not that games are boring, but anything that breaks up the monotony of regular gameplay goes a long way toward making a game even more exciting and addicting. In this section, you'll add a power-up feature that will be awarded when a player gets three consecutive kills. The power-up will let the...

Creating an Evading Sprite

You now have two types of automated sprites in your application one that moves across the screen without changing direction, and one that moves across the screen but changes direction slightly to chase the player. In this section, you'll build one more type of sprite that is similar to the chasing sprite, but this one will actually try to avoid the player. Why would you want to write a sprite that avoids the player This sprite type will be used for something that the player will want to run...

Adding Some Variety to Your Sprites

First things first, you need some more images. If you haven't already done so, download the source code for this chapter of the book. Within this chapter's source code (in the AnimatedSprites Content Images folder), you'll find some sprite sheet files for the different types of sprites. Right-click the Content Images folder in Solution Explorer and select Add Existing Item____ Navigate to the AnimatedSprites Content Images folder of the source code for this chapter and add the bolt.png,...

Dissecting a Sample HLSL Effect File

In XNA, effects are created in effect files, which have an .fx extension. They are loaded via the content pipeline and are compiled just like other content resources. Therefore, the compiler will detect compilation errors in your HLSL code just like it will in your C code, even though your effect files are stored with other content. While your HLSL code will be compiled via the content pipeline, unfortunately, you won't have access to IntelliSense when working with HLSL effect files. Now that...

Applying HLSL Using Textures

Coloring a rectangle red requires only the simplest HLSL shader, and it's something you'll rarely find in the latest video games. Typically, you'll be applying a texture to an object and then tweaking the way the texture appears by applying shininess or fog or some other effect. In this section, you'll apply a custom HLSL file to your rectangle while applying color from the trees texture as well. Open your Red.fx file again and replace the code in the file with the code shown here float4X4...

Randomly Spawning Sprites

You want to make your sprites spawn at somewhat random intervals, and you want them to spawn from the top, left, right, and bottom sides of the screen. For now, you'll just have them traveling in a straight direction across the screen, but they'll do so at varying speeds. You need to let your SpriteManager class know when to spawn the next enemy sprite. Create a class-level variable in your SpriteManager class to store a value indicating the next spawn time...

Table of Contents

Preface Creating Your First XNA Application 4 Game Development Versus Polling 11 Adding a Sprite to Your Project 15 Loading and Drawing Your Sprite 18 Transparency and Other Options 21 Adjusting the Animation Speed 36 Test Your Knowledge Exercise 39 3. User Input and Collision Detection Keeping the Sprite in the Game Window 50 Test Your Knowledge Exercise 57 4. Applying Some Object-Oriented Creating a User-Controlled Sprite Class 63 Creating an Automated Sprite 65 Test Your Knowledge Exercise...