_TITLE "Aquarium with swaying kelp" 'QB64 X 64 version 1.2 20180228/86 from git b301f92
'history:
'aquarium with swaying kelp2.sdlbas [B+=MGA] 2016-10-14
' thanks to Andy Amaya for Kelp growing idea
' Aquarium with swaying kelp.bas SmallBASIC 0.12.9 [B+=MGA] 2017-04-16
'2016-10-15 kelp2 grows faster, mod or fix sway?
'2018-07-30 translated to QB64
' size and speed depends on i, use fishFactor and powers of i
' more fish behind kelp
' 2018-08-01 kPalette added and kelp background, new size kelp at fromt level.
' Fish evoled:
' 1. fish have pectorial fins now stroking
' 2. hey a new fish type!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Press SpaceBar to grow new Kelp Bed, press escape to quit
' Press + for more fish, - for less fish
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DIM SHARED fishFactor
, restartFlag
, nFish
, back&
tColor = 0
kPalette
(tColor
) = _RGB32(0, .6 * g
, 0) tColor = tColor + 1
kPalette
(tColor
) = _RGB32(.2 * r
, .6 * g
, 0) tColor = tColor + 1
kPalette
(tColor
) = _RGB32(0, .6 * g
, .1 * r
) tColor = tColor + 1
kPalette
(tColor
) = _RGB32(.2 * r
, .6 * g
, .1 * r
) tColor = tColor + 1
nFish = 64 'for starters
restart:
restartFlag = 0
makeBackground
growKelp
fishFactor = 50 ^ (1 / nFish) 'let's use a power lesson
newFish i, 1
aquarium
ln 0, i, xmax, i
kelps = rand(100, 200)
kelp(rand(0, xmax), ymax) = rand(1, 160)
r = rand(1, 23)
CASE 1, 2, 3, 18 '1 branch node IF x
- 1 >= 0 THEN kelp
(x
- 1, y
) = kelp
(x
, y
+ 1) CASE 4, 5, 6, 7, 8, 9, 21 '1 branch node kelp(x, y) = kelp(x, y + 1)
CASE 10, 11, 12, 20 '1 branch node IF x
+ 1 <= xmax
THEN kelp
(x
+ 1, y
) = kelp
(x
, y
+ 1) CASE 13, 14, 15, 16, 17, 19 '2 branch node IF x
- 1 >= 0 THEN kelp
(x
- 1, y
) = kelp
(x
, y
+ 1) IF x
+ 1 <= xmax
THEN kelp
(x
+ 1, y
) = kelp
(x
, y
+ 1) COLOR kPalette
(kelp
(x
, y
)) frec x, y, x + 1, y + 1
kelps = rand(7, 15)
kelp
(rand
(0, .5 * xmax
), ymax
) = 3 * x
+ x
MOD 4 r = rand(1, 23)
CASE 1, 2, 3, 18 '1 branch node IF x
- 1 >= 0 THEN kelp
(x
- 1, y
) = kelp
(x
, y
+ 1) CASE 4, 5, 6, 7, 8, 9, 21 '1 branch node kelp(x, y) = kelp(x, y + 1)
CASE 10, 11, 12, 20 '1 branch node IF x
+ 1 <= xmax
THEN kelp
(x
+ 1, y
) = kelp
(x
, y
+ 1) CASE 13, 14, 15, 16, 17, 19 '2 branch node IF x
- 1 >= 0 THEN kelp
(x
- 1, y
) = kelp
(x
, y
+ 1) IF x
+ 1 <= xmax
THEN kelp
(x
+ 1, y
) = kelp
(x
, y
+ 1)
dy
= (_PI(y
/ 100) + z
) * (1 - y
/ (.5 * ymax
)) xoff
= swayLimit
* SIN(dy
) 'IF y < RND * .25 * ymax / 1.5 AND RND < .3 THEN xxoff = xoff * RND ELSE xxoff = xoff
frec x * 2 + xoff - 1, y * 2 - 1, x * 2 + xoff + 2, y * 2 + 2
COLOR kPalette
(kelp
(x
, y
)) fcirc x * 2 + xoff, y * 2, 1
'the size and speed of a fish depends upon it's i number
'it only has to be setup if tfStart
IF tfStart
THEN 'starting app place fish anywhere in sight f(i).sz = 10 + fishFactor ^ i
f(i).dx = .2 * f(i).sz
f(i).x = rand(0, xmax)
f(i).stroke = rand(0, 3)
'choose a side to come in from fix x and dx accordingly
IF f
(i
).dx
< 0 THEN f
(i
).dx
= f
(i
).dx
* -1 f(i).x = 0
IF f
(i
).dx
> 0 THEN f
(i
).dx
= f
(i
).dx
* -1 f(i).x = xmax
f(i).stroke = 0
f(i).y = rand(f(i).sz, ymax - f(i).sz)
f
(i
).red
= RND ^ 2: f
(i
).green
= RND ^ 2: f
(i
).blue
= RND ^ 2
f(i).x = f(i).x + f(i).dx
IF f
(i
).x
< 0 - 1.5 * f
(i
).sz
OR f
(i
).x
> xmax
+ 1.5 * f
(i
).sz
THEN newFish i
, 0 f(i).y = f(i).y + rand(-4, 4) * f(i).sz / 60
f
(i
).stroke
= (f
(i
).stroke
+ 1) MOD 4 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
)) 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
frec f(i).x + ra, f(i).y - ra, f(i).x + ra, f(i).y + ra
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
frec f(i).x - ra, f(i).y - ra, f(i).x - ra, f(i).y + ra
FOR ra
= 3 TO .3 * f
(i
).sz
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
)) frec f(i).x + f(i).sz + ra, f(i).y - ra, f(i).x + f(i).sz + ra, f(i).y + ra
frec f(i).x - f(i).sz - ra, f(i).y - ra, f(i).x - f(i).sz - ra, f(i).y + ra
COLOR _RGB32(0, 0, 0): fcirc f
(i
).x
+ .2 * f
(i
).sz
, f
(i
).y
, .09 * f
(i
).sz
COLOR _RGB32(0, 0, 0): fcirc f
(i
).x
- .2 * f
(i
).sz
, f
(i
).y
, .09 * f
(i
).sz
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
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
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
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
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
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
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
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
dz
= .25: z
= 0: hf
= INT(9 * nFish
/ 10) IF 2 * nFish
<= maxFish
THEN nFish
= 2 * nFish: restartFlag
= 1:
EXIT SUB IF nFish
/ 2 >= 2 THEN nFish
= nFish
/ 2: restartFlag
= 1:
EXIT SUB FOR i
= 1 TO hf
'draw some fish behind kelp drawfish (i)
z = z + dz
IF z
> swayLimit
OR z
< -1 * swayLimit
THEN dz
= dz
* -1 showKelp z
FOR i
= hf
+ 1 TO nFish
'draw the rest of the fish drawfish (i)
rand%
= INT(RND * (hi%
- lo%
+ 1)) + lo%
RadiusError = -subRadius
X = subRadius
Y = 0
' Draw the middle span here so we don't draw it twice in the main loop,
' which would be a problem with blending turned on.
LINE (CX
- X
, CY
)-(CX
+ X
, CY
), , BF
RadiusError = RadiusError + Y * 2 + 1
LINE (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), , BF
LINE (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), , BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
LINE (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), , BF
LINE (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), , BF
LINE (x1
, y1
)-(x2
, y2
), , B
SUB frec
(x1
, y1
, x2
, y2
) LINE (x1
, y1
)-(x2
, y2
), , BF
scale = yRadius / xRadius
LINE (CX
, CY
- yRadius
)-(CX
, CY
+ yRadius
), , BF
y
= scale
* SQR(xRadius
* xRadius
- x
* x
) LINE (CX
+ x
, CY
- y
)-(CX
+ x
, CY
+ y
), , BF
LINE (CX
- x
, CY
- y
)-(CX
- x
, CY
+ y
), , BF