Advertisement
476179

newnew

Jan 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.  
  5. <meta charset="utf-8">
  6.  
  7. <title>Space Ship Culminating Activity</title>
  8.  
  9. </head>
  10.  
  11. <canvas id="canvas1" width="640" height="480" style="border: 1px solid black;position:absolute; left:0; top 0; z-index: 0,"></canvas>
  12.  
  13. <canvas id="canvas2" width="640" height="480" style="border: 1px solid black;position:absolute; left: 0; top 0; z-index: 1;"></canvas>
  14.  
  15. <canvas id="canvas3" width="640" height="480" style="border: 1px solid black;position:absolute; left: 0; top 0; z-index: 1;"></canvas>
  16.  
  17. <canvas id="canvas4" width="640" height="480" style="border: 1px solid black;position:absolute; left: 0; top 0; z-index: 1;"></canvas>
  18.  
  19. <canvas id="canvas5" width="640" height="480" style="border: 1px solid black;position:absolute; left: 0; top 0; z-index: 1;"></canvas>
  20.  
  21. <canvas id="canvas6" width="640" height="480" style="border: 1px solid black;position:absolute; left: 0; top 0; z-index: 1;"></canvas>
  22.  
  23. <body>
  24.  
  25. <script>
  26. //these variables are helping setup my canvas' so i can easily draw on them
  27. var layer1 = document.getElementById("canvas1");
  28. var dt1 = layer1.getContext("2d");
  29.  
  30. var layer2 = document.getElementById("canvas2");
  31. var dt2 = layer2.getContext("2d");
  32.  
  33. var layer3 = document.getElementById("canvas3");
  34. var dt3 = layer3.getContext("2d");
  35.  
  36. var layer4 = document.getElementById("canvas4");
  37. var dt4 = layer4.getContext("2d");
  38.  
  39. var layer5 = document.getElementById("canvas5");
  40. var dt5 = layer5.getContext("2d");
  41.  
  42. var layer6 = document.getElementById("canvas6");
  43. var dt6 = layer6.getContext("2d");
  44.  
  45. //this is declaring some od the variables for the rocketship
  46. var xPos1 = 0;
  47. var yPos1 = Math.floor(Math.random()*300);
  48. var speed = 6;
  49.  
  50. //this is colouring the backround of space black
  51. dt1.fillStyle = "#000000";
  52. dt1.fillRect(0, 0, layer1.width, layer1.height);
  53.  
  54.  
  55. //this is the greeting alert
  56. alert("Greetings visitor! This program was written on Monday January 14th, 2019 (earth years) by Mitchell Palermo, a student of Mrs.Ostapiak class. As a part of their culminating activity they had to program a sceen from space with moving objects and other things of the sort. Prepare for an out of this world experience as Mitchell combines all of the lessons and activities from this semester into one final program! However first you must answer a few questions... ")
  57.  
  58. //this section asks which galaxy colour the user wants to go to, this determines the colour of the stars
  59. var galaxyChoice = parseFloat(prompt("Where would you like to travel? Please enter the value associated with the galaxy which you would like to travel to."+"\n 1 = Red Galaxy 3 = Green Galaxy\n 2 = Blue Galaxy 4 = Purple Galaxy"));
  60.  
  61. //this program below processes the users choice, assigns the colour of choice to the variable galaxyColour and outputs a comment
  62. if ( galaxyChoice === 1)
  63. {// this alert is for when you chose option 1
  64. alert("Red Galaxy, Good choice!\n you can also move the mouse around to find the black hole!");
  65. var galaxyColour = "#ff0000";
  66. galaxyName = "Green Galaxy";
  67. galaxyName = "Cosmos Redshift 7 galaxy!";
  68. runFunc1()
  69. }
  70. else if (galaxyChoice === 2)
  71. {// this alert is for when you chose option 2
  72. alert("Blue Galaxy, My favorite!\n you can also move the mouse around to find the black hole!");
  73. galaxyColour = "#33ccff";
  74. galaxyName = "EGSY8P7 Galaxy!";
  75. runFunc1()
  76. }
  77. else if( galaxyChoice === 3)
  78. {// this alert is for when you chose option 3
  79. alert("Green Galaxy, Excellent choice!\n you can also move the mouse around to find the black hole!");
  80. galaxyColour = "#33cc33";
  81. galaxyName = "Centaurus A Galaxy!";
  82. runFunc1()
  83. }
  84. else if ( galaxyChoice === 4)
  85. { // this alert is for when you chose option 4
  86. alert("Purple Galaxy, Great choice!\n you can also move the mouse around to find the black hole!");
  87. galaxyColour = "#9933ff";
  88. var galaxyName = "Messier 81 Galaxy!";
  89. runFunc1()
  90. }
  91. else
  92. {//this alert is for when you have enterd a invalid variable
  93. alert("sorry that galaxy is too far to travel to! stay in ours instead!\n you can also move the mouse around to find the black hole!");
  94. var galaxyColour = "#ffffff";
  95. galaxyName = "Milky Way Galaxy!";
  96. runFunc1()
  97. }
  98.  
  99.  
  100.  
  101. //this listens for a click and calls a function if it detects one
  102. document.addEventListener("click",runFunc1);
  103. document.addEventListener("click",draw);
  104.  
  105. //this is the fucntion called when a click is detected, it will clear the canvas and create loops to draw 30 stars and 8 planets
  106. function runFunc1()
  107. {
  108.  
  109. //this clears the canvas'
  110. dt2.clearRect(0,0,canvas2.width,canvas2.height);
  111. dt4.clearRect(0,0,canvas4.width,canvas4.height);
  112. //loop for the stars
  113. for (var i = 1; i<=30;i++)
  114. {
  115. stars(Math.ceil(Math.random()*640),Math.floor(Math.random()*480))
  116. }
  117.  
  118. //this loop is for the planets
  119. for (var e = 1; e<= 8;e++)
  120. {
  121. planets(Math.ceil(Math.random()*640),Math.floor(Math.random()*480),Math.floor(Math.random()*40))
  122. }
  123. //this calls the rocketship function alone with some parameters
  124. xPos1 = 0;
  125. yPos = Math.floor(Math.random()*300);
  126.  
  127. }
  128. //this function holds the code that makes the stars
  129. function stars(xPos, yPos)
  130. {
  131.  
  132. //this draws the stars
  133. dt2.fillStyle = galaxyColour;
  134. dt2.beginPath();
  135. dt2.arc(xPos, yPos, 2, 0, 2*Math.PI);
  136. dt2.fill();
  137.  
  138.  
  139. }
  140.  
  141. //this function creates the planets
  142. function planets(xPos2,yPos2,size)
  143. {
  144. //this creates the planets behind the rocket
  145. dt2.fillstyle = galaxyColour;
  146. dt2.beginPath();
  147. dt2.arc(xPos2, yPos2,size, 0, 2*Math.PI);
  148. dt2.fill();
  149.  
  150. //this creates the planets infront of the rocket
  151. dt4.fillStyle = galaxyColour;
  152. dt4.beginPath();
  153. dt4.arc(xPos2, yPos2, size, 0, 2*Math.PI);
  154. dt4.fill();
  155. }
  156.  
  157.  
  158. setInterval(rocketship, 30);
  159. var xPos1=0;
  160. var yPos1=Math.floor(Math.random()*480);
  161. var speed =5;
  162. //this function has the code that makes the rocketship
  163. function draw()
  164. {
  165. rocketship();
  166. trail();
  167. }
  168. function rocketship()
  169. {
  170. //this declares the speed of the rocket
  171.  
  172. //this draws the top wing or red triangle on the rocket
  173. dt3.clearRect(0, 0, layer3.width, layer3.height);
  174. dt3.fillStyle = "red";
  175. dt3.beginPath();
  176. dt3.lineWidth = 1;
  177. dt3.moveTo(xPos1+20, yPos1);
  178. dt3.lineTo(xPos1-10, yPos1-17);
  179. dt3.lineTo(xPos1-10, yPos1);
  180. dt3.fill();
  181.  
  182. //this draws the bottom wing or triangle on the rocket
  183. dt3.fillStyle = "red";
  184. dt3.beginPath();
  185. dt3.lineWidth = 1;
  186. dt3.moveTo(xPos1+20, yPos1+30);
  187. dt3.lineTo(xPos1-10, yPos1+47);
  188. dt3.lineTo(xPos1-10, yPos1);
  189. dt3.fill();
  190.  
  191. //this draws the white rectangulary body of the rocket
  192. dt3.fillStyle = "#ffffff";
  193. dt3.beginPath();
  194. dt3.fillRect(xPos1-10, yPos1, 40, 30);
  195. dt3.fill();
  196.  
  197. //this draws the tip of the rocketship
  198. dt3.fillstyle = "#ff0000";
  199. dt3.strokeStyle = "red"
  200. dt3.beginPath();
  201. dt3.lineWidth = 1;
  202. dt3.moveTo(xPos1+30, yPos1);
  203. dt3.lineTo(xPos1+50, yPos1+17);
  204. dt3.lineTo(xPos1+30, yPos1+30);
  205. dt3.closePath();
  206. dt3.fill();
  207. dt3.stroke();
  208.  
  209.  
  210.  
  211. //this makes the rocket move across the screen
  212. xPos1 = xPos1 + speed;
  213.  
  214. //this loop is for when the rocket crossses the edges of our canvas to turn around and start in a diffrent location
  215. if (xPos1 <0 || xPos1 >640)
  216. {
  217. //this resets the x and y positions of the rocket so it will reappear coming from left to right on the screen
  218. xPos1 = 0;
  219. yPos1 = (Math.floor(Math.random()*300));
  220. // this clears the previous trail left by the rocket and the stars then redraws the stars
  221. dt3.clearRect(0,0,canvas3.width,canvas3.height);
  222. runFunc1()
  223. }
  224.  
  225. }
  226. //this creates a black radial gradient starting in the middle of the canvas and expands outwards
  227. var radGrd = dt5.createRadialGradient(320, 250, 30, 350, 260, 300);
  228. radGrd.addColorStop(1,"#fff");
  229. radGrd.addColorStop(0,"#000");
  230.  
  231. dt5.fillStyle = radGrd;
  232. dt5.fillRect(0,0,640,480);
  233.  
  234. // this tracks where the mouse moves
  235. document.addEventListener("mousemove", lineDraw);
  236.  
  237. // this clears the rest of layer five except what is inside the circle, the circle is attached to the mouse so whenyou move the mouse you can see a hidden "black hole" in the canvas
  238. function lineDraw()
  239. {
  240. dt5.clearRect(0,0,648,480);
  241. dt5.beginPath();
  242. dt5.arc(event.clientX,event.clientY,50,0,2*Math.PI);
  243. dt5.stroke();
  244. dt5.fill();
  245. }
  246. setInterval(trail, 20);
  247.  
  248. function trail()
  249. {
  250. dt4.fillStyle = "#ffffff";
  251. dt4.fillRect(xPos1-20, yPos1+20, 5, 5);
  252.  
  253. dt4.fillStyle = "red";
  254. dt4.fillRect(xPos1-20, yPos1+30, 5, 5);
  255. dt4.fillRect(xPos1-20, yPos1+10, 5, 5);
  256. Pos1 = xPos1 + speed;
  257. if (xPos1 <0 || xPos1 > 639)
  258. {
  259. dt4.clearRect(0,0,canvas4.width,canvas4.height);
  260. }
  261. }
  262.  
  263.  
  264. //this creates the rectangle that the words are printed on
  265. dt6.fillStyle = galaxyColour;
  266. dt6.beginPath();
  267. dt6.fillRect(460, 380, 200,30);
  268. dt6.fill();
  269.  
  270. //this displays "welcome on the rectangle in the bottom right corner
  271. dt6.font = "6pt Verdana";
  272. dt6.fillStyle = "black";
  273. dt6.fillText("welcome",500,390);
  274.  
  275. //this lets the user know what galaxy they are currently visiting in the bottom right box on the canvas
  276. dt6.font = "6pt Verdana";
  277. dt6.fillStyle = "black";
  278. dt6.fillText("you are in the:\n"+galaxyName+" ",470,400);
  279.  
  280.  
  281. </script>
  282. </body>
  283.  
  284. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement