Author Topic: Snow  (Read 5138 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Snow
« on: August 12, 2019, 02:27:16 pm »
I made this funky snowing program today that when they land on the hill, they slide down the hill a bit. lol I tried it with a ton of DIM commands, like the Starfields, but it used too much memory I think and slowed down after a few seconds. That's when I had the snow pile up on the mountain. This time there's no DIM commands and nothing piles up. Just something goofy. lol

Code: QB64: [Select]
  1.  
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. amount = 20
  4. s = .5
  5. GOSUB make:
  6.  
  7.  
  8.     FOR t = 1 TO amount
  9.         a$ = INKEY$
  10.         IF a$ = CHR$(27) THEN END
  11.         y = y + dy * s
  12.         IF y > _HEIGHT THEN GOSUB make:
  13.         IF POINT(x, y) = _RGB32(0, 255, 0) THEN
  14.             dy = 1
  15.             xx = x
  16.             yy = y
  17.             szz = sz
  18.             GOSUB make:
  19.         END IF
  20.         CIRCLE (x, y), sz, _RGB32(255, 255, 255)
  21.         IF sz < 1 THEN GOTO nopaint:
  22.         PAINT (x, y), _RGB32(255, 255, 255)
  23.         nopaint:
  24.     NEXT t
  25.     LINE (0, 0)-(800, 600), _RGB32(0, 0, 0, 30), BF
  26.     _DISPLAY
  27.  
  28.     'Hill
  29.     CIRCLE (400, 800), 600, _RGB32(0, 255, 0)
  30.     PAINT (400, 590), _RGB32(0, 255, 0)
  31.     FOR ttt = 1 TO 2000
  32.         _LIMIT 20000
  33.         RANDOMIZE TIMER
  34.         l = (RND * .5) + -.25
  35.         xx = xx + l
  36.         RANDOMIZE TIMER
  37.         l2 = (RND * .05)
  38.         yy = yy + l2
  39.         CIRCLE (xx, yy), szz, _RGB32(255, 255, 255)
  40.         IF szz < 1 THEN GOTO nopaint2:
  41.         PAINT (xx, yy), _RGB32(255, 255, 255)
  42.         nopaint2:
  43.     NEXT ttt
  44.  
  45. make:
  46. dy = INT(RND * 10) + 4
  47. x = (RND * 800)
  48. sz = (RND * 2)
  49. y = 0
  50.  
  51.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Snow
« Reply #1 on: August 12, 2019, 04:26:01 pm »
Yeah, nice snow worms!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Snow
« Reply #2 on: August 12, 2019, 05:08:03 pm »
My snow:

Code: QB64: [Select]
  1. TYPE Snow
  2.     X AS LONG
  3.     Y AS LONG
  4.     dx AS SINGLE
  5.     dy AS SINGLE
  6.  
  7. DIM SHARED S(1 TO 2000) AS Snow
  8.  
  9. SCREEN _NEWIMAGE(800, 600, 32)
  10. SaveScreen = _NEWIMAGE(800, 600, 32)
  11. 'creating one image with snow (is more fastest as if is every one draw to screen)
  12. S& = _NEWIMAGE(50, 50, 32)
  13. This = _DEST
  14. CIRCLE (25, 25), 5, &HFFFFFFFF
  15. PAINT (25, 25), &HFFFFFFFF, &HFFFFFFFF
  16. _DEST This
  17. _CLEARCOLOR &HFF000000, S& 'clear black color in image with snow
  18. CLS 'set alpha on my screen to 255
  19. 'create hill on my screen:
  20. CIRCLE (400, 900), 500
  21. PAINT (400, 500), &HFF0FFFFF, &HFFFFFFFF
  22.  
  23.  
  24. 'load shifts to array
  25. FOR sh = 1 TO UBOUND(s)
  26.     S(sh).dx = 1 + RND * 3
  27.     S(sh).dy = 1 + RND * 10
  28.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  29.     S(sh).X = m * RND * 800
  30.     S(sh).Y = RND * 40
  31. NEXT sh
  32.  
  33.  
  34. 'save current screen
  35. _PUTIMAGE , This, SaveScreen
  36. 'my loop
  37.     _PUTIMAGE , SaveScreen, This
  38.     i& = 0
  39.     iend& = UBOUND(s)
  40.     DO UNTIL i& = iend&
  41.         i& = i& + 1
  42.         S(i&).X = S(i&).X + S(i&).dx
  43.         S(i&).Y = S(i&).Y + S(i&).dy
  44.         IF S(i&).Y > 600 THEN Restart i& 'is snow am bottom? Return it back.
  45.  
  46.         'hill colission detection:
  47.         xy& = ((S(i&).X + 25 - 400) ^ 2) + ((S(i&).Y + 25 - 900) ^ 2) 'Pythagorean theorem, snow patircle - hill center
  48.         _PUTIMAGE (S(i&).X, S(i&).Y), S& 'place snow particle
  49.         IF 500 ^ 2 >= xy& THEN
  50.             _PUTIMAGE (S(i&).X, S(i&).Y), S&, SaveScreen 'place snow particle to virtual screen for restoring image
  51.             Restart i&
  52.         END IF
  53.     LOOP
  54.     _DISPLAY
  55.     _LIMIT 20
  56.  
  57. SUB Restart (I AS LONG)
  58.     S(I).dx = 1 + RND * 3
  59.     S(I).dy = 1 + RND * 10
  60.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  61.     S(I).X = m * RND * 800
  62.     S(I).Y = RND * 40
  63.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Snow
« Reply #3 on: August 12, 2019, 05:53:20 pm »
Pretty cool Petr!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Snow
« Reply #4 on: August 12, 2019, 09:18:54 pm »
I couldn't resist:
Code: QB64: [Select]
  1. _TITLE "B+ NE Ohio Snow: you have got to keep an eye on the level of snow fall!"
  2. 'B+ 2019-08-12 ha! I started with Petr's and changed just about everything!
  3. TYPE Snow
  4.     X AS LONG
  5.     Y AS LONG
  6.     dx AS SINGLE
  7.     dy AS SINGLE
  8. nflakes = 1000
  9. DIM SHARED S(nflakes) AS Snow
  10. SCREEN _NEWIMAGE(800, 600, 32)
  11. _SCREENMOVE 300, 40
  12. FOR i = 0 TO nflakes
  13.     IF RND < .5 THEN r = -1 ELSE r = 1
  14.     S(i).dx = r * (1 + RND * 3)
  15.     S(i).dy = 1 + RND * 10
  16.     S(i).X = RND * _WIDTH
  17.     S(i).Y = RND * _HEIGHT * .9
  18. SCREEN _NEWIMAGE(800, 600, 32)
  19. horizon = _HEIGHT * .9
  20.     CLS
  21.     FOR i = 0 TO _HEIGHT
  22.         LINE (0, i)-STEP(_WIDTH, 0), _RGB32(i / _HEIGHT * 50, 0, i / _HEIGHT * 120)
  23.     NEXT
  24.     d = _HEIGHT - horizon
  25.     c = 1
  26.     FOR i = horizon TO _HEIGHT
  27.         c = c + 1
  28.         LINE (0, i)-(_WIDTH, i), _RGB32(128 + c / d * 128, 128 + c / d * 128, 128 + c / d * 128), BF
  29.     NEXT
  30.     horizon = horizon - .3
  31.     FOR i = 0 TO nflakes
  32.         CIRCLE (S(i).X, S(i).Y), 1
  33.         S(i).X = S(i).X + S(i).dx
  34.         S(i).Y = S(i).Y + S(i).dy
  35.         IF S(i).X < -25 OR S(i).X > _WIDTH + 25 THEN newFlake i
  36.         IF S(i).Y > _HEIGHT THEN newFlake i
  37.     NEXT
  38.     _DISPLAY
  39.     _LIMIT 10
  40.  
  41. SUB newFlake (i)
  42.     IF RND < .5 THEN r = -1 ELSE r = 1
  43.     S(i).dx = r * (1 + RND * 3)
  44.     S(i).dy = 1 + RND * 10
  45.     S(i).X = RND * (_width + 100) - 50
  46.     S(i).Y = -100 * RND
  47.  
« Last Edit: August 12, 2019, 09:25:22 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Snow
« Reply #5 on: August 12, 2019, 10:14:00 pm »
LOL good job B+. I waited until the snow engulfed my entire screen. LOL

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Snow
« Reply #6 on: August 12, 2019, 10:26:00 pm »
Bplus,

Couldn't resist either...

Modified your version with fewer flakes and different sizes...

Make me cold just looking at it... Brrr...

J

Code: QB64: [Select]
  1. _TITLE "B+ NE Ohio Snow: you have got to keep an eye on the level of snow fall!"
  2. 'B+ 2019-08-12 ha! I started with Petr's and changed just about everything!
  3. TYPE Snow
  4.     X AS LONG
  5.     Y AS LONG
  6.     dx AS SINGLE
  7.     dy AS SINGLE
  8.     size AS SINGLE
  9. nflakes = 500
  10. DIM SHARED S(nflakes) AS Snow
  11. SCREEN _NEWIMAGE(800, 600, 32)
  12. _SCREENMOVE 300, 40
  13. FOR i = 0 TO nflakes
  14.     IF RND < .5 THEN r = -1 ELSE r = 1
  15.     S(i).dx = r * (1 + RND * 3)
  16.     S(i).dy = 1 + RND * 10
  17.     S(i).X = RND * _WIDTH
  18.     S(i).Y = RND * _HEIGHT * .9
  19.     S(i).size = RND * 3
  20. SCREEN _NEWIMAGE(800, 600, 32)
  21. horizon = _HEIGHT * .9
  22.     CLS
  23.     FOR i = 0 TO _HEIGHT
  24.         LINE (0, i)-STEP(_WIDTH, 0), _RGB32(i / _HEIGHT * 50, 0, i / _HEIGHT * 120)
  25.     NEXT
  26.     d = _HEIGHT - horizon
  27.     c = 1
  28.     FOR i = horizon TO _HEIGHT
  29.         c = c + 1
  30.         LINE (0, i)-(_WIDTH, i), _RGB32(128 + c / d * 128, 128 + c / d * 128, 128 + c / d * 128), BF
  31.     NEXT
  32.     horizon = horizon - .3
  33.     FOR i = 0 TO nflakes
  34.         IF S(i).size > 1 THEN
  35.             CIRCLE (S(i).X, S(i).Y), S(i).size
  36.             PAINT (S(i).X, S(i).Y)
  37.         ELSE
  38.             CIRCLE (S(i).X, S(i).Y), S(i).size
  39.         END IF
  40.         S(i).X = S(i).X + S(i).dx
  41.         S(i).Y = S(i).Y + S(i).dy
  42.         IF S(i).X < -25 OR S(i).X > _WIDTH + 25 THEN newFlake i
  43.         IF S(i).Y > _HEIGHT THEN newFlake i
  44.     NEXT
  45.     _DISPLAY
  46.     _LIMIT 10
  47.  
  48. SUB newFlake (i)
  49.     IF RND < .5 THEN r = -1 ELSE r = 1
  50.     S(i).dx = r * (1 + RND * 3)
  51.     S(i).dy = 1 + RND * 10
  52.     S(i).X = RND * (_WIDTH + 100) - 50
  53.     S(i).Y = -100 * RND
  54.     S(i).size = RND * 3
  55.  
  56.  
  57.  
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Snow
« Reply #7 on: August 12, 2019, 10:37:35 pm »
Nice one Johnno!


Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Snow
« Reply #8 on: August 13, 2019, 04:02:25 am »
Small upgrade, inspired by BPlus:

Code: QB64: [Select]
  1. TYPE Snow
  2.     X AS LONG
  3.     Y AS LONG
  4.     dx AS SINGLE
  5.     dy AS SINGLE
  6.     oy AS LONG
  7.  
  8. DIM SHARED S(1 TO 2000) AS Snow
  9.  
  10. SCREEN _NEWIMAGE(800, 600, 32)
  11. SaveScreen = _NEWIMAGE(800, 600, 32)
  12. 'creating one image with snow (is more fastest as if is every one draw to screen)
  13. S& = _NEWIMAGE(50, 50, 32)
  14. This = _DEST
  15. CIRCLE (25, 25), 5, &HFFFFFFFF
  16. PAINT (25, 25), &HFFFFFFFF, &HFFFFFFFF
  17. _DEST This
  18. _CLEARCOLOR &HFF000000, S& 'clear black color in image with snow
  19. CLS 'set alpha on my screen to 255
  20. 'create hill on my screen:
  21. CIRCLE (400, 900), 500
  22. PAINT (400, 500), &HFF0FFFFF, &HFFFFFFFF
  23.  
  24.  
  25. 'load shifts to array
  26. FOR sh = 1 TO UBOUND(s)
  27.     S(sh).dx = 1 + RND * 3
  28.     S(sh).dy = 1 + RND * 10
  29.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  30.     S(sh).X = m * RND * 800
  31.     S(sh).Y = RND * 40
  32. NEXT sh
  33.  
  34.  
  35. 'save current screen
  36. _PUTIMAGE , This, SaveScreen
  37. 'my loop
  38.     _PUTIMAGE , SaveScreen, This
  39.     i& = 0
  40.     iend& = UBOUND(s)
  41.     DO UNTIL i& = iend&
  42.         i& = i& + 1
  43.         S(i&).X = S(i&).X + S(i&).dx
  44.         S(i&).Y = S(i&).Y + S(i&).dy + S(i&).oy
  45.         IF S(i&).Y > 600 THEN Restart i& 'is snow am bottom? Return it back.
  46.  
  47.         'hill colission detection:
  48.         xy& = ((S(i&).X + 25 - 400) ^ 2) + ((S(i&).Y + 25 + S(i&).oy - 900) ^ 2) 'Pythagorean theorem, snow patircle - hill center
  49.         _PUTIMAGE (S(i&).X, S(i&).Y), S& 'place snow particle
  50.         IF 500 ^ 2 >= xy& THEN
  51.             _PUTIMAGE (S(i&).X, S(i&).Y), S&, SaveScreen 'place snow particle to virtual screen for restoring image
  52.             S(i&).oy = S(i&).oy + 2
  53.             Restart i&
  54.         END IF
  55.     LOOP
  56.     _DISPLAY
  57.     _LIMIT 20
  58.  
  59. SUB Restart (I AS LONG)
  60.     S(I).dx = 1 + RND * 3
  61.     S(I).dy = 1 + RND * 10
  62.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  63.     S(I).X = m * RND * 800
  64.     S(I).Y = RND * 40
  65.  

:)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Snow
« Reply #9 on: August 13, 2019, 10:56:51 am »
Small upgrade inspired by all!

Code: QB64: [Select]
  1. _TITLE "Snow by Ken, Petr, Bplus and Johnno "
  2. TYPE Snow
  3.     X AS LONG
  4.     Y AS LONG
  5.     dx AS SINGLE
  6.     dy AS SINGLE
  7.     sz AS INTEGER
  8.     oy AS LONG
  9.  
  10. DIM SHARED S(1 TO 2000) AS Snow
  11.  
  12. SCREEN _NEWIMAGE(800, 720, 32)
  13. _SCREENMOVE 300, 10
  14.  
  15. 'create hill on my screen:
  16. FOR i = 0 TO _HEIGHT
  17.     LINE (0, i)-STEP(_WIDTH, 0), _RGBA32(i / _HEIGHT * 120 + 40, 0, i / _HEIGHT * 180 + 40, 180)
  18. fcirc 400, _HEIGHT + 300, 500, &HFFBBCCDD
  19. SaveScreen = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
  20. 'save current screen
  21. _PUTIMAGE , 0, SaveScreen
  22.  
  23. 'load shifts to array
  24. FOR sh = 1 TO UBOUND(s)
  25.     S(sh).dx = 1 + RND * 3
  26.     S(sh).dy = 1 + RND * 10
  27.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  28.     S(sh).X = m * RND * 800
  29.     S(sh).Y = RND * _HEIGHT
  30.     IF sh < 1500 THEN
  31.         S(sh).sz = 1
  32.     ELSEIF sh < 1750 THEN
  33.         S(sh).sz = 2
  34.     ELSEIF sh < 1875 THEN
  35.         S(sh).sz = 3
  36.     ELSEIF sh < 1970 THEN
  37.         S(sh).sz = 4
  38.     ELSE
  39.         S(sh).sz = 5
  40.     END IF
  41.     xy& = ((S(sh).X - 400) ^ 2) + ((S(sh).Y + S(sh).oy - (_HEIGHT + 300)) ^ 2) 'Pythagorean theorem, snow patircle - hill center
  42.     IF 500 ^ 2 >= xy& THEN
  43.         Restart sh
  44.     END IF
  45. NEXT sh
  46.  
  47. 'my loop
  48.     _PUTIMAGE , SaveScreen, 0
  49.     i& = 0
  50.     iend& = UBOUND(s)
  51.     DO UNTIL i& = iend&
  52.         i& = i& + 1
  53.         S(i&).X = S(i&).X + S(i&).dx
  54.         S(i&).Y = S(i&).Y + S(i&).dy + S(i&).oy
  55.         IF S(i&).Y > _HEIGHT THEN Restart i& 'is snow am bottom? Return it back.
  56.  
  57.         'hill colission detection:
  58.         xy& = ((S(i&).X - 400) ^ 2) + ((S(i&).Y + S(i&).oy - (_HEIGHT + 300)) ^ 2) 'Pythagorean theorem, snow patircle - hill center
  59.         fcirc S(i&).X, S(i&).Y, S(i&).sz, &HFFFFFFFF
  60.         IF S(i&).sz > 1 THEN PAINT (S(i&).X, S(i&).Y)
  61.         IF 500 ^ 2 >= xy& THEN
  62.             _DEST SaveScreen
  63.             fcirc S(i&).X, S(i&).Y, S(i&).sz, &HFFFFFFFF
  64.             _DEST 0
  65.             S(i&).oy = S(i&).oy + 2
  66.             Restart i&
  67.         END IF
  68.     LOOP
  69.     _DISPLAY
  70.     _LIMIT 20
  71.  
  72. SUB Restart (I AS LONG)
  73.     S(I).dx = 1 + RND * 3
  74.     S(I).dy = 1 + RND * 10
  75.     IF RND * 5 > 2 THEN m = -1 ELSE m = 1
  76.     S(I).X = m * RND * 800
  77.     S(I).Y = -RND * 100
  78.  
  79. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  80.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  81.     DIM X AS INTEGER, Y AS INTEGER
  82.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  83.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  84.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  85.     WHILE X > Y
  86.         RadiusError = RadiusError + Y * 2 + 1
  87.         IF RadiusError >= 0 THEN
  88.             IF X <> Y + 1 THEN
  89.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  90.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  91.             END IF
  92.             X = X - 1
  93.             RadiusError = RadiusError - X * 2
  94.         END IF
  95.         Y = Y + 1
  96.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  97.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  98.     WEND
  99.  

« Last Edit: August 13, 2019, 10:58:55 am by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Snow
« Reply #10 on: August 13, 2019, 11:29:08 am »
Nice improvement... I only have one question. Where did you find the image of the top of my head? lol
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Snow
« Reply #11 on: August 13, 2019, 12:39:16 pm »
That's awesome B+! I will have to study it soon to see how it doesn't slow down.

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Snow
« Reply #12 on: August 16, 2019, 10:52:48 am »
Very cool animation! I love it. So all you guys collaborated to make this? Super. I love it. Team players on the forum. forum made programs. What a great idea.
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Snow
« Reply #13 on: August 16, 2019, 12:50:48 pm »
Very cool animation! I love it. So all you guys collaborated to make this? Super. I love it. Team players on the forum. forum made programs. What a great idea.

+1