How to apply random sizes and colors to points

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
klewis
Prole
Posts: 7
Joined: Fri Jan 10, 2020 1:54 pm

How to apply random sizes and colors to points

Post 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
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to apply random sizes and colors to points

Post 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
Attachments
2021-07-15T18_39_54-Untitled.png
2021-07-15T18_39_54-Untitled.png (6.79 KiB) Viewed 7527 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
klewis
Prole
Posts: 7
Joined: Fri Jan 10, 2020 1:54 pm

`

Post 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!
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: `

Post 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
Attachments
Animation (34).gif
Animation (34).gif (249.21 KiB) Viewed 7492 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to apply random sizes and colors to points

Post by togFox »

Heh. Nice. Can they move right to left instead?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to apply random sizes and colors to points

Post 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
Attachments
Animation (35).gif
Animation (35).gif (307.8 KiB) Viewed 7442 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to apply random sizes and colors to points

Post 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
Attachments
Animation (36).gif
Animation (36).gif (793.96 KiB) Viewed 7436 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to apply random sizes and colors to points

Post by togFox »

Looks real. :)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
klewistech
Prole
Posts: 1
Joined: Wed Jun 15, 2022 6:53 pm

Re: How to apply random sizes and colors to points

Post by klewistech »

@darkfrei thank you so much for the tutorial on this!
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests