xxxxxxxxxx
153
// Wander (Main)
// The Nature of Code
// The Coding Train / Daniel Shiffman
// https://youtu.be/ujsR2vcJlLk
// https://thecodingtrain.com/learning/nature-of-code/5.5-wander.html
// Main: https://editor.p5js.org/codingtrain/sketches/LVtVlS52Q
// With Sliders: https://editor.p5js.org/codingtrain/sketches/uxemh7FGc
// Deleting Positions: https://editor.p5js.org/codingtrain/sketches/EWHjy--Os
// 3D: https://editor.p5js.org/codingtrain/sketches/t6sFXmVrk
// Displacement: https://editor.p5js.org/codingtrain/sketches/VdHUvgHkm
// Perlin Noise: https://editor.p5js.org/codingtrain/sketches/XH2DtikuI
// sfx
let collectionSound;
let gameOverSound;
// plane and particles
let paperAirplane;
let startX = 100;
let startY = 100;
let particles = [];
let particleWeight = 7;
let particleLifeSpan = 5000; // 5 seconds
let particleSpawnRate = 500; // 0.5 seconds
let time = 0;
function preload() {
soundFormats('wav');
collectionSound = loadSound('collection');
gameOverSound = loadSound('gameOver');
}
function setup() {
createCanvas(800, 500);
paperAirplane = new Vehicle(startX, startY, false)
setInterval(function() {
if (paperAirplane.maxSpeed > 0.1) {
time += 0.01
}
}, 10)
setInterval(function() {
if (paperAirplane.maxSpeed > 0.1) {
let offset = 30
let x = random(offset, width - offset)
let y = random(offset, height - offset);
particles.push({
p: createVector(x, y),
spawnedAt: millis(),
brightness: 255
});
}
}, particleSpawnRate)
}
function reset() {
particles = [];
paperAirplane.pos = createVector(startX, startY)
paperAirplane.maxSpeed = 3.0;
paperAirplane.maxForce = 0.2;
time = 0;
}
function draw() {
background("#171e26");
// information
textSize(15);
fill("#989898")
noStroke()
text("Randomized paper airplane game.", 290, 40)
text("Collecting the yellow particles helps the plane stay moving for a longer time.", 150, 60)
// show particles
for (let i = 0; i < particles.length; i++) {
if (millis() - particles[i].spawnedAt > particleLifeSpan) {
//fade away particle
stroke(255, 244, 79, particles[i].brightness)
strokeWeight(particleWeight)
point(particles[i].p.x, particles[i].p.y)
particles[i].brightness--;
if (particles[i].brightness < 0) {
particles.splice(i, 1);
}
} else {
stroke(255, 244, 79)
strokeWeight(particleWeight)
point(particles[i].p.x, particles[i].p.y)
}
}
// collecting particles
for (let i = 0; i < particles.length; i++) {
let collectionRadius = 30
if (p5.Vector.dist(paperAirplane.pos, particles[i].p) < collectionRadius) {
collectionSound.play();
particles.splice(i, 1); // remove particle
paperAirplane.maxSpeed += 0.2;
paperAirplane.maxForce += 0.005
if (paperAirplane.maxSpeed > 3) {
paperAirplane.maxSpeed = 3
}
if (paperAirplane.maxForce > 0.2) {
paperAirplane.maxForce = 0.2
}
}
}
// wander
if (paperAirplane.maxSpeed > 0.3) {
paperAirplane.wander();
} else {
gameOverSound.play();
textSize(40)
noStroke()
fill("#50C878")
text(`Score: ${time.toFixed(2)} seconds`, 200, height/2)
noLoop();
setTimeout(function(){
reset();
loop();
}, 3000)
paperAirplane.maxSpeed = 0.00001
}
// update airplane
paperAirplane.update();
paperAirplane.show();
paperAirplane.edges();
// statistics
textSize(15);
noStroke()
text(`Airtime: ${time.toFixed(2)}`, 6, height-32);
if (paperAirplane.maxSpeed > 2) {
fill("#50C878")
} else if(paperAirplane.maxSpeed > 1) {
fill("#ffae42")
} else if(paperAirplane.maxSpeed > 0) {
fill("#ff3131")
}
noStroke()
text(`Max Speed: ${paperAirplane.maxSpeed.toFixed(2)}`, 7, height - 10);
}