Advertisement
Guest User

Casse-briques

a guest
Mar 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. local largeur
  2. local hauteur
  3.  
  4. local pad = {}
  5. pad.x = 0
  6. pad.y = 0
  7. pad.largeur = 80
  8. pad.hauteur = 20
  9.  
  10. local balle = {}
  11. balle.x = 0
  12. balle.y = 0
  13. balle.vx = 0
  14. balle.vy = 0
  15. balle.angle = 0
  16. balle.angleNeutre = 0 - ((math.pi*2) / 2)
  17. balle.vitesse = 0
  18. balle.colle = true
  19. balle.rayon = 10
  20. degre45 = (math.pi*2) / (360/45)
  21.  
  22. local brique = {}
  23. local niveau = {}
  24.  
  25. function Demarre()
  26. pad.y = hauteur - (15/2)
  27.  
  28. brique.hauteur = 25
  29. brique.largeur = largeur / 15
  30.  
  31. balle.angle = 0 - degre45
  32. balle.vitesse = 300
  33. balle.colle = true
  34.  
  35. for l = 1,6 do
  36. niveau[l] = {}
  37. for c = 1,15 do
  38. niveau[l][c] = 1
  39. end
  40. end
  41. end
  42.  
  43. function love.load()
  44. largeur = love.graphics.getWidth()
  45. hauteur = love.graphics.getHeight()
  46. Demarre()
  47. end
  48.  
  49. function love.update(dt)
  50. pad.x = love.mouse.getX()
  51. if pad.x < 0 then pad.x = 0 end
  52. if pad.x > largeur then pad.x = largeur
  53.  
  54. end
  55. if balle.colle== true then
  56. balle.x = pad.x
  57. balle.y = pad.y - (pad.hauteur/2) - (balle.rayon)
  58. else
  59. balle.x = balle.x + balle.vx*dt
  60. balle.y = balle.y + balle.vy*dt
  61. local c = math.floor((balle.x / brique.largeur)) + 1
  62. local l = math.floor((balle.y / brique.hauteur)) + 1
  63.  
  64. if l > 0 and l <= #niveau and c >= 1 and c <= 15 then
  65. if niveau[l][c] == 1 then
  66. niveau [l][c] = 0
  67. local brickx = ((c-1) * brique.largeur) + brique.largeur/2
  68. local bricky = ((l-1) * brique.hauteur) + brique.hauteur/2
  69. balle.vy = 0 - balle.vy
  70. end
  71. end
  72.  
  73. if balle.y > (pad.y - pad.hauteur/2) - balle.rayon then
  74. local distancex = math.abs(pad.x - balle.x)
  75. if distancex < pad.largeur / 2 then
  76. balle.vy = 0 - balle.vy
  77. balle.y = (pad.y - pad.hauteur/2) - balle.rayon
  78. end
  79. end
  80.  
  81. if balle.x > largeur or balle.x < 0 then balle.vx = 0 - balle.vx end
  82. if balle.y < 0 then balle.vy = 0 - balle.vy end
  83.  
  84. if balle.y > hauteur then
  85. balle.colle = true
  86. end
  87. end
  88.  
  89. end
  90.  
  91. function love.draw()
  92. local bx, by = 0,0
  93. for l = 1,6 do
  94. bx = 0
  95. for c = 1,15 do
  96. if niveau[l][c] == 1 then
  97. love.graphics.rectangle("fill",bx + 1, by + 1, brique.largeur - 2, brique.hauteur - 2)
  98. end
  99. bx = bx + brique.largeur
  100. end
  101. by = by + brique.hauteur
  102. end
  103.  
  104. love.graphics.rectangle("fill", pad.x - (pad.largeur/2), pad.y - (pad.hauteur/2), pad.largeur, pad.hauteur)
  105. love.graphics.circle("fill", balle.x, balle.y, balle.rayon)
  106.  
  107. end
  108. function love.mousepressed(px, py, pn)
  109. if balle.colle == true then
  110. balle.colle = false
  111. balle.vx = balle.vitesse * math.cos(balle.angle)
  112. balle.vy = balle.vitesse * math.sin(balle.angle)
  113. end
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement