Author Topic: Aquarium with swaying kelp  (Read 6224 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Aquarium with swaying kelp
« on: July 30, 2018, 11:21:18 pm »
Here is update of old favorite of mine. No other BASIC I've run this on can handle full screen kelp drawing.
Code: QB64: [Select]
  1. _TITLE "Aquarium with swaying kelp"
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3. '2018-07-30 translated from
  4. ' Aquarium with swaying kelp.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16
  5. 'from
  6. 'aquarium with swaying kelp2.sdlbas [B+=MGA] 2016-10-14
  7. 'thanks to Andy Amaya for Kelp growing idea
  8. '2016-10-15 kelp2 grows faster, mod or fix sway?
  9.  
  10.  
  11. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  12.  
  13. '   Press SpaceBar to grow new Kelp Bed, press escape to quit
  14.  
  15. '   Press + for more fish, - for less fish
  16.  
  17. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  18.  
  19. CONST xmax = 1260
  20. CONST ymax = 720
  21. CONST swayLimit = 6
  22. CONST maxFish = 500
  23.  
  24. SCREEN _NEWIMAGE(xmax, ymax, 32)
  25.  
  26. TYPE fishType
  27.     x AS SINGLE
  28.     y AS SINGLE
  29.     dx AS SINGLE
  30.     sz AS SINGLE
  31.     red AS SINGLE
  32.     green AS SINGLE
  33.     blue AS SINGLE
  34.  
  35. DIM SHARED kelp(xmax, ymax), f(maxFish) AS fishType
  36. DIM SHARED fishFactor, restartFlag, nFish
  37.  
  38. nFish = 25 'for starters
  39.  
  40. restart:
  41. restartFlag = 0
  42. fishFactor = 50 ^ (1 / nFish) 'let's use a power lesson
  43. growKelp
  44. FOR i = 0 TO nFish
  45.     newFish i, 1
  46. aquarium
  47. IF restartFlag THEN GOTO restart
  48.  
  49.  
  50. SUB growKelp ()
  51.     ERASE kelp
  52.     kelps = rand(25, 45)
  53.     FOR x = 1 TO kelps
  54.         kelp(rand(0, xmax), ymax) = rand(1, 15)
  55.     NEXT
  56.     FOR y = ymax - 1 TO 0 STEP -1
  57.         FOR x = 0 TO xmax
  58.             IF kelp(x, y + 1) THEN
  59.                 r = rand(1, 23)
  60.                 SELECT CASE r
  61.                     CASE 1, 2, 3, 18 '1 branch node
  62.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  63.                     CASE 4, 5, 6, 7, 8, 9, 21 '1 branch node
  64.                         kelp(x, y) = kelp(x, y + 1)
  65.                     CASE 10, 11, 12, 20 '1 branch node
  66.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  67.                     CASE 13, 14, 15, 16, 17, 19 '2 branch node
  68.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  69.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  70.                 END SELECT
  71.             END IF
  72.         NEXT
  73.     NEXT
  74.  
  75. SUB showKelp (z)
  76.     FOR y = 0 TO ymax
  77.         dy = (_PI(y / 180) + z) * (1 - y / ymax)
  78.         xoff = swayLimit * SIN(dy)
  79.         FOR x = 0 TO xmax
  80.             IF kelp(x, y) > 0 AND kelp(x, y) < 16 THEN
  81.                 COLOR _RGB32(0, kelp(x, y) * 16, 0)
  82.                 recf x + xoff, y, x + xoff + 1, y + 1
  83.             END IF
  84.         NEXT
  85.     NEXT
  86.  
  87. SUB newFish (i, tfStart)
  88.     'the size and speed of a fish depends upon it's i number
  89.     'it only has to be setup if tfStart
  90.     IF tfStart THEN 'starting app place fish anywhere in sight
  91.         f(i).sz = 10 + fishFactor ^ i
  92.         f(i).dx = .2 * f(i).sz
  93.         f(i).x = rand(0, xmax)
  94.         IF RND < .5 THEN f(i).dx = f(i).dx * -1
  95.     ELSE
  96.         'choose a side to come in from fix x and dx accordingly
  97.         IF RND < .5 THEN
  98.             IF f(i).dx < 0 THEN f(i).dx = f(i).dx * -1
  99.             f(i).x = 0
  100.         ELSE
  101.             IF f(i).dx > 0 THEN f(i).dx = f(i).dx * -1
  102.             f(i).x = xmax
  103.         END IF
  104.     END IF
  105.     f(i).y = rand(f(i).sz, ymax - f(i).sz)
  106.     f(i).red = RND ^ 2: f(i).green = RND ^ 2: f(i).blue = RND ^ 2
  107.  
  108. SUB drawfish (i)
  109.     f(i).x = f(i).x + f(i).dx
  110.     IF f(i).x < 0 - 1.5 * f(i).sz OR f(i).x > xmax + 1.5 * f(i).sz THEN newFish i, 0
  111.     f(i).y = f(i).y + rand(-4, 4) * f(i).sz / 60
  112.     FOR ra = 1 TO f(i).sz
  113.         COLOR _RGB32(127 + 127 * SIN(f(i).red * .5 * ra), 127 + 127 * SIN(f(i).green * .5 * ra), 127 + 127 * SIN(f(i).blue * .5 * ra))
  114.         IF f(i).dx < 0 THEN
  115.             recf f(i).x + ra, f(i).y - ra, f(i).x + ra, f(i).y + ra
  116.         ELSE
  117.             recf f(i).x - ra, f(i).y - ra, f(i).x - ra, f(i).y + ra
  118.         END IF
  119.     NEXT
  120.     FOR ra = 3 TO .3 * f(i).sz
  121.         COLOR _RGB32(127 + 127 * SIN(f(i).red * 2 * ra), 127 + 127 * SIN(f(i).green * 2 * ra), 127 + 127 * SIN(f(i).blue * 2 * ra))
  122.         IF f(i).dx < 0 THEN
  123.             recf f(i).x + f(i).sz + ra, f(i).y - ra, f(i).x + f(i).sz + ra, f(i).y + ra
  124.         ELSE
  125.             recf f(i).x - f(i).sz - ra, f(i).y - ra, f(i).x - f(i).sz - ra, f(i).y + ra
  126.         END IF
  127.     NEXT
  128.     IF f(i).dx < 0 THEN
  129.         COLOR _RGB32(0, 0, 0): fcirc f(i).x + .2 * f(i).sz, f(i).y, .09 * f(i).sz
  130.         COLOR _RGB32(255, 255, 0): CIRCLE (f(i).x + .2 * f(i).sz, f(i).y), .07 * f(i).sz
  131.     ELSE
  132.         COLOR _RGB32(0, 0, 0): fcirc f(i).x - .2 * f(i).sz, f(i).y, .09 * f(i).sz
  133.         COLOR _RGB32(255, 255, 0): CIRCLE (f(i).x - .2 * f(i).sz, f(i).y), .07 * f(i).sz
  134.     END IF
  135.  
  136. SUB aquarium ()
  137.     dz = .25: z = 0: hf = INT(9 * nFish / 10)
  138.     WHILE NOT _KEYDOWN(27)
  139.         IF _KEYDOWN(32) THEN restartFlag = 1: EXIT SUB
  140.         IF _KEYDOWN(43) THEN 'plus more fish
  141.             IF 2 * nFish <= maxFish THEN nFish = 2 * nFish + 5: restartFlag = 1: EXIT SUB
  142.         END IF
  143.         IF _KEYDOWN(95) THEN 'minus less fish
  144.             IF nFish \ 2 >= 5 THEN nFish = nFish \ 2: restartFlag = 1: EXIT SUB
  145.         END IF
  146.  
  147.         FOR i = 0 TO ymax
  148.             COLOR _RGB32(0, 0, 255 - (i / ymax) * 255)
  149.             ln 0, i, xmax, i
  150.         NEXT
  151.         FOR i = 0 TO hf 'draw some fish behind kelp
  152.             drawfish (i)
  153.         NEXT
  154.         z = z + dz
  155.         IF z > swayLimit OR z < -1 * swayLimit THEN dz = dz * -1
  156.         showKelp (z)
  157.         FOR i = hf + 1 TO nFish 'draw the rest of the fish
  158.             drawfish (i)
  159.         NEXT
  160.  
  161.         _DISPLAY
  162.         _LIMIT 10
  163.     WEND
  164.  
  165. FUNCTION rand% (lo%, hi%)
  166.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  167.  
  168. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  169.     DIM subRadius AS LONG, RadiusError AS LONG
  170.     DIM X AS LONG, Y AS LONG
  171.  
  172.     subRadius = ABS(R)
  173.     RadiusError = -subRadius
  174.     X = subRadius
  175.     Y = 0
  176.  
  177.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  178.  
  179.     ' Draw the middle span here so we don't draw it twice in the main loop,
  180.     ' which would be a problem with blending turned on.
  181.     LINE (CX - X, CY)-(CX + X, CY), , BF
  182.  
  183.     WHILE X > Y
  184.         RadiusError = RadiusError + Y * 2 + 1
  185.         IF RadiusError >= 0 THEN
  186.             IF X <> Y + 1 THEN
  187.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  188.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  189.             END IF
  190.             X = X - 1
  191.             RadiusError = RadiusError - X * 2
  192.         END IF
  193.         Y = Y + 1
  194.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  195.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  196.     WEND
  197.  
  198. SUB ln (x1, y1, x2, y2)
  199.     LINE (x1, y1)-(x2, y2)
  200.  
  201. SUB rec (x1, y1, x2, y2)
  202.     LINE (x1, y1)-(x2, y2), , B
  203.  
  204. SUB recf (x1, y1, x2, y2)
  205.     LINE (x1, y1)-(x2, y2), , BF
  206.  
  207.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Aquarium with swaying kelp
« Reply #1 on: August 01, 2018, 07:01:09 am »
This is wonderful!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Aquarium with swaying kelp
« Reply #2 on: August 01, 2018, 08:28:29 am »
Well... That program almost made me sick! Oh. The program is great... it's the swaying of the kelp... back and forth; back and forth.....

Great job!!

J
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Aquarium with swaying kelp
« Reply #3 on: August 01, 2018, 09:13:07 am »
Thanks guys!

I never noticed the seasickness until going full screen, watch it for awhile and then look at something steady...

I thought spinning was bad, ha!

FellippeHeitor

  • Guest
Re: Aquarium with swaying kelp
« Reply #4 on: August 01, 2018, 11:56:35 am »
Cool effect, bplus!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Aquarium with swaying kelp
« Reply #5 on: August 02, 2018, 10:03:23 am »
Well with such encouragement... version 2
Quote
' 2018-08-01 kPalette added and kelp background, new size kelp at front level.
' Fish evoled:
' 1. fish have pectorial fins now stroking
' 2. hey a new fish type!

Code: QB64: [Select]
  1. _TITLE "Aquarium with swaying kelp"
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3.  
  4. 'history:
  5. 'aquarium with swaying kelp2.sdlbas [B+=MGA] 2016-10-14
  6. ' thanks to Andy Amaya for Kelp growing idea
  7. ' Aquarium with swaying kelp.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16
  8. '2016-10-15 kelp2 grows faster, mod or fix sway?
  9. '2018-07-30 translated to QB64
  10. ' size and speed depends on i, use fishFactor and powers of i
  11. ' more fish behind kelp
  12. ' 2018-08-01 kPalette added and kelp background, new size kelp at fromt level.
  13. ' Fish evoled:
  14. ' 1. fish have pectorial fins now stroking
  15. ' 2. hey a new fish type!
  16.  
  17. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  18.  
  19. '   Press SpaceBar to grow new Kelp Bed, press escape to quit
  20.  
  21. '   Press + for more fish, - for less fish
  22.  
  23. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24.  
  25. CONST xmax = 1260
  26. CONST ymax = 720
  27. CONST swayLimit = 3
  28. CONST maxFish = 1024
  29.  
  30. SCREEN _NEWIMAGE(xmax, ymax, 32)
  31.  
  32. TYPE fishType
  33.     x AS SINGLE
  34.     y AS SINGLE
  35.     dx AS SINGLE
  36.     sz AS SINGLE
  37.     stroke AS INTEGER
  38.     red AS SINGLE
  39.     green AS SINGLE
  40.     blue AS SINGLE
  41.  
  42. DIM SHARED kelp(xmax, ymax), f(maxFish) AS fishType
  43. DIM SHARED fishFactor, restartFlag, nFish, back&
  44. tColor = 0
  45. DIM SHARED kPalette(287) AS LONG
  46. FOR g = 15 TO 255 STEP 15
  47.     FOR r = 15 TO .5 * g STEP 15
  48.         kPalette(tColor) = _RGB32(0, .6 * g, 0)
  49.         tColor = tColor + 1
  50.         kPalette(tColor) = _RGB32(.2 * r, .6 * g, 0)
  51.         tColor = tColor + 1
  52.         kPalette(tColor) = _RGB32(0, .6 * g, .1 * r)
  53.         tColor = tColor + 1
  54.         kPalette(tColor) = _RGB32(.2 * r, .6 * g, .1 * r)
  55.         tColor = tColor + 1
  56.     NEXT
  57. back& = _NEWIMAGE(xmax, ymax, 32)
  58. nFish = 64 'for starters
  59.  
  60. restart:
  61. restartFlag = 0
  62. makeBackground
  63. growKelp
  64. fishFactor = 50 ^ (1 / nFish) 'let's use a power lesson
  65. FOR i = 1 TO nFish
  66.     newFish i, 1
  67. aquarium
  68. IF restartFlag THEN GOTO restart
  69.  
  70. SUB makeBackground ()
  71.     _DEST back&
  72.     ERASE kelp
  73.     FOR i = 0 TO ymax
  74.         COLOR _RGB32(0, 0, 255 - (i / ymax) * 255)
  75.         ln 0, i, xmax, i
  76.     NEXT
  77.     kelps = rand(100, 200)
  78.     FOR x = 1 TO kelps
  79.         kelp(rand(0, xmax), ymax) = rand(1, 160)
  80.     NEXT
  81.     FOR y = ymax - 1 TO 0 STEP -1
  82.         FOR x = 0 TO xmax
  83.             IF kelp(x, y + 1) THEN
  84.                 r = rand(1, 23)
  85.                 SELECT CASE r
  86.                     CASE 1, 2, 3, 18 '1 branch node
  87.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  88.                     CASE 4, 5, 6, 7, 8, 9, 21 '1 branch node
  89.                         kelp(x, y) = kelp(x, y + 1)
  90.                     CASE 10, 11, 12, 20 '1 branch node
  91.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  92.                     CASE 13, 14, 15, 16, 17, 19 '2 branch node
  93.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  94.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  95.                 END SELECT
  96.             END IF
  97.         NEXT
  98.     NEXT
  99.     FOR y = 0 TO ymax
  100.         FOR x = 0 TO xmax
  101.             IF kelp(x, y) > 0 THEN
  102.                 COLOR kPalette(kelp(x, y))
  103.                 frec x, y, x + 1, y + 1
  104.             END IF
  105.         NEXT
  106.     NEXT
  107.     _DEST 0
  108.  
  109. SUB growKelp ()
  110.     ERASE kelp
  111.     kelps = rand(7, 15)
  112.     FOR x = 1 TO kelps
  113.         kelp(rand(0, .5 * xmax), ymax) = 3 * x + x MOD 4
  114.     NEXT
  115.     FOR y = ymax - 1 TO 0 STEP -1
  116.         FOR x = 0 TO xmax
  117.             IF kelp(x, y + 1) THEN
  118.                 r = rand(1, 23)
  119.                 SELECT CASE r
  120.                     CASE 1, 2, 3, 18 '1 branch node
  121.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  122.                     CASE 4, 5, 6, 7, 8, 9, 21 '1 branch node
  123.                         kelp(x, y) = kelp(x, y + 1)
  124.                     CASE 10, 11, 12, 20 '1 branch node
  125.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  126.                     CASE 13, 14, 15, 16, 17, 19 '2 branch node
  127.                         IF x - 1 >= 0 THEN kelp(x - 1, y) = kelp(x, y + 1)
  128.                         IF x + 1 <= xmax THEN kelp(x + 1, y) = kelp(x, y + 1)
  129.                 END SELECT
  130.             END IF
  131.         NEXT
  132.     NEXT
  133.  
  134. SUB showKelp (z)
  135.     FOR y = 0 TO .5 * ymax
  136.         dy = (_PI(y / 100) + z) * (1 - y / (.5 * ymax))
  137.         xoff = swayLimit * SIN(dy)
  138.         FOR x = 0 TO .5 * xmax
  139.             IF kelp(x, y) > 0 THEN
  140.                 rc = x * y MOD 4
  141.                 SELECT CASE rc
  142.                     CASE IS = 0: COLOR kPalette(kelp(x, y))
  143.                     CASE IS = 1: COLOR kPalette(kelp(x, y) + 5)
  144.                     CASE IS = 2: COLOR kPalette(kelp(x, y) + 10)
  145.                     CASE ELSE: COLOR kPalette(kelp(x, y) + 15)
  146.                 END SELECT
  147.                 'IF y < RND * .25 * ymax / 1.5 AND RND < .3 THEN xxoff = xoff * RND ELSE xxoff = xoff
  148.                 frec x * 2 + xoff - 1, y * 2 - 1, x * 2 + xoff + 2, y * 2 + 2
  149.                 COLOR kPalette(kelp(x, y))
  150.                 fcirc x * 2 + xoff, y * 2, 1
  151.             END IF
  152.         NEXT
  153.     NEXT
  154.  
  155. SUB newFish (i, tfStart)
  156.     'the size and speed of a fish depends upon it's i number
  157.     'it only has to be setup if tfStart
  158.     IF tfStart THEN 'starting app place fish anywhere in sight
  159.         f(i).sz = 10 + fishFactor ^ i
  160.         f(i).dx = .2 * f(i).sz
  161.         f(i).x = rand(0, xmax)
  162.         IF RND < .5 THEN f(i).dx = f(i).dx * -1
  163.         f(i).stroke = rand(0, 3)
  164.     ELSE
  165.         'choose a side to come in from fix x and dx accordingly
  166.         IF RND < .5 THEN
  167.             IF f(i).dx < 0 THEN f(i).dx = f(i).dx * -1
  168.             f(i).x = 0
  169.         ELSE
  170.             IF f(i).dx > 0 THEN f(i).dx = f(i).dx * -1
  171.             f(i).x = xmax
  172.         END IF
  173.         f(i).stroke = 0
  174.     END IF
  175.     f(i).type = rand(0, 1)
  176.     f(i).y = rand(f(i).sz, ymax - f(i).sz)
  177.     f(i).red = RND ^ 2: f(i).green = RND ^ 2: f(i).blue = RND ^ 2
  178.  
  179. SUB drawfish (i)
  180.     f(i).x = f(i).x + f(i).dx
  181.     IF f(i).x < 0 - 1.5 * f(i).sz OR f(i).x > xmax + 1.5 * f(i).sz THEN newFish i, 0
  182.     f(i).y = f(i).y + rand(-4, 4) * f(i).sz / 60
  183.     f(i).stroke = (f(i).stroke + 1) MOD 4
  184.     FOR ra = 1 TO f(i).sz
  185.         COLOR _RGB32(127 + 127 * SIN(f(i).red * .5 * ra), 127 + 127 * SIN(f(i).green * .5 * ra), 127 + 127 * SIN(f(i).blue * .5 * ra))
  186.         IF f(i).dx < 0 THEN
  187.             IF f(i).type AND ra > .5 * f(i).sz THEN
  188.                 frec f(i).x + ra, f(i).y - .7 * f(i).sz + .7 * ra, f(i).x + ra, f(i).y + .7 * f(i).sz - .7 * ra
  189.             ELSE
  190.                 frec f(i).x + ra, f(i).y - ra, f(i).x + ra, f(i).y + ra
  191.             END IF
  192.         ELSE
  193.             IF f(i).type AND ra > .5 * f(i).sz THEN
  194.                 frec f(i).x - ra, f(i).y - .7 * f(i).sz + .7 * ra, f(i).x - ra, f(i).y + .7 * f(i).sz - .7 * ra
  195.             ELSE
  196.                 frec f(i).x - ra, f(i).y - ra, f(i).x - ra, f(i).y + ra
  197.             END IF
  198.         END IF
  199.     NEXT
  200.     FOR ra = 3 TO .3 * f(i).sz
  201.         COLOR _RGB32(127 + 127 * SIN(f(i).red * 2 * ra), 127 + 127 * SIN(f(i).green * 2 * ra), 127 + 127 * SIN(f(i).blue * 2 * ra))
  202.         IF f(i).dx < 0 THEN
  203.             frec f(i).x + f(i).sz + ra, f(i).y - ra, f(i).x + f(i).sz + ra, f(i).y + ra
  204.         ELSE
  205.             frec f(i).x - f(i).sz - ra, f(i).y - ra, f(i).x - f(i).sz - ra, f(i).y + ra
  206.         END IF
  207.     NEXT
  208.     IF f(i).dx < 0 THEN
  209.         COLOR _RGB32(0, 0, 0): fcirc f(i).x + .2 * f(i).sz, f(i).y, .09 * f(i).sz
  210.         COLOR _RGB32(255, 255, 0): CIRCLE (f(i).x + .2 * f(i).sz, f(i).y), .07 * f(i).sz
  211.     ELSE
  212.         COLOR _RGB32(0, 0, 0): fcirc f(i).x - .2 * f(i).sz, f(i).y, .09 * f(i).sz
  213.         COLOR _RGB32(255, 255, 0): CIRCLE (f(i).x - .2 * f(i).sz, f(i).y), .07 * f(i).sz
  214.     END IF
  215.     COLOR _RGBA32(255, 255, 255, 100)
  216.     IF f(i).dx < 0 THEN
  217.         SELECT CASE f(i).stroke
  218.             CASE 0: fEllipse f(i).x + .5 * f(i).sz, f(i).y + .2 * f(i).sz, .01 * f(i).sz, .1 * f(i).sz
  219.             CASE 1: fEllipse f(i).x + .55 * f(i).sz, f(i).y + .2 * f(i).sz, .07 * f(i).sz, .1 * f(i).sz
  220.             CASE 2: fEllipse f(i).x + .6 * f(i).sz, f(i).y + .2 * f(i).sz, .15 * f(i).sz, .1 * f(i).sz
  221.             CASE 3: fEllipse f(i).x + .5 * f(i).sz, f(i).y + .2 * f(i).sz, .07 * f(i).sz, .1 * f(i).sz
  222.         END SELECT
  223.     ELSE
  224.         SELECT CASE f(i).stroke
  225.             CASE 0: fEllipse f(i).x - .5 * f(i).sz, f(i).y + .2 * f(i).sz, .01 * f(i).sz, .1 * f(i).sz
  226.             CASE 1: fEllipse f(i).x - .55 * f(i).sz, f(i).y + .2 * f(i).sz, .07 * f(i).sz, .1 * f(i).sz
  227.             CASE 2: fEllipse f(i).x - .6 * f(i).sz, f(i).y + .2 * f(i).sz, .15 * f(i).sz, .1 * f(i).sz
  228.             CASE 3: fEllipse f(i).x - .5 * f(i).sz, f(i).y + .2 * f(i).sz, .07 * f(i).sz, .1 * f(i).sz
  229.         END SELECT
  230.     END IF
  231.  
  232. SUB aquarium ()
  233.     dz = .25: z = 0: hf = INT(9 * nFish / 10)
  234.     WHILE NOT _KEYDOWN(27)
  235.         IF _KEYDOWN(32) THEN restartFlag = 1: EXIT SUB
  236.         IF _KEYDOWN(43) THEN 'plus more fish
  237.             IF 2 * nFish <= maxFish THEN nFish = 2 * nFish: restartFlag = 1: EXIT SUB
  238.         END IF
  239.         IF _KEYDOWN(95) THEN 'minus less fish
  240.             IF nFish / 2 >= 2 THEN nFish = nFish / 2: restartFlag = 1: EXIT SUB
  241.         END IF
  242.         _PUTIMAGE , back&, 0
  243.         FOR i = 1 TO hf 'draw some fish behind kelp
  244.             drawfish (i)
  245.         NEXT
  246.         z = z + dz
  247.         IF z > swayLimit OR z < -1 * swayLimit THEN dz = dz * -1
  248.         showKelp z
  249.         FOR i = hf + 1 TO nFish 'draw the rest of the fish
  250.             drawfish (i)
  251.         NEXT
  252.         COLOR _RGB32(255, 255, 255), _RGB(0, 0, 98)
  253.         _PRINTSTRING (10, 10), LTRIM$(STR$(nFish))
  254.         _DISPLAY
  255.         _LIMIT 12
  256.     WEND
  257.  
  258. FUNCTION rand% (lo%, hi%)
  259.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  260.  
  261. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  262.     DIM subRadius AS LONG, RadiusError AS LONG
  263.     DIM X AS LONG, Y AS LONG
  264.  
  265.     subRadius = ABS(R)
  266.     RadiusError = -subRadius
  267.     X = subRadius
  268.     Y = 0
  269.  
  270.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  271.  
  272.     ' Draw the middle span here so we don't draw it twice in the main loop,
  273.     ' which would be a problem with blending turned on.
  274.     LINE (CX - X, CY)-(CX + X, CY), , BF
  275.  
  276.     WHILE X > Y
  277.         RadiusError = RadiusError + Y * 2 + 1
  278.         IF RadiusError >= 0 THEN
  279.             IF X <> Y + 1 THEN
  280.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  281.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  282.             END IF
  283.             X = X - 1
  284.             RadiusError = RadiusError - X * 2
  285.         END IF
  286.         Y = Y + 1
  287.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  288.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  289.     WEND
  290.  
  291. SUB ln (x1, y1, x2, y2)
  292.     LINE (x1, y1)-(x2, y2)
  293.  
  294. SUB rec (x1, y1, x2, y2)
  295.     LINE (x1, y1)-(x2, y2), , B
  296.  
  297. SUB frec (x1, y1, x2, y2)
  298.     LINE (x1, y1)-(x2, y2), , BF
  299.  
  300. SUB fEllipse (CX AS LONG, CY AS LONG, xRadius AS LONG, yRadius AS LONG)
  301.     DIM scale AS SINGLE, x AS LONG, y AS LONG
  302.     scale = yRadius / xRadius
  303.     LINE (CX, CY - yRadius)-(CX, CY + yRadius), , BF
  304.     FOR x = 1 TO xRadius
  305.         y = scale * SQR(xRadius * xRadius - x * x)
  306.         LINE (CX + x, CY - y)-(CX + x, CY + y), , BF
  307.         LINE (CX - x, CY - y)-(CX - x, CY + y), , BF
  308.     NEXT
  309.  
  310.  
« Last Edit: August 02, 2018, 10:07:05 am by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Aquarium with swaying kelp
« Reply #6 on: August 02, 2018, 07:51:28 pm »
Nice... Now where did I put my Dramamine?... lol Very cool indeed...
Logic is the beginning of wisdom.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Aquarium with swaying kelp
« Reply #7 on: August 07, 2018, 11:42:00 am »
Very nice... Where is the shark ?
Why not yes ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Aquarium with swaying kelp
« Reply #8 on: August 09, 2018, 10:05:13 am »
Very nice... Where is the shark ?

:)

