XNA código

0 views
Skip to first unread message

Gilberto Junior

unread,
Apr 4, 2012, 9:10:44 AM4/4/12
to EN05140 - Tópicos Especiais em Computação: Desenvolvimento de Jogos
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 FisicaColisao
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

//Posicao e Textura do da Sprite Mario
Vector2 marioPosition = new Vector2(74, 0);
Texture2D marioSpriteTexture;

//Posicao e Textura do da Sprite Mario
Vector2 luigiPosition = new Vector2(0, 0);
Texture2D luigiSpriteTexture;

//Dados para colisão por pixel
Color[] dadosTexturaMario;
Color[] dadosTexturaLuigi;


//Para entrada de teclado
KeyboardState keyboardState;

//Velocidade do Jogador
float playerMoveSpeed;

//Variaveis de colisao
bool isColisao = false;
BoundingBox boxMario, boxLuigi;

//Fonte
SpriteFont fonte;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

}
/// <summary>
/// Allows the game to perform any initialization it needs to
before starting to run.
/// This is where it can query for any required services and
load any non-graphic
/// related content. Calling base.Initialize will enumerate
through any components
/// and initialize them as well.
/// </summary>
///
protected override void Initialize()
{
this.IsMouseVisible = true;
keyboardState = Keyboard.GetState();
playerMoveSpeed = 2.0f;
base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place
to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw
textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

fonte = Content.Load<SpriteFont>("gameFont");

marioSpriteTexture =
this.Content.Load<Texture2D>("mario");
luigiSpriteTexture =
this.Content.Load<Texture2D>("luigi");

dadosTexturaMario = new Color[marioSpriteTexture.Width *
marioSpriteTexture.Height];
marioSpriteTexture.GetData(dadosTexturaMario);

dadosTexturaLuigi = new Color[luigiSpriteTexture.Width *
luigiSpriteTexture.Height];
luigiSpriteTexture.GetData(dadosTexturaLuigi);
}

/// <summary>
/// UnloadContent will be called once per game and is the
place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing
audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing
values.</param>
protected override void Update(GameTime gameTime)
{
moverMario();

//colisaoComIf();

//colisaoComIntersects();

colisaoComIntersectsEporPixel();

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing
values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

if (isColisao)
{
GraphicsDevice.Clear(Color.Red);
}
else
{
GraphicsDevice.Clear(Color.Green);
}

spriteBatch.Begin();
spriteBatch.DrawString(fonte,"Posicao mario X:
"+marioPosition.X+"| Y: "+marioPosition.Y ,new
Vector2(400,0),Color.White);
spriteBatch.DrawString(fonte, "Posicao luigi X: " +
luigiPosition.X + "| Y: " + luigiPosition.Y, new Vector2(400, 50),
Color.White);
spriteBatch.Draw(marioSpriteTexture, marioPosition,
Color.White);
spriteBatch.Draw(luigiSpriteTexture,luigiPosition,
Color.White);
spriteBatch.End();

base.Draw(gameTime);
}


void moverMario()
{
#region Entrada de teclado

// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed)
this.Exit();

//Ensinar a mover a spirte na tela
keyboardState = Keyboard.GetState();//Pega o Stado do
Teclado
if (keyboardState.IsKeyDown(Keys.Left)) // Move o teclado
para a esquerda
{
marioPosition.X -= playerMoveSpeed;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
marioPosition.X += playerMoveSpeed;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
marioPosition.Y -= playerMoveSpeed;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
marioPosition.Y += playerMoveSpeed;
}

#endregion teclado
}

void colisaoComIf()
{


}

void colisaoComIntersects()
{

}

void colisaoComIntersectsEporPixel()
{

}

//void colisaoEsfera()
//{
// //similar a boundingbox colisão com intersects
// centroDaImagemX = posicaoBola1.X+texturaBola1.Width;
// centroDaImagemY = posicaoBola1.Y+texturaBola1.Height;
// bola1 = new BoundingSphere(new Vector3(centroDaImagemX,
centroDaImagemY), raio);
//}

void colisaoPorPixel()
{





}
}
}
Reply all
Reply to author
Forward
0 new messages