Sunday, February 20, 2011

Walky Talky Conversation Do You Copy

A modular game with XNA (Convert SDL game to XNA - 2) Convert


The skeleton that we have created with XNA is for simple games, but a single large source is not as simple to maintain. As the game logic is complicated, it is desirable to break down into classes that encapsulate the details of the conduct of our character, our (s) enemy (s), etc.

Thus, we might start by creating a class "graphic element", similar to that already used with SDL (although much simpler), which inherit to create your character, enemies, objects in the background etc. This class could be this time:

  

ElemGrafico class {protected
Texture2D MyPicture;
protected int x, y;
incrX protected int, incrY;
public
ElemGrafico (string filename) {

x = 0, y = 0;
incrX = 0; incrY = 0 ;
CargarImagen (filename);}


/ / / Move the graphic element to another position
public void Move (int nuevaX, int nuevaY)

{x = (short) nuevaX;
y = (short) nuevaY;


} / / / Load the image that represents this graphic element
CargarImagen public void (string name) {

MyPicture = Texture2D.FromStream (miPartida.GetGraphics (),
new FileStream (name, FileMode.Open));

contieneImagen = true;
contieneSecuencia = false;}



/ / / Draw on screen hidden as part of a "SpriteBatch" public void
DibujarOculta (SpriteBatch listaSprites) {

listaSprites.Draw (MyPicture, new Vector2 (x, y), Color.White);
}}

And from it we could create class as representing our enemy as well:
  
class Enemy: ElemGrafico

{/ / Constructor public
Enemy (item p)
: base ("images / enemigo.png)

{x = 400, / / \u200b\u200bInitial values \u200b\u200b
y = 400;
incrX = 2;}


/ / Methods public void
motion Move ()

{x + = incrX;

if ((x <> 700))
incrX = (short) (-incrX)
}}



So the body of the program would have things like

  
LoadContent protected override void ( )

{/ / Create a new SpriteBatch, Which Can Be Used to draw textures.
SpriteBatch = new SpriteBatch (GraphicsDevice)

miPersonaje = New Character ();
miPersonaje.MoverA (400, 300);

miEnemigo = new Enemy ();}



UnloadContent protected override void ()

{/ / TODO: Unload Any non ContentManager
content here


} protected override void Update (GameTime gametime) {

MoverElementos ();
ComprobarTeclas ();

base.Update (gametime)



} protected override void Draw (GameTime gametime) {
GraphicsDevice
. Clear (Color.Black);

spriteBatch.Begin ();
miPersonaje.DibujarOculta (SpriteBatch)
miEnemigo.DibujarOculta (SpriteBatch)
spriteBatch.End ();

base.Draw (gametime)
} public void


MoverElementos () {

miEnemigo.Mover ();}



ComprobarTeclas public void ()

{if ( Keyboard.GetState (). IsKeyDown (Keys.Left))
miPersonaje.MoverIzquierda ();
if (Keyboard.GetState (). IsKeyDown (Keys.Right))
miPersonaje.MoverDerecha ();
...
}


words, create the character, the enemy will move and draw, and we can even create MoverElementos auxiliary functions, ComprobarTeclas, ComprobarColisiones, etc., That bring the rigid structure to the structure proposed XNA "classic" that we had created over SDL.

If you want to see the full source, you can download the mini-project from Google Code page, under the name miner002XNA:

http://code.google.com/p / manic-miner/downloads/list

0 comments:

Post a Comment