From a69c683f8d4cb973a0fb443fe1313e6ca7863969 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Fri, 19 Apr 2024 00:51:23 +0300 Subject: [PATCH] Modernized code for better readability --- graphics/3D/stars.bas | 246 +++++++++++++++++++++--------------------- 1 file changed, 121 insertions(+), 125 deletions(-) diff --git a/graphics/3D/stars.bas b/graphics/3D/stars.bas index 7feee8a..dbd0701 100755 --- a/graphics/3D/stars.bas +++ b/graphics/3D/stars.bas @@ -1,141 +1,137 @@ -' 3D starfield -' made by Svjatoslav Agejenko -' in 2003.03 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu +' 3D Starfield Simulation +' Originally made by Svjatoslav Agejenko in 2003.03 +' In 2024 code was modernized using artificial intelligence +' Homepage: svjatoslav.eu +' Email: svjatoslav@svjatoslav.eu -DECLARE SUB setstar (x2!, y2!, z2!) -DECLARE SUB galaxy () -Dim Shared mitu -Dim Shared mituv +DECLARE SUB AddStar (xPosition AS SINGLE, yPosition AS SINGLE, zPosition AS SINGLE) +DECLARE SUB CreateGalaxy () +Dim Shared totalStars As Integer +Dim Shared maxStars As Integer Randomize Timer -mituv = 2000 -mitu = mituv -rns = 500 -wl = 0 +maxStars = 2000 +totalStars = maxStars +starFieldDepth = 500 + +Dim Shared starXPositions(1 To maxStars + 1000) As Single +Dim Shared starYPositions(1 To maxStars + 1000) As Single +Dim Shared starZPositions(1 To maxStars + 1000) As Single + +' Initialize the positions of the stars +For starIndex = 1 To totalStars + starZPositions(starIndex) = Rnd * starFieldDepth + 20 + angle = Rnd * 100 + starXPositions(starIndex) = Sin(angle) * 20 + starYPositions(starIndex) = Cos(angle) * 20 +Next starIndex -Dim Shared px(1 To mitu + 1000) -Dim Shared py(1 To mitu + 1000) -Dim Shared pz(1 To mitu + 1000) +Screen 13 -For a = 1 To mitu - pz(a) = Rnd * 500 + 20 - n = Rnd * 100 - px(a) = Sin(n) * 20 - py(a) = Cos(n) * 20 -Next a +Do + + ' Calculate the camera's rotation and position offsets + frameCount = frameCount + 1 + cameraRotation = (3.1412 / 2) + Sin(frameCount / 35) / 100 + Sin(frameCount / 21) / 100 + rs1 = Sin(cameraRotation) + rc1 = Cos(cameraRotation) + + ' Update and draw each star + For starIndex = 1 To totalStars + x = starXPositions(starIndex) + y = starYPositions(starIndex) + z = starZPositions(starIndex) + + ' Project the star's 3D position onto the 2D screen + projectedX = (x / z) * 160 + 160 + projectedY = (y / z) * 100 + 100 + PSet (projectedX, projectedY), 0 ' Erase the previous position + + ' Rotate the star's position around the camera + x5 = x * rs1 - y * rc1 + y5 = x * rc1 + y * rs1 + + ' Update the star's position with camera movement + x = x5 + Sin(frameCount / 21) * 3 + y = y5 + Sin(frameCount / 18) * 3 + + ' Move the star closer to the viewer and wrap around if too close + z = z - 3 + If z < 10 Then + z = Rnd * 300 + 400 + x = Rnd * 800 - 400 + y = Rnd * 800 - 400 + End If + + ' Project the new position and draw with perspective-based brightness + projectedX = (x / z) * 160 + 160 + projectedY = (y / z) * 100 + 100 + colorCode = 3000 / z + 15 + If colorCode > 31 Then colorCode = 31 + PSet (projectedX, projectedY), colorCode + + ' Update the star's array positions + starXPositions(starIndex) = x + starYPositions(starIndex) = y + starZPositions(starIndex) = z + Next starIndex + + ' Add new stars to the galaxy if needed + If maxStars - totalStars > Rnd * 800 + 100 Then CreateGalaxy: totalStars = totalStars + 1 + + ' Remove the two farthest stars and replace them with new ones + For a = 1 To 2 + starIndex = Int(Rnd * (totalStars - 10)) + 1 + Swap starXPositions(totalStars), starXPositions(starIndex) + Swap starYPositions(totalStars), starYPositions(starIndex) + Swap starZPositions(totalStars), starZPositions(starIndex) + + x = starXPositions(totalStars) + y = starYPositions(totalStars) + z = starZPositions(totalStars) + projectedX = (x / z) * 160 + 160 + projectedY = (y / z) * 100 + 100 + PSet (projectedX, projectedY), 0 ' Erase the star + totalStars = totalStars - 1 + Next a -Screen 13 + ' Check for user input to exit the program + If InKey$ <> "" Then System -frm = 10 -1 -fps = fps + 1 -If tm$ <> Time$ Then - 'LOCATE 1, 1 - 'PRINT fps - If fps > 20 Then wl = wl + 2 Else wl = wl - 1 - If wl < 0 Then wl = 0 - fps = 0 - tm$ = Time$ -End If -frm = frm + 1 -xp = Sin(frm / 21) * 3 -yp = Sin(frm / 18) * 3 - -nrk = (3.1412) / 2 + Sin(frm / 35) / 100 + Sin(frm / 21) / 100 -rs1 = Sin(nrk) -rc1 = Cos(nrk) - -For a = 1 To mitu - x = px(a) - y = py(a) - z = pz(a) - x1 = x / z * 160 + 160 - y1 = y / z * 100 + 100 - PSet (x1, y1), 0 - - x5 = x * rs1 - y * rc1 - y5 = x * rc1 + y * rs1 - - x = x5 - y = y5 - - z = z - 3 - x = x + xp - y = y + yp - If z < 10 Then - z = Rnd * 300 + 400 - x = Rnd * 800 - 400 - y = Rnd * 800 - 400 - End If - - x1 = x / z * 160 + 160 - y1 = y / z * 100 + 100 - c = 3000 / z + 15 - If c > 31 Then c = 31 - PSet (x1, y1), c - - px(a) = x - py(a) = y - pz(a) = z -Next a - - -If mituv - mitu > rns Then galaxy: rns = Rnd * 800 + 100 - -For a = 1 To 2 - b = Rnd * (mitu - 10) + 1 - Swap px(mitu), px(b) - Swap py(mitu), py(b) - Swap pz(mitu), pz(b) - - x = px(mitu) - y = py(mitu) - z = pz(mitu) - x1 = x / z * 160 + 160 - y1 = y / z * 100 + 100 - PSet (x1, y1), 0 - mitu = mitu - 1 -Next a - -'LOCATE 2, 1 -'PRINT wl -For a = 0 To wl - For b = 0 To 1000 - c = c / 100 - Next b -Next a - -If InKey$ <> "" Then System -sound 0,1 -GoTo 1 - -Sub galaxy - - xf = Rnd * 4 - 2 - yf = Rnd * 4 - 2 - xp = Rnd * 200 - 100 - yp = Rnd * 200 - 100 - - For a = 1 To Rnd * 15 + 10 Step .04 - x = Sin(a) * a * a / 10 - y = Cos(a) * a * a / 10 - setstar x + Rnd * a * a / 30 + xp, y + Rnd * a * a / 30 + yp, 700 + Rnd * a * a / 30 + (x * xf) + (y * yf) - Next a + ' sleep, to limit framerate + Sound 0, 1 +Loop + +' Subroutine to create a new galaxy of stars +Sub CreateGalaxy + xForce = Rnd * 4 - 2 + yForce = Rnd * 4 - 2 + xPositionOffset = Rnd * 200 - 100 + yPositionOffset = Rnd * 200 - 100 + + ' Add a new set of stars with varying positions and velocities + For starIndex = 1 To Int(Rnd * 15) + 10 Step .04 + x = Sin(starIndex) * starIndex * starIndex / 10 + y = Cos(starIndex) * starIndex * starIndex / 10 + AddStar x + RND * starIndex * starIndex / 30 + xPositionOffset, _ + y + RND * starIndex * starIndex / 30 + yPositionOffset, _ + 700 + RND * starIndex * starIndex / 30 + (x * xForce) + (y * yForce) + Next starIndex - 'SOUND 1000, 1 + ' Play a sound when creating new stars (commented out) + ' SOUND 1000, 1 End Sub -Sub setstar (x2, y2, z2) - mitu = mitu + 1 - s = mitu +' Subroutine to add a new star at the specified position +Sub AddStar (xPosition As Single, yPosition As Single, zPosition As Single) + totalStars = totalStars + 1 + starIndex = totalStars - px(s) = x2 - py(s) = y2 - pz(s) = z2 + starXPositions(starIndex) = xPosition + starYPositions(starIndex) = yPosition + starZPositions(starIndex) = zPosition End Sub -- 2.20.1