We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am making pacman and i used an image(its the pacman maze) as background and i made a pathway out of lines, is there a way to make the pacman object follow the path of lines?
//Here is the code in my void setup and draw tab
Pacman pac;
PImage img;
PImage startscreen;
float x = 250.0;
float y = 250.0;
int direction = 1;
int direction2 = 0;
int radius = 15;
void setup()
{
background(0);
size(723, 800);
pac = new Pacman(100, 200);
}
void draw()
{
// startscreen = loadImage("startscreen2.jpg");
img = loadImage("psb2.jpg");
stroke(0);
image(img, 0, 0);
pac.display();
render();
stroke(255);
strokeWeight(3.0);
line(40, 40, 40, 225);//pathway
line(40, 225, 165, 225);//pathway
line(40, 40, 325, 40);//pathway
line(165, 40, 165, 685);
line(165, 685, 40, 685);
line(40, 685, 40, 755);
line(40, 755, 690, 755);
line(690, 755, 690, 685);
line(690, 685, 560, 685);
line(560, 685, 560, 40);
line(400, 40, 690, 40);
line(400, 40, 400, 145);
line(690, 40, 690, 145);
line(690, 145, 40, 145);
line(325, 40, 325, 145);
line(245, 145, 245, 225);
line(245, 225, 320, 225);
line(320, 225, 320, 300);
line(250, 300, 480, 300);
line(250, 300, 250, 375);
line(480, 300, 480, 375);
line(480, 375, 800, 375);
line(250, 375, 0, 375);
line(250, 375, 250, 525);
line(40, 525, 320, 525);
line(40, 525, 40, 610);
line(320, 525, 320, 610);
line(320, 610, 165, 610);
line(40, 610, 90, 610);
line(90, 610, 90, 685);
line(320, 610, 560, 610);
line(475, 610, 475, 685);
line(475, 685, 405, 685);
line(405, 685, 405, 755);
line(320, 755, 320, 680);
line(320, 680, 250, 680);
line(250, 680, 250, 610);
line(400, 610, 400, 525);
line(400, 525, 680, 525);
line(680, 525, 680, 610);
line(680, 610, 630, 610);
line(630, 610, 630, 685);
line(480, 375, 480, 525);
line(480, 455, 250, 455);
line(400, 300, 400, 225);
line(400, 225, 480, 225);
line(480, 225, 480, 145);
line(690, 145, 690, 220);
line(690, 220, 560, 220);
}
void keyPressed() {//https://forum.processing.org/two/discussion/1573/spoof-of-pacman-game-for-project
if (key == CODED) {
if (keyCode == LEFT) {
x = x - 10;
direction = -1;
direction2 = 0;
} else if (keyCode == RIGHT) {
x = x + 10;
direction = 1;
direction2 = 0;
} else if (keyCode == UP) {
y = y - 10;
direction = 0;
direction2 = -1;
} else if (keyCode == DOWN) {
y = y + 10;
direction = 0;
direction2 = 1;
}
}
}
void render() {//https://forum.processing.org/two/discussion/1573/spoof-of-pacman-game-for-project
for ( int i=-1; i < 2; i++) {
for ( int j=-1; j < 2; j++) {
pushMatrix();
translate(x + (i * width), y + (j*height));
if ( direction == -1) {
rotate(PI);
}
if ( direction2 == 1) {
rotate(HALF_PI);
}
if ( direction2 == -1) {
rotate( PI + HALF_PI );
}
arc(0, 0, radius, radius, map((millis() % 500), 0, 500, 0, 0.52), map((millis() % 500), 0, 500, TWO_PI, 5.76) );
popMatrix();
// mouth movement //
}
}
}
//***here is my pacman class
public class Pacman
{
float x;
float y;
int radius = 15;
int direction = 1;
int direction2 = 0;
public Pacman(float xn, float xy)
{
x = xn;
y = xy;
}
public void display()
{
//yellow circle
ellipseMode(RADIUS);//https://forum.processing.org/two/discussion/1573/spoof-of-pacman-game-for-project
smooth();
noStroke();
fill(255, 255, 0);
}
}
public interface Game
{
public void move();
}
public class Enemies implements Game
{
public void move()
{
}
}
public abstract class Item
{
}
Any help would be great thanks!!
Answers
Can you show an example of the image? What are those lines you are calling in lines 30 to 78?
So the idea is that you input an image on your code and somehow you define the lines where pacman can move?
Kf
Sure here's a screenshot and lines 30-78 are the pathway !
yes i use the white lines as a pathway for pacman, I have looked at line intersection, my teacher said to have if pacman touches white keep moving if not pacman cant move, or use a 2D array but im just overall confused on how to do it? I was thinking of having a 2D array that stores every point where lines intersect but i don't know how and im not sure how i could do my teachers idea
@SanchezLeslie -- if you are trying to actually read pixels for navigation then:
image(myPGraphics,0,0)
to display the lines in draw(); on the screen as well, e.g. for debuggingyou have 65 intersections and 95 pathways
you could use arrays storing the coordinates of the intersections, what pathway connects to which intersection and make everything fit together
but I think looking at the pixels would be an easier route
@jeremydouglass Thank you so much for your answer!! I have a question about checking the pixels, what would i put when the pixel is not white?
@SanchezLeslie --
You can use myPGraphics.get() or myPGraphics.pixels[].
So something like this:
@jeremydouglass - so i didnt have a "move" method for pacman so i moved the render and keypressed to the pacman class cause no other object uses that but the check pixels still doesnt work and pacman will keep moving even when I am not pressing the arrow keys. I did the PImages things and as of right now i have the pathway showing so i can see if pacman is actually following the pathway.
Your checkPixels is:
@jeremydouglass I am so sorry to keep bugging but thank you for all of your help, I am one of the worst programmers in my class and I figured pacman wouldn't be too bad to make for my final project (spoke too soon ). I have one more question, if translate cant be used with Pgraphics then how can i stop the pacman from keep going?
Translate can be used with PGraphics --
pg.translate()
.I'm just saying that you using currently
translate()
changes the screen coordinates, so you can draw pacman at 0,0.However, it doesn't change the coordinates in pg! So if you use pg.get(0,0), that will get the pixel in the upper left hand corner of the buffer -- not the pixel where pacman currently is. You either need to use:
or:
ohh okay so now it can't move because if i called the the keyPressed() then it wont follow the pathway. What can i do to get it back to a white pixel?
Will this method work with slanting lines? I'm not sure of it will, since anti-aliasing may reduce the color of the pixels slightly.
@Lord_of_the_Galaxy -- you are right, at a certain point (such as working with diagonal lines, or at certain resolutions) you might need to use one or more of these approaches:
@jeremydouglass and @Lord_of_the_Galaxy thanks again! i got it to work mostly :) and now I am doing the the dots but when my pacman goes out of the game is gets stuck why is that happening?