bem ainda não é quase nada, mas pelo menos os sprite esta se movendo na tela =)
![]()
Tem a classe Sprite que eh uma variação da classe usada em http://www.xnadevelopment.com/tutorials/thewizard/theWizard.shtml e a classe principal do jogo em si Game1.
o que acontece eh que no método update() da classe do jogo verifico o estado do teclado e de acordo com este estado chamo métodos do objeto chicken para mover pela tela.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace ChickenMaze
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Sprite chicken;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = “Content”;
}
protected override void Initialize()
{
chicken = new Sprite(“chicken”);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
chicken.LoadContent(this.Content);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateKeyboard(Keyboard.GetState());
base.Update(gameTime);
}
public void UpdateKeyboard(KeyboardState keyboardState)
{
if( keyboardState.IsKeyDown(Keys.Left)){
chicken.Left();
}
if (keyboardState.IsKeyDown(Keys.Right))
{
chicken.Right();
}
if (keyboardState.IsKeyDown(Keys.Down))
{
chicken.Down();
}
if (keyboardState.IsKeyDown(Keys.Up))
{
chicken.Up();
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
chicken.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
e na classe Sprite esta a carga da imagem e posicionamento da imagem na hora do desenho.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace ChickenMaze
{
class Sprite
{
private int speed = 5;
private string assetName;
public Sprite(string assetName)
{
this.assetName = assetName;
}
//The current position of the Sprite
public Vector2 Position = new Vector2(0,0);
//The texture object used when drawing the sprite
private Texture2D mSpriteTexture;
//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager)
{
mSpriteTexture = theContentManager.Load
}
//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position, Color.White);
}
public void Left()
{
Position.X -= speed;
}
public void Right()
{
Position.X += speed;
}
public void Down()
{
Position.Y += speed;
}
public void Up()
{
Position.Y -= speed;
}
}
}
