xxxxxxxxxx
// create arrays to keep track of the x and y positions
// the mouse (PacMan character) has traveled
let traveledx = [];
let traveledy = [];
// define the set up function (will only run once)
// create a canvas that is the width and height of the
// window
function setup() {
createCanvas(windowWidth, windowHeight);
}
// define the draw function (this will run over and over)
function draw() {
// draw a black background
background(0);
// set the fill colour to white
fill(255);
// use two for loops to draw the dot grid
// every 50 pixels exectute the following for loop from
// left to right
for (let i = 0; i < windowWidth; i = i + 50){
// for each 50 pixels from top to bottom draw the following
// circle
for (let j = 0; j < windowHeight; j = j + 50){
// draw a circle at the x and y positions with a width of
// 5 pixels
circle(i, j, 5);
}
}
// draw the ghost guys
// ghost 1
fill(255, 0, 0);
circle(100,100, 50);
fill(255);
circle(90, 95, 5);
circle(110, 95, 5);
// ghost 2
fill(0, 255, 0);
circle(300,200, 50);
fill(255);
circle(290, 195, 5);
circle(310, 195, 5);
// ghost 3
fill(0, 0, 255);
circle(700,200, 50);
fill(255);
circle(690, 195, 5);
circle(710, 195, 5);
// cover the dots that are eaten, have been covered by
// PacMan character (mouse)
// for each position in the traveled arrays
for (let pos = 0; pos < traveledx.length; pos = pos + 1) {
// set the fill colour to black
fill(0);
// draw a ^black^ circle at the traveled position with a
// width of 50 pixels (to cover the circle)
circle(traveledx[pos], traveledy[pos], 50);
}
// draw the PacMan character again in its new mouse position
// set the fill to the PacMan colour
fill(255, 242, 0);
// draw the arc
arc(mouseX, mouseY, 50, 50, PI/4, 7*PI/4);
// add the current mouse position to the traveled arrays
traveledx.push(mouseX);
traveledy.push(mouseY);
}