0

I am trying to do pacman and I've made some Collisions:

So this is only the collision for the Top wall but its the unnecessary. Here is an image http://i.epvpimg.com/XsXvg.png So how my game works is, that I have a lot of shapes where the pacman jumps if he walks so its a little bit vintage. It is not an perfect movement but thats not the point.

The code says: If the pacman(Shape1) is in the same spot as the other shapes then you cant go up.

So what I want to know is. Is there a way I can shorten the code with arrays or somehow.

  // 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;
JensG
  • 13,148
  • 4
  • 45
  • 55
  • 3
    This is certainly the entirely wrong approach to a graphic application. You should be doing all your drawing yourself, not using controls - and these lines should be virtual. You will only lead into more nightmares like this the further you go. – Jerry Dodge Dec 17 '15 at 19:48
  • 3
    Yes, you can shorten this code using arrays. And yes, using controls for your actors is a bad idea. – David Heffernan Dec 17 '15 at 19:55
  • Ok Thanks. Do you know a way how I can shorten it? And yeah iam a beginner and I dont know more than that. – HDGoldenGaming Dec 17 '15 at 19:58
  • I like to turn my actors into data classes that have no painter code, and write painter classes that paint my data objects, if they are visible and their co-ordinates are somewhere on or very near the viewport. I believe you should get rid of your shapes, and just have a paintbox and paint stuff on it, for "learning how to write painters". Then for sprite collision, you want to do better than just matching top/left. You might want to go get a 2d game engine (google it) – Warren P Dec 17 '15 at 19:59
  • What do you want us to do for you? Re-write your code? Re-design it? Teach you what an array is? Frankly SO is not for tutorials on basic things like what an array is. – David Heffernan Dec 17 '15 at 20:08
  • 1
    Here's a sample question I've asked in the past on collision detection. You can use the points learned here to guide you. http://stackoverflow.com/questions/15308077/delphi-custom-animation-collision-detection – Jerry Dodge Dec 17 '15 at 20:10
  • 2
    You can also study another amateur guy who wrote a pacman for delphi, and put it up on googlecode. I rescued the sources and shoved them here for reference. It would do you good to learn how to download and install a version control tool called TortoiseHG, and then go to a command prompt ant type the command `hg clone https://bitbucket.org/wpostma/pacmanx` to fetch this sample code and study it a bit. This code is by someone named Gummo, I didn't write it. – Warren P Dec 17 '15 at 20:18
  • PS,welcome to stack overflow. Nobody hates you, but you should try to put some effort into learning basic things. if you don't know what an array or a list is, stackoverflow is not the right place for you, yet. – Warren P Dec 17 '15 at 20:20

1 Answers1

0

Firstly you need to separate graphics from the game itself. So in your case maybe something like

Type TCellType = (ctEmpty, ctWall, ctPacMan, ctGhost); //etc.

then create a 2 dimensional array

var Game = array[ 1..GameWidth, 1..GameHeight] of TCellType;

This has (at least) two benefits. Firstly you don't have the overheads of visual components. Secondly the collision detect becomes much simpler in that you only need to look at one cell (the one where you are trying to go ). So if pacman position is (PacX, PacY) you only need to look at the cell Game[PacX-1, PacY] if you are moving to the left.

If you do move, then redraw the cells you are moving to and from.

I'll leave the details to you.

Dsm
  • 5,870
  • 20
  • 24