Author Topic: 🐠 Curious Fish  (Read 4481 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
🐠 Curious Fish
« on: December 02, 2021, 12:08:53 pm »
Code: QB64: [Select]
  1. fishleft$ = "<*)))<{"
  2. fishright$ = "}<(((*>"
  3. x = 1
  4. y = Rnd * _Height
  5. direction = 1
  6. maxSpeed = 4
  7. speed = maxSpeed
  8.  
  9.     Color 2, 1
  10.     Cls
  11.  
  12.     Select Case direction
  13.         Case -1
  14.             _PrintString (x, y), fishleft$
  15.         Case 1
  16.             _PrintString (x, y), fishright$
  17.     End Select
  18.  
  19.     If speed > 0 Then
  20.         x = x + direction * speed
  21.         speed = speed - .2
  22.         If speed <= 0 Then
  23.             rest = Timer
  24.             restFor = Rnd * 3
  25.         End If
  26.     Else
  27.         If Timer - rest > restFor Then
  28.             speed = maxSpeed
  29.             bubbleX = Rnd * _Width
  30.             bubbleY = _Height
  31.             If Rnd < .5 Then
  32.                 y = y + 1
  33.             Else
  34.                 y = y - 1
  35.             End If
  36.         End If
  37.     End If
  38.  
  39.     If x < 1 Then x = 2: direction = direction * -1
  40.     If x + Len(fishleft$) > _Width Then x = _Width - Len(fishleft$): direction = direction * -1
  41.  
  42.     If y > _Height Then y = _Height
  43.     If y < 1 Then y = 1
  44.  
  45.     Color 7
  46.     If bubbleY >= 1 Then
  47.         _PrintString (bubbleX, Int(bubbleY)), "o"
  48.         bubbleY = bubbleY - Rnd
  49.     End If
  50.     _Display
  51.     _Limit 30
  52.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 🐠 Curious Fish
« Reply #1 on: December 02, 2021, 02:05:52 pm »
  • Best Answer
  • Nice :)

    Offline Richard Frost

    • Seasoned Forum Regular
    • Posts: 316
    • Needle nardle noo. - Peter Sellers
      • View Profile
    Re: 🐠 Curious Fish
    « Reply #2 on: December 02, 2021, 03:06:47 pm »
  • Best Answer
  • Cute.  Should have Don Ho's "Tiny Bubbles" playing in the background.
    It works better if you plug it in.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: 🐠 Curious Fish
    « Reply #3 on: December 03, 2021, 01:17:03 pm »
  • Best Answer
  • This looks like fun!

    Code: QB64: [Select]
    1. _Title "     Fish:    press m for more,    l for less" 'b+ 2021-12-03
    2. Const sw = 1024, sh = 700, LHead$ = "<*", LBody$ = ")", LTail$ = "<{", RHead$ = "*>", RBody$ = "(", RTail$ = "}<"
    3. Type fish
    4.     As Integer LFish, X, Y, DX
    5.     As String fish
    6.     As _Unsigned Long Colr
    7.  
    8. Screen _NewImage(sw, sh, 32)
    9. _ScreenMove 180, 40
    10.  
    11.  
    12. '_FullScreen ' <<<<<<<<<<<<<<<   goto full screen once you know instructions for more and less fish
    13.  
    14.  
    15.  
    16. Color _RGB32(220), _RGB32(0, 0, 60)
    17. Dim As Integer i, nFish
    18. Dim k$
    19. nFish = 20
    20.  
    21. restart:
    22. ReDim Shared school(1 To nFish) As fish, kelp(sw, sh) As _Unsigned Long
    23. growKelp
    24. For i = 1 To nFish
    25.     NewFish i, -1
    26.     Cls
    27.     k$ = InKey$
    28.     If k$ = "m" Then ' more fish
    29.         nFish = nFish * 2
    30.         If nFish > 300 Then Beep: nFish = 300
    31.         GoTo restart
    32.     End If
    33.     If k$ = "l" Then ' less fish
    34.         nFish = nFish / 2
    35.         If nFish < 4 Then Beep: nFish = 4
    36.         GoTo restart
    37.     End If
    38.     For i = 1 To nFish ' draw fish behind kelp
    39.         If _Red32(school(i).Colr) < 160 Then
    40.             Color school(i).Colr
    41.             _PrintString (school(i).X, school(i).Y), school(i).fish 'draw fish
    42.             school(i).X = school(i).X + school(i).DX
    43.             If school(i).LFish Then
    44.                 If school(i).X + Len(school(i).fish) * 8 < 0 Then NewFish i, 0
    45.             Else
    46.                 If school(i).X - Len(school(i).fish) * 8 > _Width Then NewFish i, 0
    47.             End If
    48.         End If
    49.     Next
    50.     showKelp
    51.     For i = 1 To nFish ' draw fish in from of kelp
    52.         If _Red32(school(i).Colr) >= 160 Then
    53.             Color school(i).Colr
    54.             _PrintString (school(i).X, school(i).Y), school(i).fish 'draw fish
    55.             school(i).X = school(i).X + school(i).DX
    56.             If school(i).LFish Then
    57.                 If school(i).X + Len(school(i).fish) * 8 < 0 Then NewFish i, 0
    58.             Else
    59.                 If school(i).X - Len(school(i).fish) * 8 > _Width Then NewFish i, 0
    60.             End If
    61.         End If
    62.     Next
    63.  
    64.     _Display
    65.     _Limit 10
    66.  
    67. Sub NewFish (i, initTF)
    68.     Dim gray
    69.     gray = Rnd * 200 + 55
    70.     school(i).Colr = _RGB32(gray) ' color
    71.     If Rnd > .5 Then
    72.         school(i).LFish = -1
    73.         school(i).fish = LHead$ + String$(Int(Rnd * 5) + -2 * (gray > 160) + 1, LBody$) + LTail$
    74.     Else
    75.         school(i).LFish = 0
    76.         school(i).fish = RTail$ + String$(Int(Rnd * 5) + -2 * (gray > 160) + 1, RBody$) + RHead$
    77.     End If
    78.     If initTF Then
    79.         school(i).X = _Width * Rnd
    80.     Else
    81.         If school(i).LFish Then school(i).X = _Width + Rnd * 35 Else school(i).X = -35 * Rnd - Len(school(i).fish) * 8
    82.     End If
    83.     If gray > 160 Then
    84.         If school(i).LFish Then school(i).DX = -18 * Rnd - 3 Else school(i).DX = 18 * Rnd + 3
    85.     Else
    86.         If school(i).LFish Then school(i).DX = -6 * Rnd - 1 Else school(i).DX = 6 * Rnd + 1
    87.     End If
    88.     school(i).Y = _Height * Rnd
    89.  
    90. Sub growKelp
    91.     Dim kelps, x, y, r
    92.     ReDim kelp(sw, sh) As _Unsigned Long
    93.     kelps = Int(Rnd * 20) + 20
    94.     For x = 1 To kelps
    95.         kelp(Int(Rnd * sw / 8), (sh - 16) / 16) = _RGB32(0, Rnd * 128, 0)
    96.     Next
    97.     For y = sh / 16 To 0 Step -1
    98.         For x = 0 To sw / 8
    99.             If kelp(x, y + 1) Then
    100.                 r = Int(Rnd * 23) + 1
    101.                 Select Case r
    102.                     Case 1, 2, 3, 18 '1 branch node
    103.                         If x - 1 >= 0 Then kelp(x - 1, y) = kelp(x, y + 1)
    104.                     Case 4, 5, 6, 7, 8, 9, 21 '1 branch node
    105.                         kelp(x, y) = kelp(x, y + 1)
    106.                     Case 10, 11, 12, 20 '1 branch node
    107.                         If x + 1 <= sw Then kelp(x + 1, y) = kelp(x, y + 1)
    108.                     Case 13, 14, 15, 16, 17, 19 '2 branch node
    109.                         If x - 1 >= 0 Then kelp(x - 1, y) = kelp(x, y + 1)
    110.                         If x + 1 <= sw Then kelp(x + 1, y) = kelp(x, y + 1)
    111.                 End Select
    112.             End If
    113.         Next
    114.     Next
    115.  
    116. Sub showKelp
    117.     Dim y, x
    118.     For y = 0 To sh / 16
    119.         For x = 0 To sw / 8
    120.             If kelp(x, y) Then
    121.                 Color kelp(x, y)
    122.                 _PrintString (x * 8, y * 16), Mid$("kelp", Int(Rnd * 4) + 1, 1)
    123.             End If
    124.         Next
    125.     Next
    126.  

    EDIT: removed unused variables
    « Last Edit: December 03, 2021, 01:31:16 pm by bplus »

    FellippeHeitor

    • Guest
    Re: 🐠 Curious Fish
    « Reply #4 on: December 03, 2021, 07:29:26 pm »
  • Best Answer
  • Whooooooa! Your mods never disappoint, bplus!

    Offline johnno56

    • Forum Resident
    • Posts: 1270
    • Live long and prosper.
      • View Profile
    Re: 🐠 Curious Fish
    « Reply #5 on: December 03, 2021, 07:41:27 pm »
  • Best Answer
  • Now where have I seen this program before? It looks familiar. Can't quite put my finger on it... LOL
    Text or not. It still looks impressive. Nicely done!
    Logic is the beginning of wisdom.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: 🐠 Curious Fish
    « Reply #6 on: December 03, 2021, 11:26:15 pm »
  • Best Answer
  • Thanks guys, yeah new use for old code. The random letters from "kelp" worked really nice for water effect.