# Moving objects: move game ghost back and forth

**URL:** <https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206>\
**Category:** Coding Questions\
**Created:** [November 5, 2018, 2:02am UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206 "2018-11-05T02:02:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ari\_y](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@ari\_y](https://discourse.processing.org/u/ari_y)\
**Post date:** [November 5, 2018, 2:02am UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/1 "2018-11-05T02:02:46Z")

</div>

I’m new to coding and I’m trying to write a program that is similar to the pacman game. I can’t figure out how to move the ghost back and forth using parameters.

here’s part of my code:

```auto
void drawRedghost(int x, int y)
{
  fill(255,0,0);
  rect(x,y,30,30);
  arc(x,y-15,30,30,radians(180),radians(360));
  triangle(x-15,y+15,x-5,y+15,x-15,y+20);
  triangle(x-5,y+15,x+5,y+15,x,y+20);
  triangle(x+5,y+15,x+15,y+15,x+15,y+20);
  //eyes
  stroke(255);
  strokeWeight(10);
  point(x+7,y-10);
  point(x-8,y-10);
  stroke(0);
  strokeWeight(5);
  point(x+6,y-10);
  point(x-7,y-10);
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [November 5, 2018, 2:08am UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/2 "2018-11-05T02:08:30Z")

</div>

### _Please format your code_ 😊

It consist on these two steps:

1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: `ctrl+t`
2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: `</>`

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and \>\> **save** \<\< the edits.

## Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [November 5, 2018, 2:11am UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/3 "2018-11-05T02:11:31Z")

</div>

Unfortunately the code you provided is not enough to help you. Can you provide a minimum amount of code showing what it can do so far?

Kf

---

<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 5, 2018, 10:08pm UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/4 "2018-11-05T22:08:11Z")

</div>



---

<div class="post-metadata">

**Author:** ![ari\_y](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@ari\_y](https://discourse.processing.org/u/ari_y)\
**Post date:** [November 5, 2018, 10:08pm UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/5 "2018-11-05T22:08:44Z")

</div>

ok sure

```auto
int pacx=500;
int pacy=240;

int LEFT = 0;
int RIGHT = 1;
int DOWN = 2;
int UP = 3;

int dir=RIGHT;

void setup()
{
  size(500,600);
  rectMode(CENTER);
  ellipseMode(CENTER);
}

void draw()
{
  background(0);
  drawPacman();
  drawRedghost(40,550);
  drawPurpleghost();
  drawTealghost();
  
  //background
  stroke(21,45,129);
  strokeWeight(2);
  line(0,95,500,95);
  line(0,105,500,105);
  line(0,195,500,195);
  line(0,205,500,205);
  line(0,295,500,295);
  line(0,305,500,305);
  line(0,395,500,395);
  line(0,405,500,405);
  line(0,495,500,495);
  line(0,505,500,505);
  
}

void drawPacman()
{
  background(0);
  
  noStroke();
  fill(255,255,0);
  
  if(dir==LEFT){
        arc(pacx,pacy,50,50,radians(45),radians(315));
    pacx++;
  }
  else if(dir==RIGHT){
      arc(pacx,pacy,50,50,radians(225),radians(495));
    pacx--;
  }
  else if(dir==DOWN){
     arc(pacx,pacy,50,50,radians(225),radians(495));
    pacy++;
  }
  else if(dir==UP){
     arc(pacx,pacy,50,50,radians(225),radians(495));
    pacy--;
  }
  
  if(pacx==-50 && dir==RIGHT)
    dir=DOWN;
  else if(pacy==450 && dir==DOWN)
    dir=LEFT;
  else if(pacy==250 && dir==UP)
    dir=RIGHT;
  else if(pacx==520 && dir==LEFT)
    dir=UP;
}

void drawRedghost(int x, int y)
{
  fill(255,0,0);
  rect(x,y,30,30);
  arc(x,y-15,30,30,radians(180),radians(360));
  triangle(x-15,y+15,x-5,y+15,x-15,y+20);
  triangle(x-5,y+15,x+5,y+15,x,y+20);
  triangle(x+5,y+15,x+15,y+15,x+15,y+20);
  //eyes
  stroke(255);
  strokeWeight(10);
  point(x+7,y-10);
  point(x-8,y-10);
  stroke(0);
  strokeWeight(5);
  point(x+6,y-10);
  point(x-7,y-10);
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 5, 2018, 10:41pm UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/6 "2018-11-05T22:41:26Z")

</div>

🍍 Your code is easy to get running, which means we can see what it does. You posted it formatted properly, and asked a question. For this you are awarded one pineapple.

Look carefully at the function that draws your ghost: `drawRedGhost()`.  
Notice that it takes two values as inputs - `x` and `y`.  
These are then added to all the positions for all the shapes that are drawn to display the ghost.  
As such, when you call drawRedGhost() with two numbers, those numbers are the position at which the ghost is drawn.

If you were to change the values that are passed to that function, then the ghost would be drawn in a different position.

You are already doing this with pacman. You now need to do it with the ghost as well.

Create two more global variables - like `pacx` and `pacy`, that will be the current position of the red ghost. Let’s call them `ghost_x_0` and `ghost_y_0`:

```auto
int ghost_x_0, ghost_y_0;

```

Since these are the current position of the ghost, use them when you call the function that draws the ghost:

```auto
  drawRedghost( ghost_x_0, ghost_y_0 );

```

Now all you need to do is add some code that changes the values in those variables. And when you do that, the ghost will also move. I suggest:

```auto
// Somewhere inside draw()...
ghost_x_0++;
ghost_y_0++;

```

See if you can get the ghost moving now! Post the code of your attempt if you need more help.

---

<div class="post-metadata">

**Author:** ![ari\_y](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@ari\_y](https://discourse.processing.org/u/ari_y)\
**Post date:** [November 5, 2018, 11:18pm UTC](https://discourse.processing.org/t/moving-objects-move-game-ghost-back-and-forth/5206/7 "2018-11-05T23:18:14Z")

</div>

It worked, thank you so much!
