In this first approach, we see the basic guidelines of how to make a character move around the screen by pressing the arrow keys. Later I extend to you may seem a little more (as far as possible) to the classes that were using to hide SDL.
The first step is to download the necessary tools to create games with XNA: Visual Studio as development and XNA Game Studio as a library of games.
First, download and install Visual Studio. If we go to the last version (2010), in its variant "Express" (evaluation), can be found here:
http://www.microsoft.com/express/Downloads/ # 2010 Visual-CS-
We can choose to download only Visual C # in Castilian (download an installer small, then it downloads over all that is needed) or an ISO image that contains all of Visual Studio to install later locally.
then download and install XNA. Its latest version is now 4, only in English, which can be found here:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9ac86eca-206f- 4274-97f2-ef6c8b1f478f
If now we enter into Visual Studio and indicate that we want to create a new project. Among the templates that are proposed, you should see one called "Windows Game (4.0)."
We give a name to the project and see a source called "Game1.cs" containing something like (removing comments):
game1 public class: Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch SpriteBatch;
public
game1 ()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit ();
/ / TODO: Add your update logic here
base.Update (gametime);}
protected override void
Draw (GameTime gametime) {
GraphicsDevice.Clear (Color.CornflowerBlue)
/ / TODO: Add your drawing code here
base.Draw (gametime);}
}
The basic ideas are
- "Initialize" is the initialization function. In principle, it should create the infrastructure that was necessary, except for graphics.
- "LoadContent" is responsible for loading the "content" that we were to use. They prepare our images.
- "UnloadContent" will do the opposite step, if necessary (in our simple examples will not be).
- "Update" (update) should take care of all the game logic: update the world, collision check, check user-input buttons, joystick, mouse-play sounds, etc..
- "Draw" draws screen elements.
If we want a bit closer to our style, the image of the character would be a "Texture2D" (a two-dimensional texture), which declared in the Attributes:
Texture2D imagenPersonaje ;
To load the image, we can "circumvent" the "contents" of XNA and do it our way, loading a PNG file from the program. This, in XNA 3.1 can be achieved with Texture2D.FromFile:
imagenPersonaje = Texture2D.FromFile (graphics, "images \\ \\ personaje.png");
but this syntax is not valid in XNA 4, but we have to use "FromStream" a little uncomfortable, because we read the image from a "FileStream", so for example:
imagenPersonaje = Texture2D.FromStream (graphics.GraphicsDevice,
new FileStream ("images \\ \\ personaje.png ", FileMode.Open));
(as we use a FileStream, we add "using System.IO;" at the beginning of our source.)
The easy alternative is to leave the preset images as part of the "content" (content) of the project, but we are not looking for the easiest solution, but a way to remember our previous work, when we used Tao . .. SDL
For position, we can use two integers, x and y, or we can use an existing data type in XNA, the "Vector2 (a vector of 2 components): it would declare an attribute:
Vector2 position;
that give value from LoadContent:
position = new Vector2 (400, 300);
From "Update" we can see if you press ESC to exit or any of the arrows to change the position of the character:
if (Keyboard.GetState (). IsKeyDown (Keys.Escape))
this.Exit ();
if (Keyboard. GetState (). IsKeyDown (Keys.Left))
posicion.X -= 5;
if (Keyboard.GetState (). IsKeyDown (Keys.Right))
posicion.X + = 5;
if (Keyboard.GetState (). IsKeyDown (Keys.Up))
posicion.Y -= 5;
if (Keyboard.GetState (). IsKeyDown (Keys.Down))
posicion.Y + = 5;
And to draw the character, and other sprites of the game, we will from "Draw" as part of the "batch sprites "(SpriteBatch), thus:
protected override void Draw (GameTime gametime) {
GraphicsDevice.Clear (Color.Black);
spriteBatch.Begin ();
spriteBatch.Draw (imagenPersonaje, position, Color.White);
spriteBatch.End ();
base.Draw (gametime)
}
changes If all went well, our project should compile properly and maybe even work, with two considerations:
- We will create a folder "images" inside the folder our project executable (bin / debug) and copy into it the image named "personaje.png."
- is possible that by launching the project, complaining that my graphic card is not powerful enough and "does not support HiDef profile." No problem: go to Project menu, choose "Properties ... (Project name) "and the first tab in the" Game profile, leave the option "Use Reach" instead of "Use HiDef.
can download the mini-project from Google Code page, under the name miner001XNA:
http://code.google.com/p/manic-miner/downloads / list
0 comments:
Post a Comment