question about love.graphics.points and starfields

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
paul54000
Prole
Posts: 22
Joined: Mon Nov 28, 2016 12:19 am

question about love.graphics.points and starfields

Post by paul54000 »

main.love
(479 Bytes) Downloaded 115 times
Hi
Is it possible to make the stars scintillate randomly and move ?
If so, how to do it ?

https://love2d.org/wiki/love.graphics.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
      stars[i] = {x, y}   -- stick the values into the table
   end
end
 
function love.draw()
   love.graphics.points(stars)  -- draw all stars as points
end
Last edited by paul54000 on Tue Dec 06, 2016 3:10 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: question about love.graphics.points and starfields

Post by raidho36 »

I believe what you're looking for is particle effects, not "point" rendering mode.
User avatar
Jimanzium
Party member
Posts: 103
Joined: Sun Jun 03, 2012 2:39 pm
Contact:

Re: question about love.graphics.points and starfields

Post by Jimanzium »

Can you be more specific in how you want them to move?

If you just want them to scroll right to left infinitely you could use something like this:

Code: Select all

local scrollSpeed = 100
local screenWidth = love.graphics.getWidth()

function love.update(dt)
	for i,v in ipairs(stars) do
		v.x = v.x - scrollSpeed * dt
		if(v.x < 0) then
			v.x = v.x + screenWidth
		end
	end
end
I'd need more information to help you more.
paul54000
Prole
Posts: 22
Joined: Mon Nov 28, 2016 12:19 am

Re: question about love.graphics.points and starfields

Post by paul54000 »

your code didn't work, could you help me please ?

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

local scrollSpeed = 100
local screenWidth = love.graphics.getWidth()

function love.update(dt)
   for i,v in ipairs(stars) do
      v.x = v.x - scrollSpeed * dt
      if(v.x < 0) then
         v.x = v.x + screenWidth
      end
   end
end
 
function love.draw()
   love.graphics.points(stars)  -- draw all stars as points
end
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: question about love.graphics.points and starfields

Post by Sulunia »

Finally, this works. I assume this is what you're looking for.
Read the comments, don't just use the snippet for whatever reason. Try to learn from it! ^^

Code: Select all

--Global vars
totalDt = 0
stars = {}   -- table which will hold our stars
screen_width, screen_height = love.graphics.getDimensions()

function love.load()
 
	local max_stars = 500   -- how many stars we want
 
	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 = 255
		local g = 255 -- Set colors to white
		local b = 255
		local a = love.math.random()*254
		stars[i] = {x, y, r, g, b, a}   -- stick the values into the table
	end
end

function love.update(dt)
	totalDt = totalDt + dt
	if totalDt >= 0.05 then --Reduce this comparison value to increase scroll speed/blink speed
		screen_width, screen_height = love.graphics.getDimensions() --Gets current screen dimensions
		for i, v in ipairs(stars) do
			--Iterate over every star and assign a new alpha value for then
			--Assign a new Y value if you want
			stars[i][6] = love.math.random(60, 254)
			stars[i][2] = stars[i][2] - 1
			if stars[i][2] == 0 then --Wraps around screen
				stars[i][1] = love.math.random(5, screen_width-5) --new X
				stars[i][2] = screen_height - 5 --new Y, starting at bottom
			end
		end
		totalDt = 0 --Reset dt time count
	end
end
 
function love.draw()
	love.graphics.points(stars) --Draws all stars on the table
end
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 197 guests