Share My Creation Starfiel

Similar threads

B4A Code Snippet [B4X] [XUI] Simple Halo Animation
B4A Code Snippet [B4X] [XUI] Accurate Text Measurement and Drawing
B4A Code Snippet Mathemetics Pendulum
B4A Code Snippet Color Picker
B4A Code Snippet [B4X] [BitmapCreator] Blur Image
B4A Library Android Things

Gigatron

Member
Licensed User
Starfield

Hi all

I am very tired it's 4.49 , i have worked about 6 hours to do
the legendary AMIGA starfield on B4A..

You can improve it with starfield colors, it's not hard,
OK Done;


Let's begin like AMIGA

Lea $30000 :)

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim time As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim cnv As Canvas
   Dim stpanel As Panel
   Dim xitbtn As Button
      
   Dim starx(100) As Int  'xpos
   Dim stary(100) As Int  'ypos
   Dim starspeed(100) As Int 'stars
   Dim nbstar(100) As Int    'numstars
   
    
   

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("starscroll")   'nothing special just stpanel + exitbtn in layout
   cnv.Initialize(stpanel)
    
   
   For i=0 To nbstar.Length-1  
     starx(i)=Rnd(1,460)           'store xpos in table  100 xpos
     stary(i)=Rnd(1,800)           'store ypos   ..100 ypos
    starspeed(i)=Rnd(1,8)         'store starspeed  .. 100 speed
   Next
      time.Initialize("time",5)
      time.Enabled=True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub time_Tick
   drawstars
End Sub


Sub DrawStars
  ' nothing HARD here :) just clear stars ypos -starspeed(i) from table...
  
       dim clr as int

    For i=0 To nbstar.Length-1
     cnv.DrawCircle(starx(i),stary(i)-starspeed(i),2,Colors.black,True,2dip)
     cnv.DrawCircle(starx(i),stary(i),2,clr,True,2dip)
    stary(i)=stary(i)+starspeed(i)
   


        If starspeed(i)=1 Then clr=Colors.RGB(50,50,50)
    If starspeed(i)=2 Then clr=Colors.RGB(80,80,80)
    If starspeed(i)=3 Then clr=Colors.RGB(100,100,100)
    If starspeed(i)=4 Then clr=Colors.RGB(150,150,150)
    If starspeed(i)=5 Then clr=Colors.RGB(180,180,180)
        If starspeed(i)=6 Then clr=Colors.RGB(200,200,200)
    If starspeed(i)=7 Then clr=Colors.RGB(230,230,230)
    If starspeed(i)=8 Then clr=Colors.RGB(255,255,255)
    




         If stary(i)>860 Then stary(i)=-10
    stpanel.Invalidate
    Next
  
  
  End Sub

Sub xitbtn_Click
        time.enabled=false
   activity.Finish
End Sub
Good luck
 
Last edited:

vb1992

Well-Known Member
Licensed User
Looks good.

If you don't make the panel Black color, and
leave the default color, it kind looks like the Matrix
at first, I thought that was your effect, but then I
changed the panel to black.
 

Gigatron

Member
Licensed User
Ok let's add Bouncing logo to the Starfield source (forgot the "d" in title :) )


B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim time As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim cnv As Canvas
   Dim stpanel As Panel
   Dim xitbtn As Button
      
   Dim starx(150) As Int  'xpos
   Dim stary(150) As Int  'ypos
   Dim starspeed(150) As Int 'stars
   Dim nbstar(150) As Int    'numstars
   
    ' Ok let's add more fun,  bouncing logo
   

    Dim   sintable(128) As Int
    Dim   numpos(128) As Int
    Dim  barpos As Int
   
   Dim barre1 As ImageView  ' For logo
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
      Activity.LoadLayout("starscroll")   'nothing special just stpanel + exitbtn + 1 imageView in layout
      cnv.Initialize(stpanel)
      
      ' For starfield tables
      For i=0 To nbstar.Length-1  
      starx(i)=Rnd(1,460)           'store xpos in table  100 xpos
      stary(i)=Rnd(1,850)           'store ypos   ..100 ypos
      starspeed(i)=Rnd(1,8)         'store starspeed  .. 100 speed
      Next
      
      '
      
      ' Create sintable 128 values of logo position
      
         For i=0 To sintable.Length-1
         sintable(i)=450-Sin(i*3.14159265/128)*550 'formula from Brian Postma , creator if BP soundmon on AMIGA 
          numpos(i)=sintable(i)                  ' Copie de sintable vers numpos
         Next
         barpos=0
      
         time.Initialize("time",5)
         time.Enabled=True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub time_Tick
   drawstars
   drawlogo  ' Dessin du LOGO.. Logo drawing
