Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <style>
  6.  
  7. table {
  8. background: #153256;
  9. border-collapse: collapse;
  10. border: 1px solid black;
  11. }
  12.  
  13. th, td {
  14. border: 1px solid grey;
  15. }
  16.  
  17. td {
  18. padding: 3px;
  19. }
  20.  
  21. .selected {
  22. background: red;
  23. }
  24.  
  25. .target {
  26. background: black;
  27. }
  28.  
  29. </style>
  30. </head>
  31. <body>
  32. <script>
  33.  
  34. var table = document.createElement('table');
  35. var tbody = document.createElement('tbody');
  36. document.body.appendChild(table);
  37. table.appendChild(tbody);
  38.  
  39. const UP = 40;
  40. const DOWN = 38;
  41. const LEFT = 37;
  42. const RIGHT = 39;
  43.  
  44. var rows = 27;
  45. var cols = 48;
  46.  
  47. var selected = false;
  48.  
  49. for (var i = 0; i < rows; i++) {
  50. var tr = document.createElement('tr');
  51. for (var j = 0; j < cols; j++) {
  52. var td = document.createElement('td');
  53. tr.appendChild(td);
  54.  
  55. if (!selected) {
  56. selected = true;
  57. td.classList.add('selected');
  58. }
  59. }
  60. tbody.appendChild(tr);
  61. }
  62.  
  63. document.body.onkeydown = function(event) {
  64. if (event.keyCode === UP) {
  65. moveUp();
  66. } else if (event.keyCode === DOWN) {
  67. moveDown();
  68. } else if (event.keyCode === LEFT) {
  69. moveLeft();
  70. } else if (event.keyCode === RIGHT) {
  71. moveRight();
  72. }
  73. }
  74.  
  75. var x = 0;
  76. var y = 0;
  77.  
  78. var targetX, targetY;
  79.  
  80.  
  81. function moveNext () {
  82.  
  83. }
  84.  
  85. function moveUp() {
  86. y++;
  87. if (y >= rows) {
  88. y = 0;
  89. }
  90.  
  91. moveCell();
  92. }
  93.  
  94. function moveDown() {
  95. y--;
  96. if (y < 0) {
  97. y = rows - 1;
  98. }
  99.  
  100. moveCell();
  101. }
  102.  
  103. function moveLeft() {
  104. x--;
  105. if (x < 0) {
  106. x = cols -1;
  107. }
  108.  
  109. moveCell();
  110. }
  111.  
  112. function moveRight() {
  113. x++;
  114. if (x >= cols) {
  115. x = 0;
  116. }
  117.  
  118. moveCell();
  119. }
  120.  
  121. function moveCell() {
  122. var selected = document.querySelector('.selected');
  123.  
  124. if (selected) {
  125. selected.classList.remove('selected');
  126. }
  127.  
  128. if (x === targetX && y === targetY) {
  129. tbody.children[y].children[x].classList.remove('target');
  130. moveNext()
  131. }
  132.  
  133. tbody.children[y].children[x].classList.add('selected');
  134. }
  135.  
  136. </script>
  137.  
  138. </body>
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement