Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. HTML
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8"/>
  7. <title>Battles</title>
  8. <script type="text/javascript" src="balls.js">
  9.  
  10. </script>
  11. <style type="text/css">
  12. canvas { border: 1px solid black; }
  13. </style>
  14. </head>
  15.  
  16. <body onload="myBallDraw();">
  17. <canvas id="tutorial" width="800" height="600"></canvas>
  18. </body>
  19. </html>
  20.  
  21. ------------------------------------------------------------------------------------------------------
  22. JS
  23. function myBallDraw()
  24. {
  25. var canvas = document.getElementById('tutorial');
  26. if (canvas.getContext) {
  27. var ctx = canvas.getContext('2d');
  28. }
  29.  
  30. var raf;
  31. var lives = 3;
  32.  
  33. function draw() {
  34. ctx.clearRect(0, 0, canvas.width, canvas.height);
  35. ball.draw();
  36. ball.x += ball.vx;
  37. ball.y += ball.vy;
  38.  
  39. if(ball.y + ball.radius + ball.vy > canvas.height
  40. || ball.y - ball.radius + ball.vy < 0) {
  41. ball.vy = -ball.vy;
  42. }
  43.  
  44. if(ball.x + ball.radius + ball.vx > canvas.width
  45. || ball.x - ball.radius + ball.vx < 0) {
  46. ball.vx = -ball.vx;
  47. }
  48.  
  49. raf = window.requestAnimationFrame(draw);
  50. }
  51.  
  52.  
  53.  
  54.  
  55. var ball = {
  56. x: 100,
  57. y: 100,
  58. vx: 5,
  59. vy: 1,
  60. radius: 100,
  61. color: 'blue',
  62. is_mouse_over: function(x,y) {
  63. // return (x -radius) < xParam < (x+radius)
  64. // && ((y-radius) < yParam < (y+radius));
  65. return ((this.x -radius) < x < (this.x+radius))
  66. && ((this.y-radius) < y < (this.y+radius));
  67. },
  68. draw: function() {
  69. ctx.beginPath();
  70. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
  71. ctx.closePath();
  72. ctx.fillStyle = this.color;
  73. ctx.fill();
  74. }
  75. };
  76.  
  77. canvas.addEventListener('click', function(e)
  78. {
  79.  
  80. if((e.x - 10 < ball.x + ball.radius && e.x - 10 > ball.x - ball.radius)
  81. && (e.y - 10 < ball.y + ball.radius && e.y - 10 > ball.y - ball.radius)
  82. ){
  83. ball.radius *= Math.random();
  84. ball.color = "#"+Math.floor(Math.random()*16777215).toString(16);
  85. ball.vx+=4;
  86. ball.vy+=4;
  87. } else {
  88. lives--;
  89. }
  90.  
  91. if(lives < 1){
  92. //blabasd.dd
  93. location.reload();
  94. alert("You died");
  95. }
  96. });
  97.  
  98. draw();
  99. }
  100. myBallDraw();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement