Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: DrewPee on May 23, 2011

Title: Parallax starfield in less than 10 lines of code
Post by: DrewPee on May 23, 2011
Thought I would share this - but can this be compressed any more?

Code: [Select]
#include once "tinyptc_ext.bi"
const xres=640,yres=480,sf=512:Dim shared as integer size,q,w,a,x(sf),y(sf),s(sf),sb(xres*yres):size=8
for a=0 to sf-1:x(a)=(int(rnd(1)*xres)-1):y(a)=1+(int(rnd(1)*yres)-1):s(a)=1+(int(rnd(1)*8)):next:ptc_setdialog(0,"Fullscreen?",0,1)
If( ptc_open( "Starfield in less than 10 lines of code", XRES, YRES ) = 0 ) Then
End -1:End If
#define pp(x,y,argb) sb(y*XRES+x)=argb
While(GETASYNCKEYSTATE(VK_ESCAPE)<> -32767 and PTC_GETLEFTBUTTON=FALSE):for a=0 to sf-1: x(a)=x(a)-s(a):if x(a)<=0 then x(a)=(xres-1):y(a)=1+(int(rnd(1)*yres)-1)
for q=1 to size:for w=1 to size:if x(a)+q>0 and x(a)+q<xres-3 and (y(a)+w)>0 and (y(a)+w)<yres-2 then pp(x(a)+q,(y(a)+w),rgb(s(a)*16,s(a)*16,s(a)*16))
next:next:next:ptc_update @sb(0):erase sb:Wend:ptc_close():end

DrewPee
Title: Re: Parallax starfield in less than 10 lines of code
Post by: benny! on May 23, 2011
Yeah, I like those limited line codes. Unfortunately, I do not know Freebasic so I cannot improve this code.

Just wondering if you could write the if-statement in one line:

Quote
If( ptc_open( "Starfield in less than 10 lines of code", XRES, YRES ) = 0 ) End -1
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Clyde on May 23, 2011
Maybe! Take a look at the 20 lines compo for some hints: http://www.dbfinteractive.com/forum/index.php?topic=5140.0 (http://www.dbfinteractive.com/forum/index.php?topic=5140.0)
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Shockwave on May 23, 2011
In 3 lines, delta timed:

Code: [Select]
#Include Once "tinyptc.bi"
If( ptc_open( "PARALAX STARFIELD BY SHOCKWAVE", 800, 600 ) = 1 ) Then dim shared buffer(800*600) as double, px(200),py(200) as double ,ps(200) as double:for n=0 to 200:px(n)=rnd(1)*790:py(n)=rnd(1)*590:ps(n)=1+(rnd(1)*5):next :dim shared dv as double, old as double
0 for n=0 to 200: old=timer: if px(n)>0 and px(n)<800 then buffer(int(px(n))+(int(py(n))*800))=&hffffff: px(n)=px(n)+(ps(n)*dv) else px(n)=1:  end if:next:ptc_update@buffer(0): sleep 1: dv=(timer-old)*40:erase buffer:if inkey$="" then 0
Title: Re: Parallax starfield in less than 10 lines of code
Post by: benny! on May 23, 2011
Wow Shocky ... that is indeed a good pack algo you use there ;-)
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Shockwave on May 23, 2011
Wow Shocky ... that is indeed a good pack algo you use there ;-)

These things are quite good fun but kind of pointless unless you place some physical limit of the amount of characters or instructions you can have in the code :)  Maybe we should run something like this again sometime.
Title: Re: Parallax starfield in less than 10 lines of code
Post by: bj on May 24, 2011
For a ZXSpectrum oneliner the limit was what could fit on one 32x24character screen of basic code. We had oneline versions of Space invaders, Pacman, and even Elite!

http://www.zxspectrum.00freehost.com/oneliners.html (http://www.zxspectrum.00freehost.com/oneliners.html)

The mega-thread in WoS is at
http://www.worldofspectrum.org/forums/showthread.php?t=16019&highlight=oneliner (http://www.worldofspectrum.org/forums/showthread.php?t=16019&highlight=oneliner)
Title: Re: Parallax starfield in less than 10 lines of code
Post by: benny! on May 24, 2011
....Maybe we should run something like this again sometime.

Yeah, that would be fun indeed - however I guess the rules would be hard since this kind of comp really depends on
the programming language you choose.
Title: Re: Parallax starfield in less than 10 lines of code
Post by: DrewPee on May 24, 2011
In 3 lines, delta timed:

Code: [Select]
#Include Once "tinyptc.bi"
If( ptc_open( "PARALAX STARFIELD BY SHOCKWAVE", 800, 600 ) = 1 ) Then dim shared buffer(800*600) as double, px(200),py(200) as double ,ps(200) as double:for n=0 to 200:px(n)=rnd(1)*790:py(n)=rnd(1)*590:ps(n)=1+(rnd(1)*5):next :dim shared dv as double, old as double
0 for n=0 to 200: old=timer: if px(n)>0 and px(n)<800 then buffer(int(px(n))+(int(py(n))*800))=&hffffff: px(n)=px(n)+(ps(n)*dv) else px(n)=1:  end if:next:ptc_update@buffer(0): sleep 1: dv=(timer-old)*40:erase buffer:if inkey$="" then 0

 :goodpost:

Nice one Shockwave - very impressive indeed - I was pleased with less than 10 lines of code so . . . excellent work indeed! :)

DrewPee
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Shockwave on May 24, 2011
For a ZXSpectrum oneliner the limit was what could fit on one 32x24character screen of basic code. We had oneline versions of Space invaders, Pacman, and even Elite!

http://www.zxspectrum.00freehost.com/oneliners.html (http://www.zxspectrum.00freehost.com/oneliners.html)

The mega-thread in WoS is at
http://www.worldofspectrum.org/forums/showthread.php?t=16019&highlight=oneliner (http://www.worldofspectrum.org/forums/showthread.php?t=16019&highlight=oneliner)

K++

What a fantastic link!  I've just spent the last 20 minutes mesmerised by these brilliant little programs!
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Shockwave on May 24, 2011
Nice one Shockwave - very impressive indeed - I was pleased with less than 10 lines of code so . . . excellent work indeed! :)

Thanks Andy - it would be a good challenge to run something like this again, it's a bit pointless as a comp unless there's a physical limit on instructions or characters thinking about what Benny said, I have a cunning plan to make it viable for everyone.
Title: Re: Parallax starfield in less than 10 lines of code
Post by: rain_storm on May 27, 2011
An excellent piece of code. But I think you guys are taking an excessive liberty by not considering ':' as a line break, these symbols are treated as such by the compiler. That takes all the fun out of the exercise   ;)
One trick I use in ps2Yabasic for defining variable values was this...
Code: [Select]
data 0,1,2,3,4,5,6,7,8,9
read a,b,c,d,e,f,g,h,i,j
[/font]
eliminates line breaks, if you can do such things in FB.
Title: Re: Parallax starfield in less than 10 lines of code
Post by: Shockwave on May 27, 2011
That's a cool trick Rainstorm!

Btw, I have a really cool idea for the next comp that is based on this in a loose kind of way but I'm looking for a free and obscure language that none of us use at the moment so that there is a level playing field!