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

[c-sharp]  view plain copy print ?
  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

[c-sharp]  view plain copy print ?
  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

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值