xxxxxxxxxx
87
class PacMan {
constructor() {
this.xPos = -20;
this.yPos = random(0, 400);
this.xSpeed = 3;
this.ySpeed = 0;
}
random() {
this.xSpeed = random(20);
this.ySpeed = random(0);
}
move() {
// Move PacMan
if (keyIsPressed == true){
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
}
}
reset() {
if (this.xPos > 450){
this.xPos = -50;
this.yPos = random(0,400);
}
}
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(x, y, z) {
fill(x, y, z);
arc(this.xPos, this.yPos, 50, 50, PI * 1/4, PI * 7/4, PIE);
}
}
let Pac;
let Pac2;
let Pac3 = [];
let PacColor1= [];
let PacColor2= [];
let PacColor3= [];
function setup() {
createCanvas(400, 400);
Pac = new PacMan();
Pac2 = new PacMan();
for (let i = 0; i < 10; i++) {
// each ball will have a slightly different
// initial speed in the X axis, but all balls
// will have the same speed in the Y axis
Pac3[i] = new PacMan();
PacColor1[i] = random(255);
PacColor2[i] = random(255);
PacColor3[i] = random(255);
}
}
function draw() {
background(30, 30, 30);
Pac.draw(247, 224, 30);
Pac.move();
Pac.random();
Pac.reset();
Pac2.draw(230, 30, 30);
Pac2.move();
Pac2.random();
Pac2.reset();
for (let i = 0; i < 10; i++) {
Pac3[i].draw(PacColor1[i], PacColor2[i], PacColor3[i]);
Pac3[i].move();
Pac3[i].random();
Pac3[i].reset();
}
}