Page 1 of 1
How to apply random sizes and colors to points
Posted: Thu Jul 15, 2021 3:02 pm
by klewis
Hello, I'm trying to randomize sizes and colors for the use of love.graphics.points()
I've been playing with the following logic to give it a go. What can I do to get closer to my goal?
Code: Select all
function love.load()
local screen_width, screen_height = love.graphics.getDimensions()
local max_stars = 100 -- how many stars we want
stars = {} -- table which will hold our stars
for i = 1, max_stars do -- generate the coords of our stars
local x = love.math.random(5, screen_width - 5) -- generate a "random" number for the x coord of this star
local y = love.math.random(5, screen_height - 5) -- both coords are limited to the screen size, minus 5 pixels of padding
stars[i] = {x, y} -- stick the values into the table
end
end
function love.draw()
love.graphics.setPointSize(3)
love.graphics.points(stars) -- draw all stars as points
end
Re: How to apply random sizes and colors to points
Posted: Thu Jul 15, 2021 4:22 pm
by darkfrei
Use circles, not points:
Code: Select all
function love.load()
local screen_width, screen_height = love.graphics.getDimensions()
local max_stars = 100 -- how many stars we want
stars = {} -- table which will hold our stars
for i=1, max_stars do -- generate the coords of our stars
local x = love.math.random(5, screen_width-5) -- generate a "random" number for the x coord of this star
local y = love.math.random(5, screen_height-5) -- both coords are limited to the screen size, minus 5 pixels of padding
local r = math.random(1, 3)
local color = {0.5+0.5*math.random(), 0.5+0.5*math.random(), 0.5+0.5*math.random()}
local star = {x=x, y=y, r=r, color = color}
table.insert (stars, star) -- stick the values into the table
end
end
function love.draw()
for i, star in pairs (stars) do
love.graphics.setColor(star.color)
love.graphics.circle('fill', star.x, star.y, star.r)
end
end
`
Posted: Thu Jul 15, 2021 5:19 pm
by klewis
A million thanks on showing me a way to think about and tackle my situation to reach my goal. This looks amazing. and we are using pairs because of our custom key value pairs.
Thanks again!
Re: `
Posted: Thu Jul 15, 2021 8:13 pm
by darkfrei
klewis wrote: ↑Thu Jul 15, 2021 5:19 pm
A million thanks on showing me a way to think about and tackle my situation to reach my goal. This looks amazing. and we are using pairs because of our custom key value pairs.
Thanks again!
And moving stars:
Code: Select all
function new_star ()
local padding = 5
local x = love.math.random(padding, screen_width-padding) -- generate a "random" number for the x coord of this star
local y = love.math.random(padding, screen_height-padding) -- both coords are limited to the screen size, minus 5 pixels of padding
local r = math.random(1, 3)
-- local r = 1
local color = {0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5}
local star = {x=x, y=y, r=r, color = color}
return star
end
function love.load()
screen_width, screen_height = love.graphics.getDimensions()
center = {x=screen_width/2, y=screen_height/2}
local max_stars = 100 -- how many stars we want
stars = {} -- table which will hold our stars
for i=1, max_stars do -- generate the coords of our stars
local star = new_star ()
table.insert (stars, star) -- stick the values into the table
end
end
function love.update(dt)
for i, star in pairs (stars) do
star.x = star.x + dt*(star.x-center.x)
star.y = star.y + dt*(star.y-center.y)
star.r = star.r + dt*2
if (star.x < 0) or (star.y < 0) or (star.x > screen_width) or (star.y > screen_height) then
-- out of screen, replace the star with the new one
stars[i] = new_star ()
end
end
end
function love.draw()
for i, star in pairs (stars) do
love.graphics.setColor(star.color)
love.graphics.circle('fill', star.x, star.y, star.r)
end
end
Re: How to apply random sizes and colors to points
Posted: Thu Jul 15, 2021 11:32 pm
by togFox
Heh. Nice. Can they move right to left instead?
Re: How to apply random sizes and colors to points
Posted: Fri Jul 16, 2021 3:01 am
by darkfrei
togFox wrote: ↑Thu Jul 15, 2021 11:32 pm
Heh. Nice. Can they move right to left instead?
Main changing in the update:
Code: Select all
star.x = star.x + dt*(-star.r*100)
Code: Select all
function new_star (bool)
local padding = 5
local x = love.math.random(padding, screen_width-padding) -- generate a "random" number for the x coord of this star
if bool then -- new stars on edge
x = screen_width
end
local y = love.math.random(padding, screen_height-padding) -- both coords are limited to the screen size, minus 5 pixels of padding
local r = 1+5*math.random()^4
-- local r = 1
local color = {0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5}
local star = {x=x, y=y, r=r, color = color}
return star
end
function love.load()
screen_width, screen_height = love.graphics.getDimensions()
center = {x=screen_width/2, y=screen_height/2}
local max_stars = 100 -- how many stars we want
stars = {} -- table which will hold our stars
for i=1, max_stars do -- generate the coords of our stars
local star = new_star ()
table.insert (stars, star) -- stick the values into the table
end
end
function love.update(dt)
for i, star in pairs (stars) do
star.x = star.x + dt*(-star.r*100)
if (star.x < 0) or (star.y < 0) or (star.x > screen_width) or (star.y > screen_height) then
stars[i] = new_star (true) -- out of screen, replace the star with the new one
end
end
end
function love.draw()
for i, star in pairs (stars) do
love.graphics.setColor(star.color)
love.graphics.circle('fill', star.x, star.y, star.r)
end
end
Re: How to apply random sizes and colors to points
Posted: Fri Jul 16, 2021 8:43 am
by darkfrei
But the extra high speed needs lines, not circles:
Code: Select all
function new_star (bool)
local padding = 5
local x = love.math.random(padding, screen_width-padding) -- generate a "random" number for the x coord of this star
if bool then -- new stars on edge
x = screen_width
end
local y = love.math.random(padding, screen_height-padding) -- both coords are limited to the screen size, minus 5 pixels of padding
local r = 0.5+5*math.random()^2
-- local r = 1
local color = {0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5, 0.5+0.5*love.math.random()^0.5}
local star = {x=x, y=y, r=r, color = color}
star.vx = (-star.r*500)*(0.1+0.9*math.random())
star.x = star.x - (1/60)*star.vx
return star
end
function love.load()
screen_width, screen_height = love.graphics.getDimensions()
center = {x=screen_width/2, y=screen_height/2}
local max_stars = 100 -- how many stars we want
stars = {} -- table which will hold our stars
for i=1, max_stars do -- generate the coords of our stars
local star = new_star ()
table.insert (stars, star) -- stick the values into the table
end
end
function love.update(dt)
for i, star in pairs (stars) do
star.x = star.x + dt*star.vx
if (star.x < 0) then
stars[i] = new_star (true) -- out of screen, replace the star with the new one
end
end
end
function love.draw()
for i, star in pairs (stars) do
love.graphics.setColor(star.color)
love.graphics.setLineWidth( star.r/2 )
love.graphics.line(star.x, star.y, star.x-(star.vx^2)*0.00002, star.y)
end
end
Re: How to apply random sizes and colors to points
Posted: Fri Jul 16, 2021 9:38 am
by togFox
Looks real.
Re: How to apply random sizes and colors to points
Posted: Wed Jun 15, 2022 6:55 pm
by klewistech
@darkfrei thank you so much for the tutorial on this!