Pacman/game.java
2026-07-09 12:50:55 +02:00

199 lines
4.6 KiB
Java

/**
* Klasse game.
*
* @author
*/
import java.util.*;
import processing.core.PApplet;
public class Game extends PApplet{
/*---------------Attribute-----*/
int cellSize = 40;
Render render;
public Pacman pacman;
boolean pressed = false;
int kCode;
int gameFrame = 0;
int[][] wall;
Ghost[] ghosts;
/*---------------Konstruktor---*/
public Game() {
String[] processingArgs = {"MySketch"};
PApplet.runSketch(processingArgs, this);
pacman = new Pacman(this);
ghosts = new Ghost[4];
for (int i = 0; i < 4; i++) {
ghosts[i] = new Ghost(this, cellSize / 2 + cellSize * (i + 4), 300);
}
render = new Render(this);
populateWalls();
}
/*---------------Methoden------*/
public void settings() {
size(11 * cellSize, 11 * cellSize);
}
public void draw() {
gameFrame ++;
if (pressed && gameFrame % 15 == 0) pacman.keyPressed(kCode);
if (gameFrame % 30 == 0) {
for (int i = 0; i < 4; i++) {
ghosts[i].move();
}
}
background(0);
for (int i = 0; i < 4; i++) {
ghosts[i].draw();
}
pacman.draw();
for (int i = 0; i < wall.length; i++) {
render.drawWall(wall[i]);
}
}
public void keyPressed() {
pressed = true;
kCode = keyCode;
}
public void keyReleased() {
pressed = false;
}
public boolean wantMove(int ox, int oy, int nx, int 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;
}
public void populateWalls(){
wall = new int[][]{
{0, 4, 0, 5},
{4,0,5,0},
{0, 5, 0, 6},
{10,4,10,5},
{10,5,10,6},
{-1,0,0,0},
{-1,1,0,1},
{1,-1,1,0},
{0,-1,0,0},
{2,-1,2,0},
{3,-1,3,0},
{4,-1,4,0},
{5,0,6,0},
{6,-1,6,0},
{7,-1,7,0},
{8,-1,8,0},
{9,-1,9,0},
{10,-1,10,0},
{10,0,11,0},
{10,1,11,1},
{10,2,11,2},
{10,3,11,3},
{10,4,11,4},
{10,6,11,6},
{10,7,11,7},
{10,8,11,8},
{10,9,11,9},
{10,10,11,10},
{10,10,10,11},
{9,10,9,11},
{8,10,8,11},
{7,10,7,11},
{6,10,6,11},
{5,10,6,10},
{4,10,5,10},
{4,10,4,11},
{3,10,3,11},
{2,10,2,11},
{1,10,1,11},
{0,10,0,11},
{-1,10,0,10},
{-1,9,0,9},
{-1,8,0,8},
{-1,7,0,7},
{-1,6,0,6},
{-1,4,0,4},
{-1,3,0,3},
{-1,2,0,2},
{4,5,5,5},
{5,5,5,6},
{5,5,6,5},
{2,4,2,5},
{3,4,3,5},
{7,4,7,5},
{8,4,8,5},
{2,5,2,6},
{3,5,3,6},
{7,5,7,6},
{8,5,8,6},
{4,3,5,3},
{4,2,5,2},
{5,3,6,3},
{5,2,6,2},
{4,7,5,7},
{5,7,6,7},
{4,8,5,8},
{5,8,6,8},
{8,8,8,9},
{7,8,8,8},
{8,8,9,8},
{2,8,2,9},
{1,8,2,8},
{2,8,3,8},
{8,2,9,2},
{8,1,8,2},
{7,2,8,2},
{1,2,2,2},
{2,2,3,2},
{1,6,1,7},
{2,6,2,7},
{3,6,3,7},
{6,1,7,1},
{6,2,7,2},
{6,3,7,3},
{3,7,4,7},
{3,8,4,8},
{3,9,4,9},
{7,3,7,4},
{8,3,8,4},
{9,3,9,4},
{1,0,1,1},
{2,0,2,1},
{3,0,3,1},
{0,1,1,1},
{0,2,1,2},
{0,3,1,3},
{3,3,3,4},
{3,3,4,3},
{1,3,1,4},
{3,1,4,1},
{0,7,1,7},
{0,8,1,8},
{0,9,1,9},
{7,0,7,1},
{8,0,8,1},
{9,0,9,1},
};
}
}