Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1.  
  2. //Declare the poison variables
  3. var strychnine;
  4. var amanitaMushrooms;
  5. var mercury;
  6. var insecticide;
  7. var formaldehyde;
  8. var hemlock;
  9.  
  10.  
  11. //Declare the antidote variables
  12. var sulphates;
  13. var charcoal;
  14. var antibodies;
  15. var paracetamol;
  16.  
  17.  
  18. //This variable is used for drawing the graph
  19. var graphs;
  20.  
  21.  
  22. function setup()
  23. {
  24.  
  25. createCanvas(800,600);
  26. strokeWeight(2);
  27.  
  28. //initialise the poisons and antidotes
  29. strychnine = 0.5;
  30. amanitaMushrooms = 0.5;
  31. mercury = 0.5;
  32. insecticide = 0.5;
  33. formaldehyde = 0.5;
  34. hemlock = 0.5;
  35. sulphates = 0.5;
  36. charcoal = 0.5;
  37. antibodies = 0.5;
  38. paracetamol = 0.5;
  39.  
  40.  
  41. //fills the graph with empty values
  42. graphs = [];
  43.  
  44. for(var i = 0; i < 6; i++)
  45. {
  46. graphs.push([]);
  47. for(var j = 0; j < 512; j++)
  48. {
  49. graphs[i].push(0.5);
  50. }
  51. }
  52.  
  53. }
  54.  
  55. function draw()
  56. {
  57.  
  58. //Develop the antidote below
  59. //Write conditional statements to change the amount of each substance ...
  60.  
  61. // - If insecticide goes above 0.38 and mercury goes above 0.65, decrement sulphates by 0.03
  62.  
  63. if(insecticide > 0.38 && mercury > 0.65)
  64. {
  65. sulphates -=0.03;
  66. }
  67.  
  68. // - 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
  69. if(hemlock < 0.3 || (strychnine > 0.54 && formaldehyde > 0.32))
  70. {
  71. sulphates +=0.05;
  72. }
  73.  
  74. // - If amanitaMushrooms goes above 0.49 or strychnine goes above 0.47, decrement charcoal by 0.03
  75. if(amanitaMushrooms > 0.49 || strychnine > 0.47)
  76. {
  77. charcoal -=0.03
  78. }
  79.  
  80. // - When either hemlock goes above 0.6, mercury dips below 0.29, or perhaps insecticide dips below 0.31, increase charcoal by 0.04
  81. if(hemlock > 0.6 || mercury < 0.29 || insecticide < 0.31)
  82. {
  83. charcoal +=0.04;
  84. }
  85.  
  86. // - 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
  87. if( (formaldehyde > 0.43 || hemlock < 0.58) && insecticide > 0.47)
  88. {
  89. antibodies -=0.01;
  90. }
  91. // - 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
  92. if(amanitaMushrooms < 0.5 && (strychnine < 0.71 || mercury > 0.64))
  93. {
  94. antibodies +=0.01;
  95. }
  96.  
  97. // - When hemlock goes above 0.7 and mercury dips below 0.58, try decreasing paracetamol by 0.02
  98. if (hemlock > 0.7 && mercury < 0.58)
  99. {
  100. paracetamol -=0.02;
  101. }
  102.  
  103. // - If amanitaMushrooms goes above 0.52 or formaldehyde dips below 0.6, try increasing paracetamol by 0.02
  104. if(amanitaMushrooms > 0.52 || formaldehyde < 0.6)
  105. {
  106. paracetamol +=0.02;
  107. }
  108.  
  109.  
  110.  
  111. //////////////////////////////////////////////////////
  112.  
  113. //the code below generates new values using random numbers
  114.  
  115. /*
  116. For testing, you might want to temporarily comment out
  117. these lines and set the same variables to constant values
  118. instead.
  119. */
  120.  
  121. strychnine = nextValue(graphs[0],strychnine);
  122. amanitaMushrooms = nextValue(graphs[1],amanitaMushrooms);
  123. mercury = nextValue(graphs[2],mercury);
  124. insecticide = nextValue(graphs[3],insecticide);
  125. formaldehyde = nextValue(graphs[4],formaldehyde);
  126. hemlock = nextValue(graphs[5],hemlock);
  127.  
  128.  
  129. sulphates = constrain(sulphates, 0, 1);
  130. charcoal = constrain(charcoal, 0, 1);
  131. antibodies = constrain(antibodies, 0, 1);
  132. paracetamol = constrain(paracetamol, 0, 1);
  133.  
  134.  
  135. ///////// DO NOT CHANGE THE CODE BELOW ///////////
  136.  
  137. //drawing code
  138.  
  139. // set background
  140. background(0);
  141. noFill();
  142.  
  143. //draw the graphs for the vitals
  144. var colors = [
  145. color(255, 0, 0),
  146. color(0, 255, 0),
  147. color(0, 0, 255),
  148. color(255, 0, 255),
  149. color(255, 255, 0),
  150. color(0, 255, 255)
  151. ];
  152.  
  153. for(var i = 0; i < graphs.length; i++)
  154. {
  155. stroke(colors[i]);
  156. drawGraph(graphs[i]);
  157. }
  158.  
  159. //draw the poisons as text
  160. noStroke();
  161. fill(colors[0]);
  162. text('strychnine: ' + nf(strychnine,1,2), 20,20);
  163. fill(colors[1]);
  164. text('amanitaMushrooms: ' + nf(amanitaMushrooms,1,2), 20,40);
  165. fill(colors[2]);
  166. text('mercury: ' + nf(mercury,1,2), 20,60);
  167. fill(colors[3]);
  168. text('insecticide: ' + nf(insecticide,1,2), 20,80);
  169. fill(colors[4]);
  170. text('formaldehyde: ' + nf(formaldehyde,1,2), 20,100);
  171. fill(colors[5]);
  172. text('hemlock: ' + nf(hemlock,1,2), 20,120);
  173.  
  174.  
  175. //draw the antidotes bar chart
  176. drawBar(sulphates,50,'sulphates');
  177. drawBar(charcoal,200,'charcoal');
  178. drawBar(antibodies,350,'antibodies');
  179. drawBar(paracetamol,500,'paracetamol');
  180.  
  181.  
  182. }
  183.  
  184. function nextValue(graph, val)
  185. {
  186. //gets the next value for a vital and puts it in an array for drawing
  187. var delta = random(-0.03,0.03);
  188.  
  189. val += delta;
  190. if(val > 1 || val < 0)
  191. {
  192. delta *= -1;
  193. val += delta * 2;
  194. }
  195.  
  196. graph.push(val)
  197. graph.shift();
  198. return val;
  199. }
  200.  
  201. function drawGraph(graph)
  202. {
  203. //draws an array as a graph
  204. beginShape();
  205. for(var i = 0; i < graph.length; i++)
  206. {
  207. vertex(width * i/512, height * 0.5 - graph[i]* height/3)
  208. }
  209. endShape();
  210. }
  211.  
  212.  
  213. function drawBar(val, x, name)
  214. {
  215. //draws the bars for bar chart
  216. noStroke();
  217. fill(0,100,100);
  218. var mh = height * 0.4 - 50;
  219. rect(x,(height - 50) - val*mh, 100, val*mh);
  220. fill(255);
  221. text(name + ": " + val, x, height - 20);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement