Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Declare the poison variables
- var strychnine;
- var amanitaMushrooms;
- var mercury;
- var insecticide;
- var formaldehyde;
- var hemlock;
- //Declare the antidote variables
- var sulphates;
- var charcoal;
- var antibodies;
- var paracetamol;
- //This variable is used for drawing the graph
- var graphs;
- function setup()
- {
- createCanvas(800,600);
- strokeWeight(2);
- //initialise the poisons and antidotes
- strychnine = 0.5;
- amanitaMushrooms = 0.5;
- mercury = 0.5;
- insecticide = 0.5;
- formaldehyde = 0.5;
- hemlock = 0.5;
- sulphates = 0.5;
- charcoal = 0.5;
- antibodies = 0.5;
- paracetamol = 0.5;
- //fills the graph with empty values
- graphs = [];
- for(var i = 0; i < 6; i++)
- {
- graphs.push([]);
- for(var j = 0; j < 512; j++)
- {
- graphs[i].push(0.5);
- }
- }
- }
- function draw()
- {
- //Develop the antidote below
- //Write conditional statements to change the amount of each substance ...
- // - If insecticide goes above 0.38 and mercury goes above 0.65, decrement sulphates by 0.03
- if(insecticide > 0.38 && mercury > 0.65)
- {
- sulphates -=0.03;
- }
- // - If hemlock dips below 0.3, or on the other hand, strychnine goes above 0.54 and formaldehyde goes above 0.32, increment sulphates by 0.05
- if(hemlock < 0.3 || (strychnine > 0.54 && formaldehyde > 0.32))
- {
- sulphates +=0.05;
- }
- // - If amanitaMushrooms goes above 0.49 or strychnine goes above 0.47, decrement charcoal by 0.03
- if(amanitaMushrooms > 0.49 || strychnine > 0.47)
- {
- charcoal -=0.03
- }
- // - When either hemlock goes above 0.6, mercury dips below 0.29, or perhaps insecticide dips below 0.31, increase charcoal by 0.04
- if(hemlock > 0.6 || mercury < 0.29 || insecticide < 0.31)
- {
- charcoal +=0.04;
- }
- // - If formaldehyde goes above 0.43 or hemlock dips below 0.58, whilst at the same time, insecticide goes above 0.47, decrement antibodies by 0.01
- if( (formaldehyde > 0.43 || hemlock < 0.58) && insecticide > 0.47)
- {
- antibodies -=0.01;
- }
- // - If amanitaMushrooms dips below 0.5, whilst at the same time, strychnine dips below 0.71 or mercury goes above 0.64, increment antibodies by 0.01
- if(amanitaMushrooms < 0.5 && (strychnine < 0.71 || mercury > 0.64))
- {
- antibodies +=0.01;
- }
- // - When hemlock goes above 0.7 and mercury dips below 0.58, try decreasing paracetamol by 0.02
- if (hemlock > 0.7 && mercury < 0.58)
- {
- paracetamol -=0.02;
- }
- // - If amanitaMushrooms goes above 0.52 or formaldehyde dips below 0.6, try increasing paracetamol by 0.02
- if(amanitaMushrooms > 0.52 || formaldehyde < 0.6)
- {
- paracetamol +=0.02;
- }
- //////////////////////////////////////////////////////
- //the code below generates new values using random numbers
- /*
- For testing, you might want to temporarily comment out
- these lines and set the same variables to constant values
- instead.
- */
- strychnine = nextValue(graphs[0],strychnine);
- amanitaMushrooms = nextValue(graphs[1],amanitaMushrooms);
- mercury = nextValue(graphs[2],mercury);
- insecticide = nextValue(graphs[3],insecticide);
- formaldehyde = nextValue(graphs[4],formaldehyde);
- hemlock = nextValue(graphs[5],hemlock);
- sulphates = constrain(sulphates, 0, 1);
- charcoal = constrain(charcoal, 0, 1);
- antibodies = constrain(antibodies, 0, 1);
- paracetamol = constrain(paracetamol, 0, 1);
- ///////// DO NOT CHANGE THE CODE BELOW ///////////
- //drawing code
- // set background
- background(0);
- noFill();
- //draw the graphs for the vitals
- var colors = [
- color(255, 0, 0),
- color(0, 255, 0),
- color(0, 0, 255),
- color(255, 0, 255),
- color(255, 255, 0),
- color(0, 255, 255)
- ];
- for(var i = 0; i < graphs.length; i++)
- {
- stroke(colors[i]);
- drawGraph(graphs[i]);
- }
- //draw the poisons as text
- noStroke();
- fill(colors[0]);
- text('strychnine: ' + nf(strychnine,1,2), 20,20);
- fill(colors[1]);
- text('amanitaMushrooms: ' + nf(amanitaMushrooms,1,2), 20,40);
- fill(colors[2]);
- text('mercury: ' + nf(mercury,1,2), 20,60);
- fill(colors[3]);
- text('insecticide: ' + nf(insecticide,1,2), 20,80);
- fill(colors[4]);
- text('formaldehyde: ' + nf(formaldehyde,1,2), 20,100);
- fill(colors[5]);
- text('hemlock: ' + nf(hemlock,1,2), 20,120);
- //draw the antidotes bar chart
- drawBar(sulphates,50,'sulphates');
- drawBar(charcoal,200,'charcoal');
- drawBar(antibodies,350,'antibodies');
- drawBar(paracetamol,500,'paracetamol');
- }
- function nextValue(graph, val)
- {
- //gets the next value for a vital and puts it in an array for drawing
- var delta = random(-0.03,0.03);
- val += delta;
- if(val > 1 || val < 0)
- {
- delta *= -1;
- val += delta * 2;
- }
- graph.push(val)
- graph.shift();
- return val;
- }
- function drawGraph(graph)
- {
- //draws an array as a graph
- beginShape();
- for(var i = 0; i < graph.length; i++)
- {
- vertex(width * i/512, height * 0.5 - graph[i]* height/3)
- }
- endShape();
- }
- function drawBar(val, x, name)
- {
- //draws the bars for bar chart
- noStroke();
- fill(0,100,100);
- var mh = height * 0.4 - 50;
- rect(x,(height - 50) - val*mh, 100, val*mh);
- fill(255);
- text(name + ": " + val, x, height - 20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement