Moving objects: move game ghost back and forth

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:

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);
}

Please format your code :blush:

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

2 Likes

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

ok sure

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);
}

:pineapple: 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:

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:

  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:

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

3 Likes

It worked, thank you so much!