Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Snowflakes
#1
Here are some spare snowflakes, plenty of them in Sweden!

Something very similar was written in n6 many years ago.

Code:
' Snowflakes
' ----------

include "list.n7"

#win32

constant BG_R = 16, BG_G = 24, BG_B = 64

set window "Snowflakes", 480*screenw()/screenh(), 480, true
set redraw off

snowflakeImage = loadimage("snowflake.png")

stars = List()
for i = 1 to 200  stars.Add([x: rnd()*2 - 1, y: rnd()*2 - 1, z: 0.1 + rnd()*0.9])

set color BG_R, BG_G, BG_B
cls

do
    for i = 0 to stars.size - 1
        s = stars[i]
        s.z = s.z - 0.01
        if s.z < 0.1
            s.z = s.z + 0.9
            s.x = rnd()*2 - 1
            s.y = rnd()*2 - 1
        endif
    next
    stars.Sort(function(a, b)
            return b.z - a.z
        endfunc)
   
    set color BG_R, BG_G, BG_B, 96
    cls
    wp = width(primary)/2
    hp = height(primary)/2
    for i = 0 to stars.size - 1
        s = stars[i]
        x = wp + s.x*wp/s.z*0.75
        y = hp + s.y*hp/s.z*0.75
        size = 16/s.z
        set color BG_R, BG_G, BG_B, s.z*255
        DrawScaledImage(snowflakeImage, x - size/2, y - size/2, size, size)
    next
    fwait 60
    redraw
until keydown(KEY_ESCAPE) or mousebutton(0)

function DrawScaledImage(img, x, y, w, h)
    du = 1/w
    u = 0
    for dx = 0 to w - 1
        draw vraster img, x + dx, y, y + h - 1, u, 0, u, 1
        u = u + du
    next
endfunc


Attached Files
.zip   snowflakes.zip (Size: 7.75 KB / Downloads: 8)
Reply
#2
Reminds me of a "Starfield" but with a "Christmas-y" feel... Cool... Nicely done!
Logic is the beginning of wisdom.
Reply
#3
(12-09-2023, 12:00 PM)johnno56 Wrote: Reminds me of a "Starfield" but with a "Christmas-y" feel... Cool... Nicely done!

Your wish is my command  Big Grin Big Grin Big Grin
Here is the source code of the Starfield Simplified ...
note : Actually, it was just a modification of Marcus' Snowflakes. Thanks to Marcus  Big Grin

Code:
'----------------
' INITIALIZATION
'----------------
set window "Starfleet - simplified",800,600,false
set redraw off
stars = []
for i = 0 to 199  stars[i] = [x: rnd()*2-1, y:rnd()*2-1, z:0.1+rnd()*0.9]
wp = 800/2; hp = 600/2 'center of the screen

'-----------------
' MAIN PROGRAM
'-----------------
do
    set color 0,0,0;cls 'black
    for i = 0 to 199
        stars[i].z = stars[i].z - 0.01
        if stars[i].z < 0.1
            stars[i].z = stars[i].z + 0.9
        endif
       
        'Coordinate (x,y) is moving from center
        x = wp + stars[i].x*wp/stars[i].z
        y = hp + stars[i].y*hp/stars[i].z
                     
        radius = 1/stars[i].z
        set color 255,255,255 'white
        draw ellipse x,y,radius,radius,1 '1=filled
    next
    fwait 60
    redraw
until keydown(KEY_ESCAPE) or mousebutton(0)
Reply
#4
Very nicely done! Smile
Logic is the beginning of wisdom.
Reply
#5
Nice! Love programs that don't require any assets! Maybe I should write a snowflake version that also generates a random snowflake image, fractal.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)