Page 1 of 1

Turtle graphics library

Posted: Wed Mar 04, 2015 10:28 pm
by Gordon
Hi - my first posting.

I got Smart BASIC yesterday and have written a little Turtle Graphics library. It's been about 25 years since I wrote any code, so I will welcome any suggestions or observations about it.

Gordon

----- Turtle Graphics ---

The turtle in this implementation is invisible.

The turtle can move forward and backward by a specified number of steps. Steps are one pixel width long.

The turtle carries a pen. If the pen is up it will move without leaving a trail. If the pen is down it will draw a line as it moves.

The turtle can turn left or right through a specified angle, measured in degrees.

The turtle's home (also invisible) is in the centre of the screen. When the turtle goes home it likes to face upwards, towards the top of the screen.

---- library -----

TURTLEGRAPHICS should be used instead of GRAPHICS. In addition to switching to graphics view it sets the representation of angles to degrees (system default is radians,) sends the turtle home and puts its pen down.

HOME sends the turtle to the centre of the screen, facing upwards.

PENUP instructs the turtle to lift its pen so it will not draw when it moves.

PENDOWN instructs the turtle to lower its pen so it will draw a line when it moves.

LEFT(degrees) instructs the turtle to turn anticlockwise by the specified angle.

RIGHT(degrees) instructs the turtle to turn clockwise by the specified angle.

FORWARD(steps) instructs the turtle to move forward the specified distance.

BACK(steps) instructs the turtle to move backward the specified distance.

----- demo -----

The demo code at the end should be commented out once it is seen to work.

It should look like this on an iPad.

Image


----- copy to your Dropbox from here -----

https://www.dropbox.com/s/zb8vx78ftlkf3 ... e.txt?dl=0

Code: Select all

def turtlegraphics
    option angle degrees
    graphics
    home
    pendown
end def

def home
    x = screen_width()/2
    y = screen_height()/2
    heading = 0
end def

def penup 
    status = 0
end def

def pendown 
    penup.status = 1
end def

def left(angle)
    home.heading = (home.heading+angle)%360
    if home.heading < 0 then home.heading += 360
end def

def right(angle)
    left(0-angle)
end def

def forward(distance)
    newx = home.x-distance*sin(home.heading)
    newy = home.y-distance*cos(home.heading)
    if penup.status = 1 then
       draw line home.x,home.y to newx,newy
    endif
    home.x = newx
    home.y = newy
end def

def back(distance)
    forward(0-distance)
end def

/* ----- demo starts here ----- */

def poly(sides,length)
    for x = 1 to sides
        forward(length)
        right(360/sides)
    next x
end def

turtlegraphics

penup 
right(90)
back(300)
left(90)
pendown

for x = 3 to 18
    poly(x,100)
next x



Re: Turtle graphics library

Posted: Wed Mar 04, 2015 10:32 pm
by DrChip
Cool! Brings back good memories! :)

Re: Turtle graphics library

Posted: Thu Mar 05, 2015 5:16 am
by Mr. Kibernetik
Turtle Robotics :geek:

Nice code!