End Sub


Sub DrawStars
  ' nothing HARD here :) just clear stars ypos -starspeed(i) from table...
    Dim clr As Int
    
    For i=0 To nbstar.Length-1
    
     cnv.DrawCircle(starx(i),stary(i)-starspeed(i),2,Colors.black,True,2dip)
     cnv.DrawCircle(starx(i),stary(i),2,Clr,True,2dip)
    stary(i)=stary(i)+starspeed(i)
    If starspeed(i)=1 Then clr=Colors.RGB(100,100,100)
    If starspeed(i)=2 Then clr=Colors.RGB(120,120,120)
    If starspeed(i)=3 Then clr=Colors.RGB(140,140,140)
    If starspeed(i)=4 Then clr=Colors.RGB(160,160,160)
    If starspeed(i)=5 Then clr=Colors.RGB(180,180,180)
     If starspeed(i)=6 Then clr=Colors.RGB(200,200,200)
    If starspeed(i)=7 Then clr=Colors.RGB(220,220,220)
    If starspeed(i)=8 Then clr=Colors.RGB(255,255,255)
    If stary(i)>860 Then stary(i)=-10
     stpanel.Invalidate
    Next
    
   
  End Sub

Sub drawlogo
   'bouncing LOGO
   
   Dim pos,y As Int
   
   barpos=barpos+2 Mod 128
   pos=barpos
   
   For i=0 To 1
   y=sintable(pos)
   barre1.Top=y
   pos=pos+(128/24)
   pos=pos Mod 128
   If barpos>124 Then barpos=0
   stpanel.Invalidate
   Next
    
End Sub

Sub xitbtn_Click
   activity.Finish
End Sub



Have fun
 

Attachments

Last edited:

Gigatron

Member
Licensed User
3d Starfield

Yet another Starfield from center of screen, we are in space now :)

Original source : Blitz basic 3d
Ported by me :)))


B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim time As Timer
   Dim MP As MediaPlayer
   Dim ec As Echo   ' this library is not working yet 
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
         
Dim MAX_STAR,STAR_SPEED,WIDTH,HEIGHT As Float       ' stars variables
MAX_STAR=300:STAR_SPEED=0.5:WIDTH=480:HEIGHT=800    ' variables data

Dim star_x(MAX_STAR),star_y(MAX_STAR),star_z(MAX_STAR) As Int  ' Tables(of vars)
   
 
   Dim cnv As Canvas
   Dim stpanel As Panel
   Dim xitbtn As Button
   Dim rec As Rect
    Dim clr As Int
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
      Activity.LoadLayout("starscroll")   'nothing special just stpanel + exitbtn + 1 imageView in layout
      cnv.Initialize(stpanel)
       
      ec.Initialize                           ' no effect
      ec.Enable(True)                         '
      mp.Initialize2("MP")               'init Mediaplayer
      mp.Load(File.DirAssets,"paradox.mp3")   'Load mp3
      mp.play                                 'play mp3
       ec.Preset=ec.PRESET_LARGEROOM           ' Echo var not working yet !
                   
      For c=0 To MAX_STAR-1                        ' Fill vars
      star_x(c)=Rnd(-stpanel.WIDTH,stpanel.WIDTH)
      star_y(c)=Rnd(-stpanel.HEIGHT,stpanel.HEIGHT)
      star_z(c)=Rnd(1,254)
      Next
           
      time.Initialize("time",5)               'init timer and enable timer
      time.Enabled=True
    
End Sub

 
Sub drawstars()
Dim s_x,s_y As Int                            ' sx sy col var
Dim col As Int
cnv.DrawColor(Colors.RGB(0,0,0))              ' fill black screen every frames

For c=0 To MAX_STAR-1                            ' Draw stars etccc......
star_z(c)=star_z(c)-STAR_SPEED
If star_z(c)<=STAR_SPEED Then star_z(c)=255
s_x=(star_x(c)/star_z(c))+(stpanel.WIDTH/2) 
s_y=(star_y(c)/star_z(c))+(stpanel.HEIGHT/2) 

col=255-star_z(c)
col=Colors.RGB(col,col,col)
'
cnv.DrawCircle(s_x,s_y,2,col,True,1dip)
stpanel.Invalidate

Next
  
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub time_Tick

    drawstars
    
End Sub

Sub xitbtn_Click
   activity.Finish
   MP.Stop
End Sub
Have fun
 

Attachments

Top