xxxxxxxxxx
// * is wall
// - is a space
// @ is food
// P is a pill
var pacimg, wallsandpills;
var frame = 0;
var numframes = 8;
var numlayers = 2;
var mazefile;
var maze;
var pacx = 1;
var pacy = 4;
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 isize = 40; // pixel width and height of one square
var moveSpeed = 5; // how often do we move
var score = 0;
var powertimer = 0; // how long do i have to eat ghosts
var numlives = 5; // how many lives do i have?
var food, powerpill;
function preload() {
pacimg = loadImage('face40x8x2.png');
wallsandpills = loadImage('objects40x4.png');
mazefile = loadStrings('maze1.txt');
// powerpill = loadSound('powerpill.mp3');
//food = loadSound('food.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
startGame();
textSize(40);
}
function draw() {
background(255);
text("SCORE: " + score, width*0.6, height*0.2);
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; // brick
if(maze[i][j]=='@') otype = 2; // space
if(maze[i][j]=='P') otype = 2; // end
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);
if(frameCount%3==0) {
frame = (frame+1)%numframes;
}
if(frameCount%moveSpeed==0) {
if(powertimer>0) powertimer--;
{
if(powertimer==0)
{
}
else
{
Ay = 8;
Ax = 11;
By = 9;
Bx = 11;
Cy = 8;
Cx = 12;
Dy = 9;
Dx = 12;
score+=25;
}
}
// 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]!='*'&&maze[pacy+yincr][pacx+xincr]!='%')
{
maze[pacy][pacx]='%';
pacx+=xincr;
pacy+=yincr;
xincr = 0;
yincr = 0;
if(pacx==13&&pacy==1) { // replace with the right coordinates
if(score==41)
{
pacx = 1;
pacy = 4;
startGame();
}
//else {
// YOU LOST
// startGame();
}
}
if(maze[pacy][pacx]=='@') // eat the food
{
maze[pacy][pacx]='-'; // replace with blank
score++;
//food.play();
//food.jump(random(food.duration()));
}
else {
//food.stop();
}
if(maze[pacy][pacx]=='P') // eat the power pill
{
maze[pacy][pacx]='-'; // replace with blank
score+=10;
powertimer = 60;
powerpill.play();
}
}
}
function keyPressed() {
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;
}
if(key=='r') { // reset
startGame();
}
}
function startGame()
{
maze = [];
for(let i = 0;i<mazefile.length;i++)
{
let m = mazefile[i].split('');
maze.push(m);
}
pacx = 1;
pacy = 4;
score = 0;
}