分类: C/C++
2008-02-25 15:35:43
private Texture2D backgroundTexture, menuTexture, gameTexture;
// Load all our content
this.backgroundTexture = this.content.Load<Texture2D>("SpaceBackground");
this.menuTexture = this.content.Load<Texture2D>("PongMenu");
this.gameTexture = this.content.Load<Texture2D>("PongGame");
this.spriteBatch.Begin();
this.spriteBatch.Draw(this.backgroundTexture,
new Rectangle(0, 0, width, height),
Color.LightGray);
this.spriteBatch.End();
private static readonly Rectangle
XnaPongLogoRect = new Rectangle(0, 0, 512, 110),
MenuSingleplayerRect = new Rectangle(0, 110, 512, 38),
MenuMultiplayerRect = new Rectangle(0, 148, 512, 38),
MenuExitRect = new Rectangle(0, 186, 512, 38),
GameLifesRect = new Rectangle(0, 222, 100, 34),
GameRedWonRect = new Rectangle(151, 222, 155, 34),
GameBlueWonRect = new Rectangle(338, 222, 165, 34),
GameRedPaddleRect = new Rectangle(23, 0, 22, 92),
GameBluePaddleRect = new Rectangle(0, 0, 22, 92),
GameBallRect = new Rectangle(1, 94, 33, 33),
GameSmallBallRect = new Rectangle(37, 108, 19, 19);
public static void TestMenuSprites()

{
StartTest(
delegate
{
testGame.RenderSprite(testGame.menuTexture,
512-XnaPongLogoRect.Width/2, 150,
XnaPongLogoRect);
testGame.RenderSprite(testGame.menuTexture,
512-MenuSingleplayerRect.Width/2, 300,
MenuSingleplayerRect);
testGame.RenderSprite(testGame.menuTexture,
512-MenuMultiplayerRect.Width/2, 350,
MenuMultiplayerRect, Color.Orange);
testGame.RenderSprite(testGame.menuTexture,
512-MenuExitRect.Width/2, 400,
MenuExitRect);
});
} // TestMenuSprites()
delegate void TestDelegate();
class TestPongGame : PongGame

{
TestDelegate testLoop;
public TestPongGame(TestDelegate setTestLoop)
{
testLoop = setTestLoop;
} // TestPongGame(setTestLoop)
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
testLoop();
} // Draw(gameTime)
} // class TestPongGame
static TestPongGame testGame;
static void StartTest(TestDelegate testLoop)

{
using (testGame = new TestPongGame(testLoop))
{
testGame.Run();
} // using
} // StartTest(testLoop)
public void RenderSprite(Texture2D texture, int x, int y,
Rectangle sourceRect, Color color)

{
// TODO
} // RenderSprite(texture, rect, sourceRect)
public void RenderSprite(Texture2D texture, int x, int y,
Rectangle sourceRect)

{
// TODO
} // RenderSprite(texture, rect, sourceRect)
static void Main(string[] args)

{
// PongGame.StartGame();
PongGame.TestMenuSprites();
} // Main(args)
public static void StartGame()

{
using (PongGame game = new PongGame())
{
game.Run();
} // using
} // StartGame()
class SpriteToRender

{
public Texture2D texture;
public Rectangle rect;
public Rectangle? sourceRect;
public Color color;
public SpriteToRender(Texture2D setTexture, Rectangle setRect,
Rectangle? setSourceRect, Color setColor)
{
texture = setTexture;
rect = setRect;
sourceRect = setSourceRect;
color = setColor;
} // SpriteToRender(setTexture, setRect, setColor)
} // SpriteToRender
List<SpriteToRender> sprites = new List<SpriteToRender>();
public void RenderSprite(Texture2D texture, Rectangle rect,
Rectangle? sourceRect, Color color)

{
sprites.Add(new SpriteToRender(texture, rect, sourceRect, color));
} // RenderSprite(texture, rect, sourceRect, color)
public void DrawSprites()

{
// No need to render if we got no sprites this frame
if (sprites.Count == 0)
return;
// Start rendering sprites
spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.BackToFront, SaveStateMode.None);
// Render all sprites
foreach (SpriteToRender sprite in sprites)

{
spriteBatch.Draw(sprite.texture,
// Rescale to fit resolution
new Rectangle(
sprite.rect.X * width / 1024,
sprite.rect.Y * height / 768,
sprite.rect.Width * width / 1024,
sprite.rect.Height * height / 768),
sprite.sourceRect, sprite.color);
}
// We are done, draw everything on screen with help of the end method.
spriteBatch.End();
// Kill list of remembered sprites
sprites.Clear();
} // DrawSprites()
public static void TestGameSprites()

{
StartTest(
delegate
{
// Show lives
testGame.ShowLives();
// Ball in center
testGame.RenderBall();
// Render both paddles
testGame.RenderPaddles();
});
} // TestGameSprites()
//PongGame.StartGame();
//PongGame.TestMenuSprites();
PongGame.TestGameSprites();
public void ShowLives()

{
// Left players lives
RenderSprite(menuTexture, 2, 2, GameLivesRect);
for (int num = 0; num < leftPlayerLives; num++)
{
RenderSprite(gameTexture, 2 + GameLivesRect.Width +
GameSmallBallRect.Width * num - 2, 9,
GameSmallBallRect);
}
// Right players lives
int rightX = 1024 - GameLivesRect.Width - GameSmallBallRect.Width * 3 - 4;
RenderSprite(menuTexture, rightX, 2, GameLivesRect);
for (int num = 0; num < rightPlayerLives; num++)
{
RenderSprite(gameTexture, rightX + GameLivesRect.Width +
GameSmallBallRect.Width * num - 2, 9,
GameSmallBallRect);
}
} // ShowLives()
public void RenderBall()

{
this.RenderSprite(this.gameTexture,
(int)((0.5f + 0.9f * this.ballPosition.X) * 1024) - GameBallRect.Width / 2,
(int)((0.02f + 0.96f * this.ballPosition.Y) * 768) - GameBallRect.Height / 2,
GameBallRect);
} // RenderBall()
public void RenderPaddles()

{
this.RenderSprite(this.gameTexture,
(int)(0.05f * 1024) - GameRedPaddleRect.Width / 2,
(int)((0.06f + 0.88f * this.leftPaddlePosition) * 768) –
GameRedPaddleRect.Height / 2,
GameRedPaddleRect);
this.RenderSprite(this.gameTexture,
(int)(0.95f * 1024) - GameBluePaddleRect.Width / 2,
(int)((0.06f + 0.88f * this.rightPaddlePosition) * 768) –
GameBluePaddleRect.Height / 2,
GameBluePaddleRect);
} // RenderPaddles()
/**////
/// Current paddle positions, 0 means top, 1 means bottom.
///
private float leftPaddlePosition = 0.5f, rightPaddlePosition = 0.5f;
/**////
/// Current ball position,again from 0 to 1, 0 is left and top, 1 is bottom and right.
///
private Vector2 ballPosition = new Vector2(0.5f, 0.5f);
/**////
/// Ball speed vector, randomized for every new ball.
/// Will be set to Zero if we are in menu or game is over.
///
private Vector2 ballSpeedVector = new Vector2(0, 0);