Author Topic: Fireflies  (Read 7093 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Fireflies
« on: August 31, 2019, 01:07:22 am »
These have probably been done before here at some time maybe. But I thought I would take a shot at it myself tonight using the trail code that B+ uses. It's 4 fireplaces dancing in the air around each other. I use part of my Fun With Math program to make them go in different directions. I just wish I could remember my Trigonometry! LOL It's non-stop so press Esc or click X to end. Thanks again B+!

(Note: There is no _LIMIT command on this one, so please scroll down to my other one, thanks.)

Code: QB64: [Select]
  1. 'Thanks to Bplus on the QB64.org forum for the trail code.
  2. 'Made on Aug. 30, 2019 by Ken G.
  3. _TITLE "Fireflies - Press Esc to End"
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5. x = 400
  6. y = 300
  7. go:
  8. c = (RND * 170) + 10
  9. c2 = (RND * 170) + 10
  10. c3 = (RND * 170) + 10
  11. c4 = (RND * 170) + 10
  12. c5 = (RND * 170) + 10
  13. c6 = (RND * 170) + 10
  14. c7 = (RND * 170) + 10
  15. c8 = (RND * 170) + 10
  16.  
  17.     IF INKEY$ = CHR$(27) THEN END
  18.     seconds = seconds + .1
  19.     s = (60 - seconds) * 6 + 180
  20.     x = INT(SIN(s / c * 3.141592) * c) + 400
  21.     y = INT(COS(s / c2 * 3.141592) * c2) + 300
  22.     x2 = INT(SIN(s / c3 * 3.141592) * c3) + 400
  23.     y2 = INT(COS(s / c4 * 3.141592) * c4) + 300
  24.  
  25.     x3 = INT(SIN(s / c5 * 3.141592) * c5) + 400
  26.     y3 = INT(COS(s / c6 * 3.141592) * c6) + 300
  27.     x4 = INT(SIN(s / c7 * 3.141592) * c7) + 400
  28.     y4 = INT(COS(s / c8 * 3.141592) * c8) + 300
  29.  
  30.     CIRCLE (x, y), 10, _RGB32(127, 216, 16)
  31.     PAINT (x, y), _RGB32(127, 216, 16)
  32.     CIRCLE (x2, y2), 10, _RGB32(255, 0, 0)
  33.     PAINT (x2, y2), _RGB32(255, 0, 0)
  34.     CIRCLE (x3, y3), 10, _RGB32(128, 238, 255)
  35.     PAINT (x3, y3), _RGB32(128, 238, 255)
  36.     CIRCLE (x4, y4), 10, _RGB32(183, 94, 255)
  37.     PAINT (x4, y4), _RGB32(183, 94, 255)
  38.  
  39.     IF seconds > 1000 THEN
  40.         seconds = 0
  41.         GOTO go:
  42.     END IF
  43.     _DELAY .001
  44.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA(0, 0, 0, 30), BF
  45.     _DISPLAY
  46.  
« Last Edit: August 31, 2019, 02:01:06 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Fireflies
« Reply #1 on: August 31, 2019, 02:05:33 am »
Fireflies. Here, in Australia, we have Fireflies towards northern Australia. Here, down in the south, we improvise. We catch them; A brief dip in some petrol; light them up as we let them go. They don't glow for as long as the 'flies' in your program... lol

Nah... We're not THAT cruel... We don't waste petrol on flies!
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #2 on: August 31, 2019, 10:25:46 am »
Flys on File (< fingers again ha! >) Flies on Fire!

Code: QB64: [Select]
  1. _TITLE "Flys on Fire" 'B+ 2019-08-31
  2. 'big mod of Great Balls Of Fire by bplus, 2017-11-24"
  3. 'Great balls of fire.bas SmallBASIC 0.12.9 (B+=MGA) 2017-11-23
  4.  
  5. CONST xmax = 600
  6. CONST ymax = 600
  7.  
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. _SCREENMOVE 360, 60 'adjust as needed _MIDDLE needs a delay .5 or more for me
  10.  
  11. TYPE bug
  12.     x AS SINGLE
  13.     y AS SINGLE
  14.     rr AS SINGLE 'random coeff = radius of flight path
  15.     m AS SINGLE
  16.     n AS SINGLE
  17.     r AS SINGLE
  18. DEFSNG A-Z
  19. DIM SHARED xxmax, yymax, xstep, ystep, acc
  20. xxmax = 300: yymax = 300 'pixels too slow
  21. cxy = 140
  22. xstep = xmax / xxmax: ystep = ymax / yymax
  23.  
  24. DIM SHARED p&(300) 'pallette thanks harixxx
  25. FOR i = 1 TO 100
  26.     fr = 240 * i / 100 + 15
  27.     p&(i) = _RGB(fr, 0, 0)
  28.     p&(i + 100) = _RGB(255, fr, 0)
  29.     p&(i + 200) = _RGB(255, 255, fr)
  30.  
  31. DIM SHARED f(xxmax, yymax) 'fire array tracks flames
  32.  
  33. bg& = _NEWIMAGE(xmax, ymax, 32)
  34. drawLandscape
  35. _PUTIMAGE , 0, bg&
  36.  
  37.  
  38. nb = 11 '              number of bugs
  39. acc = .55 '           gravity
  40. br = 15 '             bug radius
  41. brs = br * br '       bug radius squared
  42. DIM SHARED b(1 TO nb) AS bug
  43. FOR i = 1 TO nb 'bug maker
  44.     b(i).r = RND * 3 + 1
  45.     b(i).rr = RND * 110 + 40
  46.     b(i).m = RND * 25
  47.     b(i).n = RND * 25
  48.     ra = RND * _PI(2)
  49.     b(i).x = cxy + b(i).rr * (COS(b(i).n * ra) / 2 + SIN(b(i).m * ra) / 3)
  50.     b(i).y = cxy + b(i).rr * (SIN(b(i).n * ra) / 2 + COS(b(i).m * ra) / 3)
  51.  
  52. WHILE 1 'main show
  53.  
  54.     _PUTIMAGE , bg&, 0
  55.     FOR y = 1 TO yymax - 2 'fire based literally on 4 pixels below it like cellular automata
  56.         FOR x = 1 TO xxmax - 1
  57.             v = (f(x - 1, y + 1) + f(x, y + 1) + f(x + 1, y + 1) + f(x, y + 2)) / 4 - 5
  58.             IF v > 0 AND RND < .9 THEN f(x, y) = v ELSE f(x, y) = 0
  59.             IF v > 294 THEN f(x, y) = 300
  60.             'glow worms effect
  61.             'f(x, y) = max((f(x - 1, y + 1) + f(x, y + 1) + f(x + 1, y + 1) + f(x, y + 2)) / 4 - 15, 0)
  62.             LINE (x * xstep, y * ystep)-STEP(xstep, ystep), p&(f(x, y)), BF
  63.         NEXT
  64.     NEXT
  65.     a = a + .01
  66.     FOR i = 1 TO nb 'move bug
  67.         'new location unless out of bounds
  68.         b(i).x = cxy + b(i).rr * (COS(b(i).n * a) / 2 + SIN(b(i).m * a) / 3)
  69.         b(i).y = cxy + b(i).rr * (SIN(b(i).n * a) / 2 + COS(b(i).m * a) / 3)
  70.  
  71.  
  72.         'handle new location
  73.         firebug i
  74.  
  75.     NEXT
  76.     _LIMIT 30
  77.     _DISPLAY
  78.  
  79.  
  80. SUB firebug (i)
  81.     x = b(i).x: y = b(i).y: brs = b(i).r ^ 2
  82.     FOR xr = 0 TO b(i).r
  83.         yrMax = (brs - xr * xr) ^ .5
  84.         FOR yr = 0 TO yrMax
  85.             IF x + xr < xxmax - 1 AND y + yr <= yymax - 1 THEN f(x + xr, y + yr) = 300
  86.             IF x + xr < xxmax - 1 AND y - yr >= 0 THEN f(x + xr, y - yr) = 300
  87.             IF x - xr >= 0 AND y + yr <= yymax THEN f(x - xr, y + yr) = 300
  88.             IF x - xr >= 0 AND y - yr >= 0 THEN f(x - xr, y - yr) = 300
  89.         NEXT
  90.     NEXT
  91.  
  92. SUB drawLandscape
  93.     'needs midInk, irnd
  94.  
  95.     DIM i AS INTEGER, startH AS SINGLE, rr AS INTEGER, gg AS INTEGER, bb AS INTEGER
  96.     DIM mountain AS INTEGER, Xright AS SINGLE, y AS SINGLE, upDown AS SINGLE, range AS SINGLE
  97.     DIM lastx AS SINGLE, X AS INTEGER
  98.     'the sky
  99.     FOR i = 0 TO ymax
  100.         midInk 0, 0, 48, 28, 0, 88, i / ymax
  101.         LINE (0, i)-(xmax, i)
  102.     NEXT
  103.     'the land
  104.     startH = ymax - 400
  105.     rr = 40: gg = 50: bb = 60
  106.     FOR mountain = 1 TO 6
  107.         Xright = 0
  108.         y = startH
  109.         WHILE Xright < xmax
  110.             ' upDown = local up / down over range, change along Y
  111.             ' range = how far up / down, along X
  112.             upDown = (RND * .8 - .35) * (mountain * .5)
  113.             range = Xright + irnd%(15, 25) * 2.5 / mountain
  114.             lastx = Xright - 1
  115.             FOR X = Xright TO range
  116.                 y = y + upDown
  117.                 COLOR _RGB(rr, gg, bb)
  118.                 LINE (lastx, y)-(X, ymax), , BF 'just lines weren't filling right
  119.                 lastx = X
  120.             NEXT
  121.             Xright = range
  122.         WEND
  123.         rr = irnd%(rr - 15, rr): gg = irnd%(gg - 15, gg): bb = irnd%(bb - 25, bb)
  124.         IF rr < 0 THEN rr = 0
  125.         IF gg < 0 THEN gg = 0
  126.         IF bb < 0 THEN bb = 0
  127.         startH = startH + irnd%(5, 20)
  128.     NEXT
  129.  
  130. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  131.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  132.  
  133. FUNCTION irnd% (n1, n2) 'return an integer between 2 numbers
  134.     DIM l%, h%
  135.     IF n1 > n2 THEN l% = n2: h% = n1 ELSE l% = n1: h% = n2
  136.     irnd% = INT(RND * (h% - l% + 1)) + l%
  137.  
  138.  
« Last Edit: August 31, 2019, 02:56:46 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fireflies
« Reply #3 on: August 31, 2019, 02:00:19 pm »
Awesome stuff B+..

I forgot to add a _LIMIT command to mind. So I also just doubled the amount of flies to 8 and made them half the size. I also made it so the intensity of the colors fade in and out slightly from a dull to a bit brighter white. I wish I knew how to make a REAL bright white or if there's some type of corona code we could use. But anyway, this is what I have:

Code: QB64: [Select]
  1. 'Thanks to Bplus on the QB64.org forum for the trail code.
  2. 'Made on Aug. 30, 2019 by Ken G.
  3. _TITLE "Fireflies - Press Esc to End"
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5.  
  6. go:
  7. c = (RND * 170) + 10
  8. c2 = (RND * 170) + 10
  9. c3 = (RND * 170) + 10
  10. c4 = (RND * 170) + 10
  11. c5 = (RND * 170) + 10
  12. c6 = (RND * 170) + 10
  13. c7 = (RND * 170) + 10
  14. c8 = (RND * 170) + 10
  15. c9 = (RND * 170) + 10
  16. c10 = (RND * 170) + 10
  17. c11 = (RND * 170) + 10
  18. c12 = (RND * 170) + 10
  19. c13 = (RND * 170) + 10
  20. c14 = (RND * 170) + 10
  21. c15 = (RND * 170) + 10
  22. c16 = (RND * 170) + 10
  23.  
  24.  
  25.  
  26.     _LIMIT 100
  27.     IF INKEY$ = CHR$(27) THEN END
  28.     seconds = seconds + .1
  29.     s = (60 - seconds) * 6 + 180
  30.     x = INT(SIN(s / c * 3.141592) * c) + 400
  31.     y = INT(COS(s / c2 * 3.141592) * c2) + 300
  32.     x2 = INT(SIN(s / c3 * 3.141592) * c3) + 400
  33.     y2 = INT(COS(s / c4 * 3.141592) * c4) + 300
  34.     x3 = INT(SIN(s / c5 * 3.141592) * c5) + 400
  35.     y3 = INT(COS(s / c6 * 3.141592) * c6) + 300
  36.     x4 = INT(SIN(s / c7 * 3.141592) * c7) + 400
  37.     y4 = INT(COS(s / c8 * 3.141592) * c8) + 300
  38.     x5 = INT(SIN(s / c9 * 3.141592) * c9) + 400
  39.     y5 = INT(COS(s / c10 * 3.141592) * c10) + 300
  40.     x6 = INT(SIN(s / c11 * 3.141592) * c11) + 400
  41.     y6 = INT(COS(s / c12 * 3.141592) * c12) + 300
  42.     x7 = INT(SIN(s / c13 * 3.141592) * c13) + 400
  43.     y7 = INT(COS(s / c14 * 3.141592) * c14) + 300
  44.     x8 = INT(SIN(s / c15 * 3.141592) * c15) + 400
  45.     y8 = INT(COS(s / c16 * 3.141592) * c16) + 300
  46.     t = t + 1
  47.     IF t > 240 THEN t = 1
  48.     IF t < 120 THEN tt = 1
  49.     IF t >= 120 THEN tt = -1
  50.     c = c + tt
  51.     IF c > 255 THEN c = 255
  52.     IF c < 100 THEN c = 100
  53.  
  54.     CIRCLE (x, y), 5, _RGB32(c, 255, c)
  55.     PAINT (x, y), _RGB32(c, 255, c)
  56.     CIRCLE (x2, y2), 5, _RGB32(c, c, c)
  57.     PAINT (x2, y2), _RGB32(c, c, c)
  58.     CIRCLE (x3, y3), 5, _RGB32(c, c, 255)
  59.     PAINT (x3, y3), _RGB32(c, c, 255)
  60.     CIRCLE (x4, y4), 5, _RGB32(c, c, 255)
  61.     PAINT (x4, y4), _RGB32(c, c, 255)
  62.     CIRCLE (x5, y5), 5, _RGB32(c, 255, c)
  63.     PAINT (x5, y5), _RGB32(c, 255, c)
  64.     CIRCLE (x6, y6), 5, _RGB32(255, c, c)
  65.     PAINT (x6, y6), _RGB32(255, c, c)
  66.     CIRCLE (x7, y7), 5, _RGB32(c, c, 255)
  67.     PAINT (x7, y7), _RGB32(c, c, 255)
  68.     CIRCLE (x8, y8), 5, _RGB32(c, c, 255)
  69.     PAINT (x8, y8), _RGB32(c, c, 255)
  70.  
  71.     IF seconds > 300 THEN
  72.         seconds = 0
  73.         GOTO go:
  74.     END IF
  75.     _DELAY .001
  76.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA(0, 0, 0, 30), BF
  77.     _DISPLAY
  78.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #4 on: August 31, 2019, 03:49:04 pm »
Quote
I wish I knew how to make a REAL bright white or if there's some type of corona code we could use. But anyway, this is what I have:

Hey Ken, flies that glow!
Code: QB64: [Select]
  1. 'Thanks to Ken for inspiring mod fun!
  2. 'Thanks to Bplus on the QB64.org forum for the trail code.
  3. 'Made on Aug. 30, 2019 by Ken G. mod by B+
  4.  
  5. '        GLOBALS
  6. CONST glow = &H08FFFFFF, nFlies = 20
  7. TYPE flyType
  8.     cx AS SINGLE
  9.     cy AS SINGLE
  10.     r AS INTEGER
  11.     c AS _UNSIGNED LONG
  12.  
  13. '     LOCALS for main code which is all this is!
  14. DIM i, seconds, s, x, y
  15.  
  16. _TITLE "Fireflies that glow"
  17. SCREEN _NEWIMAGE(800, 600, 32)
  18.  
  19. 'setup flies
  20. DIM f(1 TO nFlies) AS flyType
  21. FOR i = 1 TO nFlies
  22.     f(i).cx = RND * 170 + 10
  23.     f(i).cy = RND * 170 + 10
  24.     f(i).r = INT(RND * 5) + 1
  25.     f(i).c = _RGB32(RND * 190 + 60, RND * 190 + 60, RND * 190 + 60)
  26.  
  27.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 50), BF ' trails a little less to show off glow
  28.     FOR i = 1 TO nFlies
  29.         seconds = seconds + .005 'slow down a tad
  30.         s = (60 - seconds) * 6 + 180 '???????????????? but it works!!
  31.         x = INT(SIN(s / f(i).cx * 3.141592) * 3 * f(i).cx) + 400 ' the * 3 and * 2 below spread flies over screen better
  32.         y = INT(COS(s / f(i).cy * 3.141592) * 2 * f(i).cy) + 300
  33.         fcirc x, y, f(i).r * 5, glow
  34.         fcirc x, y, f(i).r, f(i).c
  35.     NEXT
  36.     IF INKEY$ = CHR$(27) THEN END
  37.     _LIMIT 100
  38.     _DISPLAY
  39.  
  40. 'from Steve Gold standard
  41. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  42.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  43.     DIM X AS INTEGER, Y AS INTEGER
  44.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  45.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  46.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  47.     WHILE X > Y
  48.         RadiusError = RadiusError + Y * 2 + 1
  49.         IF RadiusError >= 0 THEN
  50.             IF X <> Y + 1 THEN
  51.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  52.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  53.             END IF
  54.             X = X - 1
  55.             RadiusError = RadiusError - X * 2
  56.         END IF
  57.         Y = Y + 1
  58.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  59.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  60.     WEND
  61.  
  62.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #5 on: August 31, 2019, 04:36:12 pm »
hmm... lost the trails but added background
Code: QB64: [Select]
  1. 'Thanks to Ken for inspiring mod fun!
  2. 'Thanks to Bplus on the QB64.org forum for the trail code.
  3. 'Made on Aug. 30, 2019 by Ken G. mod by B+
  4.  
  5. '        GLOBALS
  6. CONST glow = &H08FFFFFF, nFlies = 20, xmax = 800, ymax = 600
  7. TYPE flyType
  8.     cx AS SINGLE
  9.     cy AS SINGLE
  10.     r AS INTEGER
  11.     c AS _UNSIGNED LONG
  12.  
  13. '     LOCALS for main code which is all this is!
  14. DIM i, seconds, s, x, y, bg&
  15.  
  16. _TITLE "Fireflies that glow"
  17. SCREEN _NEWIMAGE(800, 600, 32)
  18. _SCREENMOVE 300, 60
  19.  
  20. bg& = _NEWIMAGE(xmax, ymax, 32)
  21. drawLandscape
  22. _PUTIMAGE , 0, bg&
  23.  
  24. 'setup flies
  25. DIM f(1 TO nFlies) AS flyType
  26. FOR i = 1 TO nFlies
  27.     f(i).cx = RND * 170 + 10
  28.     f(i).cy = RND * 170 + 10
  29.     f(i).r = INT(RND * 5) + 1
  30.     f(i).c = _RGB32(RND * 190 + 60, RND * 190 + 60, RND * 190 + 60)
  31.  
  32.     _PUTIMAGE , bg&, 0
  33.     FOR i = 1 TO nFlies
  34.         seconds = seconds + .005 'slow down a tad
  35.         s = (60 - seconds) * 6 + 180 '???????????????? but it works!!
  36.         x = INT(SIN(s / f(i).cx * 3.141592) * 3 * f(i).cx) + 400 ' the * 3 and * 2 below spread flies over screen better
  37.         y = INT(COS(s / f(i).cy * 3.141592) * 2 * f(i).cy) + 300
  38.         fcirc x, y, f(i).r * 5, glow
  39.         fcirc x, y, f(i).r, f(i).c
  40.     NEXT
  41.     IF INKEY$ = CHR$(27) THEN END
  42.     _LIMIT 100
  43.     _DISPLAY
  44.  
  45. 'from Steve Gold standard
  46. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  47.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  48.     DIM X AS INTEGER, Y AS INTEGER
  49.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  50.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  51.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  52.     WHILE X > Y
  53.         RadiusError = RadiusError + Y * 2 + 1
  54.         IF RadiusError >= 0 THEN
  55.             IF X <> Y + 1 THEN
  56.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  57.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  58.             END IF
  59.             X = X - 1
  60.             RadiusError = RadiusError - X * 2
  61.         END IF
  62.         Y = Y + 1
  63.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  64.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  65.     WEND
  66. SUB drawLandscape
  67.     'needs midInk, irnd
  68.  
  69.     DIM i AS INTEGER, startH AS SINGLE, rr AS INTEGER, gg AS INTEGER, bb AS INTEGER
  70.     DIM mountain AS INTEGER, Xright AS SINGLE, y AS SINGLE, upDown AS SINGLE, range AS SINGLE
  71.     DIM lastx AS SINGLE, X AS INTEGER
  72.     'the sky
  73.     FOR i = 0 TO ymax
  74.         midInk 0, 0, 48, 28, 0, 88, i / ymax
  75.         LINE (0, i)-(xmax, i)
  76.     NEXT
  77.     'the land
  78.     startH = ymax - 400
  79.     rr = 40: gg = 50: bb = 60
  80.     FOR mountain = 1 TO 6
  81.         Xright = 0
  82.         y = startH
  83.         WHILE Xright < xmax
  84.             ' upDown = local up / down over range, change along Y
  85.             ' range = how far up / down, along X
  86.             upDown = (RND * .8 - .35) * (mountain * .5)
  87.             range = Xright + irnd%(15, 25) * 2.5 / mountain
  88.             lastx = Xright - 1
  89.             FOR X = Xright TO range
  90.                 y = y + upDown
  91.                 COLOR _RGB(rr, gg, bb)
  92.                 LINE (lastx, y)-(X, ymax), , BF 'just lines weren't filling right
  93.                 lastx = X
  94.             NEXT
  95.             Xright = range
  96.         WEND
  97.         rr = irnd%(rr - 15, rr): gg = irnd%(gg - 15, gg): bb = irnd%(bb - 25, bb)
  98.         IF rr < 0 THEN rr = 0
  99.         IF gg < 0 THEN gg = 0
  100.         IF bb < 0 THEN bb = 0
  101.         startH = startH + irnd%(5, 20)
  102.     NEXT
  103.  
  104. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  105.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  106.  
  107. FUNCTION irnd% (n1, n2) 'return an integer between 2 numbers
  108.     DIM l%, h%
  109.     IF n1 > n2 THEN l% = n2: h% = n1 ELSE l% = n1: h% = n2
  110.     irnd% = INT(RND * (h% - l% + 1)) + l%
  111.  
  112.  

I wonder if there is a way to have both, sure there is! be back in a flash! Also I think the flies need different centers to fly around...
 
opps that failed, I thought I could have transparent background with fireflies that I could lay over the background image?? Ah, it doesn't stay transparent with the fade box!
« Last Edit: August 31, 2019, 05:06:14 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fireflies
« Reply #6 on: August 31, 2019, 06:04:06 pm »
Awesome glow look! But you don't have the tail code  LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA(0, 0, 0, 30), BF   
I tried it at the end of your main loop but no luck like you said, unless it can be put somewhere else?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #7 on: September 01, 2019, 09:21:56 pm »
Finally Halos, Trails and Background for these Alien Fireflies:

Yes, I found an application for cds Tools and have arrays in my TYPE Definition (sorta! ;-))
Sure it says STRING for x and y but really arrays of numbers.
Code: QB64: [Select]
  1. OPTION _EXPLICIT ' B+ 2019-09-01
  2. 'Thanks to Ken for inspiring mod fun!
  3. 'Thanks to Bplus on the QB64.org forum for the trail code.
  4. 'Made on Aug. 30, 2019 by Ken G. mod by B+
  5. '2019-09-01 mod of Fireflies #2 taking fly paths from Bugs on fire and adding Frames array
  6. ' for layering partially faded screens to get trails effect WITH a background display!
  7. ' 2 experiments have failed to produce desired trails do to my erroneous way of thinking.
  8. ' Time to pull out the heavy guns, cds Tools, and test an array like string of x, y locations.
  9. ' This might be a little complicated! We will be fading out old locations with transparencies
  10. ' rather than drawing over and over again with smoke layers.
  11.  
  12.  
  13. '        GLOBALS
  14. CONST nFlies = 25, xmax = 800, ymax = 600, cx = 400, cy = 300
  15.  
  16. TYPE flyType
  17.     x AS STRING 'a cds array
  18.     y AS STRING 'a cds array
  19.     a AS SINGLE
  20.     rr AS SINGLE 'random coeff = radius of flight path
  21.     m AS SINGLE
  22.     n AS SINGLE
  23.     r AS SINGLE
  24.     red AS INTEGER
  25.     green AS INTEGER
  26.     blue AS INTEGER
  27.  
  28. '     LOCALS for main code which is all this is!
  29. DIM i AS INTEGER, j AS INTEGER, frameI AS INTEGER, a, bg&, x, y, ma, ic AS INTEGER
  30.  
  31. _TITLE "Fireflies that glow, trails and background"
  32. SCREEN _NEWIMAGE(800, 600, 32)
  33. _SCREENMOVE 300, 60
  34.  
  35. 'prepare background image
  36. bg& = _NEWIMAGE(xmax, ymax, 32)
  37. drawLandscape
  38. _PUTIMAGE , 0, bg&
  39. 'setup trans
  40. DIM tr(1 TO 50) AS INTEGER
  41. FOR i = 1 TO 50
  42.     IF i <> 50 THEN tr(i) = INT(i / 2) + 1 ELSE tr(i) = 255
  43. 'setup flies
  44. DIM SHARED f(1 TO nFlies) AS flyType
  45. FOR i = 1 TO nFlies 'bug maker
  46.     f(i).r = RND * 3 + 1
  47.     f(i).rr = RND * 200 + 250
  48.     f(i).m = RND * 25
  49.     f(i).n = RND * 25
  50.     f(i).red = RND * 190 + 60
  51.     f(i).green = RND * 190 + 60
  52.     f(i).blue = RND * 190 + 60
  53.     f(i).a = RND * _PI(2)
  54. frameI = 0: a = 0
  55.     frameI = frameI + 1 'new image pointer for frame handler
  56.     IF frameI >= 51 THEN frameI = 1 ' frames to manage
  57.  
  58.     _PUTIMAGE , bg&, 0 'cls last screen shot
  59.  
  60.     a = a + .1
  61.     FOR i = 1 TO nFlies 'move bug
  62.         'mod a to radius of flight path  so they move approximately same rate
  63.         ma = a / f(i).rr
  64.         'calc new location
  65.         x = cx + f(i).rr * 2 * (COS(f(i).n * (ma + f(i).a)) / 2 + SIN(f(i).m * (ma + f(i).a)) / 3)
  66.         y = cy + f(i).rr * 1.5 * (SIN(f(i).n * (ma + f(i).a)) / 2 + COS(f(i).m * (ma + f(i).a)) / 3)
  67.  
  68.         'load new loacation to next frame
  69.         cdsI f(i).x, frameI, STR$(x)
  70.         cdsI f(i).y, frameI, STR$(y)
  71.  
  72.         'ok now draw all the 24 frames  starting at oldest and coming to newest
  73.         'with newest frame updated draw then all starting with oldest
  74.         ic = 0 'image counter
  75.         j = frameI 'start at oldest frame
  76.         again:
  77.         IF j >= 51 THEN j = 1
  78.         x = VAL(cdsI$(f(i).x, j)) 'extract x(j) in f().x string
  79.         y = VAL(cdsI$(f(i).y, j)) 'ectract y(j) in f().y string
  80.         ic = ic + 1
  81.         IF j <> 50 THEN 'glitched on top index have no idea why, do you?
  82.             fcirc x, y, f(i).r + 3, _RGBA32(255, 255, 255, .5 * tr(ic))
  83.             fcirc x, y, f(i).r, _RGBA32(f(i).red, f(i).green, f(i).blue, tr(ic))
  84.         END IF
  85.         j = j + 1
  86.         IF ic < 50 THEN GOTO again 'else done
  87.     NEXT
  88.     IF INKEY$ = CHR$(27) THEN END
  89.     _DISPLAY
  90.     _LIMIT 100
  91.  
  92. 'from Steve Gold standard
  93. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  94.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  95.     DIM X AS INTEGER, Y AS INTEGER
  96.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  97.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  98.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  99.     WHILE X > Y
  100.         RadiusError = RadiusError + Y * 2 + 1
  101.         IF RadiusError >= 0 THEN
  102.             IF X <> Y + 1 THEN
  103.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  104.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  105.             END IF
  106.             X = X - 1
  107.             RadiusError = RadiusError - X * 2
  108.         END IF
  109.         Y = Y + 1
  110.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  111.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  112.     WEND
  113.  
  114. SUB drawLandscape
  115.     'needs midInk, irnd
  116.  
  117.     DIM i AS INTEGER, startH AS SINGLE, rr AS INTEGER, gg AS INTEGER, bb AS INTEGER
  118.     DIM mountain AS INTEGER, Xright AS SINGLE, y AS SINGLE, upDown AS SINGLE, range AS SINGLE
  119.     DIM lastx AS SINGLE, X AS INTEGER
  120.     'the sky
  121.     FOR i = 0 TO ymax
  122.         midInk 0, 0, 48, 28, 0, 88, i / ymax
  123.         LINE (0, i)-(xmax, i)
  124.     NEXT
  125.     'the land
  126.     startH = ymax - 400
  127.     rr = 40: gg = 50: bb = 60
  128.     FOR mountain = 1 TO 6
  129.         Xright = 0
  130.         y = startH
  131.         WHILE Xright < xmax
  132.             ' upDown = local up / down over range, change along Y
  133.             ' range = how far up / down, along X
  134.             upDown = (RND * .8 - .35) * (mountain * .5)
  135.             range = Xright + irnd%(15, 25) * 2.5 / mountain
  136.             lastx = Xright - 1
  137.             FOR X = Xright TO range
  138.                 y = y + upDown
  139.                 COLOR _RGB(rr, gg, bb)
  140.                 LINE (lastx, y)-(X, ymax), , BF 'just lines weren't filling right
  141.                 lastx = X
  142.             NEXT
  143.             Xright = range
  144.         WEND
  145.         rr = irnd%(rr - 15, rr): gg = irnd%(gg - 15, gg): bb = irnd%(bb - 25, bb)
  146.         IF rr < 0 THEN rr = 0
  147.         IF gg < 0 THEN gg = 0
  148.         IF bb < 0 THEN bb = 0
  149.         startH = startH + irnd%(5, 20)
  150.     NEXT
  151.  
  152. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  153.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  154.  
  155. FUNCTION irnd% (n1, n2) 'return an integer between 2 numbers
  156.     DIM l%, h%
  157.     IF n1 > n2 THEN l% = n2: h% = n1 ELSE l% = n1: h% = n2
  158.     irnd% = INT(RND * (h% - l% + 1)) + l%
  159.  
  160.  
  161. ' ================================== cds Tools ( for Comma Delimited Strings ) 2019-08-29 B+
  162.  
  163. 'modified Item$ for dedicated comma delimited strings
  164. 'cds$ is a comma delimited string to be used like an array
  165. 'ItemNumber starts at 1 and counts up
  166. FUNCTION cdsI$ (cds$, ItemNumber AS INTEGER)
  167.     DIM c AS INTEGER, d AS INTEGER, lastd AS INTEGER
  168.     IF LEN(cds$) = 0 THEN cdsI$ = "": EXIT FUNCTION
  169.     lastd = 1: d = INSTR(lastd, cds$, ",")
  170.     WHILE d > 0
  171.         c = c + 1
  172.         IF c = ItemNumber THEN
  173.             cdsI$ = MID$(cds$, lastd, d - lastd): EXIT FUNCTION
  174.         ELSE
  175.             lastd = d + 1: d = INSTR(lastd, cds$, ",")
  176.         END IF
  177.     WEND
  178.     c = c + 1
  179.     IF c <> ItemNumber THEN cdsI$ = "" ELSE cdsI$ = MID$(cds$, lastd, LEN(cds$))
  180.  
  181. 'Description: cdsI sub modifies cds$ (comma delimited string) with addOrEditItem$ at ItemNumber
  182. 'This sub uses 2 functions:
  183. '    FUNCTION CommaCount% (s$)
  184. '    FUNCTION LocateComma% (s$, N AS INTEGER)
  185.  
  186. SUB cdsI (cds$, ItemNumber AS INTEGER, addOrEditItem$)
  187.     'cds$ is a comma delimited string to be used like an array
  188.     'ItemNumber starts at 1 and counts up
  189.  
  190.     DIM items AS INTEGER, i AS INTEGER
  191.     IF ItemNumber < 1 THEN BEEP: EXIT SUB 'signal bad call
  192.     items = CommaCount%(cds$) + 1
  193.     IF ItemNumber <= items + 1 THEN 'replace
  194.         IF items = 1 AND ItemNumber = 1 THEN
  195.             cds$ = addOrEditItem$
  196.         ELSEIF ItemNumber <= items - 1 THEN
  197.             cds$ = MID$(cds$, 1, LocateComma%(cds$, ItemNumber - 1)) + addOrEditItem$ + MID$(cds$, LocateComma(cds$, ItemNumber))
  198.         ELSEIF ItemNumber = items + 1 THEN
  199.             cds$ = cds$ + "," + addOrEditItem$
  200.         END IF
  201.     ELSE
  202.         i = items + 1
  203.         WHILE i <= ItemNumber - 1
  204.             cds$ = cds$ + ","
  205.             i = i + 1
  206.         WEND
  207.         cds$ = cds$ + "," + addOrEditItem$
  208.     END IF
  209.  
  210. 'Description: this sub removes from a cds$ (comma delimited string) itemNumber given if it exists
  211. ' This sub needs:
  212. '        FUNCTION CommaCount% (s$)
  213. '        FUNCTION cdsI$ (cds$, ItemNumber AS INTEGER)
  214. SUB cdsCut (cds$, ItemNumber)
  215.     DIM items AS INTEGER, B$, i AS INTEGER
  216.     items = CommaCount%(cds$) + 1
  217.     FOR i = 1 TO items
  218.         IF i <> ItemNumber THEN
  219.             IF B$ = "" THEN
  220.                 B$ = cdsI$(cds$, i)
  221.             ELSE
  222.                 B$ = B$ + "," + cdsI$(cds$, i)
  223.             END IF
  224.         END IF
  225.     NEXT
  226.     cds$ = B$
  227.  
  228. 'Description: return comma count in string in string
  229. FUNCTION CommaCount% (s$)
  230.     DIM c AS INTEGER, i AS INTEGER
  231.     FOR i = 1 TO LEN(s$)
  232.         IF MID$(s$, i, 1) = "," THEN c = c + 1
  233.     NEXT
  234.     CommaCount% = c
  235.  
  236. 'Description: return the loaction of the Nth comma in string if there is one.
  237. FUNCTION LocateComma% (s$, N AS INTEGER)
  238.     DIM c AS INTEGER, i AS INTEGER
  239.     FOR i = 1 TO LEN(s$)
  240.         IF MID$(s$, i, 1) = "," THEN
  241.             c = c + 1: IF c = N THEN LocateComma% = i: EXIT FUNCTION
  242.         END IF
  243.     NEXT
  244.  
  245.  


halos, trails and background.PNG
* halos, trails and background.PNG (Filesize: 44.64 KB, Dimensions: 800x637, Views: 134)
« Last Edit: September 01, 2019, 09:29:03 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fireflies
« Reply #8 on: September 01, 2019, 10:29:01 pm »
That looks totally cool! Great job!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fireflies
« Reply #9 on: September 02, 2019, 12:21:32 am »
Using my new knowledge of _PUTIMAGE, I added a little background of hills and sky to this program. Of course they don't have the trail when they are in front of the hill that uses the PUTIMAGE, like B+'s above, but they do have it in the sky, that I also made glowing from darker to brighter. I did this by adding a variable to the special LINE command that deletes the screen. For the first time instead of making it just all black I made a color that changes. :) Plus for the fun of it, I doubled the fireflies again to 16 and made them smaller again.

Code: QB64: [Select]
  1. 'Thanks to Bplus on the QB64.org forum for the trail code.
  2. 'Made on Aug. 30, 2019 by Ken G.
  3. _TITLE "Fireflies - Press Esc to End"
  4.  
  5. 'Draw Hills
  6. SCREEN _NEWIMAGE(800, 600, 32)
  7. CIRCLE (20, 600), 225, _RGB(6, 105, 127)
  8. PAINT (20, 500), _RGB(6, 105, 127)
  9. CIRCLE (780, 600), 225, _RGB(6, 105, 127)
  10. PAINT (780, 500), _RGB(6, 105, 127)
  11. CIRCLE (400, 800), 450, _RGB(0, 122, 11, 100)
  12. PAINT (400, 599), _RGB(0, 122, 11, 100)
  13. _PUTIMAGE , 0, a&
  14.  
  15. go:
  16. c = (RND * 130) + 50
  17. c2 = (RND * 130) + 50
  18. c3 = (RND * 130) + 50
  19. c4 = (RND * 130) + 50
  20. c5 = (RND * 130) + 50
  21. c6 = (RND * 130) + 50
  22. c7 = (RND * 130) + 50
  23. c8 = (RND * 130) + 50
  24. c9 = (RND * 130) + 50
  25. c10 = (RND * 130) + 50
  26. c11 = (RND * 130) + 50
  27. c12 = (RND * 130) + 50
  28. c13 = (RND * 130) + 50
  29. c14 = (RND * 130) + 50
  30. c15 = (RND * 130) + 50
  31. c16 = (RND * 130) + 50
  32. c17 = (RND * 130) + 50
  33. c18 = (RND * 130) + 50
  34. c19 = (RND * 130) + 50
  35. c20 = (RND * 130) + 50
  36. c21 = (RND * 130) + 50
  37. c22 = (RND * 130) + 50
  38. c23 = (RND * 130) + 50
  39. c24 = (RND * 130) + 50
  40. c25 = (RND * 130) + 50
  41. c26 = (RND * 130) + 50
  42. c27 = (RND * 130) + 50
  43. c28 = (RND * 130) + 50
  44. c29 = (RND * 130) + 50
  45. c30 = (RND * 130) + 50
  46. c31 = (RND * 130) + 50
  47. c32 = (RND * 130) + 50
  48.  
  49.  
  50.  
  51. c = 150
  52. n = 100
  53.     _LIMIT 100
  54.     _PUTIMAGE , a&, 0
  55.     IF INKEY$ = CHR$(27) THEN END
  56.     seconds = seconds + .1
  57.     s = (60 - seconds) * 6 + 180
  58.     x = INT(SIN(s / c * 3.141592) * c) + 400
  59.     y = INT(COS(s / c2 * 3.141592) * c2) + 300
  60.     x2 = INT(SIN(s / c3 * 3.141592) * c3) + 400
  61.     y2 = INT(COS(s / c4 * 3.141592) * c4) + 300
  62.     x3 = INT(SIN(s / c5 * 3.141592) * c5) + 400
  63.     y3 = INT(COS(s / c6 * 3.141592) * c6) + 300
  64.     x4 = INT(SIN(s / c7 * 3.141592) * c7) + 400
  65.     y4 = INT(COS(s / c8 * 3.141592) * c8) + 300
  66.     x5 = INT(SIN(s / c9 * 3.141592) * c9) + 400
  67.     y5 = INT(COS(s / c10 * 3.141592) * c10) + 300
  68.     x6 = INT(SIN(s / c11 * 3.141592) * c11) + 400
  69.     y6 = INT(COS(s / c12 * 3.141592) * c12) + 300
  70.     x7 = INT(SIN(s / c13 * 3.141592) * c13) + 400
  71.     y7 = INT(COS(s / c14 * 3.141592) * c14) + 300
  72.     x8 = INT(SIN(s / c15 * 3.141592) * c15) + 400
  73.     y8 = INT(COS(s / c16 * 3.141592) * c16) + 300
  74.     x9 = INT(SIN(s / c17 * 3.141592) * c17) + 400
  75.     y9 = INT(COS(s / c18 * 3.141592) * c18) + 300
  76.     x10 = INT(SIN(s / c19 * 3.141592) * c19) + 400
  77.     y10 = INT(COS(s / c20 * 3.141592) * c20) + 300
  78.     x11 = INT(SIN(s / c21 * 3.141592) * c21) + 400
  79.     y11 = INT(COS(s / c22 * 3.141592) * c22) + 300
  80.     x12 = INT(SIN(s / c23 * 3.141592) * c23) + 400
  81.     y12 = INT(COS(s / c24 * 3.141592) * c24) + 300
  82.     x13 = INT(SIN(s / c25 * 3.141592) * c25) + 400
  83.     y13 = INT(COS(s / c26 * 3.141592) * c26) + 300
  84.     x14 = INT(SIN(s / c27 * 3.141592) * c27) + 400
  85.     y14 = INT(COS(s / c28 * 3.141592) * c28) + 300
  86.     x15 = INT(SIN(s / c29 * 3.141592) * c29) + 400
  87.     y15 = INT(COS(s / c30 * 3.141592) * c30) + 300
  88.     x16 = INT(SIN(s / c31 * 3.141592) * c31) + 400
  89.     y16 = INT(COS(s / c32 * 3.141592) * c32) + 300
  90.  
  91.     t = t + 1
  92.     IF t > 240 THEN t = 1
  93.     IF t < 120 THEN tt = 1
  94.     IF t >= 120 THEN tt = -1
  95.     c = c + tt
  96.     IF c > 255 THEN c = 255
  97.     IF c < 150 THEN c = 150
  98.     cc& = c
  99.     ttt = ttt + .05
  100.     IF ttt > 200 THEN ttt = .5
  101.     IF ttt < 100 THEN ttt2 = .5
  102.     IF ttt >= 100 THEN ttt2 = -.5
  103.     n = n + ttt2
  104.     IF n > 100 THEN n = 100
  105.     IF n < 10 THEN n = 10
  106.  
  107.  
  108.     n = n + tt
  109.     CIRCLE (x, y), 3, _RGB(c, 255, c, cc&)
  110.     PAINT (x, y), _RGB(c, 255, c, cc&)
  111.     CIRCLE (x2, y2), 3, _RGB(c, c, c, cc&)
  112.     PAINT (x2, y2), _RGB(c, c, c, cc&)
  113.     CIRCLE (x3, y3), 3, _RGB(c, c, 255, cc&)
  114.     PAINT (x3, y3), _RGB(c, c, 255, cc&)
  115.     CIRCLE (x4, y4), 3, _RGB(c, c, 255, cc&)
  116.     PAINT (x4, y4), _RGB(c, c, 255, cc&)
  117.     CIRCLE (x5, y5), 3, _RGB(c, 255, c, cc&)
  118.     PAINT (x5, y5), _RGB(c, 255, c, cc&)
  119.     CIRCLE (x6, y6), 3, _RGB(255, c, c, cc&)
  120.     PAINT (x6, y6), _RGB(255, c, c, cc&)
  121.     CIRCLE (x7, y7), 3, _RGB(c, c, 255, cc&)
  122.     PAINT (x7, y7), _RGB(c, c, 255, cc&)
  123.     CIRCLE (x8, y8), 3, _RGB(c, c, 255, cc&)
  124.     PAINT (x8, y8), _RGB(c, c, 255, cc&)
  125.     CIRCLE (x9, y9), 3, _RGB(c, 255, c, cc&)
  126.     PAINT (x9, y9), _RGB(c, 255, c, cc&)
  127.     CIRCLE (x10, y10), 3, _RGB(c, c, c, cc&)
  128.     PAINT (x10, y10), _RGB(c, c, c, cc&)
  129.     CIRCLE (x11, y11), 3, _RGB(c, c, 255, cc&)
  130.     PAINT (x11, y11), _RGB(c, c, 255, cc&)
  131.     CIRCLE (x12, y12), 3, _RGB(c, c, 255, cc&)
  132.     PAINT (x12, y12), _RGB(c, c, 255, cc&)
  133.     CIRCLE (x13, y13), 3, _RGB(c, 255, c, cc&)
  134.     PAINT (x13, y13), _RGB(c, 255, c, cc&)
  135.     CIRCLE (x14, y14), 3, _RGB(255, c, c, cc&)
  136.     PAINT (x14, y14), _RGB(255, c, c, cc&)
  137.     CIRCLE (x15, y15), 3, _RGB(c, c, 255, cc&)
  138.     PAINT (x15, y15), _RGB(c, c, 255, cc&)
  139.     CIRCLE (x16, y16), 3, _RGB(c, c, 255, cc&)
  140.     PAINT (x16, y16), _RGB(c, c, 255, cc&)
  141.  
  142.     IF seconds > 300 THEN
  143.         seconds = 0
  144.         GOTO go:
  145.     END IF
  146.     _DELAY .001
  147.  
  148.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA(0, 0, n, 30), BF
  149.     _DISPLAY
  150.  

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Fireflies
« Reply #10 on: September 02, 2019, 09:30:19 am »
Just something that I have noticed... Do not panic. It is not an error... lol
I was watching the "flies" moving around for about a minute or so and noticed at one point in time all the flies aligned. I thought that, with all those random statements, what would be the odds that all the flies lined up?  So, I continued watching them again and about a minute or so later, they all lined up again... Cool, huh?

General question: Restarted the program and watched it longer. In a mater of minutes the flies had lined up four times... I am left to wonder, "How random is QB64's random number generator?" Just curious.

Can you guys just watch it run and see if it happens on your machines as well? If it's just me then ignore my ramblings... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #11 on: September 02, 2019, 09:36:45 am »
Just something that I have noticed... Do not panic. It is not an error... lol
I was watching the "flies" moving around for about a minute or so and noticed at one point in time all the flies aligned. I thought that, with all those random statements, what would be the odds that all the flies lined up?  So, I continued watching them again and about a minute or so later, they all lined up again... Cool, huh?

General question: Restarted the program and watched it longer. In a mater of minutes the flies had lined up four times... I am left to wonder, "How random is QB64's random number generator?" Just curious.

Can you guys just watch it run and see if it happens on your machines as well? If it's just me then ignore my ramblings... lol

Yes, Ken starts all flies from 0 "on the rail" as STxAxTIC calls it so every time the angle what Ken calls seconds reaches or comes near 2*PI the flies have flown full circle in their orbits and are back to square one = 0 degrees in the trignometric round trips.

Good eye and comment Johnno!

@ Ken, excellent practice of _PUTIMAGE, feel the power?
« Last Edit: September 02, 2019, 09:38:38 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fireflies
« Reply #12 on: September 02, 2019, 11:07:39 am »
Hi Ken,

Code: QB64: [Select]
  1. c = (RND * 130) + 50
  2. c2 = (RND * 130) + 50
  3. c3 = (RND * 130) + 50
  4. c4 = (RND * 130) + 50
  5. c5 = (RND * 130) + 50
  6. c6 = (RND * 130) + 50
  7. c7 = (RND * 130) + 50
  8. c8 = (RND * 130) + 50
  9. c9 = (RND * 130) + 50
  10. c10 = (RND * 130) + 50
  11. c11 = (RND * 130) + 50
  12. c12 = (RND * 130) + 50
  13. c13 = (RND * 130) + 50
  14. c14 = (RND * 130) + 50
  15. c15 = (RND * 130) + 50
  16. c16 = (RND * 130) + 50
  17. c17 = (RND * 130) + 50
  18. c18 = (RND * 130) + 50
  19. c19 = (RND * 130) + 50
  20. c20 = (RND * 130) + 50
  21. c21 = (RND * 130) + 50
  22. c22 = (RND * 130) + 50
  23. c23 = (RND * 130) + 50
  24. c24 = (RND * 130) + 50
  25. c25 = (RND * 130) + 50
  26. c26 = (RND * 130) + 50
  27. c27 = (RND * 130) + 50
  28. c28 = (RND * 130) + 50
  29. c29 = (RND * 130) + 50
  30. c30 = (RND * 130) + 50
  31. c31 = (RND * 130) + 50
  32. c32 = (RND * 130) + 50
  33.  

This begs for an array setup:

Nflies = 50
DIM cx(1 to Nflies), cy(1 toNflies)
for i = 1 to Nflies
  cx(i) = (RND*130) + 50
  cy(i) = (RND*130) + 50
next

you then can do 101, 010 flies without a single line of extra code, just change Nflies!

This:
Code: QB64: [Select]
  1.  
  2.     x = INT(SIN(s / c * 3.141592) * c) + 400
  3.     y = INT(COS(s / c2 * 3.141592) * c2) + 300
  4.     x2 = INT(SIN(s / c3 * 3.141592) * c3) + 400
  5.     y2 = INT(COS(s / c4 * 3.141592) * c4) + 300
  6.     x3 = INT(SIN(s / c5 * 3.141592) * c5) + 400
  7.     y3 = INT(COS(s / c6 * 3.141592) * c6) + 300
  8.     x4 = INT(SIN(s / c7 * 3.141592) * c7) + 400
  9.     y4 = INT(COS(s / c8 * 3.141592) * c8) + 300
  10.     x5 = INT(SIN(s / c9 * 3.141592) * c9) + 400
  11.     y5 = INT(COS(s / c10 * 3.141592) * c10) + 300
  12.     x6 = INT(SIN(s / c11 * 3.141592) * c11) + 400
  13.     y6 = INT(COS(s / c12 * 3.141592) * c12) + 300
  14.     x7 = INT(SIN(s / c13 * 3.141592) * c13) + 400
  15.     y7 = INT(COS(s / c14 * 3.141592) * c14) + 300
  16.     x8 = INT(SIN(s / c15 * 3.141592) * c15) + 400
  17.     y8 = INT(COS(s / c16 * 3.141592) * c16) + 300
  18.     x9 = INT(SIN(s / c17 * 3.141592) * c17) + 400
  19.     y9 = INT(COS(s / c18 * 3.141592) * c18) + 300
  20.     x10 = INT(SIN(s / c19 * 3.141592) * c19) + 400
  21.     y10 = INT(COS(s / c20 * 3.141592) * c20) + 300
  22.     x11 = INT(SIN(s / c21 * 3.141592) * c21) + 400
  23.     y11 = INT(COS(s / c22 * 3.141592) * c22) + 300
  24.     x12 = INT(SIN(s / c23 * 3.141592) * c23) + 400
  25.     y12 = INT(COS(s / c24 * 3.141592) * c24) + 300
  26.     x13 = INT(SIN(s / c25 * 3.141592) * c25) + 400
  27.     y13 = INT(COS(s / c26 * 3.141592) * c26) + 300
  28.     x14 = INT(SIN(s / c27 * 3.141592) * c27) + 400
  29.     y14 = INT(COS(s / c28 * 3.141592) * c28) + 300
  30.     x15 = INT(SIN(s / c29 * 3.141592) * c29) + 400
  31.     y15 = INT(COS(s / c30 * 3.141592) * c30) + 300
  32.     x16 = INT(SIN(s / c31 * 3.141592) * c31) + 400
  33.     y16 = INT(COS(s / c32 * 3.141592) * c32) + 300
  34.  
  35.  
becomes this
DIM fx(1 to nFlies, fy(1 to nFlies)
For i = 1 to Nflies
   fx(i) = INT(SIN(s /cx(i) * 3.141592) * cx(i)) + 400
   fy(i) = INT(COS(s / cy(i) * 3.141592) * cy(i)) + 300
next

again do 1,010,101 flies in same # of lines as 5.
« Last Edit: September 02, 2019, 11:35:13 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fireflies
« Reply #13 on: September 02, 2019, 12:25:40 pm »
LOL B+, I had a feeling you would say that. Going wayyyy back to the 80's, arrays haven't really been my friend lol. But I am learning. Thanks for telling me, I'll look into it.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fireflies
« Reply #14 on: September 02, 2019, 12:35:00 pm »
Give this a try; see if it doesn't do what you're wanting to accomplish:
Code: QB64: [Select]
  1. 'Thanks to Ken for inspiring mod fun!
  2. 'Thanks to Bplus on the QB64.org forum for the trail code.
  3. 'Made on Aug. 30, 2019 by Ken G. mod by B+
  4. 'Modded even further by SMcNeill to add _MEM fading to the fireflies.
  5.  
  6. '        GLOBALS
  7. CONST glow = &H08FFFFFF, nFlies = 20, xmax = 800, ymax = 600
  8. TYPE flyType
  9.     cx AS SINGLE
  10.     cy AS SINGLE
  11.     r AS INTEGER
  12.     c AS _UNSIGNED LONG
  13.  
  14. '     LOCALS for main code which is all this is!
  15. DIM i, seconds, s, x, y, bg&, bg2&
  16.  
  17. _TITLE "Fireflies that glow"
  18. SCREEN _NEWIMAGE(xmax, ymax, 32)
  19. _SCREENMOVE 300, 60
  20.  
  21. bg& = _NEWIMAGE(xmax, ymax, 32)
  22. bg2& = _NEWIMAGE(xmax, ymax, 32)
  23. _DEST bg& 'set the dest so we draw to the static background image first
  24. drawLandscape
  25.  
  26. 'setup flies
  27. DIM f(1 TO nFlies) AS flyType
  28. FOR i = 1 TO nFlies
  29.     f(i).cx = RND * 170 + 10
  30.     f(i).cy = RND * 170 + 10
  31.     f(i).r = INT(RND * 5) + 1
  32.     f(i).c = _RGB32(RND * 190 + 60, RND * 190 + 60, RND * 190 + 60)
  33.  
  34. DIM m AS _MEM: m = _MEMIMAGE(bg2&) 'point a memblock at our second background, so we can directly manipulate it
  35. DIM o AS _OFFSET, a AS _UNSIGNED _BYTE 'and some variables to work with it with
  36.  
  37. _DEST bg2& 'Set the destination to the second layer, which is where the fireflies will be drawn and faded
  38.  
  39.     'Instead of a line, which eventually builds to a full white color after enough repitition,
  40.     'let's make use of _MEM to manually reduce the alpha of the previous screen each pass
  41.     o = 3 '32-bit color is stored in memory as Blue, Green, Red, Alpha and starts at offset 0, so we want offset + 3 for Alpha channel
  42.     DO
  43.         _MEMGET m, m.OFFSET + o, a 'Get the current alpha values from memory
  44.         IF a < 30 THEN a = 0 ELSE a = a - 30 'reduce them by a set amount to fade them out
  45.         _MEMPUT m, m.OFFSET + o, a 'put the changed value back into memory
  46.         o = o + 4 'and move on to the next pixel
  47.     LOOP UNTIL o >= m.SIZE 'until we do the whole screen
  48.  
  49.     FOR i = 1 TO nFlies
  50.         seconds = seconds + .005 'slow down a tad
  51.         s = (60 - seconds) * 6 + 180 '???????????????? but it works!!
  52.         x = INT(SIN(s / f(i).cx * 3.141592) * 3 * f(i).cx) + 400 ' the * 3 and * 2 below spread flies over screen better
  53.         y = INT(COS(s / f(i).cy * 3.141592) * 2 * f(i).cy) + 300
  54.         fcirc x, y, f(i).r * 5, glow
  55.         fcirc x, y, f(i).r, f(i).c
  56.     NEXT
  57.     IF INKEY$ = CHR$(27) THEN END
  58.     _LIMIT 100
  59.     _PUTIMAGE , bg&, 0 'Put the background back in place, so we never fade it out
  60.     _PUTIMAGE , bg2&, 0 'And now display the firefly screen (which we faded with _MEM above), on the main display
  61.     _DISPLAY 'and show it all at once.
  62.  
  63. 'from Steve Gold standard
  64. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  65.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  66.     DIM X AS INTEGER, Y AS INTEGER
  67.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  68.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  69.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  70.     WHILE X > Y
  71.         RadiusError = RadiusError + Y * 2 + 1
  72.         IF RadiusError >= 0 THEN
  73.             IF X <> Y + 1 THEN
  74.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  75.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  76.             END IF
  77.             X = X - 1
  78.             RadiusError = RadiusError - X * 2
  79.         END IF
  80.         Y = Y + 1
  81.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  82.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  83.     WEND
  84. SUB drawLandscape
  85.     'needs midInk, irnd
  86.  
  87.     DIM i AS INTEGER, startH AS SINGLE, rr AS INTEGER, gg AS INTEGER, bb AS INTEGER
  88.     DIM mountain AS INTEGER, Xright AS SINGLE, y AS SINGLE, upDown AS SINGLE, range AS SINGLE
  89.     DIM lastx AS SINGLE, X AS INTEGER
  90.     'the sky
  91.     FOR i = 0 TO ymax
  92.         midInk 0, 0, 48, 28, 0, 88, i / ymax
  93.         LINE (0, i)-(xmax, i)
  94.     NEXT
  95.     'the land
  96.     startH = ymax - 400
  97.     rr = 40: gg = 50: bb = 60
  98.     FOR mountain = 1 TO 6
  99.         Xright = 0
  100.         y = startH
  101.         WHILE Xright < xmax
  102.             ' upDown = local up / down over range, change along Y
  103.             ' range = how far up / down, along X
  104.             upDown = (RND * .8 - .35) * (mountain * .5)
  105.             range = Xright + irnd%(15, 25) * 2.5 / mountain
  106.             lastx = Xright - 1
  107.             FOR X = Xright TO range
  108.                 y = y + upDown
  109.                 COLOR _RGB(rr, gg, bb)
  110.                 LINE (lastx, y)-(X, ymax), , BF 'just lines weren't filling right
  111.                 lastx = X
  112.             NEXT
  113.             Xright = range
  114.         WEND
  115.         rr = irnd%(rr - 15, rr): gg = irnd%(gg - 15, gg): bb = irnd%(bb - 25, bb)
  116.         IF rr < 0 THEN rr = 0
  117.         IF gg < 0 THEN gg = 0
  118.         IF bb < 0 THEN bb = 0
  119.         startH = startH + irnd%(5, 20)
  120.     NEXT
  121.  
  122. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  123.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  124.  
  125. FUNCTION irnd% (n1, n2) 'return an integer between 2 numbers
  126.     DIM l%, h%
  127.     IF n1 > n2 THEN l% = n2: h% = n1 ELSE l% = n1: h% = n2
  128.     irnd% = INT(RND * (h% - l% + 1)) + l%
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!