Hi, I am new to the community and have been taking Sockmunkee's tutorials. At the same time I would like to get started on a small project idea that I have, it is a turn based space game where all the ships will be flying in a fleet formation(basically just in rows) and will have no movement of there own except for some small simulated space movement such as moving back and forward and once a while maneuvering side to side but not within the characters control. So basically they will always be right on the screen, in the background I would like to have an infinitely scrolling star field to simulate movement, how would I go about achieving this?
Off topic I would like to say from what I've read while looking for an answer using the search function as well as other random browsing I really like the community here and am glad to be taking my first step to become a part of it.
how can I make an infinite starfield?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: how can I make an infinite starfield?
I suggest a pseudo-infinite solution: Make a starfield of the size of the screen and wrap it around as you scroll.
First create a table of star locations randomly (in love.load):
To keep track of the scrolling, create a variable called starOffset and increase it with time (in love.update)
And drawing the starfield is as follows:
This %-operator wraps the numbers such that they are always between zero and screenWidth.
I used a couple of variables, without defining them. You have to define them first (screenWidth, screenHeight, nStars, radius, segments and starImage)
First create a table of star locations randomly (in love.load):
Code: Select all
stars = {}
for i = 1,nStars do
stars[i] = {}
stars[i].x = math.random()*screenWidth
stars[i].y = math.random()*screenHeight
end
Code: Select all
starOffset = (starOffset + dt) % screenWidth
Code: Select all
for i = 1,nStars do
local thisX = (stars[i].x + Offset) % screenWidth
local thisY = stars[i].y
-- either draw an image:
love.graphics.draw(starImage,thisX,thisY)
-- or a circle
love.graphics.circle(fill,thisX,thisY,radius,segments)
end
I used a couple of variables, without defining them. You have to define them first (screenWidth, screenHeight, nStars, radius, segments and starImage)
Check out my blog on gamedev
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: how can I make an infinite starfield?
My favorite resource for rendering starfields (with nebulae etc) is http://www.sea-of-memes.com/LetsCode10/LetsCode10.html .
However, it's 3d, not 2d. But some of the techniques here are extrapolable.
However, it's 3d, not 2d. But some of the techniques here are extrapolable.
When I write def I mean function.
Re: how can I make an infinite starfield?
Thanks so much for the quick replies , I used micha's approach twice to simulate different sizes of stars and I am debating a third layer if they aren't too large, I also put less of them as they get larger which helps make it look like space is vast and random. Thanks again for your help.
Re: how can I make an infinite starfield?
Luckily enough, I have a starfield.lua consisting of 3 layers of stars. They all have different speeds. Note that it's a pretty old code (5 months old).
To use it, require the module itself. Afterwards call loadStars() in load function, updateStars() in update function, and drawStars() in draw function.
http://rghost.net/50005646
To use it, require the module itself. Afterwards call loadStars() in load function, updateStars() in update function, and drawStars() in draw function.
http://rghost.net/50005646
Github: https://github.com/chroteus
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 4 guests