Author Topic: Spark Generator  (Read 4832 times)

0 Members and 1 Guest are viewing this topic.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Spark Generator
« on: August 21, 2018, 03:16:37 am »
Here's a little spark generator I created years ago. Press any key and see some sparks. Hold a key down for spark mania!

Code: QB64: [Select]
  1. CONST FALSE = 0, TRUE = NOT FALSE
  2.  
  3. TYPE SPARK
  4.     count AS INTEGER
  5.     x AS SINGLE
  6.     y AS SINGLE
  7.     xdir AS SINGLE
  8.     ydir AS SINGLE
  9.     speed AS SINGLE
  10.     fade AS INTEGER
  11.  
  12. REDIM sparks(0) AS SPARK
  13.  
  14. SCREEN _NEWIMAGE(1024, 768, 32)
  15.  
  16.     keypress$ = INKEY$
  17.     _LIMIT 32
  18.     CLS
  19.     IF keypress$ = " " THEN MakeSparks INT(RND(1) * 1024), INT(RND(1) * 768)
  20.     UpdateSparks
  21.     _DISPLAY
  22. LOOP UNTIL keypress$ = CHR$(27)
  23.  
  24. '------------------------------
  25.  
  26. SUB MakeSparks (x%, y%)
  27.  
  28. SHARED sparks() AS SPARK
  29.  
  30. DIM cleanup%, count%, topspark%
  31.  
  32. cleanup% = TRUE
  33. FOR count% = 1 TO UBOUND(sparks)
  34.     IF sparks(count%).count <> 0 THEN
  35.         cleanup% = FALSE
  36.         EXIT FOR
  37.     END IF
  38. NEXT count%
  39. IF cleanup% THEN REDIM sparks(0) AS SPARK
  40. topspark% = UBOUND(sparks)
  41. REDIM _PRESERVE sparks(topspark% + 11) AS SPARK
  42. FOR count% = 1 TO 10
  43.     sparks(topspark% + count%).count = 32
  44.     sparks(topspark% + count%).x = x%
  45.     sparks(topspark% + count%).y = y%
  46.     sparks(topspark% + count%).fade = 255
  47.     sparks(topspark% + count%).speed = INT(RND(1) * 6) + 6
  48.     sparks(topspark% + count%).xdir = RND(1) - RND(1)
  49.     sparks(topspark% + count%).ydir = RND(1) - RND(1)
  50. NEXT count%
  51.  
  52.  
  53. '--------------------------------------
  54.  
  55. SUB UpdateSparks ()
  56.  
  57. SHARED sparks() AS SPARK
  58.  
  59. DIM count%, fade1%, fade2%
  60.  
  61. FOR count% = 1 TO UBOUND(sparks)
  62.     IF sparks(count%).count > 0 THEN
  63.         fade1% = sparks(count%).fade / 2
  64.         fade2% = sparks(count%).fade / 4
  65.         PSET (sparks(count%).x, sparks(count%).y), _RGB(sparks(count%).fade, sparks(count%).fade, sparks(count%).fade)
  66.         PSET (sparks(count%).x + 1, sparks(count%).y), _RGB(fade1%, fade1%, fade1%)
  67.         PSET (sparks(count%).x - 1, sparks(count%).y), _RGB(fade1%, fade1%, fade1%)
  68.         PSET (sparks(count%).x, sparks(count%).y + 1), _RGB(fade1%, fade1%, fade1%)
  69.         PSET (sparks(count%).x, sparks(count%).y - 1), _RGB(fade1%, fade1%, fade1%)
  70.         PSET (sparks(count%).x + 1, sparks(count%).y + 1), _RGB(fade2%, fade2%, fade2%)
  71.         PSET (sparks(count%).x - 1, sparks(count%).y - 1), _RGB(fade2%, fade2%, fade2%)
  72.         PSET (sparks(count%).x - 1, sparks(count%).y + 1), _RGB(fade2%, fade2%, fade2%)
  73.         PSET (sparks(count%).x + 1, sparks(count%).y - 1), _RGB(fade2%, fade2%, fade2%)
  74.         sparks(count%).fade = sparks(count%).fade - 8
  75.         sparks(count%).x = sparks(count%).x + sparks(count%).xdir * sparks(count%).speed
  76.         sparks(count%).y = sparks(count%).y + sparks(count%).ydir * sparks(count%).speed
  77.         sparks(count%).speed = sparks(count%).speed / 1.1
  78.         sparks(count%).count = sparks(count%).count - 1
  79.     END IF
  80. NEXT count%
  81.  
  82.  
In order to understand recursion, one must first understand recursion.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Spark Generator
« Reply #1 on: August 21, 2018, 07:50:01 pm »
Cool...
Logic is the beginning of wisdom.

FellippeHeitor

  • Guest
Re: Spark Generator
« Reply #2 on: August 21, 2018, 08:16:18 pm »
Very nice effect indeed, Terry. But by "any key" you mean the space bar, right?

Thanks for sharing!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Spark Generator
« Reply #3 on: August 22, 2018, 12:07:17 am »
Very nice effect indeed, Terry. But by "any key" you mean the space bar, right?

Thanks for sharing!

Oops, LOL -- Yes
In order to understand recursion, one must first understand recursion.