We are about to switch to a new forum software. Until then we have removed the registration on this forum.
//Variables
//user variables
int paxX=246, paxY=246;
//Gost variables
float gostX=245, gostY=245;
//Food Variables
int FoodX[] = new int [2401];
int FoodY[] = new int [2401];
//array used for eaten food
float foodeat[] = new float [2401];
//if food is in wall
int foodinwall;
//Level
int level=1;
//Lives
int lives=3;
//Score
int score=0;
int cupcake=0;
//Images
PImage Maz; //Maze image
PImage Food; //Food image
PImage Gost; //Gost image
//Setup for game
void setup(){
//setup,basic
frameRate(60);
size(500,500);
noStroke();
smooth();
//Load images
Images();
//Sets food/starting locations
Food();
}//eND sETUP
void draw(){
background(0);
Gost();
Maz();
pax_user();
}
//Food
void Food(){
for (int f=0; f < 2401; f++) {
//determination if food was eaten
if (Foodeat[f]==0){
//If food has not been eaten, draw
image (Food, FoodX[f], FoodY[f]);
foodinwall=get (FoodX[f]-1, FoodY[f]+1);
//text(""+ foodinwall,100,200);
if (foodinwall == -1.6777216E7){
foodeat[f]=1;
cupcake=cupcake+1;
}
if(paxX==FoodX[f] && FoodY[f]==paxY){
//if pax eats food
foodeat[f]=1;
//add score
cupcake=cupcake+1;
score=score+1;
}
}
}
}//End Food
void foodsetup(){
foodX[0]=6;
foodY[0]=6;
//other pieces of food
for (int i=1; i < 2401; i++){
foodX[i]=foodX[i-1]+10;
foodY[i]=foodY[i-1];
//if at edge of screen, reset and layer is put down.
if(foodX[i] ==496){
foodX[i]=6;
foodY[i]=foodY
}
void Gost() {
//gost is left of pax
if(paxX>GostX) {
//moves towards pax at specific speed
GostX=GostX+level-3;
}
//gost right of pax
(paxX<GostX) {
//moves towards pax at spesific speed
GostX=GostX-level+3;
}
//gost above pax
(paxY>GostY){
//moves toward pax at specific speed
GostY=GostY+level-3;
}
//gost be low pax
(paxY<GostY){
//moves towards pax at specific speed
GostY=GostY-level+3;
}
//Draw Gost
image(Gost,GostX,GostY);
//End gost
}
void images(){
//Images load
//Maze
Maz = loadImage("Maz.jpg");
//Food
Food = loadImage("Food.jpg");
void Maz(){
//Draw Maze
if(playmaze==1){
image(Maz,0,0);
}
}//End maz
void pax_user(){
//user movement
if (keyPressed) {
if (key==CODED) {
if (keyCode==RIGHT){
paxX=paxY+5;
}
if(keyCode==LEFT){
paxX=paxY-5;
}
if(keyCode==DOWN){
paxY=paxX+5;
}
if(keyCode==UP){
paxY=paxX-5;
}
}
}
//Drawimage
image(pax,paxX,paxY);
}//end user draw
Answers
Start by trying to run your own code. You're immediately missing a semicolon.
Fix that, try running it again. You have braces that are unmatched.
Fix those, try running it again. You are missing if keywords.
Fix those, try running it again. The function Images() is not images().
Fix that, try running it again. Variable Foodeat is not foodeat.
Fix that, try running it again. Variable foodX is not FoodX, and foodY is not FoodY. Go with foodX and foodY throughout.
Fix that, try running it again. Now you're trying to assign a float (foodY[i]) the value of an float[] (foodY). Not sure how to fix that one for you. Removing offending line.
That line removed, try running it again. Variable GostX and GostY are not gostX and gostY. Go with gostY and gostX.
Fix that, try running it again. Variable playmaze is undefined. Removing reference to it.
Fix that, try running it again. Variable pax is undefined. Replacing trying to draw that image with a yellow ellipse.
... Here ends the syntax errors. Debugging this further is probably not possible without your images. Please post them.
OR...
Scrap this completely and start from scratch. Start with a blank sketch and gradually add working elements to it. Did you know that PacMan actually uses an underlying grid to determine where the walls are?
Now, how do you determine where the walls are? You'll need some kind of an array to track that information.
Random walls were good enough for this step.
Now we want a character! Since the character will probably need many variables to track things about it, we don't want to use a bunch of different variables - we might get confused. Instead, we'll write the character as it's own object. Here, I've only included things like it's position and direction, encapsulated into a Player class
I also made the walls less random.
Now I can fix things just inside the player class to make my player work better. For one, I want it to change direction. I also fix which number means which direction, so I can rotate it properly. I also add the chomping animation, which the player class can handle on it's own (so I don't need to fret about doing it in draw()!).
Notice that I also added the keyPressed() function, and instead of doing all the logic to deal with the player interacting with the key press there, I just let my Player class deal with it.
If you've been following along at home, you may have noticed that there's a pretty major issue with our player - it waltzes right over walls like they aren't there! We can easily fix this inside the Player class by remembering where the player was, and then checking if the new position after it moves is inside a wall - if it is, we just stay where we remembered we were.
Also added is a temporary wall to test this on.
Now a pretty important aspect we're missing are the dots to eat. Since each grid square can either be a wall, a dot, a super dot, or empty space, we'll need to adjust our is_wall array to track the state for each square. Thus, we'll need to change it from being a simple 2D boolean array to one of integers. Let's use 0 for empty space, 1 for wall, 2 for dot, and 3 for super dot. We should also rename it to grid to denote that it's no longer a simple boolean array, but an actual structure that stores more information.
Man, all this messy grid logic sure is getting in the way of my simple and neat setup and draw functions. Also, these dots are kind of an eye sore. O_o
Tea break! Step back and think about it. The grid logic is a mess. It's cluttering up my setup() and draw() functions. What can I do about that?
Think hard.
Starts with an encap...
Ends with a ...sulate it into it's own class!
That's right! Encapsulate the grid into it's own class!
Give yourself a gold star if you saw this step coming.
Also notice how I now have my Player asking the grid itself if there is wall at the position it is trying to move into. This makes more sense, because the grid is what knows where there is wall and where there is not wall. Eating dots comes next.
Eating dots is accomplish quite easily. When the player moves (and it might not!), it tells the grid to eat any dots at that position. The grid can check that position to see if the dot was a super dot, and if it was, it can tell the player to be super (as well as eating the dot at that position in any case). This then also requires that the player remember when it is super, as well as adding a countdown variable that will be used to determine how long the player is super for. For now, being super means you get to be a red player for 100 frames.
Looks good! Everything BOO so far is BOO! working, so now BOOOO!!! we're free to move on to adding BOOOOOOO!!!! something new! Hmmm... I wonder BOO!!!! what we BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!! I AM A GHOST!!!! YOU SHOULD ADD MEEEEEEE!!!!! BOOOOOOOO!!!!! Ok, ok! We'll add some ghosts! Sheesh!
So here are some ghosts. BOOO!!! THESE GHOSTS SUUUUCK!!!!! WEEEE WANT REEAAAAAAL GHOOOOOSTS!!!!! BOOO!!! Shut up, ghosts! This is only a first draft!
Anyway, ghosts come in different colors, so each ghost has a type variable that corresponds to a color in the new ghost_colors array. Each ghost also has some simple logic, which includes a target position, and it moves towards that target position. It would have been a real pain to try to write these ghosts as lists of variables!
Now there are several different things that need fixing at this point. For a start, the ghost's movement logic. They need to obey the walls too. Plus they really should move along the grid, and only decide which way to go when they need to change their minds. Plus different types of ghosts need different target locations. And if we're going for full pacmanness here, they need to have a chase, flee, and corner modes. Plus a ghost house. Oh, and some level design. And the fruit! We can't forget the bonus fruit, score, and lives! Plus a start screen, and a check that all the dots are eaten! BOOO!!! THAT'S A LOT OF WORK!!! BOOO!!!! WE'RE THE GHOSTS!!!! BOOOooOoOoOOOO!!!
And the feature I work on first is... None of those! BOO!!! YOU NEED TO IMPROVE THE GHOOOOOOOSTS! BOOOOOOOOO!!!!
Instead I decided to make some changes to the grid. BOO!!!
We'll need walls for the ghosts to not go through, right? BOO! OK! FINE! BUT BOOOOOO!!!!
Trapping the player in a wall is a good way to get him to stop moving so you can fill the world with dots as you like. I'm going to assume that anyone following along at home has drawn their own level and moved it into the data folder, so that this level is loaded when the sketch starts from now on.
BOO!! THIS IN'T HELPING US KILL THE PLAYER ANY BETTER! YOU NEED TO IMPROVE OUR MOVEMENT LOGIC! AND MAKE OUR TOUCH DEADLY! BOOO!!!
Good point, ghosts. Let's add that now.
Well, that's a step in the right direction. And at this point I've run out of steam. Maybe I'll pick this up later. BOOO! Quiet! OK!
BOOO! WE'RE ACTUALLY GHOSTS!! AND WE MOVE LIKE REAL GHOSTS!!!
WHY AREN'T WE DEADLY TO THE PLAYER YET!?!?!? BOOOOOOOOOOOO!!!!
BOOOO!!! OH MY GOSH WE'RE FREAKING PRETTY!!! WOOO HOOO!!!
WHAT!? WE'RE STILL NOT DEADLY!? BOO! BOOOO!!! BOOOOOOOOO!!!!
I love all the help your giving me but its quiet confusing.... @ u @"
DEADLY GHOSTS! BOO!!!
NOW QUIT PLAYING GAMES AND GET BACK TO WORK / LEARNING TO PROGRAM!
uhh mind if u put all these together its so confusing
Hello,
I made some changes in the sketch , it has now the order of a classical processing sketch, classes at the end etc..
The Maze file
Also, when no file "world.dat" is found, a maze is done randomly now.
you can edit this by mouse. And save with "s" !
especially, edit it so that there are walls on screen borders, otherwise crashes.
unfortunately I am not sure how to post world.dat here because it's a byte file.
Concepts
There are complex concepts here, arrays and class, 2D arrays inside a class...
You need to know the concepts here first to understand the sketch.
I suggest, you read the tutorials on arrays, on objects and on 2D arrays.
https://www.processing.org/tutorials/
Then come back with questions.
Chrisir
new version that saves grid as Strings
the grid:
Perhaps continue this on a new thread? The OP posted this in 2015; this is on topic but no longer relevant to their question.
continued here
https://forum.processing.org/two/discussion/25207/pac-man-assistance-needed#latest