xxxxxxxxxx
// * is wall
// - is a space
// @ is food
// P is a pill
var pacimg, wallsandpills, ghostimg;
var frame = 0;
var numframes = 4;
var numlayers = 2;
var mazefile;
var maze;
var pacx = 10;
var pacy = 5;
var Ay = 8;
var Ax = 11;
var By = 9;
var Bx = 11;
var Cy = 8;
var Cx = 12;
var Dy = 9;
var Dx = 12;
var yincr = 0;
var xincr = 0;
var isplaying = false;
var isize = 40; // pixel width and height of one square
var moveSpeed = 6; // how often do we move
var score = 0;
var powertimer = 0; // how long do i have to eat ghosts
var numlives = 3; // how many lives do i have?
function preload() {
pacimg = loadImage('face40x8x2.png');
wallsandpills = loadImage('objects40x4.png');
mazefile = loadStrings('maze1.txt');
ghostimg = loadImage('ghosts40x8x2.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
maze = [];
for(let i = 0;i<mazefile.length;i++)
{
let m = mazefile[i].split('');
maze.push(m);
}
//console.log(maze);
textSize(40);
}
function draw() {
if(!isplaying) // STARTUP SCREEN
{
background('grey');
fill(255);
textAlign(CENTER);
if(frameCount%60>30) text('USE THE ARROWS TO MOVE THE FISH. PRESS ANY KEY TO BEGIN', width/2, height/2);
}
if(isplaying) // MAIN GAME LOOP
{
background(100);
text("SCORE: " + score, width*0.7, height*0.2);
text("POWER: " + powertimer, width*0.7, height*0.4);
text("LIVES: " + numlives, width*0.7, height*0.6);
var otype = 3;
for(let i = 0;i<maze.length;i++)
{
for(let j = 0;j<maze[i].length;j++)
{
x = j*isize;
y = i*isize;
if(maze[i][j]=='*') otype = 0; // brick
if(maze[i][j]=='-') otype = 1; // space
if(maze[i][j]=='@') otype = 2; // food
if(maze[i][j]=='P') otype = 3; // power pill
blend(wallsandpills, otype*40, 0, isize, isize, x, y, isize, isize, BLEND);
}
}
// draw pacman
blend(pacimg, frame*isize, (powertimer>0)*isize, isize, isize, pacx*isize, pacy*isize, isize, isize, BLEND);
// draw ghosts
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);
//text("SCORE: " + score, width*0.6, height*0.5);
//text("POWER: " + powertimer, width*0.6, height*0.7);
//text("LIVES: " + numlives, width*0.6, height*0.9);
}
if(frameCount%3==0) {
frame = (frame+1)%numframes;
}
if(frameCount%moveSpeed==0) {
if(powertimer>0) powertimer--;
// are we touching?
if((pacx==Ax&&pacy==Ay)||(pacx==Bx&&pacy==By)||(pacx==Cx&&pacy==Cy)||(pacx==Dx&&pacy==Dy))
{
if(powertimer==0)
{
numlives--;
if(numlives==0) numlives = 3;
pacx = 10;
pacy = 5;
xincr = 0;
yincr = 0;
}
else
{
Ay = 8;
Ax = 11;
By = 9;
Bx = 11;
Cy = 8;
Cx = 12;
Dy = 9;
Dx = 12;
score+=25;
}
}
// STEP 1: animate pacman
// teleport:
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]!='*')
{
pacx+=xincr;
pacy+=yincr;
}
if(maze[pacy][pacx]=='@') // eat the food
{
maze[pacy][pacx]='-'; // replace with blank
score++;
}
if(maze[pacy][pacx]=='P') // eat the power pill
{
maze[pacy][pacx]='-'; // replace with blank
score+=10;
powertimer = 30;
}
// STEP 2: animate the ghosts
// teleport:
let Axincr = 0;
let Ayincr = 0;
let Amove = random([0,1,2,3,4]);
if(Amove==0) Axincr = -1;
if(Amove==1) Axincr = 1;
if(Amove==2) Ayincr = -1;
if(Amove==3) 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]!='*')
{
Ax+=Axincr;
Ay+=Ayincr;
}
// teleport:
let Bxincr = 0;
let Byincr = 0;
let Bmove = random([0,1,2,3,4]);
if(Bmove==0) Bxincr = -1;
if(Bmove==1) Bxincr = 1;
if(Bmove==2) Byincr = -1;
if(Bmove==3) 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]!='*')
{
Bx+=Bxincr;
By+=Byincr;
}
// teleport:
let Cxincr = 0;
let Cyincr = 0;
let Cmove = random([0,1,2,3,4]);
if(Cmove==0) Cxincr = -1;
if(Cmove==1) Cxincr = 1;
if(Cmove==2) Cyincr = -1;
if(Cmove==3) 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]!='*')
{
Cx+=Cxincr;
Cy+=Cyincr;
}
// teleport:
let Dxincr = 0;
let Dyincr = 0;
let Dmove = random([0,1,2,3,4]);
if(Dmove==0) Dxincr = -1;
if(Dmove==1) Dxincr = 1;
if(Dmove==2) Dyincr = -1;
if(Dmove==3) 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]!='*')
{
Dx+=Dxincr;
Dy+=Dyincr;
}
}
}
function keyPressed() {
isplaying= true
if(keyCode==UP_ARROW) {
xincr = 0;
yincr = -1;
}
if(keyCode==DOWN_ARROW) {
xincr = 0;
yincr = 1;
}
if(keyCode==LEFT_ARROW) {
xincr = -1;
yincr = 0;
}
if(keyCode==RIGHT_ARROW) {
xincr = 1;
yincr = 0;
}
}