XNA Game Studio 3.0 : Playing with Images

9/19/2012 12:48:29 AM
Up until now, the image drawing that you performed simply displays a texture on the screen in the same place each time the Draw method is called. It would be really nice to be able to move the picture around the screen and maybe even zoom in on it. You might even find that these abilities give you an idea for a game.

1. Zooming In on an Image

When you wrote your image display program, you created a variable called jakeRect of type Rectangle. This rectangle was the destination of the draw action. The size of the rectangle was set to the full screen in the Initialize method, as follows:

protected override void Initialize()
    gameSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
    jakeRect = new Rectangle(
        0,    // X position of top left hand corner
        0,    // Y position of top left hand corner
        GraphicsDevice.Viewport.Width,   // rectangle width
        GraphicsDevice.Viewport.Height); // rectangle height


When the Draw method ran, it drew the image texture in the jakeRect rectangle:

protected override void Draw(GameTime gameTime)

    gameSpriteBatch.Draw(jakeTexture, jakeRect, Color.White);


Now you change the way that the picture is drawn by changing the values in jakeRect as the program runs. XNA can resize the picture for you so that you can move and scale your picture very easily. You start by adding the following Update method to the display program:

protected override void Update(GameTime gameTime)
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)




Each time the Update method is called, the width and height fields of the rectangle are increased by one. These fields are the data members inside the rectangle that represent the rectangle dimensions. You get a hold of a field in an object by giving the identifier of the variable a period character (.) and then the name of the field you wish to use. Remember that this is the rectangle that describes where you want Jake to be drawn, so changing the size of this rectangle changes the size of the image on the screen.

Microsoft XNA does not care about the fact that you’re "drawing off the screen" and simply shows you the part of the picture that fits on the screen. Figure 1 shows what the screen looks like after a program using this Update method has been running for a few seconds.

If you leave the program running a very long time, it only zooms in on a particular blade of grass, but it does show how you can change the way that images are placed on the screen.

Figure 1. Stretching Jake

Game Idea: Super Zoom Out

You can use this zooming ability to create a game. Rather than starting with a picture and then zooming in on it, you could start with a zoomed image and slowly pull back (zoom out) to reveal more and more of the picture. The first person to correctly identify the picture wins the game. This game is quite fun, particularly if the images are ones that are familiar to the players.

  •  XNA Game Studio 3.0 : Adding Tomato Targets
  •  iPhone Developer : Search Tables and Core Data
  •  iPhone Developer : Using Core Data for a Table Data Source
  •  Anita Mai Tan - The Container Is Worth More Than The Contents
  •  No New Toaster – Refrigerators
  •  The State Of Mobile... Creative Media In 2012 (Part 2)
  •  The State Of Mobile... Creative Media In 2012 (Part 1)
  •  A Not So New Competitor
  •  BlackBerry Java Application : installing the Java Development Kit (JDK), downloading Eclipse with the JDE plugin
  •  BlackBerry Java Application : Installing the Development Environment - downloading the Java Development Kit
  •  iOS 6 Playing It Safe (Part 1)
  •  iOS 6 Playing It Safe (Part 2)
  •  Planet Of The Apps – Travel (Part 1)
  •  Planet Of The Apps – Travel (Part 2)
  •  Planet Of The Apps – Travel (Part 3)
  •  Top 10 Smartphones August – September (Part 1) - Samsung Galaxy S III, HTC One X, Apple iPhone 4S,Nokia Lumia 800,Sony Xperia S
  •  Top 10 Smartphones August – September (Part 2) - Huawei Ascend P1, HTC Radar, Samsung Galaxy Note,BlackBerry Bold 9900
  •  Got Yourself A Fancy New Nexus Flexus
  •  Nokia 808 Pureview - Best For Convergists
  •  Tough Phones : Work or play in harsh environments
    Most View
    The Year That Went By
    Windows Server 2003 : Securing Network Communication - Securing a Wireless Network
    Kensington ClickSafe Keyed Laptop Lock
    Create The Ultimate Tracking Device (Part 1)
    How To Set Up The Ultimate Steam Box (Part 1)
    Windows 7 : Troubleshooting and Repairing Problems - System Image Recovery, Windows Memory Diagnostic
    The Outer RIM (Part 2)
    The 3DS XL : Super-Sized!
    Nikon Coolpix L820 Super-Zoom Camera Review (Part 1)
    Acer Aspire V5-551G Review – Laptop Using AMD Trinity 4 Quad-Core Chip
    Top 20
    Asus Fonepad – A Very Big Smartphone
    Asus Padfone 2 - More Than Just A Gimmick
    Best 3G Smartphone And Dongles (Part 2)
    Best 3G Smartphone And Dongles (Part 1)
    BlackBerry Z10 - Bites Back (Part 2)
    BlackBerry Z10 - Bites Back (Part 1)
    Adobe Photoshop Touch - Get Photoshop On Your Phone
    Arcam FMJ A19 Integrated Amplifier - Jumping Jack (Part 2)
    Arcam FMJ A19 Integrated Amplifier - Jumping Jack (Part 1)
    Beautiful Systems Mcintosh/ Art - Big Mac
    Fujifilm XF1 - A Slim, Retro-Styled Camera With Excellent Image Quality
    What’s Wrong With Windows 8?
    Laptops Test - The Original Act Flights Back (Part 4)
    Laptops Test - The Original Act Flights Back (Part 3)
    Laptops Test - The Original Act Flights Back (Part 2)
    Laptops Test - The Original Act Flights Back (Part 1)
    Toshiba 310 Satellite U940-11N - An Ultra-Portable Model With A Shoddy Build
    11 Essential Outdoor Apps (Part 2)
    11 Essential Outdoor Apps (Part 1)
    App Store Essentials – July 2013