var board = [[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[3, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3],
[3, 1, 3, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3, 3, 1, 3],
[3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3],
[3, 1, 3, 1, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 3, 1, 3],
[3, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 0, 3, 1, 1, 1, 1, 1, 1, 3],
[3, 1, 3, 1, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 3, 1, 3],
[3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3],
[3, 1, 3, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3, 3, 1, 3],
[3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 3],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]];
var boardRows, boardCols;
var startLocationG = [ [5, 8], [5, 9], [5, 10], [5,11] ];
var inScatterMode = true;
boardRows = board.length;
boardCols = board[0].length;
if (boardRows < boardCols) {
size = width / boardCols;
size = height / boardRows;
startLocation = findStartingLoc();
startingDir = getStartingDir();
pac = new Pacman(startLocation[1] * size + size/2, startLocation[0] * size + size/2, startingDir);
ghosts.push(new Ghost(startLocationG[0][1] * size + size/2, startLocationG[0][0] * size + size/2, "Red"));
ghosts.push(new Ghost(startLocationG[1][1] * size + size/2, startLocationG[1][0] * size + size/2, "Pink"));
ghosts.push(new Ghost(startLocationG[2][1] * size + size/2, startLocationG[2][0] * size + size/2, "Orange"));
ghosts.push(new Ghost(startLocationG[3][1] * size + size/2, startLocationG[3][0] * size + size/2, "Blue"));
timeTillSwitch = random(5000, 15000);
if (boardRows < boardCols) {
translate(0, size * boardCols/4);
translate(size * boardRows/4, 0);
for (let i = 0; i < foods.length; i++) {
for (let i = 0; i < powers.length; i++) {
for (let i = 0; i < boxes.length; i++) {
for (let i = 0; i < ghosts.length; i++) {
if (ghosts[i].collidePac()) {
if (ghosts[i].isScared) {
ghosts[i].isScared = false;
ghosts[i].pos.set(startLocationG[1][1] * size + size/2, startLocationG[1][0] * size + size/2);
ghosts[i].pos.set(startLocationG[2][1] * size + size/2, startLocationG[2][0] * size + size/2);
ghosts[i].vel.set(ghosts[i].speed, 0);
ghosts[i].vel.set(-ghosts[i].speed, 0);
for (let i = foods.length - 1; i >= 0; i--) {
if (foods[i].isEaten()) {
for (let i = powers.length - 1; i >= 0; i--) {
if (powers[i].isEaten()) {
for (let i = 0; i < ghosts.length; i++) {
ghosts[i].isScared = true;
ghosts[i].scaredST = millis();
textAlign(CENTER, CENTER);
translate(boardCols*size/2, boardRows * size/2);
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
foods.push(new Food(j * w + w/2, i * w + w/2));
} else if (board[i][j] == 2) {
powers.push(new Power(j * w + w/2, i * w + w/2, w/1.5));
} else if (board[i][j] == 3) {
boxes.push(new Box(j, i));
function updateGameBasedOnTime() {
if (totalTime - switchST > timeTillSwitch) {
if (totalTime - escapeST > 5500) {
for (let i = 0; i < ghosts.length; i++) {
function switchGhostPatterns() {
for (let i = 0; i < ghosts.length; i++) {
timeTillSwitch = random(3000, 7000);
timeTillSwitch = random(10000, 25000);
for (let i = 0; i < ghosts.length; i++) {
inScatterMode = !inScatterMode;
function findStartingLoc() {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
let loc = int(random(0, possLocs.length));
function getStartingDir() {
if (board[startLocation[0]][startLocation[1]+1] == 1) {
} else if (board[startLocation[0]][startLocation[1]-1] == 1) {
} else if (board[startLocation[0]-1][startLocation[1]] == 1) {
} else if (board[startLocation[0]+1][startLocation[1]] == 1) {