Sample of the Week, II

This week's featured sample comes from Jeff Sidlosky. It is a star field simulation - you know the one where it appears like you're moving fast through space, with stars whizzing past you.

 ' Star-field simulation
Initialize()   
Main()   
  
Sub Initialize   
    GraphicsWindow.Title = "Super Star Field!"  
    GraphicsWindow.Width = 640   
    GraphicsWindow.Height = 480   
    GraphicsWindow.BackgroundColor = "black"  
    GraphicsWindow.PenWidth = 0   
    GraphicsWindow.Show()   
       
    num_stars = 200   
       
    For index = 0 To num_stars   
        NewStar()   
    EndFor   
EndSub   
  
Sub NewStar   
    Array.SetValue("star_x", index, Math.GetRandomNumber(100) - 50)   
    Array.SetValue("star_y", index, Math.GetRandomNumber(100) - 50)   
       
    ' Pick a random z depth   
    z = (Math.GetRandomNumber(50) / 100) + 0.50   
       
    Array.SetValue("star_z", index, z)   
       
    ' Start with a dark color and save our shape   
    GraphicsWindow.BrushColor = "DimGray"  
    Array.SetValue("star_shape", index, GraphicsWindow.AddRectangle(2, 2))   
    Array.SetValue("star_color", index, 0)   
EndSub   
  
Sub Update   
    For index = 0 To num_stars   
        z = Array.GetValue("star_z", index)         
        x = (Array.GetValue("star_x", index) / z) + 320   
        y = (Array.GetValue("star_y", index) / z) + 240   
        shape = Array.GetValue("star_shape", index)   
           
        ' Next z position   
        z = z - 0.02   
           
        If(x < 0 Or x > 639 Or y < 0 Or y > 479 Or z <= 0) Then  
            GraphicsWindow.RemoveShape(shape)   
            NewStar()   
        Else  
            ' Check if we should make the star brighter   
            If(z < 0.4) Then  
                If(Array.GetValue("star_color", index) = 0) Then  
                    GraphicsWindow.BrushColor = "White"  
                    GraphicsWindow.RemoveShape(shape)   
                    shape = GraphicsWindow.AddRectangle(2, 2)   
                    Array.SetValue("star_shape", index, shape)    
                    Array.SetValue("star_color", index, 1)   
                EndIf   
            EndIf   
               
            GraphicsWindow.MoveShape(shape, x, y)   
                
            Array.SetValue("star_z", index, z)           
        EndIf       
    EndFor   
EndSub   
  
Sub Main   
    ' Run forever   
    While(1 = 1)   
        Update()   
    EndWhile   
EndSub  

And here's the screenshot! The picture really doesn't do justice. Try this program on your copy of Small Basic to experience the real deal.

Do you want your samples to be featured here? Post them in our forums and we'll pick one each week.