Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. var c = document.getElementById("myCanvas")
  2. var ctx = c.getContext("2d")
  3.  
  4. var all_balls = []
  5. var explosions = []
  6. var level = 1
  7. var score = 0
  8. var can_click = false
  9.  
  10. buildBalls(4)
  11.  
  12. function gameLoop() {
  13. drawBackground()
  14. drawBalls()
  15. }
  16. setInterval(gameLoop, 10)
  17.  
  18. function buildBalls(number) {
  19. all_balls = []
  20. for (var i = 0; i < number; i++) {
  21. var ball = {
  22. x:Math.random() * c.width,
  23. y:Math.random() * c.height,
  24. x_speed:Math.random() * 3 + 1,
  25. y_speed:Math.random() * 3 + 1,
  26. color:"black",
  27. size:10
  28. }
  29. all_balls.push(ball)
  30. }
  31. }
  32.  
  33. function drawBalls() {
  34.  
  35.  
  36. for (var i = all_balls.length - 1; i >= 0; i--) {
  37. var ball = all_balls[i]
  38. updateBall(ball)
  39. drawObject(ball)
  40. }
  41. }
  42.  
  43. function updateBall(ball) {
  44. if (ball.x > c.width || ball.x < 0) {
  45. ball.x_speed = ball.x_speed * -1
  46. }
  47. if (ball.y > c.height || ball.y < 0) {
  48. ball.y_speed = ball.y_speed * -1
  49. }
  50.  
  51. ball.x = ball.x + ball.x_speed
  52. ball.y = ball.y + ball.y_speed
  53. }
  54.  
  55. function drawObject(obj) {
  56. ctx.beginPath()
  57. ctx.arc(obj.x,obj.y,obj.size,0,2*Math.PI)
  58. ctx.fillStyle = obj.color
  59. ctx.fill()
  60. }
  61.  
  62. function drawBackground() {
  63. ctx.beginPath()
  64. ctx.rect(0,0,c.width,c.height)
  65. ctx.fillStyle = "white"
  66. ctx.fill()
  67. }
  68.  
  69. c.onclick = function(event) {
  70. if (can_click) {
  71. can_click = false
  72. var x = event.pageX - c.offsetLeft
  73. var y = event.pageY - c.offsetTop
  74.  
  75. makeExplosion(x, y)
  76. }
  77. }
  78.  
  79. function makeExplosion(x, y) {
  80. var exp = {
  81.  
  82. }
  83. explosions.push(exp)
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement