xxxxxxxxxx
// Particle Explosion Effect with Random Pickup Lines in p5.js for OpenProcessing
let particles = []; // Array to store particles
let explosionCenter; // Center of explosion
let numParticles = 100; // Number of particles to generate
let pickupLines = [
"我有個秘密,\n需要嘴對嘴告訴你。",
"我優點很多,\n但有一個缺點,\n缺點你。",
"我有兩個心願,\n在你身邊和你在身邊。",
"報告!\n我變心了,\n今天變得比昨天還喜歡你。",
"外面風很大,\n有什麼事我們被窩裡說。",
"你能不能好好走路,\n都撞上我的心了。",
"距離不是問題,\n你離我不夠近才是問題。",
"我結婚你一定要來,\n要不然沒有新郎很尷尬。",
"我做事向來十拿九穩,\n差的那一穩,\n是你的吻。",
"我有超能力,\n超級喜歡你。" // 在逗號後加上 \n 实现换行
];
let currentLine = ''; // Variable to store the current random line
function setup() {
createCanvas(400, 400);
background(0);
explosionCenter = createVector(width / 2, height / 2); // Set explosion center
textAlign(CENTER, CENTER);
textSize(14); // Make the text smaller
}
function draw() {
background(0, 50); // Fade background for trails
// Update and display particles
for (let i = particles.length - 1; i >= 0; i--) {
let p = particles[i];
p.update();
p.show();
// Remove particle if it fades out completely
if (p.alpha <= 0) {
particles.splice(i, 1);
}
}
// Display the random pickup line at the explosion center
displayRainbowText(currentLine, explosionCenter.x, explosionCenter.y);
}
function mousePressed() {
// Trigger explosion at mouse location
explosionCenter = createVector(mouseX, mouseY);
// Randomly pick a new pickup line from the list
currentLine = random(pickupLines);
for (let i = 0; i < numParticles; i++) {
particles.push(new Particle(explosionCenter));
}
}
// Function to display text with rainbow gradient effect
function displayRainbowText(pickupText, x, y) {
let rainbowColors = [
color(255, 0, 0), // Red
color(255, 165, 0), // Orange
color(255, 255, 0), // Yellow
color(0, 255, 0), // Green
color(0, 0, 255), // Blue
color(75, 0, 130), // Indigo
color(238, 130, 238) // Violet
];
// Shuffle the rainbow colors for a random order
shuffle(rainbowColors, true);
// Draw each character with a different color from the shuffled gradient
let lines = pickupText.split('\n'); // Split the text by newline character
let textLength = 0;
for (let j = 0; j < lines.length; j++) { // Loop through each line
let line = lines[j];
let lineLength = 0;
for (let i = 0; i < line.length; i++) {
let c = line.charAt(i); // Use line instead of pickupText
fill(rainbowColors[i % rainbowColors.length]);
text(c, x + lineLength, y + j * 20); // Add vertical offset for each line
lineLength += textWidth(c);
}
}
}
// Particle class
class Particle {
constructor(position) {
this.pos = position.copy();
this.vel = p5.Vector.random2D();
this.vel.mult(random(2, 5)); // Random speed
this.size = random(4, 10); // Random size
this.alpha = 255; // Starting opacity
this.color = color(random(100, 255), random(100, 255), random(100, 255));
}
update() {
this.pos.add(this.vel); // Move particle
this.vel.mult(0.96); // Apply slight drag
this.alpha -= 4; // Fade out
}
show() {
noStroke();
fill(red(this.color), green(this.color), blue(this.color), this.alpha);
ellipse(this.pos.x, this.pos.y, this.size);
}
}