I would love other people to contribute fish types, they could use these as parameters to a drawing routine:
TYPE fishType
    x AS SINGLE
    y AS SINGLE
    dx AS SINGLE
    sz AS SINGLE
    stroke AS INTEGER
    type AS INTEGER
    red AS SINGLE
    green AS SINGLE
    blue AS SINGLE
END TYPE

Since this type includes a fish type, I could just add SELECT CASE block in the drawFish routine for adding fish or sharks or starfish or... maybe not whales. I guess there would be some sort of maximum size to allow. I would prefer a drawing routine as opposed to adding images as that would be in the spirit of this aquarium program.

So, so far the sharks are just in our imaginations...  ;)

FellippeHeitor

  • Guest
Re: Aquarium with swaying kelp
« Reply #9 on: August 09, 2018, 12:27:06 pm »
I feel represented by SUB fEllipse!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Aquarium with swaying kelp
« Reply #10 on: August 10, 2018, 12:59:02 am »
:-)) Speaking of representing, here is a handy drawing tool that may help with fins, it won't leak PAINT:
Code: QB64: [Select]
  1.  
  2. 'use radians
  3. SUB mArc (x, y, r, raStart, raStop, c AS _UNSIGNED LONG)
  4.     'x, y origin, r = radius, c = color
  5.  
  6.     'raStart is first angle clockwise from due East = 0 degrees
  7.     ' arc will start drawing there and clockwise until raStop angle reached
  8.  
  9.     IF raStop < raStart THEN
  10.         mArc x, y, r, raStart, _PI(2), c
  11.         mArc x, y, r, 0, raStop, c
  12.     ELSE
  13.         ' modified to easier way suggested by Steve
  14.         'Why was the line method not good? I forgot.
  15.         al = _PI * r * r * (raStop - raStart) / _PI(2)
  16.         FOR a = raStart TO raStop STEP 1 / al
  17.             PSET (x + r * COS(a), y + r * SIN(a)), c
  18.         NEXT
  19.     END IF
  20.