Advertisement
Guest User

Code

a guest
Dec 4th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Document</title>
  9. <style>
  10. body {
  11. overflow: hidden;
  12. margin: 0px;
  13. }
  14. </style>
  15. </head>
  16.  
  17. <body>
  18.  
  19.  
  20. <canvas></canvas>
  21.  
  22. <script>
  23.  
  24. class Canvas {
  25. constructor(canvas) {
  26. this.canvas = canvas;
  27. this.ctx = canvas.getContext('2d');
  28.  
  29. this.shapes = [];
  30. console.log(this.shapes);
  31. }
  32.  
  33. addShape(shape) {
  34. this.shapes.push(shape);
  35. }
  36.  
  37. removeShape(shape) {
  38. this.shapes = this.shapes.filter(s => s !== shape);
  39. }
  40.  
  41. clear() {
  42. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  43. }
  44.  
  45. draw() {
  46. this.clear();
  47. this.shapes.forEach(shape => shape.draw(this.ctx));
  48. }
  49.  
  50. resize() {
  51. this.canvas.width = window.innerWidth;
  52. this.canvas.height = window.innerHeight;
  53. }
  54. }
  55.  
  56. class Player {
  57. constructor(x, y, radius, color) {
  58. this.x = x;
  59. this.y = y;
  60. this.radius = radius;
  61. this.color = color;
  62.  
  63. // velocity cause y not after all awhitehatcoder go BRRR kdfaijsdfkla
  64. this.dx = 5;
  65. this.dy = 5;
  66. }
  67.  
  68. draw(ctx) {
  69. ctx.beginPath();
  70. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  71. ctx.fillStyle = this.color;
  72. ctx.fill();
  73. ctx.closePath();
  74. // im goin crazy cause i am smadfsd
  75. this.update();
  76. }
  77.  
  78. update() {
  79. window.addEventListener('keydown', (e) => {
  80. if (event.defaultPrevented) {
  81. return; // Do nothing if the event was already processed cause idk what im doing HA
  82. }
  83. switch (e.key) {
  84. case 'ArrowLeft':
  85. this.x -= this.dx;
  86. console.log("balls");
  87. break;
  88. case 'ArrowRight':
  89. this.x += this.dx;
  90. console.log("balls");
  91. break;
  92. case 'ArrowUp':
  93. this.y -= this.dy;
  94. break;
  95. case 'ArrowDown':
  96. this.y += this.dy;
  97. break;
  98. default:
  99. return;
  100. }
  101.  
  102. event.preventDefault();
  103. });
  104. }
  105. }
  106.  
  107. const player1 = new Player(500, 500, 50, 'gold')
  108.  
  109. const canvas = new Canvas(document.querySelector('canvas'));
  110. canvas.resize();
  111. canvas.addShape(player1);
  112.  
  113. function animate() {
  114. canvas.draw();
  115. requestAnimationFrame(animate);
  116. }
  117.  
  118. animate();
  119. </script>
  120. </body>
  121.  
  122. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement