locked
starfield RRS feed

  • Question

  • is there a program that draws a starfield that works with current version of small basic found one on forum but not working
    Sunday, November 14, 2010 11:34 PM

Answers

  • I found Jeff Sidlosky's Super Star Field in the forums and updated to work with Small Basic v0.9

    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)  
      star_x[index] = Math.GetRandomNumber(100) - 50
      'Array.SetValue("star_y", index, Math.GetRandomNumber(100) - 50)  
      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)  
      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))  
      star_shape[index] = Shapes.AddRectangle(2, 2)
      'Array.SetValue("star_color", index, 0)  
      star_color[index] = 0
    EndSub  

    Sub Update  
      For index = 0 To num_stars  
        'z = Array.GetValue("star_z", index)        
        z = star_z[index]
        'x = (Array.GetValue("star_x", index) / z) + 320  
        x = (star_x[index] / z) + 320
        'y = (Array.GetValue("star_y", index) / z) + 240  
        y = (star_y[index] / z) + 240
       
        'shape = Array.GetValue("star_shape", index)  
        shape = 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)  
          Shapes.Remove(shape)
          NewStar()  
        Else 
          ' Check if we should make the star brighter  
          If(z < 0.4) Then 
          'If(Array.GetValue("star_color", index) = 0) Then 
          If (star_color[index] = 0) Then
              GraphicsWindow.BrushColor = "White" 
              'GraphicsWindow.RemoveShape(shape)  
              Shapes.Remove(shape)
              'shape = GraphicsWindow.AddRectangle(2, 2)  
              shape = Shapes.AddRectangle(2, 2)
              'Array.SetValue("star_shape", index, shape)   
              star_shape[index] = shape
              'Array.SetValue("star_color", index, 1)  
              star_color[index] = 1
             
            EndIf  
          EndIf  
         
          'GraphicsWindow.MoveShape(shape, x, y)  
          Shapes.Move(shape, x, y)
         
          'Array.SetValue("star_z", index, z)
          star_z[index] = z
        EndIf      
      EndFor  
    EndSub  

    Sub Main  
      ' Run forever  
      While(1 = 1)  
        Update()  
      EndWhile  
    EndSub 

    • Marked as answer by SkidMarcUK Monday, November 15, 2010 10:38 AM
    Monday, November 15, 2010 5:44 AM

All replies

  • I found Jeff Sidlosky's Super Star Field in the forums and updated to work with Small Basic v0.9

    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)  
      star_x[index] = Math.GetRandomNumber(100) - 50
      'Array.SetValue("star_y", index, Math.GetRandomNumber(100) - 50)  
      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)  
      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))  
      star_shape[index] = Shapes.AddRectangle(2, 2)
      'Array.SetValue("star_color", index, 0)  
      star_color[index] = 0
    EndSub  

    Sub Update  
      For index = 0 To num_stars  
        'z = Array.GetValue("star_z", index)        
        z = star_z[index]
        'x = (Array.GetValue("star_x", index) / z) + 320  
        x = (star_x[index] / z) + 320
        'y = (Array.GetValue("star_y", index) / z) + 240  
        y = (star_y[index] / z) + 240
       
        'shape = Array.GetValue("star_shape", index)  
        shape = 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)  
          Shapes.Remove(shape)
          NewStar()  
        Else 
          ' Check if we should make the star brighter  
          If(z < 0.4) Then 
          'If(Array.GetValue("star_color", index) = 0) Then 
          If (star_color[index] = 0) Then
              GraphicsWindow.BrushColor = "White" 
              'GraphicsWindow.RemoveShape(shape)  
              Shapes.Remove(shape)
              'shape = GraphicsWindow.AddRectangle(2, 2)  
              shape = Shapes.AddRectangle(2, 2)
              'Array.SetValue("star_shape", index, shape)   
              star_shape[index] = shape
              'Array.SetValue("star_color", index, 1)  
              star_color[index] = 1
             
            EndIf  
          EndIf  
         
          'GraphicsWindow.MoveShape(shape, x, y)  
          Shapes.Move(shape, x, y)
         
          'Array.SetValue("star_z", index, z)
          star_z[index] = z
        EndIf      
      EndFor  
    EndSub  

    Sub Main  
      ' Run forever  
      While(1 = 1)  
        Update()  
      EndWhile  
    EndSub 

    • Marked as answer by SkidMarcUK Monday, November 15, 2010 10:38 AM
    Monday, November 15, 2010 5:44 AM
  • Thanks very much. This is exactly what i was looking for.  :-)
    Monday, November 15, 2010 11:04 AM