smallbasic

Small Basic random colored objects order themselves?


I'm trying to give my Small Basic program a random background:

If File.ReadContents(Program.Directory + "\Turtle.settings") = "" Then
  Reset()
EndIf

Background = File.ReadLine(Program.Directory + "\Turtle.settings", 1)
Color = File.ReadLine(Program.Directory + "\Turtle.settings", 2)
Distance = File.ReadLine(Program.Directory + "\Turtle.settings", 3)
Mode = File.ReadLine(Program.Directory + "\Turtle.settings", 4)
Speed = File.ReadLine(Program.Directory + "\Turtle.settings", 5)
Width = File.ReadLine(Program.Directory + "\Turtle.settings", 6)

Controls.ButtonClicked = ButtonClicked
GraphicsWindow.KeyDown = KeyDown
GraphicsWindow.PenColor = Color
GraphicsWindow.PenWidth = Width
GraphicsWindow.Title = "Turtle"
Turtle.Speed = Speed

TextBoxOne = Controls.AddTextBox(10, 10)
Controls.SetTextBoxText(TextBoxOne, "Maximize the window")
Start = Controls.AddButton ("OK", 180, 8)

Sub KeyDown
EndSub

Sub ButtonClicked

  If Controls.LastClickedButton = Start Then
    Controls.Remove(TextBoxOne)
    Controls.Remove(Start)
    Turtle.X = GraphicsWindow.Width / 2
    Turtle.Y = GraphicsWindow.Height / 2
    Turtle.Show()

    If Background = 1 Then
      For I = 1 To 400
        X = Math.GetRandomNumber(GraphicsWindow.Width)
        Y = Math.GetRandomNumber(GraphicsWindow.Height)
        Z = Math.GetRandomNumber(1)
        GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
        GraphicsWindow.FillEllipse(X, Y, 20, 20)
      EndFor
    EndIf

    Menu()
  EndIf

EndSub

Sub Reset
  File.WriteLine(Program.Directory + "\Turtle.settings", 1, "1")
  File.WriteLine(Program.Directory + "\Turtle.settings", 2, "#000000")
  File.WriteLine(Program.Directory + "\Turtle.settings", 3, "100")
  File.WriteLine(Program.Directory + "\Turtle.settings", 4, "0")
  File.WriteLine(Program.Directory + "\Turtle.settings", 5, "10")
  File.WriteLine(Program.Directory + "\Turtle.settings", 6, "3")
EndSub

Sub Menu
EndSub

These are the results:

As you can see, sometimes the dots are at random positions, but otherwise the dots are ordered by color. I didn't make any changes to the code in between.

Changing Z = Math.GetRandomNumber(1) to Z = 1 prevents the dots from appearing ordered, even though the value of Z remains the same.

Removing all File.ReadLine() commands works too. The dots are always placed randomly if I remove the commands. I still want to use these, though, so I need another solution.

Removing Turtle.Y = GraphicsWindow.Height / 2 or moving it behind the background making code, also ensures a random placement of dots. I'll keep this as a temporary solution, but I'm still looking for a real fix.

If I put Turtle.Y = GraphicsWindow.Height / 2 before and after the background making code, the program will crash. I'll assume that this is due to an error in Small Basic itself, and that this is also what is causing my problems.


Solution

  • The error was caused by a problem in Small Basic itself. Therefore, it's impossible to correct it without editing the program. The only possible solution is removing Turtle.Y = GraphicsWindow.Height / 2 or moving it behind the background making code.