paddle.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. function Paddle(options) {
  2. if (typeof(options) == 'undefined') options={};
  3. var x = options.x !== undefined ? options.x : 0;
  4. var y = options.y !== undefined ? options.y : 0;
  5. var sx = options.sx !== undefined ? options.sx : 0;
  6. var size = options.size !== undefined ? options.size : 0;
  7. function movePaddle(dir) {
  8. x += dir * sx;
  9. }
  10. function getX1() {
  11. return x - (size / 2);
  12. }
  13. function getX2() {
  14. return x + (size / 2);
  15. }
  16. function getY() {
  17. return y;
  18. }
  19. function increaseDifficulty() {
  20. size -= 4;
  21. if (size < 20) {
  22. size = 20;
  23. }
  24. sx += 2;
  25. }
  26. function update(ctx) {
  27. if (x <= (size / 2)) {
  28. x = size / 2;
  29. }
  30. if (x >= (ctx.canvas.width - (size / 2))) {
  31. x = (ctx.canvas.width - (size / 2));
  32. }
  33. }
  34. function draw(ctx) {
  35. ctx.beginPath();
  36. ctx.fillStyle = '#000000';
  37. ctx.strokeStyle = '#000000';
  38. ctx.lineWidth = 5;
  39. ctx.moveTo(x - size / 2, y);
  40. ctx.lineTo(x + size / 2, y);
  41. ctx.stroke();
  42. }
  43. return {
  44. 'x': x,
  45. 'y': y,
  46. 'sx': sx,
  47. 'size': size,
  48. 'movePaddle': movePaddle,
  49. 'getX1': getX1,
  50. 'getX2': getX2,
  51. 'getY': getY,
  52. 'increaseDifficulty': increaseDifficulty,
  53. 'update': update,
  54. 'draw': draw
  55. };
  56. }