"Let it snow" said BumbleBee - a simple snow flurry

Previous topic - Next topic

Quentin

I just started in programming in GLBasic and I'm surprised how easy it is to create nice effects. This simple example shows a snow flurry which should be more or less realistic ;-)))

Code (glbasic) Select
// --------------------------------- //
// Project: schnee
// Start: Friday, June 15, 2007
// IDE Version: 4.218

// a simple snow flurry

// TYPE declaration for a snowflake
TYPE tSnowFlake
  x            // X - position
  y            // Y - position
  size         // size of a snowflake
  speed        // speed with which the flakes fall to to ground
  rotate       // flag if a flake will rotate while falling
  angle        // value for the libration of the snowflakes
ENDTYPE

GLOBAL SnowFlakes[] AS tSnowFlake   // an array for our snowflakes
GLOBAL newFlake AS tSnowFlake       // a single flake to insert into the array

GLOBAL screen_width, screen_height  // current screen size

// Konstanten
MAX_SNOWFLAKES = 1000               // maximum count of the flakes


// Hauptprogramm
Init_Snow()                         // initialisation
WHILE TRUE
  Update_Snow()                     // show the snow flurry
  SHOWSCREEN
WEND




// ------------------------------------------------------------- //
// -=#  INIT_SNOW  #=-
// ------------------------------------------------------------- //
FUNCTION Init_Snow:

  LOCAL i

  // draw the snow flake
  DRAWLINE 0, 5, 10, 5, RGB(RND(255), 255, 255)
  DRAWLINE 5, 0, 5, 10, RGB(RND(255), 255, 255)
  DRAWLINE 1, 1, 9, 9, RGB(RND(255), 255, 255)
  DRAWLINE 9, 1, 1, 9, RGB(RND(255), 255, 255)

  // store the "artwork" in a sprite
  GRABSPRITE 0, 0, 0, 10, 10

  // get the current screen size
  GETSCREENSIZE screen_width, screen_height

  // now draw the snow flakes
  // - the position is randomized all over the screen
  // - the falling speed should be 0.1 to 1.5 pixels per cycle
  //   in case the RND() function returns a Null we will set
  //   the speed to 0.5
  // - the size should be 0.1 to 1 of the original sprite
  // - the field "rotate" will tell us if a snowflake will rotate or not
  //   function RND(1) will return a 0 = not rotate or 1 = rotate
  // - for the libration of the flakes we will use the SIN() function
  //   see function Update_Snow()
  // - last we store the new snowflake the the array SnowFlakes[]
  FOR i = 1 TO MAX_SNOWFLAKES
    newFlake.x = RND(screen_width)
    newFlake.y = RND(screen_height)
    newFlake.speed = RND(150) / 100
    IF newFlake.speed = 0
      newFlake.speed = 0.5
    ENDIF
    newFlake.size = RND(10) / 10
    newFlake.rotate = RND(1)
    newFlake.angle = RND(360)
    DIMPUSH SnowFlakes[], newFlake
  NEXT

ENDFUNCTION // INIT_SNOW


// ------------------------------------------------------------- //
// -=#  UPDATE_SNOW  #=-
// ------------------------------------------------------------- //
FUNCTION Update_Snow:

  LOCAL lsnow AS tSnowFlake

  FOREACH lsnow IN SnowFlakes[]

    // rotate or not?
    IF lsnow.rotate
      // if rotate then increase the rotation for the next cycle by one
      ROTOZOOMSPRITE 0, lsnow.x, lsnow.y, lsnow.rotate, lsnow.size
      INC lsnow.rotate, 1
    ELSE
      // otherwise only display the flake in the given size
      ZOOMSPRITE 0, lsnow.x, lsnow.y, lsnow.size, lsnow.size
    ENDIF

    INC lsnow.y, lsnow.speed         // calculate new Y position
    INC lsnow.angle, 2               // increase value for libration
    INC lsnow.x, SIN(lsnow.angle)    // and add it to the X position
    IF lsnow.y > screen_height       // if a flake will disappear on the ground of the screen
      lsnow.y = 0                    // then we show it again at the top
      lsnow.x = RND(screen_width)
    ENDIF

  NEXT

ENDFUNCTION // UPDATE_SNOW

Kitty Hello


Brice Manuel

I agree, very nice.  It is 98F here today, I really wish this was real :D