Tuesday, April 3, 2012

SmallBasic Turtle Logo Source Code 2

'Turtle Logo in Small Basic
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
'Turtle show hide
'
' I did this in one afternoon. Could've been faster if I ignore Show/Hide Turtle
' Updated with Shapes. Sure is a lot faster had I known this earlier!

GraphicsWindow.Show()
GraphicsWindow.Clear()

cmd1="init"
Toitle()
cmd1="size"
cmd2=8
Toitle()



For loop=1 to 360 Step 20
cmd1="moveto"
cmd2=sx/2
cmd3=sy/2
Toitle()
cmd1="turn"
cmd2=loop
Toitle()
cmd1="move"
cmd2=200
Toitle()
Program.Delay(1000)
endfor



beginloop:
GraphicsWindow.Clear()
cmd1="init"
Toitle()
Program.Delay(2000)
cmd1="show"
Toitle()

For loop=1 to 50
cmd1="penup"
Toitle()
cmd1="moveto"
cmd2=Math.GetRandomNumber(sx)
cmd3=Math.GetRandomNumber(sy)
Toitle()
cmd1="pendown"
Toitle()
cmd1="color"
cmd2=GraphicsWindow.GetRandomColor()
Toitle()
cmd1="size"
cmd2=Math.GetRandomNumber(10)
Toitle()
' Show Turtle is very slow!
' If (loop=10) Then
' cmd1="show"
' Toitle()
' ElseIf (loop=30) then
' cmd1="hide"
' Toitle()
' EndIf
For loop2=1 to Math.GetRandomNumber(6)
cmd1="turn"
cmd2=Math.GetRandomNumber(360)
Toitle()
cmd1="move"
cmd2=Math.GetRandomNumber(1000)
Toitle()
endfor
endfor
Program.Delay(15000)
Goto beginloop



Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
'Bug: GetPixel does not take Background color
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(0,0,sx,sy)
x1=sx/2
y1=sy/2
x0=x1
y0=y1
p=1
angle=180
col="blue"
GraphicsWindow.BrushColor=col
siz=1
ts=1
SaveBG()
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
' LoadBG()
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
' SaveBG()
elseif (cmd1="moveto") then
' LoadBG()
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
' SaveBG()
elseif (cmd1="turn") then
LoadBG()
angle=cmd2
SaveBG()
elseif (cmd1="turnleft") then
LoadBG()
angle=angle+cmd2
SaveBG()
elseif (cmd1="turnright") then
LoadBG()
angle=angle-cmd2
SaveBG()
elseif (cmd1="color") then
col=cmd2
elseif (cmd1="size") then
siz=math.Max(1,cmd2)
siz=math.Min(10,siz)
elseif (cmd1="show") then
if (ts<>1) then
ts=1
SaveBG()
endif
elseif (cmd1="hide") then
LoadBG()
ts=0
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
' RemBugFix2()
'x1=Math.Remainder(x1,sx)
'y1=Math.Remainder(y1,sy)
'angle=Math.Remainder(angle,360)

If (ts=1) then
' DrawTurtle()
' Shapes.Move(TT,x1,y1)
Shapes.Move(TT,px+(siz/2),py+(siz/2))
endif

endsub

Sub DrawTurtle
tx1=10*Math.Sin(Math.GetRadians(angle))
ty1=10*Math.Cos(Math.GetRadians(angle))
tx2=10*Math.Sin(Math.GetRadians(angle+140))
ty2=10*Math.Cos(Math.GetRadians(angle+140))
tx3=10*Math.Sin(Math.GetRadians(angle+220))
ty3=10*Math.Cos(Math.GetRadians(angle+220))
TT=Shapes.AddTriangle(tx1,ty1,tx2,ty2,tx3,ty3)
endsub


Sub RemBugFix
While (px<0) px=px+sx EndWhile While (py<0) py=py+sy EndWhile endsub Sub RemBugFix2 While (x1<0) x1=x1+sx EndWhile While (y1<0) y1=y1+sy EndWhile While (angle<0) angle=angle+360 EndWhile endsub 'Draw Sub Draw If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
mx1=0
mx3=m
For i=0 To m
my1=x0
my3=x1
mx2=i
map()
px=my2
my1=y0
my3=y1
mx2=i
map()
py=my2
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
If (siz>1) then
GraphicsWindow.BrushColor=col
GraphicsWindow.FillEllipse(px,py,siz,siz)
else
GraphicsWindow.SetPixel(px,py,col)
endif
Shapes.Move(TT,px+(siz/2),py+(siz/2))
EndFor
endif
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)

endsub

'map function
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'(y3-y1)*(x2-x1)/(x3-x1)=(y2-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub


sub SaveBG
If (ts=1) then
DrawTurtle()



' BGc=0
' BGx1=Math.Floor(Math.Max(0,x1-10))
' BGx1=Math.Floor(Math.Min(sx,BGx1))
' BGy1=Math.Floor(Math.Max(0,y1-10))
' BGy1=Math.Floor(Math.Min(sy,BGy1))
' BGx2=Math.Floor(Math.Max(0,x1+10))
' BGx2=Math.Floor(Math.Min(sx,BGx2))
' BGy2=Math.Floor(Math.Max(0,y1+10))
' BGy2=Math.Floor(Math.Min(sy,BGy2))
' For BGy=BGy1 To BGy2
' For BGx=BGx1 To BGx2
' BGc=BGc+1
' BG_Array[BGc]=GraphicsWindow.GetPixel(BGx,BGy)
' EndFor
' endfor
' TextWindow.Write("SaveBG ")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
endsub

Sub LoadBG
If (ts=1) then
Shapes.Remove(TT)

' BGc=0
' For BGy=BGy1 To BGy2
' For BGx=BGx1 To BGx2
' BGc=BGc+1
' GraphicsWindow.SetPixel(BGx,BGy,BG_Array[BGc])
' EndFor
' endfor
' TextWindow.Write("LoadBG")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
EndSub

No comments: