Projection Starfield

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Projection Starfield

#1 Post by Human Bolt Diary »

Hi everybody,

The attached module lets you a display an animated, Pseudo-3D Projection Starfield in your project. Usage is as easy as dropping it into your project's "game" folder and following the example below.

The math is cribbed from:
http://codentronix.com/2011/05/28/3d-st ... nd-pygame/

As such, this module is available under the MIT License.

Optional arguments are:
Perspective (float) : Default is 128.0. Changes the angle of the viewing frustum. Can be played with to adapt to your project's screen size.
Child (displayable): Defaults to a white Solid inside a Fixed. If provided, lets you use a displayable as the star.

Code: Select all

init:
  
 define space = Solid((0, 0, 0, 255))  
 define stars = ProjectionStarfield(count=512,depth=16)

#Screen where the displayable is shown 
screen starfield:
 add space
 add stars
 
# The game starts here.
label start:
 show screen starfield
 "Space."
 hide screen starfield
 "The final frontier."
 return
Attachments
ProjectionStarfield.rpy
(2.45 KiB) Downloaded 248 times

Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Re: Projection Starfield

#2 Post by Human Bolt Diary »

I wasn't satisfied with the previous version as it had serious performance issues on slower machines. Version 2 below can support hundreds more stars on screen compared to the original before any slowdown occurs.

Usage remains as easy as dropping the .rpy file into your project's "game" folder and following the example below. Note that usage is slightly different from Version 1.

Methods:

start()
Nothing will be displayed until the start() method is called.

add_stars(amount)
Increase the number of displayed stars by the given amount.

subtract_stars(amount)
Decrease the number of displayed stars by the given amount.

Code: Select all

init:
  
 #Simple black background to show under the stars 
 define space = Solid((0, 0, 0, 255))  
 
 #Create the starfield displayables. Optional keyword arguments can tweak the default display.
 #One with a generic square for the star.
 $n_starfield = ProjectionStarfield()
 
 #and one with an image used, plus optional keyword arguments.
 $i_starfield = ProjectionStarfield(star_amount=512, depth=16, perspective=128.0, speed=0.19, image="star_image.png")
 
#Screens where the displayables are shown 
screen starfield:
 add space
 add n_starfield
 
screen starfield_with_image:
 add space
 add i_starfield
 
# The game starts here.
label start:
 show screen starfield
 $n_starfield.start()
 
 "Space."
 
 $n_starfield.add_stars(512)
 
 "The final frontier."
 
 $n_starfield.subtract_stars(512)
 
 "These are the voyages of the starship Enterprise."
 
 hide screen starfield
 show screen starfield_with_image
 $i_starfield.start()
 
 "It's continuing mission, to explore strange new worlds."
 return
Attachments
ProjectionStarfield.rpy
(4.19 KiB) Downloaded 270 times

User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Projection Starfield

#3 Post by Kia »

beautiful :o
if you ever decided to make other versions like side scroll, top down, random shapes, multiple pictures, clickable or shoot em down let us know please :mrgreen: you know once you got things moving on the screen you are one step away from making a mini game.

User avatar
effenelle
Regular
Posts: 39
Joined: Thu Nov 29, 2012 12:41 pm
Projects: Un:Wanted [GxB]
Deviantart: effenelle
Location: inside imagination
Contact:

Re: Projection Starfield

#4 Post by effenelle »

this is cool. it can also be used to sort of portray the situation where you're lying on the grass and looking up into the rainy sky. just change the "star_image.png" into a water image. Thanks for this!
Un:Wanted (GxB)
A game made from free resources found in LemmaSoft's Creative Commons!
\(^▽^)/
Thanks everyone!

M-77
Regular
Posts: 56
Joined: Tue Sep 04, 2018 7:58 am
Contact:

Re: Projection Starfield

#5 Post by M-77 »

I always get error messages like: "n_starfield" or "ProjectionStarfield" not defined. in line bla, bla.
Where to exactly put what? Your script file in game directory. How to activate it in game.
And how to do this effect for the "Main Menu" as a background. And later put a cutout .PNG over it. So the stars coming from behind. As a game title.
Like this:
Image
Last edited by M-77 on Sun Oct 07, 2018 10:34 am, edited 1 time in total.

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Projection Starfield

#6 Post by bonnie_641 »

M-77 wrote: Thu Oct 04, 2018 4:37 pm Where to exactly put what? Your script file in game directory. How to activate it in game.
The file "ProjectionStarfield.rpy" is placed in the game directory.
Example: In the "Demo" folder (name of the project when you create it with the Ren'py program), there is a subfolder called "game". Leave her there
(Demo > game > "ProjectionStarfield.rpy")

When you open the "script.rpy" file, type:

Code: Select all

##################################################################################################################
# This sector in the first line of the "script.rpy". If there are blocks of type "init -1 python" or similar, leave it in second place
##################################################################################################################
init:
  
	define space = Solid((0, 0, 0, 255))  
	define stars = ProjectionStarfield(count=512,depth=16)

##################################################################################################################
# Here are the screens. Screen where the displayable is shown 
##################################################################################################################
	
	screen starfield:
		add space
		add stars

################################################################################################################## 
# The game starts here.
##################################################################################################################
label start:
	show screen starfield
 	"Space."
 	hide screen starfield
	"The final frontier."
	return
This code is the same as provided Human Bolt Diary, but the indentation has 8 spaces. You will probably have to edit it as you have it configured in your favorite text editor (Editra, Atom, Notepad ++, etc.)
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

M-77
Regular
Posts: 56
Joined: Tue Sep 04, 2018 7:58 am
Contact:

Re: Projection Starfield

#7 Post by M-77 »

Thank you for answering, I still have error messages like:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 52, in script
    define stars = ProjectionStarfield(count=512,depth=16)
  File "game/script.rpy", line 52, in <module>
    define stars = ProjectionStarfield(count=512,depth=16)
TypeError: __init__() got an unexpected keyword argument 'count'
Or if I change the indentations from 8 spaces to 4 or none I get this:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 47: init statement expects a non-empty block.
    init:
         ^

Ren'Py Version: Ren'Py 7.0.0.196
Fri Oct 05 08:08:54 2018
Interessting. I have another "Init" above this, one for a Love Points and Money system ("init -2 python:"), and the build-in at the beginning of the script ("# The script of the game goes in this file.
define config.layers = [ 'master', 'transient', 'screens', 'overscreens', 'overlay' ]
init python:")
And adding the "phyton" to "init" result in this:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 47: expected statement.
    init phyton:
               ^

Ren'Py Version: Ren'Py 7.0.0.196
Fri Oct 05 08:17:02 2018
Any clue what I do wrong?

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Projection Starfield

#8 Post by bonnie_641 »

Here is the demo working (but you must add the interface -gui or legacy-)
Attachments
00-starfield.zip
(358.42 KiB) Downloaded 62 times
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Projection Starfield

#9 Post by DragoonHP »

M-77 wrote: Fri Oct 05, 2018 2:14 am And adding the "phyton" to "init" result in this:
It should be python.

M-77
Regular
Posts: 56
Joined: Tue Sep 04, 2018 7:58 am
Contact:

Re: Projection Starfield

#10 Post by M-77 »

Thank you, "phyton" or corrected to "python" was not the cause.
The seperate demo works for me. Including it like coded in the demo script worked for me now. It was about the spacing, I think.
I put the code just after the build in "init:" but before my "Money"+"Love Point" meters with their "init -2".
It must have been a conflict about the ranking of the multiple "init:" indentations.
So each "init:" block with different function need an own spacing, to not been coincidented with others?
And I add just ONE space after the (starfield passage) "init:". Just like in the demo.
Now I want go to have this as Background in the Main Menu. I will just try myself, but if anyone know how to do this, it will save me some time.

User avatar
Donmai
Eileen-Class Veteran
Posts: 1958
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: Projection Starfield

#11 Post by Donmai »

M-77 wrote: Sat Oct 06, 2018 5:27 amNow I want go to have this as Background in the Main Menu.
Change the first lines of your main menu screen definition like this:

Code: Select all

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"
    
    use starfield

    # add gui.main_menu_background
Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

M-77
Regular
Posts: 56
Joined: Tue Sep 04, 2018 7:58 am
Contact:

Re: Projection Starfield

#12 Post by M-77 »

Hello, thank you all for quick help, the code from above does working now. But you have to put in into the "gui" as well as into the "screens" file.
Image
And how to put a cut out picture (transparent/mask) (.png) over the starfield so that both are displayed, and the stars moving from behind?
Or like this:
Image
This is fun, thank you.

User avatar
Donmai
Eileen-Class Veteran
Posts: 1958
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: Projection Starfield

#13 Post by Donmai »

Create your image with a transparent background. In script.rpy:

Code: Select all

    image my_mm_overlay = "images/your image.png"
In screens.rpy:

Code: Select all

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"
    
    use starfield
    add "my_mm_overlay"
Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

Post Reply

Who is online

Users browsing this forum: No registered users