### XNA-2D 打磚塊遊戲（一）

XNA-2D 打磚塊遊戲（一）

• 遊戲畫面為800X600。
• 物件有磚塊、球、板子、能力膠囊。
• 磚塊大小都固定，但是可以設定被打幾下才會消失。
• 使用像素碰撞偵測

01 public override void Collided(ICollidable obj, Vector2 collidePoint) {
02             if (obj is Ball) {
03                 Ball b = obj as Ball;
04                 float d1 = Vector2.DistanceSquared(position, b.position);
05                 float d2 = Vector2.DistanceSquared(position + velocity, b.position + b.velocity);
06                 if (d1 > d2) {
07                     //指向我的法線
08                     Vector2 normal = new Vector2(collideRectangle.Center.X - collidePoint.X, collideRectangle.Center.Y - collidePoint.Y);
09                     normal.Normalize();
10                     Vector2 momentum = normal * (Vector2.Dot(b.Velocity, normal) + Vector2.Dot(Velocity, -normal));
11
12                     velocity += momentum;
13                     b.velocity -= momentum;
14                 }

15             }
else if (obj is Brick) {
16                 Brick b = obj as Brick;
17                 Vector2 center = new Vector2(collideRectangle.Center.X, collideRectangle.Center.Y);
18                 float d1 = Vector2.DistanceSquared(center, collidePoint);
19                 float d2 = Vector2.DistanceSquared(center + velocity, collidePoint);
20                 if (d1 > d2) {
21                     Vector2 normal = center - collidePoint;
22                     normal.Normalize();
23                     Vector2 momentum = normal * (Vector2.Dot(Velocity, -normal) * 2);
24
25                     velocity += momentum;
26                 }

27             }

28         }

2D 打磚塊遊戲（一）.rar