using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.Audio;
usingMicrosoft.Xna.Framework.Content;
usingMicrosoft.Xna.Framework.GamerServices;
usingMicrosoft.Xna.Framework.Graphics;
usingMicrosoft.Xna.Framework.Input;
usingMicrosoft.Xna.Framework.Media;
namespace Game1
{
///<summary>
///This is the main type for your game
///</summary>
publicclassGame1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatchspriteBatch;
Texture2D back;
Texture2D mark;
Texture2D ball;
Texture2D bar;
SpriteFont font1;
int count = 0;
boolupdown = true;
enumGS { Menu, Play, Help, Quit, Over };
GS gamestate = GS.Menu;
intCurrentSelect = 0;
KeyboardStatepks;
GamePadStatepgs;
Vector2BallPosition = Vector2.Zero;
Vector2BallSpeed = newVector2(500.0f, 500.0f);
Vector2BarPosition = newVector2(320, 580);
int score = 0;
public Game1()
{
graphics = newGraphicsDeviceManager(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>
protectedoverridevoid Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
base.Initialize();
}
///<summary>
///LoadContent will be called once per game and is the place to load
/// all of your content.
///</summary>
protectedoverridevoidLoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = newSpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
back = Content.LoadTexture2D>("back");
mark = Content.LoadTexture2D>("mark");
ball = Content.LoadTexture2D>("ball");
bar = Content.LoadTexture2D>("bar");
font1 = Content.LoadSpriteFont>("font");
}
///<summary>
///UnloadContent will be called once per game and is the place to unload
/// all content.
///</summary>
protectedoverridevoidUnloadContent()
{
// 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
protectedoverridevoid Update(GameTimegameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
KeyboardStateks = Keyboard.GetState();
GamePadStategs = GamePad.GetState(PlayerIndex.One);
switch (gamestate)
{
caseGS.Menu:
if (updown)
{
count++;
if (count == 20)
updown = false;
}
else
{
count--;
if (count == 0)
updown = true;
}
if (((ks.IsKeyDown(Keys.Up) == true) & (pks.IsKeyDown(Keys.Up) == false)) ||
((gs.IsButtonDown(Buttons.DPadUp) == true) & (pgs.IsButtonDown(Buttons.DPadUp) == false)))
if (CurrentSelect > 0)
CurrentSelect--;
if (((ks.IsKeyDown(Keys.Down) == true) & (pks.IsKeyDown(Keys.Down) == false)) ||
((gs.IsButtonDown(Buttons.DPadDown) == true) & (pgs.IsButtonDown(Buttons.DPadDown) == false)))
if (CurrentSelect < 2)
CurrentSelect++;
if (((ks.IsKeyDown(Keys.Enter) == true) & (pks.IsKeyDown(Keys.Enter) == false)) ||
((gs.IsButtonDown(Buttons.A) == true) & (pgs.IsButtonDown(Buttons.A) == false)))
switch (CurrentSelect)
{
case 0:
gamestate = GS.Play;
break;
case 1:
gamestate = GS.Help;
break;
case 2:
gamestate = GS.Quit;
break;
}
break;
//NO.1
caseGS.Play:
if (((ks.IsKeyDown(Keys.Escape) == true) & (pks.IsKeyDown(Keys.Escape) == false)) ||
((gs.IsButtonDown(Buttons.B) == true) & (pgs.IsButtonDown(Buttons.B) == false)))
gamestate = GS.Menu;
if ((ks.IsKeyDown(Keys.Left) == true) || (gs.IsButtonDown(Buttons.DPadLeft) == true))
{
BarPosition.X -= 10;
if (BarPosition.X < 0)
BarPosition.X = 0;
}
if ((ks.IsKeyDown(Keys.Right) == true) || (gs.IsButtonDown(Buttons.DPadRight) == true))
{
BarPosition.X += 10;
if (BarPosition.X > 800 - 160)
BarPosition.X = 800 - 160;
}
// Move the sprite by speed, scaled by elapsed time.
BallPosition +=
BallSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
intMaxX =
graphics.GraphicsDevice.Viewport.Width - ball.Width;
intMinX = 0;
intMaxY =
graphics.GraphicsDevice.Viewport.Height - bar.Height - ball.Height;
intMinY = 0;
// Check for bounce.
if (BallPosition.XMaxX)
{
BallSpeed.X *= -1;
BallPosition.X = MaxX;
}
elseif (BallPosition.XMinX)
{
BallSpeed.X *= -1;
BallPosition.X = MinX;
}
if (BallPosition.YMaxY)
{
if ((BallPosition.XBarPosition.X - 10) & (BallPosition.XBarPosition.X + 160 + 10))
{
BallSpeed.Y *= -1;
BallPosition.Y = MaxY;
score++;
}
else
gamestate = GS.Over;
}
elseif (BallPosition.YMinY)
{
BallSpeed.Y *= -1;
BallPosition.Y = MinY;
}
break;
//NO.2
caseGS.Help:
if (((ks.IsKeyDown(Keys.Escape) == true) & (pks.IsKeyDown(Keys.Escape) == false)) ||
((gs.IsButtonDown(Buttons.B) == true) & (pgs.IsButtonDown(Buttons.B) == false)))
gamestate = GS.Menu;
break;
caseGS.Over:
if (((ks.IsKeyDown(Keys.Escape) == true) & (pks.IsKeyDown(Keys.Escape) == false)) ||
((gs.IsButtonDown(Buttons.B) == true) & (pgs.IsButtonDown(Buttons.B) == false)))
{
gamestate = GS.Menu;
BallPosition = Vector2.Zero;
BarPosition = newVector2(320, 580);
score = 0;
}
break;
caseGS.Quit:
this.Exit();
break;
}
pks = ks;
pgs = gs;
base.Update(gameTime);
}
///<summary>
///This is called when the game should draw itself.
///</summary>
///param name="gameTime">Provides a snapshot of timing values./param
protectedoverridevoid Draw(GameTimegameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
switch (gamestate)
{
caseGS.Menu:
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(mark, newVector2(100, 100), newColor(0, (byte)(count * 12), (byte)(count * 12)));
if (CurrentSelect == 0)
spriteBatch.DrawString(font1, "開始遊戲", newVector2(350, 400), Color.Yellow, 0, newVector2(0, 0), 1.1f, SpriteEffects.None, 0.5f);
else
spriteBatch.DrawString(font1, "開始遊戲", newVector2(350, 400), Color.White, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
if (CurrentSelect == 1)
spriteBatch.DrawString(font1, "使用方法", newVector2(350, 450), Color.Yellow, 0, newVector2(0, 0), 1.1f, SpriteEffects.None, 0.5f);
else
spriteBatch.DrawString(font1, "使用方法", newVector2(350, 450), Color.White, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
if (CurrentSelect == 2)
spriteBatch.DrawString(font1, "結束程式", newVector2(350, 500), Color.Yellow, 0, newVector2(0, 0), 1.1f, SpriteEffects.None, 0.5f);
else
spriteBatch.DrawString(font1, "結束程式", newVector2(350, 500), Color.White, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.End();
break;
//NO.1
caseGS.Play:
graphics.GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.FrontToBack,
BlendState.Additive);
spriteBatch.Draw(back, newVector2(0, 0), Color.White);
spriteBatch.Draw(ball, BallPosition, Color.White);
spriteBatch.Draw(bar, BarPosition, Color.White);
spriteBatch.DrawString(font1, "分數:" + score, newVector2(500, 0), Color.Pink);
spriteBatch.End();
break;
//NO.2
caseGS.Help:
graphics.GraphicsDevice.Clear(Color.Orange);
spriteBatch.Begin();
spriteBatch.DrawString(font1, "使用方法", newVector2(0, 0), Color.Yellow, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "Windows環境下", newVector2(0, 100), Color.Yellow, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "使用鍵盤上下左右鍵", newVector2(0, 150), Color.Green, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "Enter鍵(確認)、Esc鍵(取消)來控制", newVector2(0, 200), Color.Green, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "XBox 360環境下", newVector2(0, 300), Color.Yellow, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "使用搖桿上下左右鍵", newVector2(0, 350), Color.Green, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(font1, "A鍵(確認)、B鍵(取消)來控制", newVector2(0, 400), Color.Green, 0, newVector2(0, 0), 1f, SpriteEffects.None, 0.5f);
spriteBatch.End();
break;
//NO.3
caseGS.Over:
graphics.GraphicsDevice.Clear(Color.Red);
spriteBatch.Begin();
spriteBatch.DrawString(font1, "Game Over", newVector2(0, 0), Color.White, 0, newVector2(0, 0), 2f, SpriteEffects.None, 0.5f);
spriteBatch.End();
break;
}
//NO.4
base.Draw(gameTime);
}
}
}