Advertisement
Guest User

HTMLGAME

a guest
Mar 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  5. <style>
  6. canvas {
  7. border:1px solid #d3d3d3;
  8. background-color: #f1f1f1;
  9. }
  10. </style>
  11. </head>
  12. <body onload="startGame()">
  13. <script>
  14.  
  15. var myGamePiece;
  16. var myObstacles = [];
  17. var myScore;
  18.  
  19. function startGame() {
  20. myGamePiece = new component(30, 30, "red", 10, 120);
  21. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  22. myGameArea.start();
  23. }
  24.  
  25. var myGameArea = {
  26. canvas : document.createElement("canvas"),
  27. start : function() {
  28. this.canvas.width = 480;
  29. this.canvas.height = 270;
  30. this.context = this.canvas.getContext("2d");
  31. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  32. this.frameNo = 0;
  33. this.interval = setInterval(updateGameArea, 20);
  34. },
  35. clear : function() {
  36. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  37. },
  38. stop : function() {
  39. clearInterval(this.interval);
  40. }
  41. }
  42.  
  43. function component(width, height, color, x, y, type) {
  44. this.type = type;
  45. this.width = width;
  46. this.height = height;
  47. this.speedX = 0;
  48. this.speedY = 0;
  49. this.x = x;
  50. this.y = y;
  51. this.update = function() {
  52. ctx = myGameArea.context;
  53. if (this.type == "text") {
  54. ctx.font = this.width + " " + this.height;
  55. ctx.fillStyle = color;
  56. ctx.fillText(this.text, this.x, this.y);
  57. } else {
  58. ctx.fillStyle = color;
  59. ctx.fillRect(this.x, this.y, this.width, this.height);
  60. }
  61. }
  62. this.newPos = function() {
  63. this.x += this.speedX;
  64. this.y += this.speedY;
  65. }
  66. this.crashWith = function(otherobj) {
  67. var myleft = this.x;
  68. var myright = this.x + (this.width);
  69. var mytop = this.y;
  70. var mybottom = this.y + (this.height);
  71. var otherleft = otherobj.x;
  72. var otherright = otherobj.x + (otherobj.width);
  73. var othertop = otherobj.y;
  74. var otherbottom = otherobj.y + (otherobj.height);
  75. var crash = true;
  76. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  77. crash = false;
  78. }
  79. return crash;
  80. }
  81. }
  82.  
  83. function updateGameArea() {
  84. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  85. for (i = 0; i < myObstacles.length; i += 1) {
  86. if (myGamePiece.crashWith(myObstacles[i])) {
  87. myGameArea.stop();
  88. return;
  89. }
  90. }
  91. myGameArea.clear();
  92. myGameArea.frameNo += 1;
  93. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  94. x = myGameArea.canvas.width;
  95. minHeight = 20;
  96. maxHeight = 200;
  97. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  98. minGap = 50;
  99. maxGap = 200;
  100. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  101. myObstacles.push(new component(10, height, "green", x, 0));
  102. myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  103. }
  104. for (i = 0; i < myObstacles.length; i += 1) {
  105. myObstacles[i].speedX = -1;
  106. myObstacles[i].newPos();
  107. myObstacles[i].update();
  108. }
  109. myScore.text="SCORE: " + myGameArea.frameNo;
  110. myScore.update();
  111. myGamePiece.newPos();
  112. myGamePiece.update();
  113. }
  114.  
  115. function everyinterval(n) {
  116. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  117. return false;
  118. }
  119.  
  120. function moveup() {
  121. myGamePiece.speedY = -1;
  122. }
  123.  
  124. function movedown() {
  125. myGamePiece.speedY = 1;
  126. }
  127.  
  128. function moveleft() {
  129. myGamePiece.speedX = -1;
  130. }
  131.  
  132. function moveright() {
  133. myGamePiece.speedX = 1;
  134. }
  135.  
  136. function clearmove() {
  137. myGamePiece.speedX = 0;
  138. myGamePiece.speedY = 0;
  139. }
  140. </script>
  141. <div style="text-align:center;width:480px;">
  142. <button onmousedown="moveup()" onmouseup="clearmove()" ontouchstart="moveup()">FEL</button><br><br>
  143. <button onmousedown="moveleft()" onmouseup="clearmove()" ontouchstart="moveleft()">BALRA</button>
  144. <button onmousedown="moveright()" onmouseup="clearmove()" ontouchstart="moveright()">JOBBRA</button><br><br>
  145. <button onmousedown="movedown()" onmouseup="clearmove()" ontouchstart="movedown()">LE</button>
  146. <form method="get" action="index.html">
  147. <button type="submit">Újraindítás</button>
  148. </form>
  149. </div>
  150. </body>
  151. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement