var pic1, pic2, pic3, pic4;
var move1, move2, move3, move4;
pic1 = pic2 = loadImage("gry.png");
pic3 = pic4 = loadImage("blk.png");
rain = loadSound("rain2.mp3");
lightning = loadSound("lightning.mp3");
howOften = floor(random(100, 110));
if (frameCount % howOften === 0) {
flashGroupTime = random(0, 15);
if (flashGroupTime > 0) sheetFlash();
if (frameCount % 180 === 0 && !isTree) lightningStrike();
if (isTree === true) tree.display();
if (frameCount % 180 === 0 && chance < .9 && makeSound) lightning.play();
function lightningStrike() {
strt = createVector(random(width), 0);
dir = createVector(random(width) - strt.x, (height) - strt.y);
tree = new Tree(strt, dir, 0.02 * dir.mag());
if (move1 <= -1400) move1 = 1400;
if (move2 <= -1400) move2 = 1400;
if (move3 <= -1400) move3 = 1400;
if (move4 <= -1400) move4 = 1400;
rect(950, 0, 250, height);
rect(0, 600, width, 100);
this.vel = createVector(4, 0);
this.pos = createVector(-10, height / 2);
this.display = function () {
ellipse(this.pos.x, this.pos.y, 5, random(8, 18));
ellipse(this.pos.x, this.pos.y, 12, 3);
this.turn = function () {
if (this.pos.x > width + 100) {
this.pos.y = random(425, 500);
this.vel.set(random(-1, -2), random(-.1, .3));
this.pos.y = random(425, 500);
this.vel.set(random(1, 2), random(-.1, .3));
flashTime = random(0, 10);
if (incrFlashTime < flashTime) {
rect(0, 0, width, height);
function Tree(startPoint, direction, initialWeight) {
this.branches = [this.treeSize];
this.branches[0] = new Twig(startPoint, direction, initialWeight);
this.currentDirection = direction;
for (var i = 0; i < this.treeSize - 1; i++) {
this.branchIndex = floor(random(0.0, this.branches[i].getPointSum() * 0.75));
this.branches[this.treeIndex] = new Twig(this.branches[i].getPoint(this.branchIndex), this.currentDirection, this.branches[i].getWeight(this.branchIndex));
if (this.treeIndex < this.treeSize) this.treeIndex++;
this.display = function () {
for (var j = 0; j < this.treeSize; j++) {
this.branches[j].displayTwig();
function Twig(startPoint, direction, initialWeight) {
this.maxPoints = floor(random(10.0, 10 * initialWeight));
this.maxLen = initialWeight;
this.ref = createVector(1.0, 0.0);
this.ang = p5.Vector.angleBetween(direction, this.ref);
if (direction.y < 0) this.ang *= -1.0;
this.pointSum = this.maxPoints;
this.pathPoints = [this.maxPoints];
this.pathPoints[0] = startPoint;
for (var j = 1; j < this.maxPoints; j++) {
this.magnitude = random(0.0, this.maxLen);
this.ang3 = random(-QUARTER_PI, QUARTER_PI) + this.ang2;
this.nextPoint = createVector(this.magnitude * cos(this.ang3 + this.ang), this.magnitude * sin(this.ang3 + this.ang));
this.ang2 = this.ang3 * 0.85;
this.nextPoint.add(this.pathPoints[j - 1]);
this.pathPoints[j] = this.nextPoint;
this.weights = [this.maxPoints];
for (var j = 0; j < this.maxPoints; j++) {
this.weights[j] = map(j, 0, this.maxPoints - 1, initialWeight, 2);
for (var j = 0; j < this.maxPoints; j++) {
this.displayTwig = function () {
if (isTree) stroke(tree.fade);
for (var j = 1; j < this.pointSum; j++) {
strokeWeight(this.weights[j] * 0.3);
line(this.pathPoints[j - 1].x, this.pathPoints[j - 1].y, this.pathPoints[j].x, this.pathPoints[j].y);
if (this.pathPoints[j].y > 500) makeSound = true;
this.getPoint = function (index) {
return this.pathPoints[index];
this.getPointSum = function () {
this.getWeight = function (index) {
return this.weights[index];