Delphi 我可以缩短这个冲突代码吗?

Delphi 我可以缩短这个冲突代码吗?,delphi,lazarus,pacman,Delphi,Lazarus,Pacman,我正在试着做“吃豆人”,我做了一些碰撞: 所以这只是顶墙的碰撞,但它是不必要的。 这是一张图片 所以我的游戏是如何运作的,我有很多形状,当吃豆人走路时会跳起来,所以有点过时。这不是一个完美的动作,但这不是重点 代码是这样的:如果吃豆人(形状1)和其他形状在同一个位置,那么你就不能上去 所以我想知道的是。是否有一种方法可以通过数组或某种方式缩短代码 // COLLISION IF (Shape1.Top=Shape30.Top) or (Shape1.Top=Shape41.T

我正在试着做“吃豆人”,我做了一些碰撞:

所以这只是顶墙的碰撞,但它是不必要的。 这是一张图片 所以我的游戏是如何运作的,我有很多形状,当吃豆人走路时会跳起来,所以有点过时。这不是一个完美的动作,但这不是重点

代码是这样的:如果吃豆人(形状1)和其他形状在同一个位置,那么你就不能上去

所以我想知道的是。是否有一种方法可以通过数组或某种方式缩短代码

  // COLLISION
    IF (Shape1.Top=Shape30.Top)
    or (Shape1.Top=Shape41.Top) and (Shape1.Left=Shape41.Left)
    or (Shape1.Top=Shape42.Top) and (Shape1.Left=Shape42.Left)
    or (Shape1.Top=Shape56.Top) and (Shape1.Left=Shape56.Left)
    or (Shape1.Top=Shape57.Top) and (Shape1.Left=Shape57.Left)
    or (Shape1.Top=Shape58.Top) and (Shape1.Left=Shape58.Left)
    or (Shape1.Top=Shape72.Top) and (Shape1.Left=Shape72.Left)
    or (Shape1.Top=Shape74.Top) and (Shape1.Left=Shape74.Left)
    or (Shape1.Top=Shape76.Top) and (Shape1.Left=Shape76.Left)
    or (Shape1.Top=Shape118.Top) and (Shape1.Left=Shape118.Left)
    or (Shape1.Top=Shape120.Top) and (Shape1.Left=Shape120.Left)
    or (Shape1.Top=Shape122.Top) and (Shape1.Left=Shape122.Left)
    or (Shape1.Top=Shape143.Top) and (Shape1.Left=Shape143.Left)
    or (Shape1.Top=Shape144.Top) and (Shape1.Left=Shape144.Left)
    or (Shape1.Top=Shape160.Top) and (Shape1.Left=Shape160.Left)
    or (Shape1.Top=Shape162.Top) and (Shape1.Left=Shape162.Left)
    or (Shape1.Top=Shape175.Top) and (Shape1.Left=Shape175.Left)
    or (Shape1.Top=Shape176.Top) and (Shape1.Left=Shape176.Left)
    or (Shape1.Top=Shape192.Top) and (Shape1.Left=Shape192.Left)
    or (Shape1.Top=Shape194.Top) and (Shape1.Left=Shape194.Left)
    or (Shape1.Top=Shape167.Top) and (Shape1.Left=Shape167.Left)
    or (Shape1.Top=Shape168.Top) and (Shape1.Left=Shape168.Left)
    or (Shape1.Top=Shape182.Top) and (Shape1.Left=Shape182.Left)
    or (Shape1.Top=Shape184.Top) and (Shape1.Left=Shape184.Left)
    or (Shape1.Top=Shape186.Top) and (Shape1.Left=Shape186.Left)
    or (Shape1.Top=Shape235.Top) and (Shape1.Left=Shape235.Left)
    or (Shape1.Top=Shape267.Top) and (Shape1.Left=Shape267.Left)
    or (Shape1.Top=Shape289.Top) and (Shape1.Left=Shape289.Left)
    or (Shape1.Top=Shape219.Top) and (Shape1.Left=Shape219.Left)
    or (Shape1.Top=Shape220.Top) and (Shape1.Left=Shape220.Left)
    or (Shape1.Top=Shape221.Top) and (Shape1.Left=Shape221.Left)
    or (Shape1.Top=Shape269.Top) and (Shape1.Left=Shape269.Left)
    or (Shape1.Top=Shape273.Top) and (Shape1.Left=Shape273.Left)
    or (Shape1.Top=Shape283.Top) and (Shape1.Left=Shape283.Left)
    or (Shape1.Top=Shape284.Top) and (Shape1.Left=Shape284.Left)
    or (Shape1.Top=Shape230.Top) and (Shape1.Left=Shape230.Left)
    or (Shape1.Top=Shape231.Top) and (Shape1.Left=Shape231.Left)
    or (Shape1.Top=Shape232.Top) and (Shape1.Left=Shape232.Left)
    or (Shape1.Top=Shape246.Top) and (Shape1.Left=Shape246.Left)
    or (Shape1.Top=Shape248.Top) and (Shape1.Left=Shape248.Left)
    or (Shape1.Top=Shape250.Top) and (Shape1.Left=Shape250.Left)
    or (Shape1.Top=Shape262.Top) and (Shape1.Left=Shape262.Left)
    or (Shape1.Top=Shape263.Top) and (Shape1.Left=Shape263.Left)
    or (Shape1.Top=Shape264.Top) and (Shape1.Left=Shape264.Left)
    or (Shape1.Top=Shape278.Top) and (Shape1.Left=Shape278.Left)
    or (Shape1.Top=Shape280.Top) and (Shape1.Left=Shape280.Left)
    or (Shape1.Top=Shape282.Top) and (Shape1.Left=Shape282.Left)
    or (Shape1.Top=Shape294.Top) and (Shape1.Left=Shape294.Left)
    or (Shape1.Top=Shape303.Top) and (Shape1.Left=Shape303.Left)
    or (Shape1.Top=Shape320.Top) and (Shape1.Left=Shape320.Left)
    or (Shape1.Top=Shape322.Top) and (Shape1.Left=Shape322.Left)
    or (Shape1.Top=Shape335.Top) and (Shape1.Left=Shape335.Left)
    or (Shape1.Top=Shape336.Top) and (Shape1.Left=Shape336.Left)
    or (Shape1.Top=Shape309.Top) and (Shape1.Left=Shape309.Left)
    or (Shape1.Top=Shape339.Top) and (Shape1.Left=Shape339.Left)
    or (Shape1.Top=Shape357.Top) and (Shape1.Left=Shape357.Left)

    then
     begin
         Up:=false;
    end;

