Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>My awesome game</title>
  7. <style type="text/css">
  8.  
  9. canvas {
  10. margin: 0 auto;
  11. display: block;
  12. }
  13.  
  14. h1 {
  15. display: block;
  16. text-align: center;
  17. margin-top: -300px;
  18. }
  19. </style>
  20.  
  21. <body data-rsssl=1>
  22.  
  23. <script type="text/javascript">
  24.  
  25. var gameArea;
  26. var canvas;
  27. var ball = new Ball(50,50,10);
  28.  
  29. window.onload = init;
  30. var updateInterval = setInterval(update,1000/30);
  31.  
  32. function init(){
  33. initCanvas();
  34.  
  35. gameArea.fillStyle = "lightblue";
  36. gameArea.fillRect(0, 0, canvas.width, canvas.height);
  37.  
  38. ball.draw();
  39. }
  40.  
  41. function initCanvas(){
  42. canvas = document.createElement("canvas");
  43. context = canvas.getContext('2d');
  44. canvas.width=500;
  45. canvas.height=500;
  46. document.body.appendChild(canvas);
  47. gameArea = context;
  48. }
  49.  
  50. function update(){
  51. ball.update();
  52. }
  53.  
  54. /**
  55. * Ball Class
  56. */
  57. function Ball(x,y,radius){
  58.  
  59. this.x = x;
  60. this.y = y;
  61. this.radius = radius;
  62. this.color = 'darkgrey';
  63.  
  64. this.vx = 0;
  65. this.vy = 10;
  66.  
  67. this.draw = function(){
  68. gameArea.fillStyle = this.color;
  69. gameArea.beginPath();
  70. gameArea.arc(this.x,this.y,this.radius,0,Math.PI*2,true);
  71. gameArea.fill();
  72. }
  73.  
  74. this.update = function() {
  75. this.y = this.y + this.vy;
  76. this.x = this.x + this.vx;
  77. }
  78. }
  79.  
  80. </script>
  81.  
  82. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement