add walls, movement

This commit is contained in:
hihoman23 2026-07-02 11:57:06 +02:00
parent a5e0b2d32a
commit e759c265f0
5 changed files with 36 additions and 4 deletions

Binary file not shown.

View File

@ -19,9 +19,25 @@ public class Pacman {
/*---------------Methoden------*/ /*---------------Methoden------*/
public void keyPressed(int keyCode) { public void keyPressed(int keyCode) {
if (keyCode == game.LEFT) { int oldX = x; int oldY = y;
if (keyCode == game.RIGHT) {
x += game.cellSize; x += game.cellSize;
} }
if (keyCode == game.LEFT) {
x -= game.cellSize;
}
x = (x + (10 * game.cellSize)) % (10 * game.cellSize);
if (keyCode == game.UP) {
y -= game.cellSize;
}
if (keyCode == game.DOWN) {
y += game.cellSize;
}
y = (y + (10 * game.cellSize)) % (10 * game.cellSize);
if (!game.wantMove(oldX/game.cellSize, oldY/game.cellSize, x/game.cellSize, y/game.cellSize)) {
x = oldX;
y = oldY;
}
} }
public void draw() { public void draw() {

Binary file not shown.

View File

@ -10,8 +10,8 @@ comment3.params=
comment3.target=void\ draw() comment3.target=void\ draw()
comment4.params= comment4.params=
comment4.target=void\ keyPressed() comment4.target=void\ keyPressed()
comment5.params= comment5.params=ox\ oy\ nx\ ny
comment5.target=boolean\ wantMove() comment5.target=boolean\ wantMove(int,\ int,\ int,\ int)
comment6.params= comment6.params=
comment6.target=void\ populateWalls() comment6.target=void\ populateWalls()
numComments=7 numComments=7

View File

@ -38,7 +38,23 @@ public class Game extends PApplet{
pacman.keyPressed(keyCode); pacman.keyPressed(keyCode);
} }
public boolean wantMove() { public boolean wantMove(int ox, int oy, int nx, int ny) {
System.out.println(","+ox+","+ oy+","+ nx+","+ ny);
if (ox == nx && oy > ny) {
int a = oy;
oy = ny;
ny = a;
} else if(ox > nx) {
int a = ox;
ox = nx;
nx = a;
}
for (int i = 0; i < wall.length; i++) {
int[] w = wall[i];
if (w[0] == ox && w[1] == oy && w[2] == nx && w[3] == ny) {
return false;
}
}
return true; return true;
} }