xxxxxxxxxx
193
let spritesheet;
let sprites = [];
function preload() {
spritesheet = loadImage("pacman_sprites.png");
}
class ghost {
constructor() {
this.xPos = width / 2;
this.yPos = random(100, 300);
this.xSpeed = 4;
this.ySpeed = 4;
let w = int(spritesheet.width / 18.5); //0.5 for making the //image appear in full
let h = int(spritesheet.height / 18.5);
for (let y = 0; y < 19; y++) {
sprites[y] = [];
for (let x = 0; x < 19; x++) {
sprites[y][x] =
spritesheet.get(x * w, y * h, w, h);
} // iterate over rows
} // iterate over columns
}
// showsprite() {
// image(sprites[0][0], this.xPos, this.yPos);
// }
moving() {
// move the ball
if (keyCode === DOWN_ARROW) {
this.yPos = this.yPos + this.ySpeed;
image(sprites[4][17], this.xPos, this.yPos);
}
if (keyCode === UP_ARROW) {
this.yPos = this.yPos - this.ySpeed;
image(sprites[10][17], this.xPos, this.yPos);
}
if (keyCode === LEFT_ARROW) {
this.xPos = this.xPos - this.xSpeed;
image(sprites[7][17], this.xPos, this.yPos);
}
if (keyCode === RIGHT_ARROW) {
this.xPos = this.xPos + this.xSpeed;
image(sprites[1][17], this.xPos, this.yPos);
}
// this.xPos += this.xSpeed;
// this.yPos += this.ySpeed;
}
checkForCollisions() {
// check first for left and right wall
if (this.xPos <= 15 || this.xPos >= width - 15) {
this.xSpeed = -this.xSpeed;
}
// do the same for the ceiling and the floor
if (this.yPos <= 15 || this.yPos >= height - 15) {
this.ySpeed = -this.ySpeed;
}
}
draw() {
circle(this.xPos, this.yPos, 1);
}
}
class Pacman {
constructor() {
this.xPos = width / 2;
this.yPos = random(100, 300);
this.xSpeed = 4;
this.ySpeed = 4;
let w = int(spritesheet.width / 18.5); //0.5 for making the //image appear in full
let h = int(spritesheet.height / 18.5);
for (let y = 0; y < 19; y++) {
sprites[y] = [];
for (let x = 0; x < 19; x++) {
sprites[y][x] =
spritesheet.get(x * w, y * h, w, h);
} // iterate over rows
} // iterate over columns
}
// showsprite() {
// image(sprites[0][0], this.xPos, this.yPos);
// }
moving() {
// move the ball
if (keyCode === DOWN_ARROW) {
this.yPos = this.yPos + this.ySpeed;
image(sprites[4][17], this.xPos, this.yPos);
}
if (keyCode === UP_ARROW) {
this.yPos = this.yPos - this.ySpeed;
image(sprites[10][17], this.xPos, this.yPos);
}
if (keyCode === LEFT_ARROW) {
this.xPos = this.xPos - this.xSpeed;
image(sprites[7][17], this.xPos, this.yPos);
}
if (keyCode === RIGHT_ARROW) {
this.xPos = this.xPos + this.xSpeed;
image(sprites[1][17], this.xPos, this.yPos);
}
// this.xPos += this.xSpeed;
// this.yPos += this.ySpeed;
}
checkForCollisions() {
// check first for left and right wall
if (this.xPos <= 15 || this.xPos >= width - 15) {
this.xSpeed = -this.xSpeed;
}
// do the same for the ceiling and the floor
if (this.yPos <= 15 || this.yPos >= height - 15) {
this.ySpeed = -this.ySpeed;
}
}
draw() {
circle(this.xPos, this.yPos, 1);
}
}
let myPacman;
function setup() {
createCanvas(500, 500);
// let w = int(spritesheet.width / 19);
// let h = int(spritesheet.height / 20);
// for (let y = 0; y < 19; y++) {
// sprites[y] = [];
// for (let x = 0; x < 20; x++) {
// sprites[y][x] = spritesheet.get(x * w, y * h, w, h);
// } // iterate over rows
// } // iterate over columns
imageMode(CENTER);
myPacman = new Pacman();
// // display all sprites
// let w = int(spritesheet.width / 18);
// let h = int(spritesheet.height / 20);
// for (let y = 0; y < 20; y++) {
// sprites[y] = [];
// for (let x = 0; x < 19; x++) {
// sprites[y][x] =
// spritesheet.get(x * w, y * h, w, h);
// } // iterate over rows
// } // iterate over columns
// for (y = 0; y< 20; y++) {
// for (x = 0 ; x < 19; x++){
// image(sprites[y][x], x*50+30, y *50);
// }
// }
}
function draw() {
background(240);
myPacman.moving();
myPacman.checkForCollisions();
myPacman.draw();
}