15

Click here to load reader

Xna game studio presentación 05

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Xna game studio   presentación 05

Desplazamientos y

movimientos de SpritesJuan Carlos Zuluaga

Page 2: Xna game studio   presentación 05

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

Page 3: Xna game studio   presentación 05

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;

Page 4: Xna game studio   presentación 05

Agregue el método Initializate con el siguiente código:public void Initialize(Texture2D textura, int x, int y, int imageWidth, intimageHeigth,

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);

}

Page 5: Xna game studio   presentación 05

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;

}

Page 6: Xna game studio   presentación 05

else if (sentido == 2){

y1 -= velocidad;y2 -= velocidad;

}else{

y1 += velocidad;y2 += velocidad;

}

if (sentido == 0 || sentido == 1){

rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);rec2 = new Rectangle(x2, y1, imageWidth, imageHeigth);

}else{

rec1 = new Rectangle(x1, y1, imageWidth, imageHeigth);rec2 = new Rectangle(x1, y2, imageWidth, imageHeigth);

}

Page 7: Xna game studio   presentación 05

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;}

}

Page 8: Xna game studio   presentación 05

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;}

}}

Page 9: Xna game studio   presentación 05

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;

Page 10: Xna game studio   presentación 05

Agregue el método Initializate con el siguiente código:public void Initialize(Texture2D textura, int x, int y, int imageWidth, intimageHeigth,

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:

Page 11: Xna game studio   presentación 05

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:

Page 12: Xna game studio   presentación 05

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 sizewidth = 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();

Page 13: Xna game studio   presentación 05

En el Load:Texture2D texturaEscena = Content.Load<Texture2D>("Imagenes/Background2");escena.Initialize(texturaEscena, 0, 0, width, height, width, height, 5, 0);

Texture2D texturaArbol = Content.Load<Texture2D>("Imagenes/Arbol");arbol.Initialize(texturaArbol, 50, 200, 200, 200, width, height, 5, 0);

Texture2D texturaPiedra = Content.Load<Texture2D>("Imagenes/Piedra");piedra.Initialize(texturaPiedra, 300, 200, 200, 200, width, height, 5, 0);

Texture2D texturaBala1 = Content.Load<Texture2D>("Imagenes/BalaMarioBros");bala1.Initialize(texturaBala1, width - 80, 100, 80, 50, width, height, 10, 1);

Texture2D texturaBala2 = Content.Load<Texture2D>("Imagenes/BalaMarioBros2");bala2.Initialize(texturaBala2, 0, 100, 80, 50, width, height, 10, 0);

Texture2D texturaHorse = Content.Load<Texture2D>("Imagenes/animacionHorse2");horse.Initialize(texturaHorse, new Vector2(300, 400), 150, 98, 5, 80, Color.White, 1f, true);

En el Update:escena.Update();piedra.Update();arbol.Update();bala1.Update();bala2.Update();horse.Update(gameTime);

Page 14: Xna game studio   presentación 05

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

Page 15: Xna game studio   presentación 05

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.