Der deutsche Education Blog

November, 2008

Blog - Title

November, 2008

  • Small Basic

    Code Gallery for Small Basic

    • 7 Comments

    I've just created a code gallery page for Small Basic (now with a Wiki).  Check it out: http://code.msdn.microsoft.com/smallbasic.

    In addition to a new link to Small Basic API reference, you can also find information about the upcoming release there.

  • Small Basic

    Sample of the Week, II

    • 4 Comments

    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.

  • Small Basic

    Small Basic and the "Goto" keyword

    • 8 Comments

    For the past 40 years, there have been on-going debates in the programming world about the utility and usage of the keyword, Goto.  While it’s been generally accepted that uncontrolled (no pun here) usage of Goto leads to unmanageable and spaghetti code, many, including Donald Knuth, believe that Goto is essential and when used with care will actually lead to clearer and more manageable code.

    When designing the Small Basic language, I had the beginner programmer in mind.  Some of them would graduate and move on to other more powerful languages, while some won't.  Those that won't, will be content having learned a bit of programming and will remain hobbyists forever.  The addition of concepts to the language had to be very carefully done between these two sets of audience.

    For those that are taking the first step into the world of programming, there isn’t an easier and crisper way of teaching branching.  Goto is such a clear and easy to explain this concept that I felt was necessary to include for a beginner.

    Now, CS purists would argue that this is setting a bad trend for beginners that might become professional programmers later on and carry with them the bad practice.  I believe that it is better to learn, understand and forget a bad habit than to be ignorant.  Every structured programming language has some version of restricted Goto in the form of break, continue, etc. Understanding that fundamental concept is crucial for professional programmers.  Besides, if they’re graduating from Small Basic, they’re going to run into unrestricted Goto in at least 7 out of the top 10 programming languages.

    Coming back to the Small Basic audience, here’s my theory.  Those that are content being hobbyist programmers in Small Basic, don’t really care about the nuances of using Goto.  And those that graduate on to more advanced programming from Small Basic, would be able to appreciate why unrestricted Gotos are “bad.”

    PS: Here’s a thread with Linus that I found was interesting.

  • Small Basic

    Using Command Line Arguments

    • 1 Comments

    The Arguments object in the standard library allows you to work with Command Line Arguments from inside a Small Basic program.  The Arguments object includes a Count property that gives the total number of arguments passed to the program, and a GetArgument(index) operation that allows you to access each of these arguments using their index.

    The following program prints out all the prime numbers less than the number passed at the command line.

    If (Arguments.Count = 1) Then
      maxNumber = Arguments.GetArgument(1)
      
      If (maxNumber < 2) Then
        TextWindow.WriteLine("No Primes less than 2.")
      EndIf
      
      For i = 2 To maxNumber
        CheckPrime()
        If (isPrime = "True") Then
          TextWindow.WriteLine(i)
        EndIf
      EndFor
    Else
      TextWindow.WriteLine("Usage: prime.exe ")
    EndIf
    
    Sub CheckPrime
      isPrime = "True"
      For j = 2 To Math.SquareRoot(i)
        If (Math.Remainder(i, j) = 0) Then
          isPrime = "False"
        EndIf
      EndFor
    EndSub
    

    In this program, Small Basic tries to coerce any passed argument as a number.  If you try passing a non-number, the program ends up with “0” and will print out “No Primes less than 2.”

     

  • Small Basic

    Sample of the Week

    • 4 Comments

    This week's featured sample of the week comes from mcleod_ideafix.  This is a fun little point and shoot game, involving the Turtle in a clever way.

    'Point-and-shoot   
    '(C)1989 McLeod/IdeaFix. http://www.zxprojects.com   
      
    'Target size   
    TargetSize=30   
    winner=0   
    wanttoplay=1   
      
    'A vector-screen style window   
    GraphicsWindow.BackgroundColor="Black"  
    GraphicsWindow.PenColor="Green"  
    GraphicsWindow.Clear()   
      
    Game()   
      
    Sub Game   
        While (wanttoplay=1)   
            GetRnd()   
            DrawTarget()   
            Turtle.PenUp()   
               
            'Game loop   
            While (winner=0)   
                Shoot()   
            EndWhile   
               
            winner=0   
            TextWindow.Write("Another game? (y/n) ")   
            answ = TextWindow.Read()   
            If (Text.StartsWith(Text.ConvertToLowerCase(answ),"n")) Then  
                wanttoplay=0   
            Else  
                GraphicsWindow.Clear()   
                Turtle.PenDown()   
                GraphicsWindow.PenColor="Green"  
            EndIf   
        EndWhile   
    EndSub   
      
    Sub Shoot   
        TextWindow.Write("Angle? ")   
        ang=TextWindow.ReadNumber()   
        TextWindow.Write("Distance? ")   
        dist=TextWindow.ReadNumber()   
      
        Turtle.Turn(ang)   
        Turtle.Move(dist)   
        Turtle.Turn(-ang)   
           
        xs=dist*Math.Cos(Math.GetRadians(ang))   
        ys=dist*Math.Sin(Math.GetRadians(ang))   
           
        If (xs>=xmin And xs<=xmax And ys>=ymin And ys<=ymax) Then  
            Sound.PlayChimes()   
            Turtle.Speed=100   
            Turtle.Move(-15)   
            Turtle.PenDown()   
            'The boom! visual effect   
            For n=1 To 36   
                GraphicsWindow.PenColor=GraphicsWindow.GetRandomColor()   
                Turtle.Move(30)   
                Turtle.Turn(150)   
            EndFor   
            Turtle.PenUp()   
            Turtle.Move(15)   
            Turtle.Turn(ang)   
            Turtle.Move(-dist)   
            Turtle.Turn(-ang)           
            Turtle.Hide()        
            TextWindow.WriteLine("Target cleared!! You WIN")   
            winner=1   
        Else  
            TextWindow.Write("Target failed!! Try again (press RETURN)")   
            TextWindow.Read()   
            Turtle.Speed=100   
            Turtle.Turn(ang)   
            Turtle.Move(-dist)   
            Turtle.Turn(-ang)   
            Turtle.Speed=7   
            winner=0   
        EndIf   
    EndSub   
      
    Sub GetRnd   
        'These variables should store the current Height and Width of 
        'GraphicsWindow, but it seems not to work   
        he=480   
        wi=640   
           
        If (he < wi) Then  
            distance=40+Math.GetRandomNumber(he/2-TargetSize-40)   
        Else  
            distance=40+Math.GetRandomNumber(wi/2-TargetSize-40)   
        EndIf   
        angle=Math.GetRandomNumber(360)   
           
        xmin=distance*Math.Cos(Math.GetRadians(angle))   
        ymin=distance*Math.Sin(Math.GetRadians(angle))   
        xmax=xmin+TargetSize   
        ymax=ymin+TargetSize   
    EndSub   
      
    Sub DrawTarget   
        Turtle.Speed=100       
        Turtle.PenUp()   
        Turtle.Turn(angle)   
        Turtle.Move(distance)   
        Turtle.Turn(-angle)   
        Turtle.PenDown()   
        For n=1 To 4   
            Turtle.Move(TargetSize)   
            Turtle.TurnRight()   
        EndFor   
        Turtle.PenUp()   
        Turtle.Turn(angle)   
        Turtle.Move(-distance)   
        Turtle.Turn(-angle)   
        Turtle.PenDown()   
        Turtle.Speed=7   
    EndSub
    

    And here's the screenshot.

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

Page 1 of 1 (5 items)