MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 2) - Positioning Your Game Sprite on the Screen

4/18/2011 11:46:00 AM

2. Positioning Your Game Sprite on the Screen

In computer gaming terms, you can think of the image of Jake as a sprite. A sprite is a flat, preloaded image that is used as part of a computer game. Sprites can be large, such as background sky, or smaller, such as spaceships and missiles in a space shooter game. From the point of view of XNA, a sprite is an image resource along with location information that tells XNA where to draw the image. This means that you need a way to tell XNA where on the screen you want to put your sprite. You do this by using yet another XNA type, the Rectangle. This holds information about the position and size of a rectangle. You don’t need to worry about how a rectangle works at the moment; you need only know how to create one and set the size and position of it. Figure 4 shows how you use a rectangle to express where on the screen you want Jake to be drawn.

Figure 4. Placing a drawing rectangle on the screen


The position of the rectangle is given by the coordinates of its top left corner. You can regard the screen as a piece of graph paper. You express a position on the screen by giving an x coordinate value (the distance across the screen from the left) and a y coordinate value (the distance down the screen from the top). This means that the position with the coordinate of (0, 0) is the top left corner. Note that this is not quite the same as graphs that you may have drawn in the past. In a conventional graph, the y value increases as you go up the page. In computer graphics, the y value increases as you move down the page.

In Figure 4-14, you can see that the top left corner of the Jake sprite is at position (30, 20). This means 30 steps across and 20 down. The units are called pixels. Pixel, an abbreviation for "picture element," refers to the smallest dot that can be drawn on the screen. The Xbox can drive displays with a range of different sizes, so the pixel at position (30, 20) may be a different physical distance across the screen, depending on the type of screen being used. Later, you’ll find out how to write games that automatically scale themselves to fit any screen.

A rectangle is also used to give the width and height of the sprite. In Figure 4-14, I am drawing the texture in an area that is 600 pixels wide and 450 pixels high. The good thing about this is that I don’t have worry about the original size of the image; XNA simply scales the image to fit in a rectangle that size. Later, you’ll have some fun modifying the size. The rectangle where Jake is drawn is another item in the game world for the program, as shown here:

// The Game World

Texture2D jakeTexture;
Rectangle jakeRect;

The actual Rectangle you are going to use is created using new:

jakeRect = new Rectangle(30, 20, 600, 450);

This code sets a Rectangle variable to one with the position and dimensions that you need. When the rectangle is created, it is passed the x, y, width, and height values so that these can be held within the rectangle structure. This means that if you ever want to move the image or change its size on the screen, you need to change only one of the values that is held in the rectangle. These values are members of the Rectangle structure. In C#, members that hold values are called fields.

You can think of a field as a variable that has been declared inside a structure or class. In the case of your Game1 class, the game world data that you created (for example, the color intensity values for your mood light) are fields of that class. Later, you’ll see how to get hold of individual fields inside the Rectangle so that you can change its size and position.

The Rectangle needs to be created when the game program starts. You could do this in the LoadContent method, but XNA provides another place where it is more sensible, namely, the Initialize method. This is called when the game starts up. If all these methods are confusing, think about what happens when you organize a party. This takes a number of steps:

  1. Set up the tables and chairs.

  2. Fetch the food and drink.

  3. Repeatedly play music and dance.

  4. Tidy up afterward.

When an XNA game runs, it goes through the same process:

  1. Set things up: Initialize

  2. Load game content: LoadContent

  3. Repeatedly update the game and draw the display: Draw and Update

  4. Free up all the content: UnloadContent

When the game ends, the XNA system calls the UnloadContent method. You can add statements to that method to release resources explicitly that your game has used, but for now, you can leave this out.

In fact, you need not provide code for all these methods; they are there only so that you can take control at various points of the game’s life cycle. The code that you put in the Initialize method needs to create a Rectangle that describes the destination of the draw operations:

protected override void Initialize()
{
jakeRect = new Rectangle(30, 20, 600, 450);
base.Initialize();
}

Other  
  •  iPhone 3D Programming : Holodeck Sample (part 5) - Overlaying with a Live Camera Image
  •  iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
  •  iPhone 3D Programming : Holodeck Sample (part 3) - Handling the Heads-Up Display
  •  iPhone 3D Programming : Holodeck Sample (part 2) - Rendering the Dome, Clouds, and Text
  •  iPhone 3D Programming : Holodeck Sample (part 1) - Application Skeleton
  •  Building LOB Applications : Printing in a Silverlight LOB Application
  •  Building LOB Applications : Data Validation through Data Annotation
  •  Building LOB Applications : Implementing CRUD Operations in RIA Services
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 2) - Adding Resources to a Project
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)
  •  iPhone 3D Programming : Blending and Augmented Reality - Rendering Anti-Aliased Lines with Textures
  •  Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
  •  Programming with DirectX : Game Math - Bounding Geometry (part 1) - Bounding Boxes
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  
    Most View
    Guide To Upgrades With The Greatest Effects (Part 1)
    Cutting Edge Technology (Part 2)
    Tried And Tested – November 2012 (Part 1)
    The KDE User Guide (Part 2)
    Hard Disk Help: From Unusual Noises To Random Crashes
    Asus GeForce GTX 690 - SLI speeds on a single card
    NZXT Kraken X60 - The Best Liquid Cooling System (Part 1)
    Gigabyte GA-F2A85X-UP4 Mainboard & AMD A10-5800K Processor Review (Part 7)
    The Cure Of Slightly Rubbish Technology (Part 2)
    Which Is The Real Best-Seller Ultrabook? (Part 1) - Asus Zenbook Prime, Acer Aspire S5, HP Folio 13-2000
    Top 10
    Does Microsoft Have An Image Problem? (Part 2)
    Does Microsoft Have An Image Problem? (Part 1)
    Time For A Bigger iPhone?
    99 Mac Secrets (Part 5) - Top ten third-party apps
    99 Mac Secrets (Part 4) - iMovie secrets, GarageBand secrets, iWork secrets
    99 Mac Secrets (Part 3) : Safari secrets, Mail secrets, Safari shortcuts, Mail shortcuts, iPhoto secrets
    99 Mac Secrets (Part 2) : Customizing, Best menu bar add-ons, Quick Look secrets
    99 Mac Secrets (Part 1) : General OS X tips, Security tips, System shortcuts
    iMovie Trailers And Audio Premastered
    PowerTraveller Powerchimp 4A Battery Charger