Advertisement
Guest User

Untitled

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