Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 努力理解导师为我编写的arknoid/brickbreaker碰撞检测代码_C#_Linq_Collision Detection - Fatal编程技术网

C# 努力理解导师为我编写的arknoid/brickbreaker碰撞检测代码

C# 努力理解导师为我编写的arknoid/brickbreaker碰撞检测代码,c#,linq,collision-detection,C#,Linq,Collision Detection,我请一个三年级的学生帮我检测游戏中的碰撞,我试着理解顺序,但最后20行左右对我来说没有多大意义,有人愿意让它更干净一点吗 具体来说,我需要从关键字“Where”了解代码的含义。如能对代码进行任何改进,将不胜感激 UIElement upDownBlock = MyGrid.Children.Cast<Rectangle>().Where(i => (Grid.GetRow(i) == ballRow + vely) && (Grid.GetColumn(i) =

我请一个三年级的学生帮我检测游戏中的碰撞,我试着理解顺序,但最后20行左右对我来说没有多大意义,有人愿意让它更干净一点吗

具体来说,我需要从关键字“Where”了解代码的含义。如能对代码进行任何改进,将不胜感激

UIElement upDownBlock = MyGrid.Children.Cast<Rectangle>().Where(i => (Grid.GetRow(i) == ballRow + vely) && (Grid.GetColumn(i) == ballColumn)).FirstOrDefault();
if (upDownBlock != null)
{
    vely = -vely;
    MyGrid.Children.Remove(upDownBlock);
}
UIElement leftRightBlock = MyGrid.Children.Cast<Rectangle>().Where(i => (Grid.GetColumn(i) == ballColumn + velx) && (Grid.GetRow(i) == ballRow)).FirstOrDefault();
if (leftRightBlock != null)
{
    velx = -velx;
    MyGrid.Children.Remove(leftRightBlock);
}
UIElement upDownBlock=MyGrid.Children.Cast().Where(i=>(Grid.GetRow(i)=ballRow+vely)和&(Grid.GetColumn(i)=ballColumn)).FirstOrDefault();
if(上下行块!=null)
{
vely=-vely;
MyGrid.Children.Remove(upDownBlock);
}
UIElement leftRightBlock=MyGrid.Children.Cast()。其中(i=>(Grid.GetColumn(i)=ballColumn+velx)和&(Grid.GetRow(i)==ballRow)).FirstOrDefault();
if(leftRightBlock!=null)
{
velx=-velx;
MyGrid.Children.Remove(leftRightBlock);
}

任何形式的帮助都将不胜感激,游戏是在网格上创建的

正如我在评论中所说,你们需要学习linq,你们的导师需要使用更好的命名。这似乎更容易理解吗

Rectangle upDownBlock = MyGrid.Children.Cast<Rectangle>()
                          .Where(rect => (Grid.GetRow(rect) == ballRow + vely) && 
                                         (Grid.GetColumn(rect) == ballColumn))
                         .FirstOrDefault();
if (upDownBlock != null)
{
    vely = -vely;
    MyGrid.Children.Remove((UIElement)upDownBlock);
}
Rectangle upDownBlock=MyGrid.Children.Cast()
其中(rect=>(Grid.GetRow(rect)=ballRow+vely)和
(Grid.GetColumn(rect)=ballColumn))
.FirstOrDefault();
if(上下行块!=null)
{
vely=-vely;
MyGrid.Children.Remove((UIElement)upDownBlock);
}
基本上Where子句中的代码是针对各种矩形列表运行的,它的计算结果为true或false,并返回匹配的第一个矩形(计算结果为true)


一个改进是在各种作业中将“Where”替换为“FirstOrDefault”。这并没有像您所期望的那样提高性能,延迟求值意味着Where子句只对请求的项求值,而FirstOrDefault在找到第一个匹配项时将停止要求它求值。但它在前面更清楚地表明,您不关心整个矩形列表,只关心第一个与您的条件(即谓词)匹配的矩形。

您需要帮助理解哪些具体内容?UIElement条件主要需要从关键字“where”中具体理解代码的含义如果您需要学习LINQ,那么对代码所做的改进将是值得赞赏的。你的导师需要学习好的命名规则。基本上,where子句中的代码是针对各种矩形列表运行的,THERE子句中的代码验证为true或false,并且返回匹配的第一个矩形,如果没有匹配的矩形,则返回null。今年第二年我们关注的理论方面纯粹是基础知识和介绍。你给出的描述帮了大忙!!!现在一切都有意义了,谢谢!更好,我会尽快和LINQ合作,谢谢help@Sihle:供您将来作为导师时参考。在工作中,工作代码几乎胜过可读代码,因为指导相反的代码是正确的。最好是让可读的代码稍微出错,而不是让不可读的代码绝对正确。伪代码甚至不会假装工作,但它可以显示需要做什么。我现在就开始练习,因为这只是我的第一年,我现在知道其他人阅读不可读的代码是什么样子了