snowfall.bmx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ' Snowfall by simonh ([email protected])
  2. Strict
  3. Global width=800
  4. Global height=600
  5. Graphics width,height,16
  6. ' Load snowflake image
  7. Global flakei:TImage=LoadImage("flake.png",MIPMAPPEDIMAGE)
  8. ' Set no. of snowflakes to be created
  9. Global no_flakes=1000
  10. ' Create a snowflake type
  11. Type flake
  12. Field x#,y#,size#,speed#,sway#,phase
  13. End Type
  14. ' Create snowflake list
  15. Global flake_list:TList=New TList
  16. ' Initialise snowflakes
  17. For Local i=1 To no_flakes
  18. Local fl:flake=New flake
  19. flake_list.AddLast fl
  20. fl.size=Rnd!(0.01,0.1)
  21. fl.speed=Rnd!(1,2)
  22. fl.sway=Rnd!(1,2)
  23. fl.phase=Rand(45)
  24. fl.x=Rand(-10,width+10)
  25. fl.y=Rand(height)-height-10
  26. Next
  27. ' Main loop
  28. While Not KeyHit(KEY_ESCAPE)
  29. ' Begin loop in which we will update values of all snowflakes and draw them
  30. For Local wind=1 To 360 Step 5
  31. ' Iterate through our snowflake list
  32. For Local fl:flake=EachIn flake_list
  33. ' Just update the snowflake position values to try and make them move convincingly!
  34. fl.y=fl.y+fl.speed
  35. fl.x#=fl.x#+(Sin(wind+(fl.phase)))*fl.sway
  36. ' If snowflake has not yet reached the bottom of screen...
  37. If fl.y<height+10
  38. ' ...then draw snowflake.
  39. SetBlend LIGHTBLEND
  40. SetScale fl.size,fl.size
  41. DrawImage flakei,fl.x,fl.y
  42. '...else if snowflake has reached bottom of screen...
  43. Else
  44. '...reset snowflake values so it appears as new snowflake at top of screen.
  45. fl.speed=Rnd!(1,2)
  46. fl.sway=Rnd!(1,2)
  47. fl.phase=Rand(45)
  48. fl.x=Rand(-10,width+10)
  49. fl.y=-10
  50. EndIf
  51. Next
  52. Flip
  53. Cls
  54. Next
  55. Wend