1 rem *** TURTLE GRAPHICS ON C65 2 rem *** Turtle graphics simulates a robot on wheels with a 3 rem *** writing pen, which can perform the following actions: 4 rem *** - move forward a specified amount of steps 5 rem *** - rotate left a specified amount of degrees 6 rem *** - rotate right a specified amount of degrees 7 rem *** - lower the pen 8 rem *** - raise the pen 9 rem *** VARIABLES TO CONTROL THE TURTLE 10 rem *** s = number of steps (pixels) 11 rem *** rr = relative rotation, rotates left or right that many degrees 12 rem *** ar = absolute rotation, used internally to calculate the trajectory 13 rem *** x = current X position 14 rem *** y = current Y position 15 rem *** p = pen status (up or down) 16 rem *** SUBROUTINE LINE NUMBERS TO CONTROL THE TURTLE 17 rem *** 1000 = move forward 18 rem *** 2000 = rotate left 19 rem *** 3000 = rotate right 39 rem *** DRAWING AREA IS BEING DRAWN *** 40 graphic clr 50 screen def 1,1,1,4: rem screen def 1,0,0,4 60 screen open 1 70 palette 1,0,0,0,0 : rem black 80 palette 1,1,15,0,0 : rem red 90 palette 1,2,0,0,15 : rem blue 100 palette 1,3,0,15,0 : rem green 110 palette 1,4,15,11,0 : rem orange 120 palette 1,5,0,11,15 : rem dark cyan 121 palette 1,6,15,15,15 : rem white 122 palette 1,7,15,15,0 : rem yellow 123 palette 1,8,7,7,7 : rem medium gray 130 screen set 1,1 140 scnclr 2 150 border 2 155 rem *** turtle placed at (320,200), relative and absolute angles 0, pen down 160 x=320: y=200: ar=0: rr=0: p=1 170 c=3.14159265358979/180.0: rem constant to convert radians to degrees 200 rem *** TURTLE OPERATIONS GO HERE (this example draws a quasi-regular pentagon) 205 sn=5 210 for i=1 to sn 220 s=100: gosub 1000 230 rr=360/sn: gosub 2000 240 next i 245 rem *** AFTER THE TURTLE OPERATIONS, WAIT KEY TO RETURN TO MAIN SCREEN 250 get a$ : if a$ = "" then goto 250 260 screen close 1 270 palette restore 280 border 6 999 end 1000 rem GO FORWARD 1009 rem X COMPONENT = COSINE OF ABSOLUTE ROTATION * steps 1010 d=ar*c 1011 xc=cos(d): xc=xc*s 1019 rem Y COMPONENT = 0 - SINE OF ABSOLUTE ROTATION * steps 1020 yc=sin(d): yc=yc*s: yc=0-yc 1029 rem SAVE ORIGINAL X AND Y VALUES 1030 x0=x: y0=y 1039 rem CALCULATE NEW X AND Y VALUES 1040 x=x0+xc: y=y0+yc 1049 rem TRACE THE LINE IF THE PEN IS DOWN 1050 if p=1 then line x0,y0,x,y 1060 return 2000 rem ROTATE LEFT 2010 ar=ar+rr 2020 if ar>360 then ar=ar-360 2030 return 3000 rem ROTATE RIGHT 3010 ar=ar-rr 3020 if ar<0 then ar=ar+360 2040 return