Guest User

Untitled

a guest
May 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. var canvas = document.getElementById("canvas");
  2. var context = canvas.getContext("2d");
  3. var view = {x: 0, y: 0};
  4. var questionsArray = [];
  5. var moveCount = 0;
  6. var moveCharacter = false;
  7. var theArray = [];
  8.  
  9. var mapArray = [
  10. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  11. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  12. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  13. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
  14. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  15. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
  16. [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0],
  17. [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  18. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  19. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  20. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  21. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  22. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  23. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  24. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  25. ];
  26. function isPositionWall(ptX, ptY) {
  27. var gridX = Math.floor(ptX / 36);
  28. var gridY = Math.floor(ptY / 36);
  29. /*if(gridX > 0 || gridX >= mapArray[0].length)*/
  30. if(mapArray[gridX][gridY] === 1) {
  31. return true;
  32. /*if(gridY < 0 || gridY >= mapArray.length)
  33. return true;
  34. return mapArray[gridX][gridY];*/
  35. }
  36. }
  37.  
  38.  
  39. var Question = function(question, answer1, answer2, correctAnswer) {
  40. this.question = question;
  41. this.answer1 = answer1;
  42. this.answer2 = answer2;
  43. this.correctAnswer = correctAnswer;
  44.  
  45. this.addToArray = function(){
  46. theArray.push(this);
  47. };
  48. this.addToArray();
  49. }
  50.  
  51. Question.prototype.checkAnswer = function(answer1, answer2) {
  52. if (answer1.innerHTML == this.correctAnswer) {
  53. console.log("correct");
  54. moveCharacter = true;
  55. } else {
  56. console.log("nope")
  57. }
  58. }
  59.  
  60. var question1 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  61. var question2 = new Question("Jo ar ne?", "Ne", "Jo", "Ne");
  62. var question3 = new Question("Aha ar ne?", "Aha", "Ne", "Aha");
  63. var question4 = new Question("Ja ar ne?", "Taip", "Ne", "Taip");
  64. var question5 = new Question("Jojo ar ne?", "Jojo", "Ne", "Jojo");
  65. var question6 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  66. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  67.  
  68. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  69. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  70. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  71. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  72. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  73. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  74. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  75. var question7 = new Question("Taip ar ne?", "Taip", "Ne", "Taip");
  76. var StyleSheet = function(image, width, height, x, y) {
  77. this.image = image;
  78. this.width = width;
  79. this.height = height;
  80. this.x = x;
  81. this.y = y
  82. this.draw = function(image, sx, sy, swidth, sheight, x, y, width, height) {
  83. context.drawImage(image, sx, sy, swidth, sheight, x, y, width, height);
  84. };
  85. this.drawimage = function(image, x, y, width, height) {
  86. context.drawImage(image, x, y, width, height);
  87. };
  88. };
  89.  
  90. /* Initial Sprite Position */
  91.  
  92.  
  93. var boatPosX = canvas.height/2 - 50;
  94. var boatPosY = canvas.height/2 - 50;
  95.  
  96. function render(viewport) {
  97.  
  98. context.save();
  99. context.translate(view.x, view.y);
  100. requestAnimationFrame(render);
  101. var oldPosX = boatPosX;
  102. var oldPosY = boatPosY;
  103.  
  104.  
  105. for (let i = 0; i < mapArray.length; i++) {
  106. for (let j = 0; j < mapArray[i].length; j++) {
  107. if (mapArray[j][i] == 0) {
  108. this.sprite.draw(
  109. background,
  110. 190,
  111. 230,
  112. 26,
  113. 26,
  114. i * this.sprite.width,
  115. j * this.sprite.height,
  116. this.sprite.width,
  117. this.sprite.height
  118. );
  119. }
  120. if (mapArray[j][i] == 1) {
  121. this.sprite.draw(
  122. background,
  123. 30,
  124. 30,
  125. 26,
  126. 26,
  127. i * this.sprite.width,
  128. j * this.sprite.height,
  129. this.sprite.width,
  130. this.sprite.height
  131. );
  132.  
  133. }
  134. if (mapArray[j][i] == 2) {
  135. this.sprite.draw(
  136. background,
  137. 200,
  138. 20,
  139. 26,
  140. 26,
  141. i * this.sprite.width,
  142. j * this.sprite.height,
  143. this.sprite.width,
  144. this.sprite.height
  145. );
  146. }
  147. }
  148. }
  149. this.ship.drawimage(boat, boatPosX, boatPosY, 50, 50);
  150. /*var lineHeight = 16 * 2.286;
  151. var textWidth = context.measureText(theArray[moveCount].question).width * 3;
  152. context.textAlign = 'left';
  153. context.textBaseline = 'top';
  154. context.font="14px Verdana";
  155. context.fillStyle = 'rgba(0, 0, 0 ,0.9)';
  156. context.fillRect(boatPosX + ship.width / 2, boatPosY - ship.height / 2, textWidth, lineHeight);
  157. context.fillStyle = 'white';
  158. context.fillText(theArray[moveCount].question, boatPosX + ship.width / 2, boatPosY - ship.height / 2);
  159.  
  160. answerBtn1.innerHTML = theArray[moveCount].answer1;
  161. answerBtn2.innerHTML = theArray[moveCount].answer2;*/
  162. if(isPositionWall(boatPosX + 36, boatPosY)) {
  163. //boatPosX = oldPosY;
  164. //console.log("collision");
  165. }
  166.  
  167. //console.log(mapArray[Math.floor(boatPosX / 36)][Math.floor(boatPosX / 36)]);
  168. context.restore();
  169.  
  170. };
  171.  
  172.  
  173. var btn1 = document.getElementsByClassName("button1")[0];
  174. var btn2 = document.getElementsByClassName("button2")[0];
  175. var btn3 = document.getElementsByClassName("button3")[0];
  176. var btn4 = document.getElementsByClassName("button4")[0];
  177. var answerBtn1 = document.getElementsByClassName("answer1")[0];
  178. var answerBtn2 = document.getElementsByClassName("answer2")[0];
  179.  
  180. console.log(theArray);
  181.  
  182. answerBtn1.addEventListener("click", function(e) {
  183. theArray[moveCount].checkAnswer(answerBtn1, answerBtn2);
  184. });
  185.  
  186. answerBtn2.addEventListener("click", function(e) {
  187. question1.checkAnswer(answerBtn2, answerBtn2);
  188. });
  189.  
  190. btn1.addEventListener("click", function(e) {
  191. if (moveCharacter == true) {
  192. moveCount++;
  193. boatPosX -= 5;
  194. view.x += 5
  195. moveCharacter = false;
  196. }
  197. });
  198. btn2.addEventListener("click", function(e) {
  199. boatPosX += 5;
  200. view.x -= 5
  201. moveCount++;
  202. });
  203. btn3.addEventListener("click", function(e) {
  204. boatPosY += 5;
  205. view.Y += 5
  206. moveCount++;
  207. });
  208. btn4.addEventListener("click", function(e) {
  209. boatPosY -= 5;
  210. view.Y += 5
  211. moveCount++;
  212. });
  213.  
  214.  
  215. function move(e) {
  216. if (e.keyCode == 39) {
  217. boatPosX += 5;
  218. view.x -= 5
  219. moveCount++;
  220. console.log(moveCount);
  221. console.log("right");
  222. }
  223. if (e.keyCode == 37) {
  224. boatPosX -= 5;
  225. view.x += 5
  226. moveCount++;
  227. console.log(moveCount);
  228. console.log("left");
  229. }
  230. if (e.keyCode == 38) {
  231. boatPosY -= 5;
  232. view.Y += 5
  233. moveCount++;
  234. console.log(moveCount);
  235. console.log("up");
  236. }
  237. if (e.keyCode == 40) {
  238. boatPosY += 5;
  239. view.Y += 5
  240. moveCount++;
  241. console.log(moveCount);
  242. console.log("down");
  243. }
  244. }
  245.  
  246. document.onkeydown = move;
  247.  
  248. var background = new Image();
  249. background.src = "ground.png";
  250. var sprite = new StyleSheet(background, 36, 36, 16, 16);
  251.  
  252. var boat = new Image();
  253. boat.src = "ship.png";
  254. var ship = new StyleSheet(boat, 36, 36, 16, 16);
Add Comment
Please, Sign In to add comment