Guest User

Untitled

a guest
Jun 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>NinjaMan</title>
  4. <style type="text/css">
  5. * {
  6. margin: 0;
  7. padding: 0;
  8. }
  9. .row {
  10. line-height: 0;
  11. }
  12. .wall {
  13. background-color: blue;
  14. height: 40px;
  15. width: 40px;
  16. display: inline-block;
  17. }
  18. .blank {
  19. background-color: black;
  20. height: 40px;
  21. width: 40px;
  22. display: inline-block;
  23. }
  24. .sushi {
  25. background-color: black;
  26. height: 40px;
  27. width: 40px;
  28. display: inline-block;
  29. background-image: url("img/sushi.png");
  30. background-size: contain;
  31. }
  32. .onigiri {
  33. background-color: black;
  34. height: 40px;
  35. width: 40px;
  36. display: inline-block;
  37. background-image: url("img/onigiri.png");
  38. background-size: contain;
  39. }
  40. #ninjaman {
  41. background-color: black;
  42. height: 40px;
  43. width: 40px;
  44. display: inline-block;
  45. background-image: url("img/ninja.gif");
  46. background-size: contain;
  47. position: absolute;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="world"></div>
  53. <div id="ninjaman"></div>
  54. <div id="score"></div>
  55. </body>
  56. <script type="text/javascript">
  57. var world = [
  58. [1,1,1,1,1],
  59. [1,0,2,3,1],
  60. [1,2,1,2,1],
  61. [1,3,2,3,1],
  62. [1,1,1,1,1]
  63. ]
  64. var worldDict = {
  65. 0: 'blank',
  66. 1: 'wall',
  67. 2: 'sushi',
  68. 3: 'onigiri'
  69. }
  70. function drawWorld(){
  71. output = "";
  72. for(var x = 0; x < world.length; x++){
  73. output += "<div class = 'row'>";
  74. for(var y = 0; y < world[row].length; y++){
  75. output += "<div class = '"+worldDict[world[x][y]]+"'></div>"
  76. }
  77. output += "</div>";
  78. }
  79. document.getElementById("world").innerHTML = output;
  80. document.getElementById("score").innerHTML = "Score: " + ninjaman.score;
  81. }
  82. var ninjaman = {
  83. x: 1,
  84. y: 1,
  85. score: 0
  86. }
  87. function drawNinjaman(){
  88. document.getElementById("ninjaman").style.top = (40*ninjaman.x) + "px"
  89. document.getElementById("ninjaman").style.left = (40*ninjaman.y) + "px"
  90. }
  91. document.onkeydown = function(e){
  92. if(e.keyCode == 37){ //left
  93. if(world[ninjaman.y][ninjaman.x - 1] != 1){
  94. ninjaman.x--;
  95. }
  96. }
  97. if(e.keyCode == 39){ //right
  98. if(world[ninjaman.y][ninjaman.x + 1] != 1){
  99. ninjaman.x++;
  100. }
  101. }
  102. if(e.keyCode == 38){ //up
  103. if(world[ninjaman.y - 1][ninjaman.x] != 1){
  104. ninjaman.y--;
  105. }
  106. }
  107. if(e.keyCode == 40){ //down
  108. if(world[ninjaman.y - 1][ninjaman.x] != 1){
  109. ninjaman.y++;
  110. }
  111. }
  112. drawNinjaman();
  113. if(world[ninjaman.x][ninjaman.y] == 2){
  114. ninjaman.score += 10;
  115. }
  116. if(world[ninjaman.x][ninjaman.y] == 3){
  117. ninjaman.score += 5;
  118. }
  119. world[ninjaman.x][ninjaman.y] = 0;
  120. drawNinjaman();
  121. drawWorld();
  122. }
Add Comment
Please, Sign In to add comment