Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5.  
  6. <style>
  7. </style>
  8. </head>
  9.  
  10. <body>
  11. <script>
  12. class Ball {
  13. constructor (xx, yy,dir) {
  14. var self = this;
  15. this.x = (Math.random() * ((window.innerHeight-50)-100) + 100);
  16. this.y = (Math.random() * ((window.innerWidth-59)-100) + 100);
  17. this.dir = Math.random() * (360 - 0) + 0;
  18. this.pocet = 0;
  19.  
  20. this.rootElement = document.createElement('div');
  21.  
  22. this.pocitadlo = document.createElement('span');
  23. this.pocitadlo.innerHTML = "0";
  24.  
  25. this.rootElement.style.position = 'absolute';
  26. document.body.appendChild (this.rootElement);
  27.  
  28. this.img = document.createElement('img');
  29. this.img.src = 'ball.png';
  30. this.rootElement.style.left = self.y + 'px';
  31. this.rootElement.style.top = self.x + 'px';
  32.  
  33.  
  34. this.rootElement.style.transform = 'rotate(' + self.dir + 'deg)';
  35. this.rootElement.appendChild (this.img);
  36. this.rootElement.appendChild (this.pocitadlo);
  37. setInterval(function(){
  38. self.forward(5)},30
  39. );
  40.  
  41. }
  42.  
  43.  
  44.  
  45. }
  46.  
  47. Ball.prototype.changePos = function(x,y){
  48. this.x = x;
  49. this.y = y;
  50. this.rootElement.style.left = this.y + 'px';
  51. this.rootElement.style.top = this.x + 'px';
  52. }
  53. Ball.prototype.changeDir = function(dir){
  54. this.dir = dir;
  55. this.rootElement.style.transform = 'rotate(' + this.dir + 'deg)';
  56. }
  57.  
  58. Ball.prototype.forward = function(d){
  59. this.d = d;
  60. if(this.x < 0){
  61. this.changeDir(180 - this.dir);
  62. }
  63. else if(this.x +100 > window.innerHeight){
  64. this.changeDir(180 -this.dir);
  65. }
  66.  
  67. else if(this.y < 0){
  68. this.changeDir((-this.dir));
  69. }
  70. else if(this.y +100 > window.innerHeight){
  71. this.changeDir((-this.dir));
  72. }
  73. var xx = 1 * Math.cos( (this.dir * Math.PI/180))*this.d;
  74. var yy = 1 * Math.sin( (this.dir * Math.PI/180))*this.d;
  75. this.changePos(this.x +xx, this.y + yy);
  76.  
  77. }
  78.  
  79.  
  80. function Bar () {
  81. var self = this;
  82.  
  83. this.x = 0;
  84. this.y = window.innerHeight - 100;
  85. this.w = 200;
  86. this.h = 20;
  87.  
  88. this.rootElem = document.createElement ('div');
  89. this.rootElem.style.backgroundColor = 'black';
  90. this.rootElem.style.position = 'absolute';
  91. this.rootElem.style.width = this.w + 'px';
  92. this.rootElem.style.height = this.h + 'px';
  93. this.changePos (this.x, this.y);
  94. document.body.appendChild (this.rootElem);
  95.  
  96. window.addEventListener ('keydown', function (e) {
  97. switch (e.keyCode) {
  98. case 37:
  99. if (self.x >= 20) self.changePos (self.x - 20, self.y);
  100. break;
  101. case 39:
  102. if (self.x + 20 + self.w <= window.innerWidth) self.changePos (self.x + 20, self.y);
  103. break;
  104. }
  105. });
  106. }
  107. Bar.prototype.changePos = function (x, y) {
  108. this.x = x;
  109. this.y = y;
  110. this.rootElem.style.left = Math.round (this.x)+'px';
  111. this.rootElem.style.top = Math.round (this.y)+'px';
  112. }
  113.  
  114.  
  115.  
  116.  
  117. document.body.style.overflow = 'hidden';
  118.  
  119. var bar = new Bar ();
  120. var a = new Ball()
  121.  
  122.  
  123. </script>
  124. </body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement