//
與上面得到小人的碰撞點相似
Rectangle blockRectangle
=
new
Rectangle((
int
)blockPositions[i].X, (
int
)blockPositions[i].Y,
blockTexture.Width, blockTexture.Height);
//
若是小人與其中某一個碰撞紋理的碰撞點產生碰撞
if
(personRectangle.Intersects(blockRectangle))
personHit = true; //這時的碰撞檢測將生效
///
<summary>
///
This is the main type for your game
///
</summary>
public
class
Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D personTexture;
//
小人紋理圖像
Texture2D blockTexture;
//
撞擊點紋理圖像
//
Person
Vector2 personPosition;
//
小人2D座標
const
int
PersonMoveSpeed
=
5
;
//
小人移動速度
//
Blocks
List
<
Vector2
>
blockPositions
=
new
List
<
Vector2
>
();
//
撞擊點集合
float
BlockSpawnProbability
=
0.1f
;
//
控制撞擊點的降低個數
const
int
BlockFallSpeed
=
10
;
//
撞擊點降低速度
Random random
=
new
Random();
bool
personHit
=
false
;
//
是否產生碰撞
Rectangle safeBounds;
const
float
SafeAreaPortion
=
0.05f
;
Viewport viewport;
//
得到當前窗口的寬高對象
public
Game1()
{
graphics
=
new
GraphicsDeviceManager(
this
);
Content.RootDirectory
=
"
Content
"
;
//
Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime
=
TimeSpan.FromTicks(
333333
);
}
///
<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()
{
//
TODO: Add your initialization logic here
viewport
=
graphics.GraphicsDevice.Viewport;
safeBounds
=
new
Rectangle(
(
int
)(viewport.Width
*
SafeAreaPortion),
//
40
(
int
)(viewport.Height
*
SafeAreaPortion),
//
24
(
int
)(viewport.Width
*
(
1
-
2
*
SafeAreaPortion)),
//
720
(
int
)(viewport.Height
*
(
1
-
2
*
SafeAreaPortion)));
//
432
//
Start the player in the center along the bottom of the screen
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()
{
spriteBatch
=
new
SpriteBatch(GraphicsDevice);
blockTexture
=
Content.Load
<
Texture2D
>
(
"
Block
"
);
personTexture
=
Content.Load
<
Texture2D
>
(
"
Person
"
);
personPosition.X
=
(safeBounds.Width
-
personTexture.Width)
/
2
;
//
小人的座標縱向在屏幕居中
personPosition.Y
=
safeBounds.Height
-
personTexture.Height;
//
小人的座標豎向在屏幕底下居中
//
TODO: use this.Content to load your game content here
}
///
<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)
{
//
Allows the game to exit
if
(GamePad.GetState(PlayerIndex.One).Buttons.Back
==
ButtonState.Pressed)
this
.Exit();
TouchCollection touch
=
TouchPanel.GetState();
if
(touch.Count
>
0
)
{
if
(touch[
0
].Position.X
>
viewport.Width
/
2
)
{
personPosition.X
+=
PersonMoveSpeed;
}
else
{
personPosition.X
-=
PersonMoveSpeed;
}
}
double
ran
=
random.NextDouble();
//
TODO: Add your update logic here
if
(ran
<
BlockSpawnProbability)
//
隨機循環Double 型若是隨機的double 小於0.01f這樣作是避免產生的撞擊 點太多
{
float
x
=
(
float
)random.NextDouble()
*
//
在屏幕隨機出現
(graphics.GraphicsDevice.Viewport.Width
-
blockTexture.Width);
//
爲了避免超出屏幕
Vector2 v
=
new
Vector2(x,
1
);
blockPositions.Add(v);
}
//
得到小人的磁撞大小和碰撞的地點
//
公式爲:獲得小人所在的x、y 地點,而後在那個x、y點的區域高寬
Rectangle personRectangle
=
new
Rectangle((
int
)personPosition.X, (
int
)personPosition.Y,
personTexture.Width, personTexture.Height);
//
Update each block
personHit
=
false
;
//
默認爲不碰撞狀態
for
(
int
i
=
0
; i
<
blockPositions.Count; i
++
)
//
循環全部在集合裏面的碰撞紋理
{
//
使裏面的全部元素所有降低
blockPositions[i]
=
new
Vector2(blockPositions[i].X,
//
X座標不變
blockPositions[i].Y
+
BlockFallSpeed);
//
豎座標爲當前的Y座標每次加上降低的速度常量
//
與上面得到小人的碰撞點相似
Rectangle blockRectangle
=
new
Rectangle((
int
)blockPositions[i].X, (
int
)blockPositions[i].Y,
blockTexture.Width, blockTexture.Height);
//
若是小人與其中某一個碰撞紋理的碰撞點產生碰撞
if
(personRectangle.Intersects(blockRectangle))
personHit
=
true
;
//
這時的碰撞檢測將生效
//
若是有碰撞紋理超屏幕
if
(blockPositions[i].Y
>
graphics.GraphicsDevice.Viewport.Height)
{
//
從集合裏面移出該碰撞點
blockPositions.RemoveAt(i);
//
當刪除了其中一個碰撞點時,下一個碰撞點的索引將跟當前移除的碰撞點是同樣的,因此循環變量自動減1
i
--
;
}
}
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);
GraphicsDevice device
=
graphics.GraphicsDevice;
//
TODO: Add your drawing code here
if
(personHit)
//
當產生碰撞
{
device.Clear(Color.Red);
}
else
{
device.Clear(Color.CornflowerBlue);
}
spriteBatch.Begin();
//
Draw person
spriteBatch.Draw(personTexture, personPosition, Color.White);
//
Draw blocks
foreach
(Vector2 blockPosition
in
blockPositions)
spriteBatch.Draw(blockTexture, blockPosition, Color.White);
spriteBatch.End();
base
.Draw(gameTime);
}
}