Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> Drawing</title>
  6. <script type="text/javascript">
  7. window.onload = function(){
  8. function init(ctx){
  9. window.setInterval(ball.draw(), 1000);
  10. }
  11. function random(min,max){
  12. return Math.random() *(max-min) + min
  13. }
  14. var canvas = document.getElementById('canvas');
  15. var ctx = canvas.getContext('2d');
  16. var raf;
  17.  
  18. var ball = {
  19. x: 100,
  20. y: 100,
  21. vx: 5,
  22. vy: 2,
  23. radius: 50,
  24. color: 'blue',
  25. is_mouse_over: function(x,y){
  26. return (this.x - this.radius) < x < (this.x + this.radius)
  27. && (this.y -this.radius) < y < (this.y+this.radius);
  28. },
  29. draw: function() {
  30. ctx.beginPath();
  31. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
  32. ctx.closePath();
  33. ctx.fillStyle = this.color;
  34. ctx.fill();
  35. }
  36. };
  37.  
  38. function draw() {
  39. ctx.clearRect(0,0, canvas.width, canvas.height);
  40. ball.draw();
  41. ball.x += ball.vx;
  42. ball.y += ball.vy;
  43. if (ball.y + ball.vy > canvas.height || ball.y + ball.vy < 0) {
  44. ball.vy = -ball.vy;
  45. }
  46. if (ball.x + ball.vx > canvas.width || ball.x + ball.vx < 0) {
  47. ball.vx = -ball.vx;
  48. }
  49. raf = window.requestAnimationFrame(draw);
  50. }
  51. draw();
  52. function getMousePos(canvas,evt){
  53. var rect = canvas.getBoundingClientRect();
  54. return{
  55. x: evt.clientX - rect.left,
  56. y: evt.clientY - rect.top
  57. };
  58. }
  59. var lives = 10;
  60. var success = 0;
  61. var wins = 0;
  62. var loses = 0;
  63.  
  64. canvas.addEventListener('click', function(e){
  65. var mousePos = getMousePos(canvas, e);
  66. if((ball.x - ball.radius < mousePos.x && mousePos.x < ball.x + ball.radius) && (ball.y - ball.radius < mousePos.y && mousePos.y < ball.y + ball.radius)){
  67. if(ball.color == 'red'){
  68. ball.color = 'blue';
  69. }else{
  70. ball.color = 'red';
  71. }
  72. ball.radius *= 7/9;
  73. success++;
  74. if(success == 5){
  75. wins++;
  76. lives += 10;
  77. ball.radius = 50;
  78. ball.vx *= 1.2;
  79. ball.vy *= 1.2;
  80. success = 0;
  81. }
  82.  
  83. }else{
  84. if(lives == 0){
  85. loses++;
  86. success = 0;
  87. lives = 11;
  88. ball.radius = 50;
  89. }
  90. lives--;
  91.  
  92. }
  93. document.getElementById("success").innerHTML = success;
  94. document.getElementById("wins").innerHTML = wins;
  95. document.getElementById("loses").innerHTML = loses;
  96. document.getElementById("lives").innerHTML = lives;
  97.  
  98.  
  99. });
  100.  
  101. ball.draw();
  102. }
  103. </script>
  104. <style type="text/css">
  105. canvas { border: 2px solid pink; }
  106. </style>
  107. </head>
  108. <body>
  109. <h1>Reach 5 points to proceed to next level</h1>
  110. <canvas id="canvas" width="800" height="600" ></canvas>
  111. <h2 style = "color: green;">Lives:<span id='lives'>10</span></h2>
  112. <h2 style = "color: green;">Points:<span id='success'>0</span></h2>
  113. <h2 style = "color: green;">Level:<span id='wins'>0</span></h2>
  114. <h2 style = "color: red;">Loses:<span id='loses'>0</span></h2>
  115. </body>
  116.  
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement