5 CLEAR 32767 10 GO SUB 6000 100 REM BASIC challenge #6 - Terraspin 110 REM reddit.com/r/RetroBattlestations 120 REM written by FozzTexx - Spectrum port by /u/spectrumero 200 REM === initialize vars 210 LET sw=256: LET sh=192: LET xs=sw/1000: LET ys=(sh*4)/(1000*3) 220 LET sw=sw-1: LET sh=sh-1 230 DIM s(10) 250 REM === set turtle start at centre of screen pointing up pen up 260 LET tx=500: LET ty=sh/ys/2: LET ta=90: LET tp=0 300 REM === get command to execute 310 GO TO 8010 500 REM === Command interpreter 510 LET ip=1: LET sp=0 520 LET c$=m$(ip TO ip) 530 IF c$=" " THEN LET ip=ip+1: GO TO 520 540 IF c$>="-" AND c$<="9" THEN LET z$=m$(ip TO ): GO SUB 7000: GO SUB 1010: GO SUB 1210 550 IF c$="(" THEN LET v=ip: GO SUB 1010 560 IF c$=")" THEN GO SUB 2010 570 IF c$="M" THEN GO SUB 2510 580 IF c$="T" THEN GO SUB 3010 590 IF c$="U" THEN GO SUB 3510 600 IF c$="D" THEN GO SUB 4010 605 IF c$="I" THEN GO SUB 5510 606 IF c$="Q" THEN %close #5:%close #4: STOP 610 LET ip=ip+1 620 IF ip>LEN (m$) THEN RETURN 630 GO TO 520 1000 REM === push onto stack 1010 LET sp=sp+1: LET s(sp)=v 1020 RETURN 1100 REM === pop from stack 1110 IF sp<1 THEN PRINT #5;"Stack underflow": LET v=0: RETURN 1115 LET v=s(sp): LET sp=sp-1 1120 RETURN 1200 REM === skip over number 1210 LET ip=ip+1 1220 IF ip>LEN m$ THEN GO TO 1250 1230 LET d$=m$(ip TO ip) 1240 IF d$>="-" AND d$<="9" THEN GO TO 1210 1250 LET ip=ip-1 1260 RETURN 1500 REM === plot a line 1510 LET ox=x2-x1: LET py=sh-y1: LET oy=sh-y2-py 1515 PLOT x1,py: DRAW ox,oy 1517 PRINT #5;"draw: ": PRINT #5;x1: PRINT #5;py: PRINT #5;ox: PRINT #5;oy 1520 RETURN 2000 REM === loop instruction end 2010 GO SUB 1110: LET bp=v 2020 GO SUB 1110: LET lr=v 2030 LET lr=lr-1 2040 IF lr<1 THEN RETURN 2050 LET v=lr: GO SUB 1010 2060 LET v=bp: GO SUB 1010 2070 LET ip=bp 2080 RETURN 2500 REM === move 2510 GO SUB 1110 2520 LET lx=v*COS ((360-ta)*PI/180): LET ly=v*SIN ((360-ta)*PI/180) 2525 PRINT #5;"move lx=";lx;" ly=";ly 2530 IF tp>0 THEN LET x1=tx: LET y1=ty: LET x2=x1+lx: LET y2=y1+ly: GO SUB 4510 2540 LET tx=tx+lx: LET ty=ty+ly 2550 RETURN 3000 REM === turn 3010 GO SUB 1110 3020 LET ta=ta+v 3030 IF ta<0 THEN LET ta=ta+360: GO TO 3030 3040 IF ta>=360 THEN LET ta=ta-360: GO TO 3040 3045 PRINT #5;"turn: ta=";ta 3050 RETURN 3500 REM === pen up 3510 LET tp=0 3515 PRINT #5;"pen up" 3520 RETURN 4000 REM === pen down 4010 LET tp=1 4015 PRINT #5;"pen down" 4020 RETURN 4500 REM === draw line, clipping if needed 4510 LET x1=x1*xs: LET x2=x2*xs: LET y1=y1*ys: LET y2=y2*ys 4520 LET x=x1: LET y=y1: GO SUB 5010: LET c1=c 4530 LET x=x2: LET y=y2: GO SUB 5010: LET c2=c 4540 IF c1=0 AND c2=0 THEN GO SUB 1510: RETURN 4550 IF c1>0 AND c2>0 THEN RETURN 4560 LET c=c1: IF c=0 THEN LET c=c2 4570 POKE 40000,c: POKE 40001,1: IF USR 32768 THEN LET x=x1+(x2-x1)*(sh-y1)/(y2-y1): LET y=sh: GO TO 4610 4580 POKE 40001,2: IF USR 32768 THEN LET x=x1+(x2-x1)*(0-y1)/(y2-y1): LET y=0: GO TO 4610 4590 POKE 40001,4: IF USR 32768 THEN LET y=y1+(y2-y1)*(sw-x1)/(x2-x1): LET x=sw: GO TO 4610 4600 POKE 40001,8: IF USR 32768 THEN LET y=y1+(y2-y1)*(0-x1)/(x2-x1): LET x=0: GO TO 4610 4610 IF c=c1 THEN LET x1=x: LET y1=y: GO TO 4630 4620 LET x2=x: LET y2=y 4630 GO TO 4520 5000 REM === calculate region code 5010 LET c=0 5020 IF y>sh THEN POKE 40000,c: POKE 40001,1: LET c=USR 32780 5030 IF y<0 THEN POKE 40000,c: POKE 40001,2: LET c=USR 32780 5040 IF x>sw THEN POKE 40000,c: POKE 40001,4: LET c=USR 32780 5050 IF x<0 THEN POKE 40000,c: POKE 40001,8: LET c=USR 32780 5060 RETURN 5500 REM === change ink colour 5510 GO SUB 1110 5515 PRINT #5;"ink colour=";v 5520 PRINT #2;: INK v 5530 RETURN 5999 REM === USR 32768 bitwise AND,32780 bitwise OR. values at 40000,40001 6000 RESTORE : FOR a=32768 TO 32791 6010 READ n: POKE a,n 6020 NEXT a 6030 RETURN 6040 DATA 58,64,156,71,58,65,156,160,6,0,79,201 6050 DATA 58,64,156,71,58,65,156,176,6,0,79,201 6999 REM === Get number from string 7000 FOR z=1 TO LEN z$ 7010 IF z$(z TO z)=" " THEN LET v=VAL z$( TO z-1): RETURN 7020 NEXT z 7030 LET v=VAL z$: RETURN 8000 REM === Network routine 8020%listen #4,2000 8030%accept #5,4 8040 PRINT #5;"command> "; 8050 INPUT #5,m$ 8060 PRINT #5;"Running..." 8070 GO SUB 510 8080 PRINT #5;"Done" 8090 GO TO 8040 9000%close #5 9010 GO TO 8030