# Star generator

Remember the vector graphics star wars game from the 80’s?

I am trying to simulate the stars from that.

Very choppy, slow and needs work.

Any feedback on how to smooth it or even redo it would be great.

``````-- stars
function setup()
displayMode(FULLSCREEN)
createstars()
checkagain = false
end

function createstars()
stars = {}
for i = 1,125 do
--rx = math.random(1,1200)
-- ry= math.random(1,800)
s= Star(true)
--u = { x=rx, y=ry}
table.insert(stars,s)
end
for i = 1,50 do
--rx = math.random(1,1200)
-- ry= math.random(1,800)
s= Star(false)
--u = { x=rx, y=ry}
table.insert(stars,s)
end

end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0, 0, 0, 255)

for a,b in pairs(stars) do
b:draw()
end
movestars()

end

function movestars()
for b,s in pairs(stars) do
s:move()
end
end

``````

And.

``````Star = class()

function Star:init(init)
-- you can accept and set parameters here
self.mx = 500
self.my = 400
if init == true then
self.x = math.random(self.mx-10,self.mx+10)
self.y = math.random(self.my-10,self.my+10)
else
self.x = math.random(1,1200)
self.y = math.random(1,1200)
end
self.xspeed = math.random(1,10)
self.yspeed = math.random(1,10)
self.size = math.random(1,3)
self.maxsize =6
end

function Star:draw()
-- Codea does not automatically call this method
strokeWidth(self.size)
--  stroke(math.random(1,255),math.random(1,255), math.random(1,255))
line(self.x,self.y,self.x,self.y)
end

function Star:touched(touch)
-- Codea does not automatically call this method
end
function Star:move()

if self.x <= self.mx and self.y <= self.my then
self.x = self.x - self.xspeed
self.y = self.y - self.yspeed
end
if self.x <= self.mx and self.y >= self.my then
self.x = self.x - self.xspeed
self.y = self.y + self.yspeed
end

if self.x >= self.mx and self.y >= self.my then
self.x = self.x + self.xspeed
self.y = self.y + self.yspeed
end
if self.x >= self.mx and self.y <= self.my then
self.x = self.x + self.xspeed
self.y = self.y - self.yspeed
end

if self.x <= 0 or self.y <= 0 then
self:init(true)
return
else
if self.y >= 1000 or self.x >= 1000 then
self:init(true)
return
end
end

if math.random(1,12) == 5 then
if self.size + 1 <= self.maxsize then
self.size = self.size + 1
return
end
end

if self.yspeed + self.xspeed > 15 then
if math.random(1,5) == 5 then
if self.size + 1 <= self.maxsize then
self.size = self.size + 1
return
end
end

end

end

``````

Rebooted codea and it started to run smoother.

This version of the star class is colorized…

``````
Star = class()

function Star:init(init)
-- you can accept and set parameters here
self.mx = 500
self.my = 400
if init == true then
self.x = math.random(self.mx-10,self.mx+10)
self.y = math.random(self.my-10,self.my+10)
else
self.x = math.random(1,1200)
self.y = math.random(1,1200)
end
self.xspeed = math.random(1,10)
self.yspeed = math.random(1,10)
self.size = math.random(1,3)
self.maxsize =6
self.sc1 = math.random(1,255)
self.sc2 = math.random(1,255)
self.sc3 = math.random(1,255)

end

function Star:draw()
-- Codea does not automatically call this method
strokeWidth(self.size)
--stroke(math.random(1,255),math.random(1,255), math.random(1,255))
stroke(self.sc1,self.sc2,self.sc3)
line(self.x,self.y,self.x,self.y)
end

function Star:touched(touch)
-- Codea does not automatically call this method
end
function Star:move()

if self.x <= self.mx and self.y <= self.my then
self.x = self.x - self.xspeed
self.y = self.y - self.yspeed
end
if self.x <= self.mx and self.y >= self.my then
self.x = self.x - self.xspeed
self.y = self.y + self.yspeed
end

if self.x >= self.mx and self.y >= self.my then
self.x = self.x + self.xspeed
self.y = self.y + self.yspeed
end
if self.x >= self.mx and self.y <= self.my then
self.x = self.x + self.xspeed
self.y = self.y - self.yspeed
end

if self.x <= 0 or self.y <= 0 then
self:init(true)
return
else
if self.y >= 1000 or self.x >= 1000 then
self:init(true)
return
end
end

if math.random(1,12) == 5 then
if self.size + 1 <= self.maxsize then
self.size = self.size + 1
return
end
end

if self.yspeed + self.xspeed > 15 then
if math.random(1,5) == 5 then
if self.size + 1 <= self.maxsize then
self.size = self.size + 1
return
end
end

end

end

``````

I have no time to try this, but here’s a few possible speedups:

1. in Star:move() you check 4 different states, but only one can be true at a time.
So if the first if statement yields true, the others are no longer needed. Try rearanging the if structure by checking for x and y seperately. Use nested if’s.

2. the size thingy: Looks like you want to make sure the size doesn’t go above the max.
This can also be done with self.size = math.min(self.size+1, self.maxsize), instead of the entire if structure. Don’t know if this is faster, but you could try.

Hope this helps!

There are some other options you can have a look at here: http://codeatuts.blogspot.com.au/2012/06/interlude-5-twinkling-starfield-class.html

I mostly use @Ipad41001’s class called Twinkle. I will have to give yours a try.

Not bad, but not was I was aiming for.

I am looking for the flying through space feeling. Twinkle seemed more dreamy… And static.

It appears my performance isssue is a current version bug so my script is good.

I added the occasional solor system to it, started on cross hairs and a fire button and became distracted with life.

I also have to think about how to handle directionals for flight control.

If I make the twinkles smaller they might enhance the feel of my space flight app… I’ll play with it after I get the rest done.

Btw – this is all hobby for me. Just playing and learning.

It looks pretty good. I would recommend two things:

1. have a layer of tiny, non-moving (twinkle-type) stars in the background in addition to the stars coming at you.
2. have the stars that are coming at you fade-in from black instead of being immediately visible in the middle of the screen. This fade in would give the impression that the star or planet was initially too far away to see until you flew close enough to it. It would also keep the distractions down in the middle of the screen where, presumably, most of the action (in your game) will occur.

@Ric_Esrey - great ideas.

I did add some “Galaxies” as well to have some different shapes. I might add a few planets now and then.

I’ll post some updated code this weekend. I did add a “FIRE” button and “CROSS HAIRS” to the screen too.

Hi @wrmichael,

Look at the contributed code section - I tried several variations with stars - static and mobile. Some of them give you the impression of movement. Don’t look good on video, I put some on youtube but they are just moving dots!!!

http://twolivesleft.com/Codea/Talk/discussion/comment/10685#Comment_10685

Haven’t had chance to refine them - link to HTML5 source page where I found out how to do it.

Hope that helps

Bri_G

@Bri_G, I like it. Subtle enough to give you the feeling of movement without becoming too distracting.