We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys, I've to programm a game for a college project: I decided for Pacman and everything works except the ghosts and a general score. It d be awesome if someone could help! Thanks in advance.
class Pacman {
int x;
int y;
int durchmesser;
int state = 0;
float animTimerSec = 0;
float ANIM_CYCLE = .5;
int score;
int life;
PFont font;
boolean [] [] food = new boolean [20][15];
Pacman(int d) {
durchmesser = d;
x = d/2;
y = d/2;
}
////start over the game every time pacman touches the X and Y walls
void startOver () {
if ((pacman.x > width) || (pacman.x < 0) ||
(pacman.y > height) || (pacman.y < 0)) {
// pacman = new pacman();
life-=1;
score=0;
background (0);
textFont(font);
println("You died");
fill (0, 255, 255);
text("You died", height/2, width/2);
}
if (life==0)
{
println ("Game over");
background (0);
textFont(font);
fill (0, 255, 255);
text("GAME OVER ", height/2, width/2);
}
else if (score==100)
{
println ("YOU WON!!!");
background (0);
textFont(font);
fill (0, 255, 255);
text("YOU WON!!!", height/2, width/2);
}
}
void draw() {
noStroke();
fill(#FFE624);
animTimerSec += 1.0/frameRate;
if (animTimerSec >= ANIM_CYCLE) {
animTimerSec = 0;
state++;
state = state % 2;
}
if (state == 0) {
ellipse(x, y, durchmesser, durchmesser);
}
else if (state == 1) {
arc(x, y, durchmesser, durchmesser, radians(30),
radians(330));
}
fill(0);
ellipse(x+durchmesser/6, y-durchmesser/3, 8, 8);
}
void moveLeft() {
x -= durchmesser;
x = constrain(x, durchmesser/2, width-durchmesser/2);
}
void moveRight() {
x += durchmesser;
x = constrain(x, durchmesser/2, width-durchmesser/2);
}
void moveUp() {
y -= durchmesser;
y = constrain(y, durchmesser/2, height-durchmesser/2);
}
void moveDown() {
y += durchmesser;
y = constrain(y, durchmesser/2, height-durchmesser/2);
}
}
// Tastatursteuerung
void keyPressed() {
if (keyCode == LEFT) {
pacman.moveLeft();
}
if (keyCode == RIGHT) {
pacman.moveRight();
}
if (keyCode == UP) {
pacman.moveUp();
}
if (keyCode == DOWN) {
pacman.moveDown();
}
}
class Punkt {
int x;
int y;
int wert = 5;
boolean alive = true;
Punkt(int px, int py) {
x = px;
y = py;
}
void draw() {
// zeichne nur, wenn noch nicht gefressen
if (alive) {
noStroke();
fill(#FF52F4);
ellipse(x, y, 10, 10);
}
}
}
int score = 0;
int cellSize = 40;
Pacman pacman = new Pacman(cellSize);
Punkt[] punkte = new Punkt[30];
void setup() {
size(400, 400);
for (int i = 0; i < punkte.length; i++) {
int x = (int)random(0, 10) * cellSize + cellSize/2;
int y = (int)random(0, 10) * cellSize + cellSize/2;
punkte[i] = new Punkt(x, y);
}
}
// Gibt true zurück, wenn Punkt erfolgreich gegessen
boolean tryToEat(Punkt p) {
// Punkt ist bereits gegessen
if (!p.alive) {
return false;
}
// Wenn auf gleichem Feld: Punkt essen
if (dist(pacman.x, pacman.y, p.x, p.y) <= 2/2) {
p.alive = false;
return true; // Erfolgreich gegessen!
}
return false;
}
void draw() {
background(0);
pacman.draw();
for (int i = 0; i < punkte.length; i++) {
boolean eaten = tryToEat(punkte[i]);
if (eaten) {
score += punkte[i].wert;
}
punkte[i].draw();
}
}
class Geist {
int x;
int y;
int durchmesser;
Geist(int gx, int gy, int d) {
x = gx;
y = gy;
durchmesser = d;
}
void draw() {
noStroke();
fill(#79D3FF);
// Körper
ellipse(x, y, durchmesser, durchmesser);
rectMode(CORNER);
rect(x-durchmesser/2, y, durchmesser, durchmesser/2);
// Augen
rectMode(CENTER);
fill(0);
rect(x-durchmesser/4, y, durchmesser/4, durchmesser/8);
rect(x+durchmesser/4, y, durchmesser/4, durchmesser/8);
}
// Im Gegensatz zu Pacman wollen wir einen
// zufälligen Bewegungsschritt ausführen
void move() {
int richtung = (int)random(0, 4);
if (richtung == 0) {
moveLeft();
}
if (richtung == 1) {
moveRight();
}
if (richtung == 2) {
moveUp();
}
if (richtung == 3) {
moveDown();
}
}
void moveLeft() {
x -= durchmesser;
x = constrain(x, durchmesser/2, width-durchmesser/2);
}
void moveRight() {
x += durchmesser;
x = constrain(x, durchmesser/2, width-durchmesser/2);
}
void moveUp() {
y -= durchmesser;
y = constrain(y, durchmesser/2, height-durchmesser/2);
}
void moveDown() {
y += durchmesser;
y = constrain(y, durchmesser/2, height-durchmesser/2);
}
}
// neue globale Variablen
Geist[] geister = new Geist[3];
int leben = 3;
void setup() {
size(10*cellSize, 10*cellSize);
for (int i = 0; i < punkte.length; i++) {
int x = (int)random(0, 10) * cellSize + cellSize/2;
int y = (int)random(0, 10) * cellSize + cellSize/2;
punkte[i] = new Punkt(x, y);
}
for (int i = 0; i < geister.length; i++) {
int x = (int)random(3, 10) * cellSize + cellSize/2;
int y = (int)random(3, 10) * cellSize + cellSize/2;
geister[i] = new Geist(x, y, cellSize);
}
}
void manageGeister() {
// Alle 50 Zyklen die Geister bewegen
boolean move = false;
if (timer <= 0) {
move = true;
timer = 50;
}
timer--;
for (int i = 0; i < geister.length; i++) {
if (move) {
geister[i].move();
}
geister[i].draw();
boolean eaten = geister[i].tryToEat(pacman);
if (eaten) {
leben--;
// Pacman wieder oben links erscheinen lassen
int x = cellSize/2;
int y = cellSize/2;
pacman.x = x;
pacman.y = y;
}
}
}
Answers
I have no idea why it looks that shitty .... sry for that
ok, go back, edit your post format your code
2 empty lines before and after the code
select entire code and hit ctrl-o
before you post you can hit ctrl-t in processing (not in the forum) to get some auto-format
Thank you for the tip!
Was funktioniert nicht mit den Geistern?
you must be joking...
the code doesn't even run
this is homework
just unify the setups you got and draw the ghosts in draw()
don't be lazy....
well, it works until I declare the ghosts actually, that's the problem and I've no idea what I've done wrong. This one here should work, but it's without ghosts or anything:
???
well , if you look at the two setup() you had, now a lot of them is missing - you didn't unify them at all
also, a lot of draw() is missing
https://forum.processing.org/two/discussion/11156/pac-man-game-not-completed-asking-for-assistance#latest
I've done Pacman to death. Go see. It even has ghosts.
it's homework...
Let him work with his code....
Actually I have no idea how to implement all this stuff as I'm really new to Processing and used a tutorial to do this, but it didn't work out. That's why it would be awesome if someone could just change the code so that it works... I would really appreciate it!
Yeah, that's not going to happen. Did you even check out the link I posted? I basically outlined the whole development process for Pacman. You need to follow along better, and actually learn how to program and know what the code does.
It didn't work out??
Post your entire code and tell us where you are struggeling....
@TfGuy44 Yeah, I obviously checked out the link you posted, but it didn't work. Well, I know that one, thing is that as I'm new to Processing I'm really limited in what I can do and what not, but if I see a code I usually know what the code does. It's just hard for me, to figure stuff out. I'm actually not really talented in programming, but as I mentioned earlier it's a college project so I'm at least trying
Und was funktioniert denn nun nicht?
Your code is very flat. All the indenting is the same.
Press Ctrl + t to automatically format it and fix the indenting.
You get the resulting code:
If you try to run this, you get an error - there are two functions called setup()!
I think you need to re-evaluate where you are putting {curly brackets} ?
Interestingly enough, you have a perfectly fine working sketch in the top half:
AH! HA HA HA HA HA!
Tell me, what do you think the function tryToEat() does?
To remind you, here it is:
Specifically, look at what the parameter to this function is. It's a Punkt - a point. This function "tries to eat a point". Well, what it actually tried to do is to set this point's alive property to false, so that it is no longer drawn. This happens when pacman is in the same place as a point, making it look like pacman has eaten the point.
Here's how you're trying to use this function later, in the new code:
You're trying to get the geisters (Ghosts) to eat pacman! But pacman is a Pacman, not a Punkt (Point)!
You saw a function called "tryToEat()", assumed that if you called that function for each ghost and passed in Pacman, then the ghosts would just move towards and try to eat pacman!
Basically, you tried to take a shortcut without understanding how the sketch worked at all.
Maybe you should rename that function "killAPunkt()", to avoid confusion.
After you do that, you can probably figure out that trying to tell a ghost to killAPunkt(pacman) isn't going to work.
:))
But seriously, you should learn how to develop software yourself. Try following along with the code I posted in the thread I linked to. Make sure you can understand why I change what I change.