Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Test Game - Turtle artifact picker</title>
  6.  
  7. </head>
  8. <body>
  9.  
  10. <canvas id="myCanvas" width="400" height="525" style="background-color:yellow;padding: 3px;">
  11. </canvas>
  12.  
  13. <script>
  14.  
  15. var refreshIntervalId;
  16.  
  17. var canvas = document.getElementById("myCanvas");
  18. var ctx = canvas.getContext("2d");
  19.  
  20.  
  21. var mapCoordinates = new Array();
  22. var mapCols = canvas.width / 25; //25x25 one cell
  23. var mapRows = canvas.height / 25;
  24. var mapColNames = 'ABCDEFGHIJKLMNOPQRSTUVWZ';
  25.  
  26. for(var i = 0; i < mapRows; i ++){
  27. for(var j = 0; j < mapCols; j++){
  28. var y = i * 25;
  29. var x = j * 25;
  30. //mapCoordinates.push([x,y]);
  31. var name = mapColNames.split("")[j] + i;
  32. mapCoordinates.push({position: name,
  33. x: x,
  34. y: y,
  35. color: "#fff",
  36. status:0}); // 0 - ok, 1 - nok, 2 - jump?
  37. }
  38. }
  39.  
  40. // adds an object to test the jumping
  41. mapCoordinates[150].color = "#000";
  42. mapCoordinates[150].status = 2;
  43.  
  44. var currentRow = 1;
  45. var currentColumn = 1;
  46.  
  47. var steps = new Array();
  48. steps.push({row: 1, column: 2});
  49.  
  50. var x = getCellInfo(currentRow, currentColumn).x;
  51. var y = getCellInfo(currentRow, currentColumn).y;
  52. var movementSpeed = 2.5;
  53. var isMoving = false;
  54. var direction = "left";
  55.  
  56. var ballRadius = 12.5;
  57.  
  58. var ballColor = getRandomColor();
  59.  
  60.  
  61. var rightPressed = false;
  62. var leftPressed = false;
  63. var upPressed = false;
  64. var downPressed = false;
  65. var down = false;
  66.  
  67.  
  68. function drawBall() {
  69.  
  70. ctx.beginPath();
  71. ctx.arc(x + ballRadius, y + ballRadius, ballRadius, 0, Math.PI*2);
  72. ctx.fillStyle = ballColor;
  73. ctx.fill();
  74. ctx.closePath();
  75. }
  76.  
  77. function updateBallPosition(){
  78. if(isMoving = true){
  79.  
  80. if(direction == "right" && x < getCellInfo(currentRow, currentColumn).x){
  81. x += movementSpeed;
  82. } else if(direction == "left" && x > getCellInfo(currentRow, currentColumn).x){
  83. x -= movementSpeed;
  84. } else if(direction == "up" && y > getCellInfo(currentRow, currentColumn).y){
  85. y -= movementSpeed;
  86. } else if(direction == "down" && y < getCellInfo(currentRow, currentColumn).y){
  87. y += movementSpeed;
  88. } else{
  89. isMoving = false;
  90. down = false; // only allow to move after animation is done;
  91. }
  92. }
  93. }
  94.  
  95. function drawCell(pX, pY, pColor) {
  96.  
  97. ctx.beginPath();
  98. ctx.rect(pX, pY, 25, 25);
  99. ctx.fillStyle = pColor;
  100. ctx.fill();
  101. ctx.closePath();
  102. }
  103.  
  104.  
  105. function getRandomColor() {
  106. var letters = '0123456789ABCDEF'.split('');
  107. var color = '#';
  108. for (var i = 0; i < 6; i++ ) {
  109. color += letters[Math.floor(Math.random() * 16)];
  110. }
  111. return color;
  112. }
  113. var i = 0;
  114. function draw() {
  115. ctx.clearRect(0, 0, canvas.width, canvas.height);
  116.  
  117. for(var i = 0; i < mapCoordinates.length; i++){
  118. drawCell(mapCoordinates[i].x, mapCoordinates[i].y, mapCoordinates[i].color);
  119. }
  120. drawPosition();
  121.  
  122. drawBall();
  123. updateBallPosition();
  124.  
  125. collisionDetection();
  126.  
  127.  
  128. if(rightPressed && isMoving == false) {
  129. direction = "right";
  130. moveBallTo(currentRow, currentColumn + 1);
  131. rightPressed = false;
  132.  
  133. }
  134. else if(leftPressed && isMoving == false) {
  135. direction = "left";
  136. moveBallTo(currentRow, currentColumn - 1);
  137. leftPressed = false;
  138. } else if(upPressed && isMoving == false){
  139. direction = "up";
  140. moveBallTo(currentRow - 1, currentColumn);
  141. upPressed = false;
  142. } else if(downPressed && isMoving == false){
  143. direction = "down";
  144. moveBallTo(currentRow + 1, currentColumn);
  145. downPressed = false;
  146. }
  147.  
  148. refreshIntervalId = requestAnimationFrame(draw);
  149. }
  150.  
  151.  
  152. document.addEventListener("keydown", keyDownHandler, false);
  153. document.addEventListener("keyup", keyUpHandler, false);
  154.  
  155. function keyDownHandler(e) {
  156. if(down)
  157. return;
  158.  
  159. down = true;
  160.  
  161. if(e.keyCode == 38) {
  162. upPressed = true;
  163. } else if(e.keyCode == 40) {
  164. downPressed = true;
  165. } else if(e.keyCode == 39) {
  166. rightPressed = true;
  167. }else if(e.keyCode == 37) {
  168. leftPressed = true;
  169. }
  170. }
  171.  
  172. function keyUpHandler(e) {
  173.  
  174. if(e.keyCode == 39) {
  175. rightPressed = false;
  176. } else if(e.keyCode == 37) {
  177. leftPressed = false;
  178. } else if(e.keyCode == 38) {
  179. upPressed = false;
  180. } else if(e.keyCode == 40) {
  181. downPressed = false;
  182. }
  183. }
  184.  
  185. function collisionDetection() {
  186.  
  187. }
  188.  
  189.  
  190.  
  191. // TO-DO: make smarter function
  192. function getDirection(sCell, eCell){
  193. var direction = "left";
  194.  
  195. if(sCell.row > eCell.row && sCell.column == eCell.column){
  196. direction = "up";
  197. } else if(sCell.row < eCell.row && sCell.column == eCell.column){
  198. direction = "down";
  199. } else if(sCell.row == eCell.row && sCell.column > eCell.column){
  200. direction = "left";
  201. } else if(sCell.row == eCell.row && sCell.column < eCell.column){
  202. direction = "right";
  203. }
  204.  
  205. return direction;
  206. }
  207.  
  208. function changeCellColor(pRow, pCol, color){
  209. var arrayElementNum = pRow * mapCols + pCol;
  210.  
  211. mapCoordinates[arrayElementNum].color = color;
  212. mapCoordinates[arrayElementNum].status = status;
  213. }
  214.  
  215. function getCellInfo(pRow, pCol){
  216.  
  217. var arrayElementNum = pRow * mapCols + pCol;
  218.  
  219. // to-do: move somewhere else
  220. //mapCoordinates[arrayElementNum].color = "#eeeeee";
  221. //mapCoordinates[arrayElementNum].status = 1;
  222.  
  223. return {row: pRow, column: pCol, x: mapCoordinates[arrayElementNum].x, y:mapCoordinates[arrayElementNum].y, position: mapCoordinates[arrayElementNum].position};
  224. }
  225. function setCurrentPosition(pRow, pCol){
  226. isMoving = true;
  227.  
  228.  
  229. currentColumn = pCol;
  230. currentRow = pRow;
  231.  
  232. changeCellColor(pRow, pCol, "#eeeeee");
  233.  
  234. //x = getCellInfo(pRow, pCol).x;
  235. //y = getCellInfo(pRow, pCol).y;
  236. }
  237.  
  238. function moveBallTo(pRow, pCol){
  239.  
  240. // TO-DO: move into collisionDetection
  241. if(pRow < mapRows && pRow >= 0 && pCol < mapCols && pCol >= 0){
  242. var arrayElementNum = pRow * mapCols + pCol;
  243. if(mapCoordinates[arrayElementNum].status == 0){
  244.  
  245. setCurrentPosition(pRow, pCol);
  246.  
  247. } else if(mapCoordinates[arrayElementNum].status == 2){
  248. var oldCol = currentColumn;
  249. var oldRow = currentRow;
  250.  
  251. if(direction == "right") {
  252. setCurrentPosition(pRow, pCol + 1);
  253. }
  254. else if(direction == "left") {
  255. setCurrentPosition(pRow, pCol - 1);
  256. } else if(direction == "up"){
  257. setCurrentPosition(pRow - 1, pCol);
  258. } else if(direction == "down"){
  259. setCurrentPosition(pRow + 1, pCol);
  260. }
  261. }else {
  262. console.log("ball cannot move because of wall");
  263. }
  264.  
  265. } else {
  266. console.log("ball cannot move outside of arena");
  267. }
  268. }
  269.  
  270. function drawPosition() {
  271. ctx.font = "16px Arial";
  272. ctx.fillStyle = "#000";
  273. ctx.fillText(getCellInfo(currentRow, currentColumn).position, 8, 20);
  274. }
  275.  
  276. draw();
  277.  
  278.  
  279. </script>
  280.  
  281. </body>
  282. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement