XNA Introduction
COSC 3325 Game Programming
XNA INTRODUCTION
Spring 2011
1
Installing at Home
2


xna.com  create.msdn.com to get tools
You can install everything at once
 Designed

for phone dev so get lots extra
Or in pieces (XNA, VS 2010)
 XNA
4.0
 http://www.microsoft.com/downloads/en/details.aspx?Fami
lyID=9ac86eca-206f-4274-97f2-ef6c8b1f478f
 Visual
Studio
 You
can get the free VS 2010 Express for XNA
 You can get the full version from MSDN
COSC 3325 Game Programming
Spring 2011
Installing at Home
3

Can use XNA 3.1 or 3.0 (has books)
 Install
Visual C# 2008 or Visual Studio 2008
 Need correct DirectX and .NET Framework

Get XNA 4.0 installer from MSDN or MS.COM or
XNA.com
COSC 3325 Game Programming
Spring 2011
Let’s Create a Windows Game
4

Primarily from Nitschke
http://www.wrox.com/WileyCDA/WroxTitle/Professional
-XNA-Game-Programming-For-Xbox-360-andWindows.productCd-0470261285.html

Can download code from there
COSC 3325 Game Programming
Spring 2011
Start a New Project
5



Open XNA
File -> New -> Project
Select a type as
 XNA
Game Studio 4.0
 Windows

Game
Name it if you wish
COSC 3325 Game Programming
Spring 2011
Your Game Project
6

Initialize
LoadContent
UnloadContent
Update
Draw

So press F5 now




COSC 3325 Game Programming
Spring 2011
Allow User to Press <ESC> to Exit
7
GamePadState gamePad =
GamePad.GetState(PlayerIndex.One);
KeyboardState keyboard = Keyboard.GetState();
if (gamePad.Buttons.Back == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Escape))
this.Exit();
COSC 3325 Game Programming
Spring 2011
Adding a background Texture
8

Drag texture to solution explorer
 Drop

in content
Right click on the texture
 Choose

AssetName
 used


properties
to load (unique)
Content Importer
Content Processor
COSC 3325 Game Programming
Spring 2011
Adding a Background Texture
9

New Variable at top of class
Texture2D backgroundTexture;

Modify LoadContent()
backgroundTexture = Content.Load<Texture2D>("CityGroundSmall");

Modify Draw()
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
spriteBatch.End();
COSC 3325 Game Programming
Spring 2011
Moving the Texture
10
•
New class Variable
Float scrollPosition = 0;
•
Modify Update() add these just before
What’s This?
update
// Move 400 pixels each second
float moveFactorPerSecond = 400 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f;
// Move up and down if we press the cursor or gamepad keys.
if (gamePad.DPad.Up == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Up))
scrollPosition += moveFactorPerSecond;
if (gamePad.DPad.Down == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Down))
scrollPosition -= moveFactorPerSecond;
COSC 3325 Game Programming
Spring 2011
Moving the Texture
11
•
Modify draw()
spriteBatch.Begin();
int resolutionWidth = graphics.GraphicsDevice.Viewport.Width;
int resolutionHeight = graphics.GraphicsDevice.Viewport.Height;
for (int x=0; x<= resolutionWidth/backgroundTexture.Width; x++)
for (int y=-1;y<= resolutionHeight/backgroundTexture.Height+1; y++) {
Vector2 position = new Vector2(x * backgroundTexture.Width,
y * backgroundTexture.Height +
((int)scrollPosition) % backgroundTexture.Height);
spriteBatch.Draw(backgroundTexture, position, Color.White);
}
spriteBatch.End();
•
<F5>
COSC 3325 Game Programming
Spring 2011
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertising