## Watch the stars fly!

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Watch the stars fly!

A little somethin' I decided to whip up in a couple hours.
Stars.love
Stars ver 2.love
Last edited by Kingdaro on Mon Nov 22, 2010 8:26 pm, edited 1 time in total.

arquivista
No longer with us
Posts: 266
Joined: Tue Jul 06, 2010 8:39 am
Location: Insert Geolocation tag here
Contact:

### Re: Watch the stars fly!

Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
Stars.love
Hi! That remember more snow fall than stars but is always a nice effect. Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here. Also stars animation/trail isn't quite smooth. Probably something could be improved. Keep the good work in this demo!
--------------------------------------------------------
To Do: Insert Signature Here
--------------------------------------------------------

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

arquivista wrote:
Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
Stars.love
Hi! That remember more snow fall than stars but is always a nice effect. Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here. Also stars animation/trail isn't quite smooth. Probably something could be improved. Keep the good work in this demo!
Thanks for the feedback.

I plan to add a stars counter at the top of the screen (already did) and I want to make it more customizable, like setting speed or color or image or direction, etc.

From the stars counter I concluded that the game gets slower/laggier because of the amount of stars in the game constantly increasing. Here's my part of the script to fix it, and what came up when I tried to do this. The chunk added in is surrounded by comment brackets, and the number sign is where the error occurred, according to the blue screen of death.

Code: Select all

stars = {}
time = 1
function love.update(dt)
if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -100
star.size = math.random(0.4,2.5)
table.insert(stars,star)
end

for i=1, #stars do
if stars[i] ~= nil then
stars[i].y = stars[i].y+(150*dt*stars[i].size)
end
end
--[[
for i=1, #stars do
if stars[i].y >= 480 then --#main.lua.24 attempt to index field ? (a nil value)
table.remove(stars,i)
end
end
--]]
if time >= 1 then
love.graphics.setCaption("Watch the stars fly! ("..love.timer.getFPS().." FPS)")
time = 0
else
time = time+dt
end
end

I can't seem to find a way to fix this.

Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

### Re: Watch the stars fly!

Here's an attempt at using the particle system for a star field (move mouse around to 'turn'):

Code: Select all

local stars, center

center = {
x = love.graphics.getWidth()/2,
y = love.graphics.getHeight()/2,
}
stars = love.graphics.newParticleSystem( love.graphics.newImage('fire.png'), 300 )
stars:setPosition(center.x, center.y)
stars:setBufferSize( 2000 )
stars:setEmissionRate( 300 )
stars:setParticleLife( 6 )
stars:setColor( 55, 55, 55, 0, 255, 255, 255, 255 )
stars:setSize( 0.16, 0.1, 1 )
stars:setSpeed( 30, 80  )
end

function love.keypressed(key)
if key == 'escape' then os.exit() end
end

function love.update(dt)
local x, y = love.mouse.getPosition()
stars:setOffset((x-center.x) * -5, (y-center.y) * -5)
stars:update(dt)
end

function love.draw()
love.graphics.draw(stars, 0, 0)
end

Attachments
stars.love

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: Watch the stars fly!

Code: Select all

love.graphics.draw(love.graphics.newImage("star.png"),stars[i].x,stars[i].y,0,stars[i].size/4)
nonononono

Create the image only once!
for instance, look in love.load and love.draw for stars.image:

Code: Select all

math.randomseed( os.time() )

win = love.graphics.setMode(640,480)
stars = {}
time = 1
stars.image = love.graphics.newImage("star.png")
f = love.graphics.newFont(12)
love.graphics.setFont(f)
end

function love.update(dt)
if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -128
star.size = math.random(0.5,2)
table.insert(stars,star)
end

for i,star in ipairs(stars) do
if star.y < 480 then
star.y = star.y+(150*dt*star.size)
else
table.remove(stars,i)
end
end

if time >= 1 then
love.graphics.setCaption("Watch the stars fly! ("..love.timer.getFPS().." FPS)")
time = 0
else
time = time+dt
end
end

function love.draw()
for _,star in ipairs(stars) do
love.graphics.draw(stars.image,star.x,star.y,0,star.size/4)
end
end

And by the way, there is a problem with this in your love.update(dt):

Code: Select all

	if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -40
star.size = math.random(0.5,2)
table.insert(stars,star)
end
Since you are tying it to frame instead of time, people with higher framerates will get more stars falling.
Simply making it something like

Code: Select all

if math.random(1,2) == 2 and time>=1 then
should fix it for now. But you'll probably want the frequency to be greater than an expected star every 2 seconds.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Watch the stars fly!

TechnoCat wrote:

Code: Select all

love.graphics.draw(love.graphics.newImage("star.png"),stars[i].x,stars[i].y,0,stars[i].size/4)
nonononono
I fully support this nonononono.

EDIT: fixed misquote
Last edited by kikito on Mon Nov 22, 2010 1:08 pm, edited 2 times in total.
When I write def I mean function.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Watch the stars fly!

I support the nonononono, but I don't support the misquote.

nomnom
Prole
Posts: 7
Joined: Tue Nov 16, 2010 1:37 pm

### Re: Watch the stars fly!

arquivista wrote:
Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
Stars.love
Hi! That remember more snow fall than stars but is always a nice effect.
Yea
Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here.
It is happening here too! You are not the only one...

It is well done Stay tuned (I hope the translations service of my choice choosed the right idiom )

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

Thanks everyone for the ideas! The current state of the demo is excellent! I couldn't have done it without all your help.

Also, I'm much more comfortable with using for i=1, #table since it's what I've been using ever since I started using Lua and I never really bothered to get the concept of pairs/ipairs. (I'd rather not have anybody explain it to me. )

So, here's the demo in all it's beautified and updated glory. (See first post.)

Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

### Re: Watch the stars fly!

Kingdaro wrote:I'm much more comfortable with using for i=1, #table [..] I never really bothered to get the concept of pairs/ipairs
Iterating arrays that way can be faster (eliminating iterator function call overhead), but only if you index the table you're iterating no more than ~1-2 times in the loop body, and it only works for arrays. For instance:

Code: Select all

for i=1, #stars do
stars[i].y = stars[i].y+(speed*dt*(stars[i].size/2))
end

for i,star in ipairs(stars) do
star.y = star.y+(speed*dt*(star.size/2))
end

The second loop is faster, because we eliminate 3 table lookups, which saves more time than we lose from the iterator call (I also find it easier to read).

Kingdaro wrote:here's the demo in all it's beautified and updated glory. (See first post.)
The line "star.x = math.random(-640,1280)" is generating ~65% of your stars off-screen where they will never be seen.

### Who is online

Users browsing this forum: No registered users and 12 guests