getting started. xna game studio 4.0 to download xna game studio 4.0 itself, go to xna game

13
Getting Started

Upload: anne-cummings

Post on 29-Dec-2015

244 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Getting Started

Page 2: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

XNA Game Studio 4.0

To download XNA Game Studio 4.0 itself, go to

http://www.microsoft.com/download/en/details.aspx?id=23714

XNA Game Studio 4.0 needs the Microsoft Visual Studio 2010 development environment, XNA Game Studio 4.0 works with any of the following Microsoft Visual Studio 2010 products.

Microsoft Visual Studio 2010 Express for Windows Phone

Microsoft Visual C# 2010 Express Edition

Microsoft Visual Studio 2010 Standard Edition

Microsoft Visual Studio 2010 Professional Edition

XNA Game Studio requires the Microsoft .NET Framework 4.0. The setup program for Microsoft Windows Phone Developer Tools installs the .NET Framework if it is not installed already on your computer. You also can install the .NET Framework from the Microsoft .NET Framework Developer Center.

Page 3: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

High Dynamic Range (HDR) Rendering vs Reach

If you see this message when attempting to build an XNA game it probably means that your graphics card does not support High Dynamic Range (HDR) rendering.

You will need to change the properties/settings of your project by right-clicking the game project name and changing the Game Profile...

Page 4: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Additional Required Software Installed by Setup

The XNA Game Studio setup program also installs the following required software:

XNA Game Studio setup installs the DirectX runtime, which is required to use the XNA Framework Game API.

XNA Game Studio Setup installs multiple versions of the Microsoft Cross-platform Audio Creation Tool (XACT).

XNA Game Studio Setup installs Microsoft Games for Windows – LIVE Redistributable, version 2.0.687.0. This software provides support for various gamer services such as retrieving player preferences for local accounts, as well as programmatically displaying various LIVE Guide user interface screens.

Page 5: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 1: Select New Project, choose Visual C# from the Installed Templates list on the left and specify Windows Game (4.0). Give your project an appropriate name, such as FaceBall and then click OK.

Creating your First XNA Program

Page 6: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 2: Test Run your program. You should see a new Windows Form appear with a light blue background (CornFlowerBlue). You should note that this window is not resizable.

Before moving on, let's take a look at the automatically generated game code...

Page 7: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

namespace FaceBall{ public class faceBall : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch;  public faceBall() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }  protected override void Initialize() {

base.Initialize(); }  protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); }   protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); }  protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); base.Draw(gameTime); } }} 

your game class inherits from the Microsoft.Xna.Framework.Game class

Creates a new GraphicsDeviceManager and registers it to handle the configuration and management of the graphics device for the specified Game.

Enables a group of sprites to be drawn using the same settings.

Add your initialization logic here

This method is called once at start of game and is where you should load your content.

This is where you will update the state of your game . Update( ) is called once for each pass through the game loop.

This is where you generate the display for the current frame. Draw( ) is called once for each pass through the game loop.

Page 8: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 3: Now you will associate "content" (such as sprite bitmaps) with your game project.

Image, sound and other resources can be found in Resources (see Home Page). Download face.bmp and background.bmp and put them somewhere easy to find them such as on your desktop.

Right-click FaceBallContent in Solution Explorer, choose Add Existing Item and then navigate to one of the bitmaps being added to content. Repeat for the other bitmap.

Page 9: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 4: FaceBall is a simple demo that bounces a smiley face around in the graphics display region of the game. The following code is inserted at the top of the class.

Step 5: Add code to the Initialize( ) method to set the min and max limits of graphics viewport.

Texture2D faceTexture;Texture2D backgroundTexture;Vector2 facePosition;public int maxX, minX, maxY, minY;public int faceVx = 5;public int faceVy = 2;

Viewport view = graphics.GraphicsDevice.Viewport;

base.Initialize();

maxX = view.Width;minX = 0;maxY = view.Height;minY = 0;facePosition.X = view.Width / 2;facePosition.Y = view.Height / 2;

Page 10: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 6: Add code to the LoadContent( ) method to load the face and background bitmaps.

Step 7: Add code to the Update( ) method to keep the bouncing face inside the graphics viewport.

faceTexture = Content.Load<Texture2D>("face");backgroundTexture = Content.Load<Texture2D>("background");

facePosition.X += faceVx;if (facePosition.X > maxX - faceTexture.Width) faceVx *= -1;if (facePosition.X < minX) faceVx *= -1;facePosition.Y += faceVy;if (facePosition.Y > maxY - faceTexture.Height) faceVy *= -1;if (facePosition.Y < minY) faceVy *= -1; 

Page 11: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 8: Add code to the Draw( ) method to draw the background and the face in the current position.

spriteBatch.Begin();spriteBatch.Draw(backgroundTexture, new Rectangle(minX,minY,maxX,maxY),Color.White);spriteBatch.Draw(faceTexture, facePosition, Color.White);spriteBatch.End(); 

Note that the default transparency color in XNA is magenta (255,0,255).

The background image is automatically stretched to fit size of the rectangle specified.

Page 12: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

namespace FaceBall{ public class faceBall : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; 

public faceBall() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } 

protected override void Initialize() {

base.Initialize(); } 

protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); }  

protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } 

protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); base.Draw(gameTime); } }} 

Texture2D faceTexture;Texture2D backgroundTexture;Vector2 facePosition;public int maxX, minX, maxY, minY;public int faceVx = 5;public int faceVy = 2; Viewport view = graphics.GraphicsDevice.Viewport;

maxX = view.Width;minX = 0;maxY = view.Height;minY = 0; facePosition.X = view.Width / 2;facePosition.Y = view.Height / 2; faceTexture = Content.Load<Texture2D>("face");

backgroundTexture = Content.Load<Texture2D>("background");

facePosition.X += faceVx;if (facePosition.X > maxX - faceTexture.Width) faceVx *= -1;if (facePosition.X < minX) faceVx *= -1;facePosition.Y += faceVy;if (facePosition.Y > maxY - faceTexture.Height) faceVy *= -1;if (facePosition.Y < minY) faceVy *= -1; 

spriteBatch.Begin();spriteBatch.Draw(backgroundTexture, new Rectangle(minX, minY, maxX, maxY), Color.White);spriteBatch.Draw(faceTexture, facePosition, Color.White);spriteBatch.End(); 

FaceBall Code Summary

Page 13: Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to  XNA Game

Step 9: Run the program to verify correct operation.