var pacimg, wallsandpills, ghostimg;
var ghosteatspac, paceatsghost, food, powerpill;
pacimg = loadImage('face40x8x2.png');
wallsandpills = loadImage('objects40x4.png');
mazefile = loadStrings('maze1.txt');
ghostimg = loadImage('ghosts40x8x2.png');
ghosteatspac = loadSound('ghosteatspac.mp3');
paceatsghost = loadSound('paceatsghost.mp3');
powerpill = loadSound('powerpill.mp3');
food = loadSound('food.mp3');
createCanvas(windowWidth, windowHeight);
for(let i = 0;i<mazefile.length;i++)
let m = mazefile[i].split('');
text("SCORE: " + score, width*0.6, height*0.2);
text("POWER: " + powertimer, width*0.6, height*0.4);
text("LIVES: " + numlives, width*0.6, height*0.6);
for(let i = 0;i<maze.length;i++)
for(let j = 0;j<maze[i].length;j++)
if(maze[i][j]=='*') otype = 0;
if(maze[i][j]=='-') otype = 1;
if(maze[i][j]=='@') otype = 2;
if(maze[i][j]=='P') otype = 3;
blend(wallsandpills, otype*40, 0, isize, isize, x, y, isize, isize, BLEND);
blend(pacimg, frame*isize, (powertimer>0)*isize, isize, isize, pacx*isize, pacy*isize, isize, isize, BLEND);
blend(ghostimg, frame*isize, (powertimer>0)*isize, isize, isize, Ax*isize, Ay*isize, isize, isize, BLEND);
blend(ghostimg, frame*isize, (powertimer>0)*isize, isize, isize, Bx*isize, By*isize, isize, isize, BLEND);
blend(ghostimg, frame*isize, (powertimer>0)*isize, isize, isize, Cx*isize, Cy*isize, isize, isize, BLEND);
blend(ghostimg, frame*isize, (powertimer>0)*isize, isize, isize, Dx*isize, Dy*isize, isize, isize, BLEND);
frame = (frame+1)%numframes;
if(frameCount%moveSpeed==0) {
if(powertimer>0) powertimer--;
if((pacx==Ax&&pacy==Ay)||(pacx==Bx&&pacy==By)||(pacx==Cx&&pacy==Cy)||(pacx==Dx&&pacy==Dy))
if(numlives==0) numlives = 5;
if(pacx+xincr>maze[0].length-1) pacx=0;
else if(pacx+xincr<0) pacx=maze[0].length-1;
else if(pacy+yincr>maze.length-1) pacy=0;
else if(pacy+yincr<0) pacy=maze.length-1;
else if(maze[pacy+yincr][pacx+xincr]!='*')
if(maze[pacy][pacx]=='@')
food.jump(random(food.duration()));
if(maze[pacy][pacx]=='P')
let Amove = random([0,1,2,3,4]);
if(Amove==0) Axincr = -1;
if(Amove==2) Ayincr = -1;
if(Ax+Axincr>maze[0].length-1) Ax=0;
else if(Ax+Axincr<0) Ax=maze[0].length-1;
else if(Ay+Ayincr>maze.length-1) Ay=0;
else if(Ay+Ayincr<0) Ay=maze.length-1;
else if(maze[Ay+Ayincr][Ax+Axincr]!='*')
let Bmove = random([0,1,2,3,4]);
if(Bmove==0) Bxincr = -1;
if(Bmove==2) Byincr = -1;
if(Bx+Bxincr>maze[0].length-1) Bx=0;
else if(Bx+Bxincr<0) Bx=maze[0].length-1;
else if(By+Byincr>maze.length-1) By=0;
else if(By+Byincr<0) By=maze.length-1;
else if(maze[By+Byincr][Bx+Bxincr]!='*')
let Cmove = random([0,1,2,3,4]);
if(Cmove==0) Cxincr = -1;
if(Cmove==2) Cyincr = -1;
if(Cx+Cxincr>maze[0].length-1) Cx=0;
else if(Cx+Cxincr<0) Cx=maze[0].length-1;
else if(Cy+Cyincr>maze.length-1) Cy=0;
else if(Cy+Cyincr<0) Cy=maze.length-1;
else if(maze[Cy+Cyincr][Cx+Cxincr]!='*')
let Dmove = random([0,1,2,3,4]);
if(Dmove==0) Dxincr = -1;
if(Dmove==2) Dyincr = -1;
if(Dx+Dxincr>maze[0].length-1) Dx=0;
else if(Dx+Dxincr<0) Dx=maze[0].length-1;
else if(Dy+Dyincr>maze.length-1) Dy=0;
else if(Dy+Dyincr<0) Dy=maze.length-1;
else if(maze[Dy+Dyincr][Dx+Dxincr]!='*')
if(keyCode==DOWN_ARROW) {
if(keyCode==LEFT_ARROW) {
if(keyCode==RIGHT_ARROW) {