SOURCE

console 命令行工具 X clear

                    
>
console
let bubbles = []
// let unicorn;

function setup() {
	createCanvas(600, 400);
	for (let i = 0; i < 15; i++) {
		let x = random(width)
		let y = random(height)
		let r = random(10, 30)
		let bubble = new Bubble(x, y, r)
		bubbles.push(bubble)
	}

	// unicorn = new Bubble(300, 200, 20)
}

function draw() {
	background(0)

	// unicorn.x = mouseX
	// unicorn.y = mouseY;
	// unicorn.run()

	for (let obj of bubbles) {
		obj.run()
		let overl = false;
		for (let other of bubbles) {
			if (obj !== other && obj.intersects(other)) {
				overl = true
			}
		}
		if (overl) {
			obj.changeColor(random(255), random(255),random(255),)
		} else {
			obj.changeColor(255)
		}
	}
}

class Bubble {
	constructor(x, y, r) {
		this.x = x
		this.y = y
		this.r = r

		this.brightness = 0;
	}

	run() {
		this.move()
		this.show()
	}

	show() {
		noStroke();
		fill(this.brightness)
		ellipse(this.x, this.y, this.r * 2)
	}

	move() {
		this.x += random(-2, 2)
		this.y += random(-2, 2)
	}

	intersects(obj) {
		let d = dist(this.x, this.y, obj.x, obj.y)
		if (d < this.r + obj.r) { // 两个圆心的距离小于两个圆的半径时
			return true
		} else {
			return false
		}
	}

	changeColor(...bright) {
		this.brightness = bright
	}
}