Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. var s = 0;
  19.  
  20.  
  21. function draw() {
  22. ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
  23. ctx.fillRect(0, 0, canvas.width, canvas.height);
  24. ball.draw();
  25. ball.x += ball.vx;
  26. ball.y += ball.vy;
  27. if(ball.y + ball.radius + ball.vy > canvas.height
  28. || ball.y - ball.radius + ball.vy < 0) {
  29. ball.vy = -ball.vy;
  30. }
  31.  
  32. if(ball.x + ball.radius + ball.vx > canvas.width
  33. || ball.x - ball.radius + ball.vx < 0) {
  34. ball.vx = -ball.vx;
  35. }
  36.  
  37. ctx.font = '32px rtl';
  38. ctx.fillText(lives + ' tries until you DIE!!! ', 10, 50);
  39. ctx.font = '48px rtl';
  40. ctx.fillText( ' score: ' + s, 500, 50);
  41.  
  42. raf = window.requestAnimationFrame(draw);
  43. }
  44.  
  45.  
  46. var ball = {
  47. x: 100,
  48. y: 100,
  49. vx: 5,
  50. vy: 1,
  51. radius: 100,
  52. color: 'blue',
  53. is_mouse_over: function(x,y) {
  54. // return (x -radius) < xParam < (x+radius)
  55. // && ((y-radius) < yParam < (y+radius));
  56. return ((this.x -radius) < x < (this.x+radius))
  57. && ((this.y-radius) < y < (this.y+radius));
  58. },
  59. draw: function() {
  60. ctx.beginPath();
  61. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
  62. ctx.closePath();
  63. ctx.fillStyle = this.color;
  64. ctx.fill();
  65. }
  66. };
  67.  
  68. canvas.addEventListener('click', function(e){
  69. if((e.x - 10 < ball.x + ball.radius && e.x - 10 > ball.x - ball.radius)
  70. && (e.y - 10 < ball.y + ball.radius && e.y - 10 > ball.y - ball.radius)
  71. ){
  72. /*a = Math.random()*1.5;
  73. while(a < .1 || a > 2)
  74. {
  75. a = Math.random();
  76. }*/
  77. ball.radius *= Math.floor(Math.random() * 1.5) + .5;
  78. ball.color = getRandomColor();
  79. s ++;
  80. } else {
  81. lives--;
  82. var i = 1;
  83.  
  84.  
  85. }
  86.  
  87. if(lives < 1){
  88. //blabasd.dd
  89. location.reload();
  90. alert("You died");
  91. }
  92. });
  93.  
  94. draw();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement