Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Arkanoid</title>
  8. <style>
  9. #game {
  10. box-sizing: border-box;
  11. position: relative;
  12. }
  13.  
  14. #game>* {
  15. box-sizing: border-box;
  16. position: absolute;
  17. }
  18.  
  19. .block {
  20. transition: opacity .5s, transform .5s;
  21. }
  22.  
  23. .block.hidden {
  24. opacity: 0;
  25. transform: scale(.5);
  26. }
  27.  
  28. .player {
  29. transform: scaleX(.85);
  30. border-radius: 4px;
  31. transition: transform .3s;
  32. }
  33.  
  34. .player.touched {
  35. transform: translateY(-10px);
  36. }
  37.  
  38. .ball {
  39. transform: scale(2);
  40. background: #B38750;
  41. border-radius: 50%;
  42. border: 1px solid #AB84E8;
  43. }
  44. </style>
  45. </head>
  46. <body onkeyup="onKeyUp(event)" onkeydown="onKeyDown(event)">
  47. <div style='color: #AB84E8;'>Your lifes: <span id='counter'></span> left</div>
  48. <button style="position: absolute; color: #AB84E8; background-color: white; left: 120px; top: 3px;" onclick="myMove()">Start game</button>
  49. <div id='game'></div>
  50. <script>
  51. const GAME_WIDTH = 800
  52. const GAME_HEIGHT = 600
  53. const BLOCK_WIDTH = 35
  54. const BLOCK_HEIGHT = 20
  55. const BLOCK_MARGIN = 4
  56. const LINE_WIDTH = 50
  57. const LINE_HEIGHT = 5
  58. const BALL_SIZE = 4
  59. const map = document.getElementById('game')
  60. map.style.width = GAME_WIDTH + 'px'
  61. map.style.height = GAME_HEIGHT + 'px'
  62. map.style.border = '1px solid #AB84E8'
  63. map.style.position = 'relative'
  64. map.style.boxSizing = 'border-box'
  65. function start(){
  66. createLines(8)
  67. }
  68. start()
  69.  
  70. function createLines(lines){
  71. for (let i = 0; i < lines; i++){
  72. for (let m = 0; m < GAME_WIDTH / (BLOCK_MARGIN+BLOCK_WIDTH)-1; m++){
  73. const block = document.createElement("div")
  74. block.classList.add('block')
  75. block.style.backgroundColor = "lightblue"
  76. block.style.width = BLOCK_WIDTH + 'px'
  77. block.style.height = BLOCK_HEIGHT + 'px'
  78. block.style.margin = BLOCK_MARGIN + 'px'
  79. block.style.borderRadius = "2px"
  80. block.style.top = 10 + (i) * (BLOCK_MARGIN + BLOCK_HEIGHT) + 'px'
  81. block.style.left = 7 + (m) * (BLOCK_MARGIN + BLOCK_WIDTH) + 'px'
  82. map.appendChild(block)
  83. }
  84. }
  85. }
  86.  
  87.  
  88. const player = document.createElement('div')
  89. player.classList.add('player')
  90. player.style.width = LINE_WIDTH + 'px'
  91. player.style.height = LINE_HEIGHT + 'px'
  92. player.style.backgroundColor = '#B38750'
  93. player.style.top = (GAME_HEIGHT - LINE_HEIGHT * 2) + 'px'
  94. player.style.left = (GAME_WIDTH - LINE_WIDTH) / 2 + 'px'
  95. map.appendChild(player)
  96.  
  97.  
  98. const ball = document.createElement('div')
  99. ball.style.width = BALL_SIZE + 'px'
  100. ball.style.height = BALL_SIZE + 'px'
  101. ball.classList.add('ball')
  102. ball.style.top = (GAME_HEIGHT - BALL_SIZE*2) - LINE_HEIGHT*2 + 'px'
  103. ball.style.left = (GAME_WIDTH - BALL_SIZE) / 2 + 'px'
  104. map.appendChild(ball)
  105.  
  106.  
  107. let leftArrow = false
  108. let rightArrow = false
  109.  
  110. function onKeyUp(e){
  111. if (e.key === "ArrowRight"){
  112. rightArrow = false
  113. } else if (e.key === "ArrowLeft"){
  114. leftArrow = false
  115. }
  116. }
  117. function onKeyDown(e){
  118. if (e.key === "ArrowRight"){
  119. rightArrow = true
  120. } else if (e.key === "ArrowLeft"){
  121. leftArrow = true
  122. }
  123. }
  124. map.onmousemove = (event) => {
  125.  
  126. player.style.left = Math.min(GAME_WIDTH - LINE_WIDTH -2,
  127. Math.max(0,event.clientX - LINE_WIDTH / 2)) + 'px'
  128. }
  129.  
  130.  
  131.  
  132. const DEFAULT_BALL_SPEED = 1
  133.  
  134. let ballSpeedX = -DEFAULT_BALL_SPEED;
  135. let ballSpeedY = -DEFAULT_BALL_SPEED;
  136.  
  137. let lifes = document.getElementById("counter").innerText = 5
  138. let inGame = true
  139. setInterval(() => {
  140. if (!inGame) return
  141. const playerLeft = parseInt(player.style.left)
  142. if (leftArrow) {
  143. player.style.left = Math.min(GAME_WIDTH - LINE_WIDTH - 2,
  144. Math.max(0, playerLeft - 4)) + 'px'
  145. }
  146. if (rightArrow) {
  147. player.style.left = Math.min(GAME_WIDTH - LINE_WIDTH - 2,
  148. Math.max(0, playerLeft + 4)) + 'px'
  149. }
  150. }, 10);
  151. function myMove(){
  152. setInterval(() => {
  153. if (!inGame) return
  154. const ballLeft = parseInt(ball.style.left)
  155. const ballTop = parseInt(ball.style.top)
  156. const centerBallX = ballLeft + BALL_SIZE / 2
  157. const centerBallY = ballTop + BALL_SIZE / 2
  158.  
  159.  
  160. if (ballLeft < 0)
  161. ballSpeedX = -ballSpeedX
  162.  
  163. if (ballTop < 0)
  164. ballSpeedY = -ballSpeedY
  165.  
  166. if (ballLeft >= GAME_WIDTH - BALL_SIZE){
  167. ballSpeedX = -ballSpeedX
  168. }
  169. if (ballTop >= GAME_HEIGHT + BALL_SIZE){
  170. if (lifes == 0){
  171. inGame = false
  172. alert("Przegrałeś")
  173. } else {
  174. ball.style.top = (GAME_HEIGHT - BALL_SIZE) /2 + 'px'
  175. ball.style.left = (GAME_WIDTH - BALL_SIZE) / 2 + 'px'
  176. document.getElementById('counter').innerText = --lifes
  177. player.style.left = (GAME_WIDTH - LINE_WIDTH) / 2 + 'px'
  178. alert("Odradzam cię")
  179. return;
  180. }
  181. }
  182. const playerLeft = parseInt(player.style.left)
  183. const playerTop = parseInt(player.style.top)
  184.  
  185. if (centerBallX > playerLeft && centerBallX + BALL_SIZE < playerLeft + LINE_WIDTH &&
  186. centerBallY + BALL_SIZE / 2 > playerTop && centerBallY - ballSpeedY < playerTop){
  187.  
  188. ballSpeedY = -ballSpeedY
  189.  
  190. player.classList.add('touched')
  191. setTimeout(() =>{
  192. play.classList.remove('touched')
  193. }, 300);
  194. }
  195. for (const b of document.querySelectorAll('.block:not(.hidden)')){
  196. const blockLeft = parseInt(b.style.left)
  197. const blockTop = parseInt(b.style.top)
  198. if (centerBallX>= blockLeft && centerBallX + BALL_SIZE <= blockLeft + BLOCK_WIDTH &&
  199. centerBallY >= blockTop && centerBallY + BALL_SIZE <= blockTop + BLOCK_HEIGHT) {
  200.  
  201. if (centerBallY - ballSpeedY >= blockTop && centerBallY - ballSpeedY + BALL_SIZE <= blockTop + BLOCK_HEIGHT)
  202. ballSpeedX = -ballSpeedX
  203. else
  204. ballSpeedY = -ballSpeedY
  205.  
  206. b.classList.add('hidden')
  207. break;
  208. }}
  209.  
  210. ball.style.top = ballTop + ballSpeedY + 'px'
  211. ball.style.left = ballLeft + ballSpeedX + 'px'
  212. }, 5)}
  213.  
  214.  
  215.  
  216.  
  217. </script>
  218. </body>
  219. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement