Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. var canvas, ctx, container;
  2. canvas = document.createElement('canvas');
  3. canvas.width = 800;
  4. canvas.height = 800;
  5. ctx = canvas.getContext("2d");
  6. var ball = {
  7. y: 480
  8. };
  9. var touchGround = false;
  10. var pull= 0.43;
  11. var vy;
  12. var gravity = pull;
  13. var gameRunning = 0;
  14. var i = Math.floor(Math.random()*11);
  15. var timer;
  16. color = ["red","blue","green","yellow","purple","pink","silver","teal","turquoise","magenta","cyan", "black"];
  17.  
  18. function ballMovement() {
  19. vy += gravity;
  20. ball.y += vy;
  21.  
  22. if (ball.y + ball.radius > canvas.height) {
  23. ball.y = canvas.height - ball.radius;
  24. vy = 0;
  25.  
  26. var img = document.getElementById('gameOver');
  27. ctx.drawImage(gameOver, canvas.width/2-450, 100)
  28. ball.radius = 0;
  29. clearInterval(timer);
  30. gameRunning = 0;
  31.  
  32. }
  33. }
  34.  
  35. function init() {
  36. if(!document.querySelector('.container')){
  37. setupCanvas()
  38. var img = document.getElementById('gameOver');
  39. img.style.visibility = 'hidden';
  40. }
  41.  
  42. vy = -19;
  43. var y1 = 450
  44.  
  45. ball = {
  46. x: canvas.width/2,
  47. y: ball.y,
  48. radius: 20,
  49. status: 0,
  50. color: color[i]
  51. };
  52.  
  53. clearInterval(timer);
  54. timer = setInterval(draw, 1000 / 35);
  55. gameRunning = 1;
  56. }
  57.  
  58. function draw() {
  59. ctx.clearRect(0, 0, canvas.width, canvas.height);
  60. ctx.beginPath();
  61. ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2, false);
  62. ctx.fillStyle = ball.color;
  63. ctx.fill();
  64. ctx.closePath()
  65. ballMovement();
  66. }
  67.  
  68.  
  69. function setupCanvas() {
  70. container = document.createElement('div');
  71. container.className = "container";
  72. canvas.width = window.innerWidth;
  73. canvas.height = window.innerHeight;
  74. document.body.appendChild(container);
  75. container.appendChild(canvas);
  76. ctx.strokeStyle = "#ffffff";
  77. ctx.lineWidth = 2;
  78. }
  79.  
  80. window.onclick = function(){
  81. vy += (ball.y-(canvas.height-canvas.height))
  82. pull + 0.01;
  83. touchGround = false;
  84. if(!gameRunning){
  85. init();
  86. }
  87. else{
  88. vy = -19;
  89. }
  90.  
  91. vy+((canvas.height-canvas.height)-ball.y);
  92. }
  93. //Goal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement