Tuesday, August 21, 2018

Starfield Simulation in Commodore BASIC

I posted a challenge in the VIC-20 group on Facebook.  The idea was to replicate the forward screen of the starship Enterprise from Star Trek.  This shows stars moving from the center of the screen to the edges, simulating forward motion.  It isn't meant to be an accurate 3D simulation.  ;-)

Here is my code of this.  One other person also submitted a version of it.  This could form the basis for a game.

[Update] --- I made a new version of this in Liberty BASIC!  Click to see the new version

 10 gosub 500  
 100 for c=0 to n  
 110 xold=st(c,0)  
 120 yold=st(c,1)  
 130 st(c,0)=xold*1.23  
 140 st(c,1)=yold*1.23  
 150 x=int(st(c,0))  
 160 y=int(st(c,1))  
 165 if abs(x)<2 and abs(y)<2 then 320  
 170 if abs(x)>10 or abs(y)>10 thenst(c,0)=(rnd(1)-.5)*3:st(c,1)=(rnd(1)-.5)*3:goto150  
 180 pstar=center+x+y*22  
 190 poke center+int(xold)+int(yold)*22,32  
 200 poke pstar,108  
 300 next c  
 310 goto 100  
 320 poke center+int(xold)+int(yold)*22,32  
 400 next c  
 410 goto 100  
 500 poke 36879,14  
 501 input"# stars (1-10)";n  
 502 n=n-1  
 505 print"{clrscrn}";  
 510 dim st(9,1)  
 520 for x=0 to n  
 530 st(x,0)=(rnd(1)-.5)*10  
 540 st(x,1)=(rnd(1)-.5)*10  
 550 next x  
 590 center=7932  
 600 return  

2 comments:

BJB said...

Nice piece pf code! My first computer was a VIC-20 and I spent countless hours reading the user guide supplied with it to learn programming "basics". I remember one piece of code (perhaps in the user guide or maybe a magazine) that had a for loop using the variable k. It took a while for me to realize it wasn't "fork" but "for k"!

It's amazing how impressive "Tank vs UFO" was in those days. Never looked at <*> the same again!

Hmm.. I think there is a problem on line 165 in your listing, since line 170 is merged with it and there is odd code in there.

Carl Gundel said...

Thanks, I reposted the code. It seems that blogspot mangled it.