xxxxxxxxxx
222
// Create an array that holds the location of each of the food dots
let img;
let x = 170; // Pac Man's x-coordinate
let y = 30; // Pac Man's y-coordinate
let personalSpace = 21;
let ghostPersonalSpace = 20;
let diam = 24;
let ghostArray = new Array(4);
let xLoc = new Array(100); // // 3C
let yLoc = new Array(100); // // 3C
function preload() {
img = loadImage("maze.png");
}
function setup() {
createCanvas(500, 550);
eatenFalse();
foodLocations(); // // 2
ghostArray[0] = new Ghost(xLoc[16], yLoc[16], 98, 3, 153);
ghostArray[1] = new Ghost(xLoc[16], yLoc[16], 252, 244, 5);
ghostArray[2] = new Ghost(xLoc[16], yLoc[16], 245, 34, 227);
ghostArray[3] = new Ghost(xLoc[16], yLoc[16], 106, 163, 132);
}
// ------------------ //
// DRAW LOOP //
// ------------------ //
function draw() {
background(255);
image(img, 0, 0);
updateEaten();
drawFood();
updateScore();
drawScore();
movePacMan();
drawPacMan();
for (let i = 0; i < ghostArray.length; i++) {
ghostArray[i].displayGhost();
ghostArray[i].moveGhost();
}
}
// -------------------- //
// DRAW PACMAN //
// -------------------- //
function drawPacMan() {
noStroke();
fill(255, 255, 0);
ellipse(x, y, diam, diam);
}
function foodLocations() {
// Horizontal (x-ccordinates)
for (let a = 0; a < 10; a++) {
// Vertical (y-coordinates)
for (let b = 0; b < 10; b++) {
let k = a * 10 + b; // k == 0, then 1, then 2, all the way to 100.
xLoc[k] = 33 + a * 48; // This is where the food is in the x-coordinates
yLoc[k] = 33 + b * 48; // This is where the food is in the y-coordinates
print(k + " is at (" + xLoc[k] + ", " + yLoc[k] +")");
}
}
}
// -------------- //
// GHOST //
// -------------- //
class Ghost {
// --------- GHOST: Attributes ---------//
constructor(tempX, tempY, tempR, tempG, tempB) {
this.x = tempX;
this.y = tempY;
this.r = tempR;
this.g = tempG;
this.b = tempB;
this.speed = 2;
this.L = 0;
this.R = 1;
this.U = 2;
this.D = 3;
this.cameFrom = this.U;
this.onIntersection = true;
}
//--------- GHOST: Display ---------//
displayGhost() {
rectMode(CENTER);
fill(this.r, this.g, this.b);
rect(this.x, this.y, 20, 20);
}
//-------- GHOST: Move ---------//
moveGhost() {
//check if i'm on an intersection
for (let i = 0; i < 100; i++){
if (this.x == xLoc[i] && this.y == yLoc[i]) {
this.onIntersection = true;
}
}
if (this.onIntersection == true) {
//if yes, pick a new direction
let canMoveR = false;
let canMoveL = false;
let canMoveU = false;
let canMoveD = false;
//MOVE RIGHT
let mR = get(this.x + ghostPersonalSpace, this.y);
if (mR[0] > 200) {
// wall! don't move
} else {
canMoveR = true;
}
//MOVE LEFT
let mL = get(this.x - ghostPersonalSpace, this.y);
if (mL[0] > 200) {
// wall! don't move
} else {
canMoveL = true;
}
//MOVE UP
let mU = get(this.x, this.y - ghostPersonalSpace);
if (mU[0] > 200) {
// wall! don't move
} else {
canMoveU = true;
}
//MOVE DOWN
let mD = get(this.x, this.y + ghostPersonalSpace);
if (mD[0] > 200) {
// wall! don't move
} else {
canMoveD = true;
}
this.dir = int(random(4));
if (this.dir == 0){
if (canMoveL){
this.x += this.speed * -1;
this.cameFrom = this.L;
}
}
if (this.dir == 1){
if (canMoveR){
this.x += this.speed;
this.cameFrom = this.R;
}
}
if (this.dir == 2){
if (canMoveU){
this.y += this.speed * -1;
this.cameFrom = this.U;
}
}
if (this.dir == 3){
if (canMoveD){
this.y += this.speed;
this.cameFrom = this.D;
}
}
this.onIntersection = false;
}
//if no, keep going
}
}