Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
  2. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.js"></script>
  3. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.sound.js"></script>
  4. <script src="https://cdn.jsdelivr.net/gh/bmoren/p5.collide2D@0.5/p5.collide2d.js"></script>
  5. <script>
  6.  
  7.  
  8. var bricks = []
  9.  
  10. var ball
  11.  
  12. var numberofrows = 8
  13.  
  14.  
  15. function setup() {
  16. createCanvas(600, 600)
  17.  
  18.  
  19. ball = {
  20. x: 300,
  21. y: 400,
  22. speedx: random(-2, 2),
  23. speedy: random(0, 5)
  24. }
  25.  
  26. for (var i = 0; i < numberofrows; i++) {
  27. makeBrickRow()
  28. }
  29.  
  30. console.log(bricks)
  31.  
  32. }
  33.  
  34.  
  35. function draw() {
  36. background(51)
  37.  
  38. fill("blue")
  39. rectMode(CENTER)
  40. rect(mouseX, 550, 80, 10)
  41.  
  42. fill("red")
  43. ellipse(ball.x, ball.y, 20, 20)
  44. ball.x += ball.speedx
  45. ball.y += ball.speedy
  46.  
  47. if (ball.x > width || ball.x < 0) {
  48. ball.speedx = -ball.speedx
  49. }
  50.  
  51. if (ball.y >= 535 && ball.y <= 545 && ball.x > mouseX - 40 && ball.x < mouseX + 40) {
  52. ball.speedy = -ball.speedy
  53. ball.speedx *= 1.1
  54. console.log(ball.y)
  55. }
  56.  
  57. if (ball.y > height || ball.y < 0) {
  58. ball.speedy = -ball.speedy
  59. }
  60. // 10 rows of bricks, 12 bricks per row
  61. stroke("black")
  62. rectMode(CORNER)
  63. for (var i = 0; i < bricks.length; i++) {
  64.  
  65. var currentrow = bricks[i]
  66. for (var j = 0; j < currentrow.length; j++) {
  67. if (currentrow[j] == true) {
  68. var brickwidth = width / 12
  69. var brickheight = 250 / numberofrows
  70. var x = j*brickwidth
  71. var y = i*brickheight
  72. fill("green")
  73. rect(x, y, brickwidth, brickheight)
  74.  
  75. if (collideRectCircle(x, y, brickwidth, brickheight, ball.x, ball.y, 20)) {
  76. ball.speedx = -ball.speedx
  77. ball.speedy = -ball.speedy
  78. bricks[i][j] = false
  79. }
  80.  
  81. }
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88. }
  89.  
  90.  
  91. function makeBrickRow() {
  92. var row = []
  93. for (var i = 0; i < 12; i++) {
  94. row.push(true)
  95. }
  96. bricks.push(row)
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement