pong.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. pong.js
  3. Author: tobychui
  4. */
  5. function newPongGame(targetDom, width=200, height=400) {
  6. console.log("Pong Loaded")
  7. const game = {};
  8. game.dom = targetDom;
  9. //Initiate the DOM element css
  10. $(game.dom).css({
  11. "backdrop-filter":"blur(5px)",
  12. "background-color":"rgba(26, 26, 26, 0.7)",
  13. "width": width,
  14. "height":height
  15. });
  16. //Inject the main canvas
  17. var canvas = document.createElement('canvas');
  18. canvas.width = width;
  19. canvas.height = height;
  20. canvas.style.position = "absolute";
  21. canvas.style.top = "0px";
  22. canvas.style.left = "0px";
  23. $(canvas).css("border", "1px solid black");
  24. targetDom.appendChild(canvas);
  25. var ctx = canvas.getContext("2d");
  26. game.canvas = canvas;
  27. game.ctx = ctx;
  28. game.pause = false;
  29. //Create game state
  30. game.state = {
  31. player: {
  32. x: width/2,
  33. y: height - 30
  34. },
  35. cpu: {
  36. x: width/2,
  37. y: 30
  38. },
  39. ball: {
  40. x: width/2,
  41. y: height/2,
  42. dx: 1,
  43. dy: 1
  44. }
  45. }
  46. function drawCenterLine(){
  47. ctx.beginPath();
  48. ctx.setLineDash([4, 8]);
  49. ctx.moveTo(0, height/2);
  50. ctx.lineTo(width, height/2);
  51. ctx.strokeStyle = '#ffffff';
  52. ctx.stroke();
  53. }
  54. function drawPlate(x,y){
  55. var width = 40;
  56. var height = 6;
  57. x = x - width / 2;
  58. y = y - height / 2;
  59. game.ctx.beginPath();
  60. game.ctx.moveTo(x, y);
  61. game.ctx.lineTo(x + width, y);
  62. game.ctx.lineTo(x + width, y + height);
  63. game.ctx.lineTo(x, y + height);
  64. game.ctx.closePath();
  65. // the outline
  66. game.ctx.lineWidth = 1;
  67. game.ctx.strokeStyle = '#666666';
  68. game.ctx.stroke();
  69. // the fill color
  70. game.ctx.fillStyle = "#ffffff";
  71. game.ctx.fill();
  72. }
  73. function drwaBall(x,y){
  74. game.ctx.beginPath();
  75. game.ctx.arc(x, y, 8, 0, 2 * Math.PI);
  76. game.ctx.stroke();
  77. game.ctx.fillStyle = "#ffffff";
  78. game.ctx.fill();
  79. }
  80. function clearScreen(){
  81. game.ctx.clearRect(0, 0, canvas.width, canvas.height);
  82. }
  83. function resetGame(){
  84. var dx = 1;
  85. if (Math.random() < 0.5){
  86. dx = -1;
  87. }
  88. game.state.ball = {
  89. x: width/2,
  90. y: height/2,
  91. dx: dx,
  92. dy: game.state.ball.dy * -1
  93. }
  94. }
  95. //Register functions on that DOM
  96. game.render = function() {
  97. clearScreen();
  98. drawCenterLine();
  99. //Move the ball
  100. game.state.ball.x += game.state.ball.dx;
  101. game.state.ball.y += game.state.ball.dy;
  102. if (game.state.ball.x < 8){
  103. game.state.ball.x = 8;
  104. game.state.ball.dx = -1 * game.state.ball.dx;
  105. }else if (game.state.ball.x > width - 8){
  106. game.state.ball.x = width - 8;
  107. game.state.ball.dx = -1 * game.state.ball.dx;
  108. }
  109. if (game.state.ball.y < 0){
  110. //Player win
  111. resetGame();
  112. }else if (game.state.ball.y > height){
  113. //CPU win
  114. resetGame();
  115. }
  116. //Move the CPU plate position
  117. if (game.state.cpu.x > game.state.ball.x){
  118. game.state.cpu.x -= 0.6;
  119. }else if (game.state.cpu.x < game.state.ball.x){
  120. game.state.cpu.x += 0.6;
  121. }
  122. drawPlate(game.state.player.x, game.state.player.y);
  123. drawPlate(game.state.cpu.x, game.state.cpu.y);
  124. drwaBall(game.state.ball.x, game.state.ball.y);
  125. if (game.paused != true){
  126. window.requestAnimationFrame(game.render);
  127. }
  128. };
  129. window.requestAnimationFrame(game.render);
  130. return game;
  131. }