Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <style type="text/css">
  8. #ocean {
  9. background-image: url("ocean.jpg");
  10. width: 900px;
  11. height: 700px;
  12. }
  13. .player {
  14. position: absolute;
  15. background-image: url("player.png");
  16. width: 70px;
  17. height: 75px;
  18. }
  19. .enemy {
  20. position: absolute;
  21. background-image: url("enemy.png");
  22. width: 70px;
  23. height: 75px;
  24. }
  25. </style>
  26.  
  27. <div id="ocean">
  28. <div id="player"></div>
  29. <div id="enemy"></div>
  30. </div>
  31.  
  32.  
  33.  
  34.  
  35. <script type="text/javascript">
  36. var player = {
  37. left: 450,
  38. top: 700
  39. }
  40. var enemies = [
  41. {left: 250, top: 200},
  42. {left: 350, top: 250},
  43. {left: 450, top: 200},
  44. {left: 250, top: 250},
  45. {left: 350, top: 250},
  46. {left: 150, top: 200}
  47. ]
  48.  
  49. function drawPlayer() {
  50. content = "<div class='player' style='left: "+player.left+"px; top: "+player.top+"px'></div>";
  51. document.getElementById("players").innerHTML = content;
  52. }
  53.  
  54. function drawEnemies() {
  55. content = "";
  56. for(var i=0; i<enemy.length; i++) {
  57. content += "<div class='enemy' style='left: "+enemy[i].left+"px; top: "+enemy document.onkeydown = function(e) {
  58. [i].top+"px'></div>";
  59. }
  60. document.getElementById("enemies").innerHTML = content;
  61. if(e.keyCode == 37) {
  62. if(player.left > 0) {
  63. player.left -= 10;
  64. }
  65. }
  66. if(e.keyCode == 39) {
  67. if(player.left < 1180) {
  68. player.left += 10;
  69. }
  70. }
  71. if(e.keyCode == 38) {
  72. player.top -= 10;
  73. }
  74. if(e.keyCode == 40) {
  75. player.top += 10;
  76. }
  77. drawPlayer();
  78. }
  79. drawPlayer();
  80. drawEnemies();
  81. }
  82.  
  83. </script>
  84. </body>
  85. </html>
Add Comment
Please, Sign In to add comment