我能以某种方式缩短这个碰撞代码吗?

时间:2015-12-17 19:44:35

标签: delphi lazarus pacman

我正在尝试做pacman并且我做了一些碰撞:

所以这只是顶墙的碰撞,但不必要。 这是一张图片http://i.epvpimg.com/XsXvg.png 所以我的游戏是如何工作的,我有很多形状,如果他走路,那么它会有点复古。这不是一个完美的运动,但这不是重点。

代码说:如果pacman(Shape1)与其他形状处于同一位置,那么你就无法上升。

所以我想知道的是。有没有办法可以用数组或某种方式缩短代码。

  // 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;

1 个答案:

答案 0 :(得分:0)

首先,您需要将图形与游戏本身分开。所以在你的情况下可能像

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

然后创建一个二维数组

无功   游戏= TCellType的数组[1..GameWidth,1..GameHeight];

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

如果您移动,则重新绘制要移动的单元格。

我会把细节留给你。