Advertisement
dwisnievsky

Gra - odbijanie

Oct 9th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <canvas id="gameCanvas" width="800" height="600"> </canvas>
  3. <script>
  4. var canvas;
  5. var canvasContext;
  6. var ballX = 50; // polozenie kuli w osi X
  7. var ballY = 50; // polozenie kuli w osi Y
  8. var ballSpeedX = 10; // szybkosc przemieszczania sie kuli w osi X
  9. var ballSpeedY = 4; // szybkosc przemieszczania sie kuli w osi X
  10.  
  11. // punkty startowe graczy
  12. var player1Score = 0;
  13. var player2Score = 0;
  14.  
  15. var paddle1Y = 250; //polozenie paletki gracza na osi Y
  16. var paddle2Y = 250; //polozenie paletki komputera na osi Y
  17. const PADDLE_THICKNESS = 10; //grubosc paletek
  18. const PADDLE_HEIGHT = 100; //wysokosc paletek
  19.  
  20.  
  21. //sprawdzanie polozenia myszki
  22. function calculateMousePos(evt) {
  23.     var rect = canvas.getBoundingClientRect();
  24.     var root = document.documentElement;
  25.     var mouseX = evt.clientX - rect.left - root.scrollLeft;
  26.     var mouseY = evt.clientY - rect.top - root.scrollTop;
  27.     return {
  28.         x:mouseX,
  29.         y:mouseY
  30.     };
  31. }
  32.  
  33. window.onload = function() {
  34.     canvas = document.getElementById('gameCanvas');
  35.     canvasContext = canvas.getContext('2d');
  36.  
  37.     var framesPerSecond = 60; //ilosc klatek na sekunde
  38.     setInterval(function() {
  39.             moveEverything();
  40.             drawEverything();  
  41.         }, 1000/framesPerSecond);
  42.  
  43.     canvas.addEventListener('mousemove',
  44.         function(evt) {
  45.             var mousePos = calculateMousePos(evt);
  46.             paddle1Y = mousePos.y - (PADDLE_HEIGHT/2);
  47.         });
  48. }
  49.  
  50. // reset pozycji kuli
  51. function ballReset() {
  52.     ballSpeedX = -ballSpeedX;
  53.     ballX = canvas.width/2;
  54.     ballY = canvas.height/2;
  55. }
  56.  
  57. // automatyczne sterowanie
  58. function computerMovement() {
  59.     var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
  60.     if(paddle2YCenter < ballY - 35) {
  61.         paddle2Y = paddle2Y + 6;
  62.     } else if(paddle2YCenter > ballY + 35) {
  63.         paddle2Y = paddle2Y - 6;
  64.     }
  65. }
  66.  
  67. function moveEverything() {
  68.     computerMovement();
  69.  
  70.     ballX = ballX + ballSpeedX;
  71.     ballY = ballY + ballSpeedY;
  72.    
  73.     if(ballX < 0) {
  74.         if(ballY > paddle1Y &&
  75.             ballY < paddle1Y+PADDLE_HEIGHT) {
  76.             ballSpeedX = -ballSpeedX;
  77.         } else {
  78.             ballReset();
  79.             player2Score++;
  80.         }
  81.     }
  82.     if(ballX > canvas.width) {
  83.         if(ballY > paddle2Y &&
  84.             ballY < paddle2Y+PADDLE_HEIGHT) {
  85.             ballSpeedX = -ballSpeedX;
  86.         } else {
  87.             ballReset();   
  88.             player1Score++;
  89.         }
  90.     }
  91.     if(ballY < 0) {
  92.         ballSpeedY = -ballSpeedY;
  93.     }
  94.     if(ballY > canvas.height) {
  95.         ballSpeedY = -ballSpeedY;
  96.     }
  97. }
  98.  
  99. function drawEverything() {
  100.     // czarne tlo
  101.     colorRect(0,0,canvas.width,canvas.height,'black');
  102.  
  103.     // lewa paletka
  104.     colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  105.  
  106.     // prawa paletka (sterowana przez komputer)
  107.     colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');
  108.  
  109.     // pilka
  110.     colorCircle(ballX, ballY, 10, '#0066CC');
  111.  
  112. //tablica punktow
  113.     canvasContext.fillText(player1Score, 100, 100);
  114.     canvasContext.fillText(player2Score, canvas.width-100, 100);
  115. }
  116.  
  117. function colorCircle(centerX, centerY, radius, drawColor) {
  118.     canvasContext.fillStyle = drawColor;
  119.     canvasContext.beginPath();
  120.     canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2,true);
  121.     canvasContext.fill();
  122. }
  123.  
  124. function colorRect(leftX,topY, width,height, drawColor) {
  125.     canvasContext.fillStyle = drawColor;
  126.     canvasContext.fillRect(leftX,topY, width,height);
  127. }
  128. </script>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement