alvalongo

Turtle graphics - Pythagoras tree

Aug 11th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 100 gosub 8000:print chr$(147);chr$(5);
  2. 105 print "2016-July basic month: terraspin"
  3. 110 print "http://reddit.com/r/retrobattlestations"
  4. 115 print "written by fozztexx - commodore 64 port by fozztexx"
  5. 120 print "commodore 64 basic has bitwise operators so much of the line"
  6. 125 print "clipping logic is simplified compared to the apple ii version"
  7. 130 print "the commodore 64 has *no* graphics commands at all"
  8. 135 print "all graphics has to be done using peeks and pokes"
  9. 140 print "line drawing is handled by a complicated routine at 1500"
  10. 145 print "individual pixels are then plotted with the routine at 1700"
  11. 150 print "2016-08-10 alvalongo"
  12. 155 print "name=tortu08.bas"
  13. 170 print "========================================"
  14. 199 :
  15. 200 rem === initialize variables
  16. 210 sw=320:sh=200:xs=sw/1000:ys=(sh*4)/(1000*3):k1=1:e=1
  17. 220 sw=sw-k1:sh=sh-k1:dg=360:k7=7:k8=8:z0=0:sid=54296
  18. 230 p8={pi}/180:rem pi is a c64 constant
  19. 235 sid=54272:rem sound device
  20. 240 dim b(7),c(39),l(199):rem bitmask, column offset, line
  21. 245 gosub 8100
  22. 250 rem === set turtle start at center of screen pointing up, pen up
  23. 260 tx=500:ty=sh/ys/2:ta=90:tp=z0
  24. 265 :
  25. 300 rem === get command to execute
  26. 301 rem numeric values will be pushed onto a stack
  27. 302 rem a command will pop off a value and then execute
  28. 303 rem d - lower the pen
  29. 304 rem u - raise the pen
  30. 305 rem m - move
  31. 306 rem t - turn
  32. 307 rem ======================
  33. 310 cm$="500,700 g d 200 m [ -45 t 150 m [ -45 t 100 m [ -45 t 50 m ] 45 t 50 m"
  34. 311 cm$=cm$+" ] 45 t 100 m [ -45 t 50 m ] 45 t 50 m"
  35. 312 cm$=cm$+" ] 45 t 150 m [ -45 t 100 m [ -45 t 50 m ] 45 t 50 m"
  36. 314 cm$=cm$+" ] 45 t 100 m [ -45 t 50 m ] 45 t 50 m"
  37. 390 :
  38. 460 rem ### if the program crashes it will
  39. 470 rem ### be stuck in graphics mode.
  40. 480 rem ### type run 900 to get back to text
  41. 490 gosub 6000:gosub 6100
  42. 495 :
  43. 500 rem === command interpreter
  44. 510 ip=k1:sp=z0
  45. 520 c$=mid$(cm$,ip,k1)
  46. 530 if c$=" " or c$="," then ip=ip+k1:goto 520
  47. 531 gosub 9000
  48. 535 print ip;",";tx;",";ty;",";ta;",";tp;",";d$
  49. 540 if c$>="-" and c$<="9" then v=val(mid$(cm$,ip)):gosub 1010:gosub 1210
  50. 550 if c$="(" then v=ip:gosub 1010
  51. 560 if c$=")" then gosub 2010
  52. 570 if c$="m" then gosub 2510
  53. 580 if c$="t" then gosub 3010
  54. 590 if c$="u" then gosub 3510
  55. 600 if c$="d" then gosub 4010
  56. 601 if c$="h" then gosub 7000:rem home
  57. 602 if c$="x" then gosub 7100:rem set x
  58. 603 if c$="y" then gosub 7200:rem set y
  59. 604 if c$="g" then gosub 7300:rem set x and y
  60. 605 if c$="b" then gosub 7400:rem set bearing
  61. 606 if c$="[" then gosub 7500:rem set push
  62. 607 if c$="]" then gosub 7600:rem set pop
  63. 610 ip=ip+k1
  64. 620 if ip<=len(cm$) then 520
  65. 700 get k$:if k$="" then gosub 8200:goto 700
  66. 900 print "End"
  67. 910 gosub 8000
  68. 990 end
  69. 1000 rem === push onto stack
  70. 1010 sp=sp+k1:sk(sp)=v
  71. 1020 return
  72. 1025 :
  73. 1100 rem === pop from stack
  74. 1110 v=sk(sp):sp=sp-k1
  75. 1120 return
  76. 1125 :
  77. 1200 rem === skip over number
  78. 1210 ip=ip+k1
  79. 1220 if ip>len(cm$) then 1250
  80. 1230 c2$=mid$(cm$, ip, k1)
  81. 1240 if c2$>="-" and c2$<="9" then goto 1210
  82. 1250 ip=ip-k1
  83. 1260 return
  84. 1265 :
  85. 1500 rem === plot a line
  86. 1510 x1%=x1:x2%=x2:y1%=y1:y2%=y2
  87. 1520 dx%=abs(x2%-x1%):sx%=-k1:if x1%<x2% then sx%=k1
  88. 1530 dy%=abs(y2%-y1%):sy%=-k1:if y1%<y2% then sy%=k1
  89. 1540 er%=-dy%:if dx%>dy% then er%=dx%
  90. 1550 er%=er%/2
  91. 1560 gosub 1710
  92. 1570 if x1%=x2% and y1%=y2% then return
  93. 1580 e2%=er%
  94. 1590 if e2%>-dx% then er%=er%-dy%:x1%=x1%+sx%
  95. 1600 if x1%=x2% and y1%=y2% then return
  96. 1610 if e2%<dy% then er%=er%+dx%:y1%=y1%+sy%
  97. 1620 if x1%=x2% and y1%=y2% then return
  98. 1630 goto 1560
  99. 1640 :
  100. 1700 rem === plot a point ===========
  101. 1710 by=l(y1%)+c(x1%/k8)
  102. 1730 poke by,peek(by) or b(x1% and k7)
  103. 1740 return
  104. 1750 :
  105. 2000 rem === loop instruction end
  106. 2010 gosub 1110:bp=v
  107. 2020 gosub 1110:lr=v
  108. 2030 lr=lr-k1
  109. 2040 if lr<k1 then return
  110. 2050 v=lr:gosub 1010
  111. 2060 v=bp:gosub 1010
  112. 2070 ip=bp
  113. 2080 return
  114. 2085 :
  115. 2500 rem === move
  116. 2510 gosub 1110
  117. 2520 lx=v*cos((dg-ta)*p8):ly=v*sin((dg-ta)*p8)
  118. 2530 if tp>z0 then x1=tx:y1=ty:x2=x1+lx:y2=y1+ly:gosub 4510
  119. 2540 tx=tx+lx:ty=ty+ly
  120. 2550 return
  121. 2555 :
  122. 3000 rem === turn
  123. 3010 gosub 1110
  124. 3020 ta=ta-v
  125. 3030 if ta<z0  then ta=ta+dg:goto 3030
  126. 3040 if ta>=dg then ta=ta-dg:goto 3040
  127. 3050 return
  128. 3055 :
  129. 3500 rem === pen up
  130. 3510 tp=k0
  131. 3520 return
  132. 3525 :
  133. 4000 rem === pen down
  134. 4010 tp=k1
  135. 4020 return
  136. 4025:
  137. 4500 rem === draw line, clipping if needed
  138. 4510 x1=x1*xs:x2=x2*xs:y1=y1*ys:y2=y2*ys
  139. 4520 x=x1:y=y1:gosub 5010:c1=c
  140. 4530 x=x2:y=y2:gosub 5010:c2=c
  141. 4540 if c1=z0 and c2=z0 then gosub 1510:return
  142. 4550 if c1 and c2 then return
  143. 4560 c=c1:if c=z0 then c=c2
  144. 4570 if c and 1 then x=x1+(x2-x1)*(sh-y1)/(y2-y1):y=sh:goto 4610
  145. 4580 if c and 2 then x=x1+(x2-x1)*(z0-y1)/(y2-y1):y=z0:goto 4610
  146. 4590 if c and 4 then y=y1+(y2-y1)*(sw-x1)/(x2-x1):x=sw:goto 4610
  147. 4600 if c and 8 then y=y1+(y2-y1)*(z0-x1)/(x2-x1):x=z0:goto 4610
  148. 4610 if c=c1 then x1=x:y1=y:goto 4630
  149. 4620 x2=x:y2=y
  150. 4630 goto 4520
  151. 5000 rem === calculate region code
  152. 5010 c=z0
  153. 5020 if y>sh then c=c or k1
  154. 5030 if y<z0 then c=c or 2
  155. 5040 if x>sw then c=c or 4
  156. 5050 if x<z0 then c=c or k8
  157. 5060 return
  158. 5065 :
  159. 6000 rem === enable hi-res 320x200 screen
  160. 6010 poke 56576,(peek(56576)and 252)or 2:rem video bank 1 at $4000
  161. 6015 poke 53272,0+128:rem offset $0000 (hires)+$2000(color)
  162. 6020 poke 53265,peek(53265) or 32:rem hires-on
  163. 6030 bm=16384:rem bitmap memory at $4000
  164. 6040 cm=24576:rem color memory at $6000
  165. 6050 for x=0 to 7:b(x)=2^(k7-(x and k7)):next x:rem bit mask
  166. 6060 for x=0 to 39:c(x)=k8*x:next x:rem column offset
  167. 6070 for y=0 to 199:l(y)=bm+320*int(y/k8)+(y and k7):next y:rem line
  168. 6080 return
  169. 6095 :
  170. 6100 rem === clear hires screen
  171. 6110 for i=cm to cm+999:poke i,3:next:rem color
  172. 6120 sys 49152:rem clear hires
  173. 6130 return
  174. 6135 :
  175. 7000 rem === home
  176. 7010 tx=500:ty=sh/ys/2:ta=90
  177. 7020 return
  178. 7025 :
  179. 7100 rem === set x position
  180. 7110 gosub 1110
  181. 7120 tx=v
  182. 7130 return
  183. 7135 :
  184. 7200 rem === set y position
  185. 7210 gosub 1110
  186. 7220 ty=v
  187. 7230 return
  188. 7235 :
  189. 7300 rem === set x and y position
  190. 7310 gosub 1110
  191. 7320 ty=v
  192. 7330 gosub 1110
  193. 7340 tx=v
  194. 7350 return
  195. 7355 :
  196. 7400 rem === set bearing
  197. 7410 gosub 1110
  198. 7420 ta=abs(v)-90
  199. 7430 return
  200. 7435 :
  201. 7500 rem === push
  202. 7510 v=ta:gosub 1000
  203. 7520 v=ty:gosub 1000
  204. 7530 v=tx:gosub 1000
  205. 7540 return
  206. 7545 :
  207. 7600 rem === pop
  208. 7610 gosub 1110:tx=v
  209. 7620 gosub 1110:ty=v
  210. 7630 gosub 1110:ta=v
  211. 7640 return
  212. 7645 :
  213. 8000 rem === enable text screen ===
  214. 8010 poke 56576,(peek(56576)and 252)or 3:rem video bank 0 at $0000
  215. 8015 poke 53272,16+6:rem offset $0400+$1800
  216. 8020 poke 53265,peek(53265) and 223:rem text
  217. 8030 return
  218. 8100 rem === clear hires screen assembler
  219. 8110 s=z0:for by=49152 to 49176
  220. 8120 read a:poke by,a:s=s+a:next by
  221. 8130 read a:if s<>a then print "error assembly":end
  222. 8140 data 169,0,133,251,169,64,133,252
  223. 8150 data 162,32,160,0,169,0,145,251
  224. 8160 data 136,208,251,230,252,202,208
  225. 8170 data 246,96,3919
  226. 8180 return
  227. 8195 :
  228. 8200 rem --- beep
  229. 8210 poke sid+4,0:poke sid+5,9:poke sid+6,11:poke sid+24,15
  230. 8220 poke sid,95:poke sid+1,41:rem 622hz=10591
  231. 8230 poke sid+4,17
  232. 8240 z=ti+100
  233. 8250 if ti<z then 8250
  234. 8260 poke sid+4,16
  235. 8270 z=ti+100
  236. 8280 if ti<z then 8280
  237. 8290 return
  238. 9000 rem ===
  239. 9010 d$=c$:if c$<"-" and c$>"9" then 9070
  240. 9020 jp=ip
  241. 9030 jp=jp+k1
  242. 9040 if jp>len(cm$) then 9070
  243. 9050 c2$=mid$(cm$, jp, k1)
  244. 9060 if c2$>="-" and c2$<="9" then d$=d$+c2$:goto 9030
  245. 9070 return
Add Comment
Please, Sign In to add comment