Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. function getRandomColor() {
  2. var letters = '0123456789ABCDEF';
  3. var color = '#';
  4. for (var i = 0; i < 6; i++) {
  5. color += letters[Math.floor(Math.random() * 16)];
  6. }
  7. return color;
  8. }
  9.  
  10. function myBallDraw() {
  11. var canvas = document.getElementById('canvas');
  12. if (canvas.getContext) {
  13. var ctx = canvas.getContext('2d');
  14. }
  15.  
  16. var raf;
  17. var lives = 3;
  18.  
  19. function draw() {
  20. ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
  21. ctx.fillRect(0, 0, canvas.width, canvas.height);
  22. ball.draw();
  23. ball.x += ball.vx;
  24. ball.y += ball.vy;
  25. if(ball.y + ball.radius + ball.vy > canvas.height
  26. || ball.y - ball.radius + ball.vy < 0) {
  27. ball.vy = -ball.vy;
  28. }
  29.  
  30. if(ball.x + ball.radius + ball.vx > canvas.width
  31. || ball.x - ball.radius + ball.vx < 0) {
  32. ball.vx = -ball.vx;
  33. }
  34.  
  35.  
  36.  
  37. raf = window.requestAnimationFrame(draw);
  38. }
  39.  
  40.  
  41.  
  42.  
  43. var ball = {
  44. x: 100,
  45. y: 100,
  46. vx: 5,
  47. vy: 1,
  48. radius: 100,
  49. color: 'blue',
  50. is_mouse_over: function(x,y) {
  51. // return (x -radius) < xParam < (x+radius)
  52. // && ((y-radius) < yParam < (y+radius));
  53. return ((this.x -radius) < x < (this.x+radius))
  54. && ((this.y-radius) < y < (this.y+radius));
  55. },
  56. draw: function() {
  57. ctx.beginPath();
  58. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
  59. ctx.closePath();
  60. ctx.fillStyle = this.color;
  61. ctx.fill();
  62. }
  63. };
  64.  
  65. canvas.addEventListener('click', function(e){
  66. if((e.x - 10 < ball.x + ball.radius && e.x - 10 > ball.x - ball.radius)
  67. && (e.y - 10 < ball.y + ball.radius && e.y - 10 > ball.y - ball.radius)
  68. ){
  69. a = Math.random();
  70. while(a < .1)
  71. {
  72. a = Math.random();
  73. }
  74. ball.radius *= a;
  75. ball.color = getRandomColor();
  76. } else {
  77. lives--;
  78. }
  79.  
  80. if(lives < 1){
  81. //blabasd.dd
  82. location.reload();
  83. alert("You died");
  84. }
  85. });
  86.  
  87. draw();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement