|
- Baixar o exemplo "PlaySound" em: http://go.microsoft.com/fwlink/?LinkId=104001&clcid=0x409; - Na aba SOLUTION EXPLORER/Nave/CONTENT (Visual C# 2005), clicar em ADD/NEW FOLDER e criar uma pasta chamada Audio; - Copiar os arquivos do diretório Audio do "PlaySound" pro Audio do projeto abaixo; - Na aba SOLUTION EXPLORER/Nave/CONTENT, clicar em ADD/EXISTING ITEM e adicionar o arquivo "PlaySound.xap"; - Retirar todos os // dos comentários em Initialize, Update e Atirar. |
| Programa.cs |
using System;
namespace Nave
{
static class TPrograma
{
static void Main (string[] args)
{
using (TPrincipal Jogo= new TPrincipal ())
Jogo.Run ();
}
} } |
| Principal.cs |
namespace Nave
{
public class TPrincipal_Base: Microsoft.Xna.Framework.Game
{
protected const int cIncNaveTiroY= 10;
protected const int cIncNaveX = 10;
protected const int cIncNaveY = 10;
protected AudioEngine Dispositivo_Som_Controlador;
protected bool Atirou;
protected GraphicsDeviceManager Dispositivo;
protected int TiroInc;
protected KeyboardState Dispositivo_Tecla;
protected Rectangle NaveArea;
protected SoundBank Dispositivo_Som;
protected SpriteBatch Nave, NaveTiro;
protected Texture2D NaveTextura, NaveTiroTextura;
protected Vector2 Destino, Direcao, Olhando, Origem, OrigemTiro, Posicao, Tiro;
protected Viewport Dispositivo_VP;
protected WaveBank Dispositivo_Som_WAV;
public TPrincipal_Base ()
{
Dispositivo = new GraphicsDeviceManager (this);
Content.RootDirectory= "Content";
}
protected override void Initialize ()
{
base.Initialize ();
this.IsMouseVisible = false;
Atirou = false;
Tiro = new Vector2 (0, 0);
//Dispositivo_Som_Controlador= new AudioEngine (@"Content\Audio\PlaySound.xgs");
//Dispositivo_Som_WAV = new WaveBank (Dispositivo_Som_Controlador, @"Content\Audio\Wave Bank.xwb");
//Dispositivo_Som = new SoundBank (Dispositivo_Som_Controlador, @"Content\Audio\Sound Bank.xsb");
}
protected override void LoadContent ()
{
Dispositivo_VP = Dispositivo.GraphicsDevice.Viewport;
Nave = new SpriteBatch (GraphicsDevice);
NaveTiro = new SpriteBatch (GraphicsDevice);
NaveTextura = Content.Load<Texture2D> (@"objeto\nave");
NaveTiroTextura= Content.Load<Texture2D> (@"objeto\navetiro");
NaveArea = new Rectangle (0, 0, Dispositivo_VP.Width-NaveTextura.Width,
Dispositivo_VP.Height-NaveTextura.Height-2);
Posicao = new Vector2 ((Dispositivo_VP.Width-NaveTextura.Width)/2, NaveArea.Bottom);
}
protected override void Update (GameTime AGT)
{
if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit ();
Dispositivo_Tecla= Keyboard.GetState ();
//Dispositivo_Som_Controlador.Update ();
base.Update (AGT);
}
protected override void Draw (GameTime AGT)
{
Dispositivo.GraphicsDevice.Clear (Color.Black);
Disparar_AoDesenhar ();
base.Draw (AGT);
}
protected virtual void Disparar_AoDesenhar ()
{
}
} } |
| Maquina.cs |
namespace Nave
{
class TPrincipal: TPrincipal_Base
{
protected override void Disparar_AoDesenhar ()
{
Navegar ();
MostrarNave ();
MostrarTiro ();
}
protected void Atirar ()
{
if (!Atirou) {
Atirou= true;
Tiro.X= Posicao.X + (NaveTextura.Width-NaveTiroTextura.Width)/2;
Tiro.Y= Posicao.Y - 15;
//Dispositivo_Som.PlayCue ("kaboom");
}
}
protected virtual void MostrarTiro ()
{
if (Atirou) {
if (Tiro.Y <= 0)
Atirou= false;
else {
NaveTiro.Begin (SpriteBlendMode.Additive);
NaveTiro.Draw (NaveTiroTextura, Tiro, Color.White);
NaveTiro.End ();
Tiro.Y-= cIncNaveTiroY;
}
}
}
protected virtual void MostrarNave ()
{
Nave.Begin (SpriteBlendMode.Additive);
Nave.Draw (NaveTextura, Posicao, Color.White);
Nave.End ();
}
protected virtual void Navegar ()
{
if (Dispositivo_Tecla.IsKeyDown (Keys.Up))
if (Posicao.Y - cIncNaveY >= NaveArea.Top)
Posicao.Y-= cIncNaveY;
if (Dispositivo_Tecla.IsKeyDown (Keys.Down))
if (Posicao.Y + cIncNaveY <= NaveArea.Bottom)
Posicao.Y+= cIncNaveY;
if (Dispositivo_Tecla.IsKeyDown (Keys.Left))
if (Posicao.X >= NaveArea.Left)
Posicao.X-= cIncNaveX;
if (Dispositivo_Tecla.IsKeyDown (Keys.Right))
if (Posicao.X <= NaveArea.Right)
Posicao.X+= cIncNaveX;
if (Dispositivo_Tecla.IsKeyDown (Keys.Space))
Atirar ();
}
} } |

