c#打磚塊小遊戲之--邏輯界面層(三)

終於到了邏輯層,分析一下游戲中的邏輯關係:

小球與磚塊碰撞檢測、小球與擋板碰撞檢測、判斷遊戲結束、積分規則等……

物體碰撞檢測方法很多,其中Rectangle結構中的public bool IntersectsWith(Rectangle rect)函數便是一種方法,此方法確定兩個Rect是否相交!但我在測試過程中發現這方法並不是很準確,兩物體碰撞時並不能立即作出反應,有時會出現"靈魂附體"現象(兩物體會短時間粘貼在一起,並破壞運動規則),比如我在做小球與擋板碰撞檢測時便遇到這種情況,所以在小球與擋板的碰撞檢測只用了傳統的判斷物體相對位置這種方法,如圖:

小球x必須在1----3情形範圍內,且y值在y>=y1-y.Height範圍內,即:

x+小球.Width>=x1 &&  x<=x1+x1.Width  且  y>=y1-y.Height

當小球情形4時game over!   

這時y>=y1!

 

雖然這樣的檢測方法顯得很"傳統",但檢測的準確性還是蠻高的,如果你們有好的檢測方法不如提供一下思路,儘管這遊戲沒必要多複雜的檢測方法!

 

有了基本的邏輯關係,下面就要實現它們了,我把所有的邏輯算法、初始化畫面等都封裝在一個Controler控制類下,這樣在窗體類編程時只實例控制類即可,使窗體類與其他對象類的耦合性降低。

控制類:Controler.cs

  1. public class Controler  
  2.     {  
  3.         private Bitmap bitmap;  
  4.         private Brick brick;  
  5.         private Board board;  
  6.         private Ball ball;  
  7.         //遊戲畫面尺寸  
  8.         private int width = 0;  
  9.         private int height = 0;  
  10.         private bool isGameOver = false;  
  11.         public int sorce = 0;  
  12.           
  13.         //構造函數,初始化對象  
  14.         public Controler(int w,int h)  
  15.         {  
  16.             this.width = w;  
  17.             this.height = h;             
  18.             bitmap = new Bitmap(width, height);             
  19.             brick = new Brick();  
  20.             board = new Board(width / 2 - 45, height-18, 5);  
  21.             ball = new Ball(width / 2 - 45, height - 40, 2,3);  
  22.             brick.BrickWall();  
  23.         }  
  24.   
  25.         //初始化畫面  
  26.         public void InitGame(Graphics g)  
  27.         {  
  28.             //使用雙緩衝,減少畫面閃爍  
  29.             brick.Draw(Graphics.FromImage(bitmap)); //畫磚牆  
  30.             board.Draw(Graphics.FromImage(bitmap)); //畫擋板  
  31.             ball.Draw(Graphics.FromImage(bitmap)); //畫小球  
  32.             g.DrawImage(bitmap, 0, 0);   
  33.             g.Dispose();  
  34.         }  
  35.   
  36.         //碰撞檢測  
  37.         public void Hit()  
  38.         {  
  39.             //磚塊與小球碰撞  
  40.             for (int i = 0; i < brick.Rects.Count; i++)  
  41.             {  
  42.                 if (ball.Rect.IntersectsWith(brick.Rects[i]))  
  43.                 {  
  44.                     //刪除磚塊  
  45.                     brick.Rects.Remove(brick.Rects[i]);  
  46.                     ball.SpeedX = -ball.SpeedX;  
  47.                     ball.SpeedY = -ball.SpeedY;  
  48.                     //得分  
  49.                     sorce += new Random().Next(50, 80) + 100;  
  50.                 }               
  51.             }  
  52.             //小球與擋板碰撞  
  53.             if (ball.XPos + ball.Rect.Width - 5 >= board.XPos && ball.XPos <= board.XPos + board.Rect.Width - 5)  
  54.             {  
  55.                 if (ball.YPos >= board.YPos - ball.Rect.Height-2)  
  56.                 {  
  57.                     switch (Direction)  
  58.                     {  
  59.                         case BoardDirection.Left:  
  60.                             {  
  61.                                 ball.SpeedX = -(new Random().Next(2,5));  
  62.                             }  
  63.                             break;  
  64.                         case BoardDirection.Right:  
  65.                             {  
  66.                                 ball.SpeedX = (new Random().Next(2, 5));  
  67.                             }  
  68.                             break;  
  69.                         default:  
  70.                             break;  
  71.                     }  
  72.                     ball.SpeedY = (new Random().Next(2, 5));  
  73.                 }  
  74.             }  
  75.         }  
  76.   
  77.         //移動擋板  
  78.         public void MoveBoard()  
  79.         {  
  80.             board.Run();  
  81.         }  
  82.   
  83.         //小球運動  
  84.         public void RunBall()  
  85.         {  
  86.             ball.Run();  
  87.         }  
  88.   
  89.         //遊戲結束  
  90.         public bool IsGameOver()  
  91.         {  
  92.             if (ball.Rect.Y >= height-22)  
  93.             {  
  94.                 isGameOver = true;  
  95.             }  
  96.             return isGameOver;  
  97.         }  
  98.   
  99.         //遊戲通關  
  100.         public bool IsSuccess()  
  101.         {  
  102.             bool isSucess = false;  
  103.             //沒有磚塊  
  104.             if (brick.Rects.Count == 0)  
  105.                 isSucess = true;  
  106.             return isSucess;  
  107.         }  
  108.   
  109.         public Rectangle Board  
  110.         {  
  111.             get { return board.Rect; }  
  112.             set { board.Rect = value; }  
  113.         }  
  114.   
  115.         public BoardDirection Direction  
  116.         {  
  117.             get { return board.Direction; }  
  118.             set { board.Direction = value; }  
  119.         }  
  120.   
  121.     }  