首先,您需要将图形与游戏本身分开。你的情况可能是

类型 TCellType=(ctEmpty、ctWall、ctPacMan、ctGhost)//等等

然后创建一个二维数组

变量 Game=TCellType的数组[1..GameWidth,1..GameHeight]

这至少有两个好处。首先,您没有视觉组件的开销。其次,碰撞检测变得更加简单,因为您只需要查看一个单元(您正试图去的单元)。因此,如果吃豆人的位置是(PacX,PacY),你只需要看一下手机游戏[PacX-1,PacY],如果你向左移动

如果确实要移动,请重新绘制要移动的单元格


我把细节留给你

对于图形应用程序来说,这无疑是完全错误的方法。您应该自己绘制所有图形,而不是使用控件,这些线应该是虚拟的。你走得越远,只会导致更多类似这样的噩梦。是的,你可以使用数组来缩短代码。是的,对演员使用控制是个坏主意。好的,谢谢。你知道怎么缩短它吗?是的,我是一个初学者,我知道的不多。这里有一个我以前问过的关于碰撞检测的示例问题。你可以用这里学到的要点来指导你。你也可以研究另一个为delphi写了一个吃豆人的业余爱好者,然后把它放到谷歌代码上。我救出了消息来源并把它们推到这里作为参考。了解如何下载并安装一个名为TortoiseHG的版本控制工具,然后转到命令提示符,输入命令
hgclone,这对您有好处https://bitbucket.org/wpostma/pacmanx
获取此示例代码并稍微研究一下。这段代码是一个叫Gummo的人写的,不是我写的。