Author Topic: Parallax starfield in less than 10 lines of code  (Read 9390 times)

0 Members and 3 Guests are viewing this topic.

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
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
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
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
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Maybe! Take a look at the 20 lines compo for some hints: http://www.dbfinteractive.com/forum/index.php?topic=5140.0
« Last Edit: May 23, 2011 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
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
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Wow Shocky ... that is indeed a good pack algo you use there ;-)
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
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.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline bj

  • ZX 81
  • *
  • Posts: 20
  • Karma: 10
    • View Profile
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

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

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
....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.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
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
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
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

The mega-thread in WoS is at
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!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Parallax starfield in less than 10 lines of code
« Reply #10 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.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Parallax starfield in less than 10 lines of code
« Reply #11 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.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Parallax starfield in less than 10 lines of code
« Reply #12 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!
Shockwave ^ Codigos
Challenge Trophies Won: