Creación de juegos mediante la plataforma XNA de Microsoft Visual Studio
Links para descargar material:
Parte 01: https://www.dropbox.com/s/2b3f27izg3wfqws/01.XNA.zip
Parte 02: https://www.dropbox.com/s/ep3634jsjullkbs/02.XNA.zip
Parte 03: https://www.dropbox.com/s/9e3q068t6mlkted/03.XNA.zip
Parte 04: https://www.dropbox.com/s/u4navlzlhtecjm0/04.XNA.zip
Parte 05: https://www.dropbox.com/s/oqjll5c73kbvu1j/05.XNA.zip
2. Introducción
En la lección #3, creamos un fondo desplazable, en el cual utilizamos varias
líneas de código en varios segmentos de nuestro programa. ¿Pero que pasa si
deseamos colocar otros sprites que se muevan al unísono con el fondo
desplazable?... Pues toca replicar esa lógica para todos los sprites, haciendo
mal uso del Copy And Paste
La solución será crear una clase desplazamiento, en la cual empaquetemos
toda esta funcionalidad donde simplifiquemos la labor como programadores
Siendo aun más ambiciosos, vamos a crear otra clase de movimiento, donde
podamos mover sprites en sentido contrario como se mueve nuestro fondo
desplazable
3. Cree un proyecto XNA
Cree la carpeta Imágenes y agregue las imágenes que utilizaremos en este
proyecto: animacionHorse2.png, Arbol.png, Background2.png,
BalaMarioBros.png, BalaMarioBros2.png y Piedra.png
Agregue al proyecto la clase Animation que hemos utilizado en proyectos
anteriores, no olvide cambiarle el nombre del NameSpace por el actual
Cree la nueva clase Desplazamiento y reemplace los Using por estos:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
Agregue estos atributos a la clase Desplazamiento :
private int x1;
private int x2;
private int y1;
private int y2;
private int imageWidth;
private int imageHeigth;
private int windowWidth;
private int windowHeigth;
private int velocidad;
private int sentido; // 0: Derecha - Izquierda, 1: Izquierda - Derecha, 2: Arriba - Abajo, 3: Abajo - Arriba
private Rectangle rec1;
private Rectangle rec2;
private Texture2D textura;
4. Agregue el método Initializate con el siguiente código:
public void Initialize(Texture2D textura, int x, int y, int imageWidth, int
imageHeigth,
int windowWidth, int windowHeigth, int velocidad, int sentido)
{
this.textura = textura;
this.imageHeigth = imageHeigth;
this.imageWidth = imageWidth;
this.windowHeigth = windowHeigth;
this.windowWidth = windowWidth;
this.velocidad = velocidad;
this.sentido = sentido;
this.x1 = x;
this.y1 = y;
if (sentido == 0)
{
rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);
x2 = x1 + windowWidth;
rec2 = new Rectangle(x2, y1, imageHeigth, imageHeigth);
}
else if (sentido == 1)
{
rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);
x2 = x1 - windowWidth;
rec2 = new Rectangle(x2, y1, imageHeigth, imageHeigth);
}
5. else if (sentido == 2)
{
rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);
y2 = y1 + windowHeigth;
rec2 = new Rectangle(x1, y2, imageHeigth, imageHeigth);
}
else
{
rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);
y2 = y1 - windowHeigth;
rec2 = new Rectangle(x1, y2, imageHeigth, imageHeigth);
}
}
Agregue el método Update con el siguiente código:
public void Update()
{
if (sentido == 0)
{
x1 -= velocidad;
x2 -= velocidad;
}
else if (sentido == 1)
{
x1 += velocidad;
x2 += velocidad;
}
7. if (sentido == 0)
{
if (rec1.X == -windowWidth)
{
x1 = 0;
}
if (rec2.X == 0)
{
x2 = windowWidth;
}
}
else if (sentido == 1)
{
if (rec1.X == windowWidth)
{
x1 = 0;
}
if (rec2.X == 0)
{
x2 = -windowWidth;
}
}
8. else if (sentido == 2)
{
if (rec1.Y == -windowHeigth)
{
y1 = 0;
}
if (rec2.Y == 0)
{
y2 = windowHeigth;
}
}
else
{
if (rec1.Y == windowHeigth)
{
y1 = 0;
}
if (rec2.Y == 0)
{
y2 = -windowHeigth;
}
}
}
9. Agregue el método Draw con el siguiente código:
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(textura, rec1, Color.White);
spriteBatch.Draw(textura, rec2, Color.White);
}
Cree la nueva clase Movimiento y reemplace los Using por estos:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
Agregue estos atributos a la clase Movimiento:
private int x;
private int y;
private int imageWidth;
private int imageHeigth;
private int windowWidth;
private int windowHeigth;
private int velocidad;
private int sentido; // 0: Derecha - Izquierda, 1: Izquierda - Derecha, 2: Arriba - Abajo, 3: Abajo - Arriba
private Rectangle rectangulo;
private Texture2D textura;
10. Agregue el método Initializate con el siguiente código:
public void Initialize(Texture2D textura, int x, int y, int imageWidth, int
imageHeigth,
int windowWidth, int windowHeigth, int velocidad, int sentido)
{
this.textura = textura;
this.imageHeigth = imageHeigth;
this.imageWidth = imageWidth;
this.windowHeigth = windowHeigth;
this.windowWidth = windowWidth;
this.velocidad = velocidad;
this.sentido = sentido;
this.x = x;
this.y = y;
rectangulo = new Rectangle(x, y, imageWidth, imageHeigth);
}
Agregue el método Update con el siguiente código:
11. public void Update()
{
if (sentido == 0)
{
x += velocidad;
if (x == windowWidth) x = 0;
}
else if (sentido == 1)
{
x -= velocidad;
if (x == -imageWidth) x = windowWidth - imageWidth;
}
else if (sentido == 2)
{
y += velocidad;
if (y == windowHeigth) y = 0;
}
else
{
y -= velocidad;
if (y == -imageHeigth) y = windowHeigth - imageHeigth;
}
rectangulo.X = x;
rectangulo.Y = y;
}
Agregue el método Draw con el siguiente código:
12. public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(textura, rectangulo, Color.White);
}
Ya tenemos las tres clases que utilizaremos regularmente en casi todos los
juegos que hagamos: Animation, Desplazamiento y Movimiento
Creemos estos atributos en la clase Game1:
Desplazamiento escena;
Desplazamiento arbol;
Desplazamiento piedra;
Movimiento bala1;
Movimiento bala2;
Animation horse;
int width;
int height;
En el constructor de Game1, agreguemos las siguientes líneas:
// change windows size
width = 800;
height = 600;
graphics.PreferredBackBufferWidth = width;
graphics.PreferredBackBufferHeight = height;
En el Initialize:
escena = new Desplazamiento();
arbol = new Desplazamiento();
piedra = new Desplazamiento();
bala1 = new Movimiento();
bala2 = new Movimiento();
horse = new Animation();
14. En el Draw:
spriteBatch.Begin();
escena.Draw(spriteBatch);
piedra.Draw(spriteBatch);
arbol.Draw(spriteBatch);
bala1.Draw(spriteBatch);
bala2.Draw(spriteBatch);
horse.Draw(spriteBatch);
spriteBatch.End();
Ensaye diferentes parámetros de la clase Desplazamiento y Movimiento y
saque sus concluisones
15. Taller #5
Realizar un juego donde aparezcan 2 animaciones, 1 fondo desplazable, 3
objetos moviéndose con el fondo desplazable, 3 objetos moviéndose
separadamente del fondo desplazable, agregue una canción de fondo y 2
efectos de sonido.