Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Génération de la graine
- math.randomseed(os.time())
- -- Les constantes de la fenêtre
- local WIDTH, HEIGHT = 800, 600
- local XC, YC = WIDTH / 2, HEIGHT / 2
- -- Modifiacation de la taille de la fenêtre
- love.window.setMode(WIDTH, HEIGHT)
- -- Le soleil
- local sun = {x = XC, y = YC, r = 50, color = {1, 0.81, 0.34, 1}}
- -- Les limites pour l'aléatoire
- local rMin, rMax = math.floor(sun.r / 10), math.floor(sun.r / 4) -- Les limites du rayon INFLUENCE BEAUCOUP le nombre de planètes à l'écran
- local vMin, vMax = 10, 50 -- vitesse en degré (qui sera convertie en radians lors du tirage)
- -- Le compteur pour éviter une boucle infinie
- local securityCounter = 5000
- -- La liste des planètes et le nombre attendu
- local listPlanets = {}
- local nbPlanets = 8
- -- La boucle de création des planètes
- while #listPlanets ~= nbPlanets do
- -- MAJ du compteur
- securityCounter = securityCounter - 1
- if securityCounter == 0 then
- break
- end
- -- Création d'une nouvell planète
- local newPlanet = {}
- newPlanet.r = math.random(rMin, rMax)
- newPlanet.x = math.random(newPlanet.r, WIDTH - newPlanet.r)
- newPlanet.y = math.random(newPlanet.r, HEIGHT - newPlanet.r)
- -- Ajout de la distane Soleil - planète
- newPlanet.d = ((newPlanet.x - sun.x)^2 + (newPlanet.y - sun.y)^2)^0.5
- -- En considérant que le soleil se trouve au centre de la fenêtre, cette distance doit être:
- -- supérieure au rayon du soleil
- -- inférieure à la plus petite moitié de dimension de la fenêtre
- local goodDistance = true
- if newPlanet.d <= (newPlanet.r + sun.r) or (newPlanet.d + newPlanet.r) >= math.min(WIDTH / 2, HEIGHT / 2) then
- goodDistance = false
- end
- -- Si la distance est correcte ...
- if goodDistance then
- -- ... on vérifie que la planète n'entre pas en collision avec celles déjà créées
- local collision = false
- for _, planet in ipairs(listPlanets) do
- local d = math.abs(planet.d - newPlanet.d)
- if d <= (planet.r + newPlanet.r) then
- collision = true
- break
- end
- end
- -- S'il n'y a pas de collision, on 'officialise' la planète ...
- if collision == false then
- newPlanet.v = math.rad(math.random(vMin, vMax))
- newPlanet.angle = math.atan2(sun.y - newPlanet.y, sun.x - newPlanet.x) -- https://love2d.org/wiki/General_math
- newPlanet.color = {math.random(), math.random(), math.random(), 1}
- -- ... et on l'ajoute à la liste
- table.insert(listPlanets, newPlanet)
- end
- end
- end
- function love.update(dt)
- for _, p in ipairs(listPlanets) do
- p.angle = p.angle + (p.v * dt)
- p.x, p.y = p.d * math.cos(p.angle) + sun.x, p.d * math.sin(p.angle) + sun.y
- end
- end
- function love.draw()
- love.graphics.setColor(1, 1, 1, 1)
- love.graphics.print("Nombre de planètes: " .. #listPlanets, 10, 10)
- love.graphics.print("Compteur sécurité: " .. securityCounter, 10, 30)
- -- Affichage du soleil
- love.graphics.setColor(sun.color)
- love.graphics.circle("fill", sun.x, sun.y, sun.r)
- -- Affichage des trajectoires & planètes
- for i, p in ipairs(listPlanets) do
- -- Trajectoires
- love.graphics.setColor(0.3, 0.3, 0.3, 0.5)
- love.graphics.circle("line", sun.x, sun.y, p.d)
- -- Planètes
- love.graphics.setColor(p.color)
- love.graphics.circle("fill", p.x, p.y, p.r)
- -- -- Le numéro de création
- -- love.graphics.setColor(1, 1, 1, 1)
- -- love.graphics.print(i, p.x, p.y)
- end
- end
Add Comment
Please, Sign In to add comment