這裏要提一下所用到的雙緩衝技術,其原理就是先在內存上畫好圖,再把圖畫在界面上,達到雙緩衝的目的,以減少畫面的閃爍,效果非常明顯。如果直接把圖畫在界面上,請務必小心你的眼睛,閃爍得太厲害了,傷眼不留情啊!

 

至此,遊戲的核心代碼都已經完成(用核心這兩字彷彿完成了一個“大工程”,而這只不過是簡單的類定義和簡單算法罷了,而且我覺得我寫的代碼有點亂,讓大家見笑了!),下面的窗體類只需實例控制類並調用其函數即可,遊戲的生命由Timer點燃,遊戲的存亡憑你們左右!

窗體類:SabBoyForm.cs

  1. public partial class SabBoy : Form  
  2.     {  
  3.         private Timer timer;  
  4.         private Timer timer_time;  
  5.         private Controler controler;  
  6.         private bool isKeyDown = false;  
  7.         //遊戲時間  
  8.         int h=0, m=0, s=0;  
  9.   
  10.         public SabBoy()  
  11.         {  
  12.             InitializeComponent();  
  13.             timer = new Timer();  
  14.             timer_time = new Timer();  
  15.             controler = new Controler(this.Width, this.Height);  
  16.   
  17.             timer.Interval = 10;  
  18.             timer.Tick += new EventHandler(timer_Tick);  
  19.             timer_time.Interval = 1000;  
  20.             timer_time.Tick += new EventHandler(timer_time_Tick);  
  21.         }  
  22.           
  23.         //初始化遊戲界面  
  24.         private void SabBoy_Paint(object sender, PaintEventArgs e)  
  25.         {  
  26.             controler.InitGame(e.Graphics);  
  27.         }  
  28.   
  29.         //遊戲驅動  
  30.         public void timer_Tick(object sender, EventArgs e)  
  31.         {  
  32.             if (!controler.IsGameOver())  
  33.             {  
  34.                 timer_time.Start();  
  35.                 if (isKeyDown)  
  36.                 {  
  37.                     controler.MoveBoard();  
  38.                 }  
  39.                 controler.RunBall();  
  40.                 controler.Hit();                 
  41.   
  42.                 controler.InitGame(this.CreateGraphics());  
  43.                 txtSorce.Text = controler.sorce.ToString();  
  44.   
  45.                 if (controler.IsSuccess())  
  46.                 {  
  47.                     this.CreateGraphics().DrawString("You Win"new Font("Comic Sans MS", 25), new SolidBrush(Color.Red),   
  48.                                                       this.Width / 2 - 100, this.Height / 2 - 50);  
  49.                     timer.Stop();  
  50.                     timer_time.Stop();  
  51.                 }  
  52.             }  
  53.             else  
  54.             {  
  55.                 this.CreateGraphics().DrawString("Game Over"new Font("Comic Sans MS", 25), new SolidBrush(Color.Snow),   
  56.                                                   this.Width / 2 - 100, this.Height / 2 - 50);  
  57.                 timer_time.Stop();  
  58.             }  
  59.         }  
  60.   
  61.         private void SabBoy_KeyDown(object sender, KeyEventArgs e)  
  62.         {  
  63.             isKeyDown = true;  
  64.             switch (e.KeyCode)  
  65.             {  
  66.                 case Keys.Left:  
  67.                     {  
  68.                         controler.Direction = BoardDirection.Left;  
  69.                         timer.Start();  
  70.                     }  
  71.                     break;  
  72.                 case Keys.Right:  
  73.                     {  
  74.                         controler.Direction = BoardDirection.Right;  
  75.                         timer.Start();  
  76.                     }  
  77.                     break;  
  78.                 default:  
  79.                     break;  
  80.             }  
  81.   
  82.         }  
  83.   
  84.         private void SabBoy_KeyUp(object sender, KeyEventArgs e)  
  85.         {  
  86.             isKeyDown = false;  
  87.         }  
  88.   
  89.         //因爲我設置窗體邊框樣式爲None(個人認爲缺省的邊框不好看),自定義了關閉按鈕  
  90.         //有時間的話再美化一下窗體,目前版本算是第一板吧,以後會更新  
  91.         private void lbClose_MouseMove(object sender, MouseEventArgs e)  
  92.         {  
  93.             lbClose.ForeColor = Color.Red;  
  94.         }  
  95.   
  96.         private void lbClose_MouseLeave(object sender, EventArgs e)  
  97.         {  
  98.             lbClose.ForeColor = Color.White;  
  99.         }  
  100.   
  101.         private void lbClose_Click(object sender, EventArgs e)  
  102.         {  
  103.             Application.Exit();  
  104.         }  
  105.   
  106.         //遊戲時間  
  107.         public void timer_time_Tick(object sender, EventArgs e)  
  108.         {  
  109.             s++;  
  110.             if (s >= 59)  
  111.             {  
  112.                 m += 1;  
  113.                 s = 0;  
  114.             }  
  115.             if (m >= 59)  
  116.             {  
  117.                 h += 1;  
  118.                 m = 0;  
  119.             }  
  120.             txtTime.Text =h.ToString("00")+":"+m.ToString("00") + ":" + s.ToString("00");  
  121.         }  
  122.        
  123.     }  

 

看下游戲運行界面,醜陋了點,將就玩玩吧!

 

本想實現打到磚塊並不是磚塊消失(這樣的玩法太普遍了),而是磚塊砸下來,這樣可玩性和難度就有點提高了,但還沒想到比較理想的實現方式,便先擱置了!

 

遊戲欠缺之處:沒有開始畫面、沒有暫停功能、只有一關……這些都是將來要完善的地方!

 

遊戲製作環境:VS2010   c#

遊戲擴展性不怎麼好,如果大家能抽點時間看看我的代碼,並指導我該怎麼改善,我感激不盡!有你們的指導必然會少走很多彎路!3Q

 

 

源碼下載:http://download.csdn.net/source/2779969

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章