Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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. background-image: url("enemy.png");
  21. width: 70px;
  22. height: 75px;
  23. }
  24. .missile {
  25. position: absolute;
  26. background-color: red;
  27. width: 2px;
  28. height: 10px;
  29. }
  30. </style>
  31.  
  32. <div id="ocean">
  33. <div id="players"></div>
  34. <div id="enemies"></div>
  35. <div id="missiles"></div>
  36. </div>
  37.  
  38. <script type="text/javascript">
  39. var player = {
  40. left: 450,
  41. top: 700
  42. }
  43. var enemies = [
  44. {left: 250, top: 200},
  45. {left: 350, top: 200},
  46. {left: 450, top: 200},
  47. {left: 250, top: 300},
  48. {left: 350, top: 300},
  49. {left: 450, top: 300}
  50. ]
  51. var missiles = []
  52. function drawPlayer() {
  53. content = "<div class='player' style='left: "+player.left+"px; top: "+player.top+"px'></div>";
  54. document.getElementById("players").innerHTML = content;
  55. }
  56. function drawEnemies() {
  57. content = "";
  58. for(var i=0; i<enemies.length; i++) {
  59. content += "<div class='enemy' style='left: "+enemies[i].left+"px; top: "+enemies[i].top+"px'></div>";
  60. }
  61. document.getElementById("enemies").innerHTML = content;
  62. }
  63. function drawMissiles() {
  64. content = "";
  65. for(var i=0; i<missiles.length; i++) {
  66. content += "<div class='missile' style='left: "+missiles[i].left+"px; top: "+missiles[i].top+"px'></div>";
  67. }
  68. document.getElementById("missiles").innerHTML = content;
  69. }
  70. function moveEnemies() {
  71. for(var i=0; i<enemies.length; i++) {
  72. enemies[i].top += 1;
  73. }
  74. }
  75. function moveMissiles() {
  76. for(var i=0; i<missiles.length; i++) {
  77. missiles[i].top -= 10;
  78. }
  79. }
  80. document.onkeydown = function(e) {
  81. if(e.keyCode == 37) {
  82. if(player.left > 0) {
  83. player.left -= 10;
  84. }
  85. }
  86. if(e.keyCode == 39) {
  87. if(player.left < 1180) {
  88. player.left += 10;
  89. }
  90. }
  91. if(e.keyCode == 38) {
  92. if(player.top > 570) {
  93. player.top -= 10;
  94. }
  95. }
  96. if(e.keyCode == 40) {
  97. if(player.top < 760) {
  98. player.top += 10;
  99. }
  100. }
  101. if(e.keyCode == 32) {
  102. missiles.push({left: player.left+51, top: player.top});
  103. drawMissiles();
  104. }
  105. drawPlayer();
  106. }
  107. function gameLoop() {
  108. moveEnemies();
  109. moveMissiles();
  110. drawPlayer();
  111. drawEnemies();
  112. drawMissiles();
  113. setTimeout(gameLoop, 50);
  114. }
  115. gameLoop();
  116. </script>
  117. </body>
  118. </html>
Add Comment
Please, Sign In to add comment