# How do I attach a rectangle to a line?

**URL:** <https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423>\
**Category:** Coding Questions\
**Created:** [November 12, 2019, 1:22pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423 "2019-11-12T13:22:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucc39](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@Lucc39](https://discourse.processing.org/u/Lucc39)\
**Post date:** [November 12, 2019, 1:22pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/1 "2019-11-12T13:22:40Z")

</div>

So, I’m trying to create Pac Man and I’m not sure on how to do so. My best idea so far was to attach the rectangle (which will be Pac Man himself, the player) to certain invisible lines, as if he is a train on a railroad. The player could only move within those lines that function as railroads.  
How could I execute such thing?  
Perhaps I shoud take another approach?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 12, 2019, 1:32pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/2 "2019-11-12T13:32:25Z")

</div>

To draw pacman itself look at pie please it allows for an open mouth.

Now, there are different ways of doing this.

But since you need to draw also the walls of the labyrinth and the ghosts I would go for a grid. Each cell can either be path, wall, ghost or pacman.

Now you can use 2d array, see tutorials.

When moving pac man increase its position in the grid. (Either say x++ or y++ but this is the index of the grid not the screen position).

When there’s a wall in front in the next cell of the grid, pac man stops

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 12, 2019, 1:36pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/3 "2019-11-12T13:36:11Z")

</div>

Yes, definetly take another approach. Although you could just measure the distance to the line to get wether he leaves the path, that is not really elegant…

A better way would be to check wether you can move there or not. For that, actually using single lines as paths might be problematic.

So just draw the walls, and check wether pacman can go „that“ way or not. If so, change direction/ continue moving, Else stop pacmans motion.

Also, you can store the intended direction and continuisly check that, like how in pacman you press down and when the turn comes, only then does he change direction.

If you intend to go for a pacman clone, go with @Chrisir ‘s suggestion. If you want the paths to be any shape, you‘d probably want to go with collission detection.

---

<div class="post-metadata">

**Author:** ![Lucc39](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@Lucc39](https://discourse.processing.org/u/Lucc39)\
**Post date:** [November 12, 2019, 1:46pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/4 "2019-11-12T13:46:27Z")

</div>

Why this definitely looks like a better solution! Thank you!

---

<div class="post-metadata">

**Author:** ![Lucc39](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@Lucc39](https://discourse.processing.org/u/Lucc39)\
**Post date:** [November 12, 2019, 1:49pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/5 "2019-11-12T13:49:48Z")

</div>

Thank you for your answer! I think I’ll try @Chrisir’s suggestion first, and if it go well I’ll try yours.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 13, 2019, 10:13am UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/6 "2019-11-13T10:13:52Z")

</div>

When you have a 2D array grid

You could have it of type int[][]

each cell could be a class Cell

It contains x,y and type (wall, path, pacman, ghost…)

you could define the x,y in a nested for loop x,y

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 22, 2019, 3:37pm UTC](https://discourse.processing.org/t/how-do-i-attach-a-rectangle-to-a-line/15423/7 "2019-11-22T15:37:25Z")

</div>

Sometimes it is useful to start with a single rail – one line, with a pacman on it that can move back and forth only on that line.

Then add dots. Then turn it into a full maze of multiple lines, with ghosts.

Just so that you know, a full pacman is more complicated than a full Breakout or a full Frogger – the maze and controls can be challenging. For past discussions of Pacman projects, see also:

[https://discourse.processing.org/search?q=pacman](https://discourse.processing.org/search?q=pacman)
