正文
Winform打砖块游戏制作step by step第7节---碰撞检测
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
一 引子
为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持。
预备知识,无GDI画图基础的童鞋请先阅读一篇文章让你彻底弄懂WinForm GDI 编程基本原理
二 本节内容---碰撞检测
1.界面截图如下:
三 碰撞检测
目前碰撞检测主要发生在小球和挡板之间,以及小球和砖块之间,小球和砖块碰撞后,砖块需要被消去,主要代码如下:
//碰撞检测
public void Hit()
{ //砖块与小球碰撞
for (int i = ; i < m_bricks.m_Rects.Count; i++)
{
if (m_ball.Rect.IntersectsWith(m_bricks.m_Rects[i].m_Rect))
{ m_bricks.m_Rects.Remove(m_bricks.m_Rects[i]); m_ball.SpeedX = -m_ball.SpeedX;
m_ball.SpeedY = -m_ball.SpeedY; //得分
//score += new Random().Next(50, 80) + 100;
}
} //小球与挡板碰撞
if (m_ball.XPos + m_ball.Rect.Width - >= m_board.XPos && m_ball.XPos <= m_board.XPos + m_board.Rect.Width - )
{
if (m_ball.YPos >= m_board.YPos - m_ball.Rect.Height - )
{
switch (m_board.Direction)
{
case BoardDirection.Left:
{
m_ball.SpeedX = -(new Random().Next(, ));//创造一些摩擦力的感觉
}
break;
case BoardDirection.Right:
{
m_ball.SpeedX = (new Random().Next(, ));//创造一些摩擦力的感觉
}
break;
default:
break;
}
m_ball.SpeedY = (new Random().Next(, ));
}
}
}
四 代码下载
本节代码下载
作者:宋波
出处:http://www.cnblogs.com/ice-river/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!
出处:http://www.cnblogs.com/ice-river/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!