From 6105b88f89788173a54aced83892680a0d62cc0e Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 21 Aug 2024 08:23:44 +0300 Subject: [PATCH] Using AI to improve code readability --- Graphics/Animations/matrix4.bas | 341 ++++++++++++++++++-------------- Graphics/Animations/movdna.bas | 61 +++--- Graphics/Animations/ssaver.bas | 57 ++++-- Graphics/Animations/ssaver2.bas | 83 +++++--- Graphics/Animations/strange.bas | 68 ++++--- 5 files changed, 367 insertions(+), 243 deletions(-) diff --git a/Graphics/Animations/matrix4.bas b/Graphics/Animations/matrix4.bas index bc29cdf..ecf14b9 100755 --- a/Graphics/Animations/matrix4.bas +++ b/Graphics/Animations/matrix4.bas @@ -1,180 +1,209 @@ -' by Svjatoslav Agejenko svjatoslav@svjatoslav.eu -' 2003.04 - -DECLARE FUNCTION getc% () -DECLARE SUB mks () -DEFINT A-Y -DECLARE SUB disp () -DECLARE SUB shpal () -DECLARE SUB start () - -DIM SHARED buf(1 TO 40, 1 TO 25) AS INTEGER -DIM SHARED col(1 TO 40, 1 TO 25) AS INTEGER -DIM SHARED snd(1 TO 20) -DIM SHARED sndp - -start -'shpal - -FOR y = 1 TO 25 - FOR x = 1 TO 40 - buf(x, y) = getc - NEXT x -NEXT y - -FOR y = 1 TO 25 - FOR x = 1 TO 40 - col(x, y) = 1 - NEXT x -NEXT y - -act = 0 -1 -mks -frm = frm + 1 -IF frm > 10000 THEN frm = 1 -FOR y = 25 TO 2 STEP -1 - FOR x = 1 TO 40 - buf(x, y) = buf(x, y - 1) - NEXT x -NEXT y -mks -FOR x = 1 TO 40 - buf(x, 1) = buf(x, 25) -NEXT x -buf(RND * 39 + 1, RND * 10 + 1) = getc -act = act + 1 -disp -SELECT CASE act +' Program to render animation inspired by Matrix movie. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.04, Initial version +' 2024.08, Improved program readability using AI + + +DECLARE FUNCTION getCharacter% () +DECLARE SUB makeSound () +DEFINT A-Z +DECLARE SUB displayScreen () +DECLARE SUB showPalette () +DECLARE SUB initializeGame () + +DIM SHARED screenBuffer(1 TO 40, 1 TO 25) AS INTEGER +DIM SHARED colorBuffer(1 TO 40, 1 TO 25) AS INTEGER +DIM SHARED soundArray(1 TO 20) +DIM SHARED soundPointer + +initializeGame +'showPalette() + +' Initialize the screen buffer with random characters +FOR row = 1 TO 25 + FOR col = 1 TO 40 + screenBuffer(col, row) = getCharacter + NEXT col +NEXT row + +' Initialize the color buffer with a default color +FOR row = 1 TO 25 + FOR col = 1 TO 40 + colorBuffer(col, row) = 1 + NEXT col +NEXT row + +actionCounter = 0 + +10 ' Main game loop +makeSound +frameCounter = frameCounter + 1 +IF frameCounter > 10000 THEN frameCounter = 1 + +' Shift the screen buffer contents down by one row +FOR row = 25 TO 2 STEP -1 + FOR col = 1 TO 40 + screenBuffer(col, row) = screenBuffer(col, row - 1) + NEXT col +NEXT row +makeSound + +' Move the top row to the bottom +FOR col = 1 TO 40 + screenBuffer(col, 1) = screenBuffer(col, 25) +NEXT col + +' Randomly change a character in the buffer +screenBuffer(INT(RND * 39 + 1), INT(RND * 10 + 1)) = getCharacter +actionCounter = actionCounter + 1 +displayScreen + +SELECT CASE actionCounter CASE 1 - FOR a = 1 TO 20 - snd(a) = 0 - IF RND * 100 < 2 THEN snd(a) = RND * 4000 + 4000 - NEXT a - b = SIN(frm / 100) * 3 + 6 - FOR a = 1 TO 20 STEP b - snd(a) = 10000 - NEXT a + ' Initialize sound array with random values + FOR a = 1 TO 20 + soundArray(a) = 0 + IF RND * 100 < 2 THEN soundArray(a) = INT(RND * 4000 + 4000) + NEXT a + ' Set sound frequencies based on sine wave + b = SIN(frameCounter / 100) * 3 + 6 + FOR a = 1 TO 20 STEP b + soundArray(a) = 10000 + NEXT a CASE 2 - c = RND * 5 - x1 = RND * 38 + 1 - y = RND * 23 + 1 - x2 = RND * 38 + 1 - IF x1 > x2 THEN SWAP x1, x2 - FOR x = x1 TO x2 - col(x, y) = c - NEXT x + ' Draw a horizontal line with random color + c = INT(RND * 5) + x1 = INT(RND * 38 + 1) + y = INT(RND * 23 + 1) + x2 = INT(RND * 38 + 1) + IF x1 > x2 THEN SWAP x1, x2 + FOR x = x1 TO x2 + colorBuffer(x, y) = c + NEXT x + CASE 3 - c = RND * 5 - y1 = RND * 23 + 1 - x = RND * 38 + 1 - y2 = RND * 23 + 1 - IF y1 > y2 THEN SWAP x1, x2 - FOR y = y1 TO y2 - col(x, y) = c - NEXT y + ' Draw a vertical line with random color + c = INT(RND * 5) + y1 = INT(RND * 23 + 1) + x = INT(RND * 38 + 1) + y2 = INT(RND * 23 + 1) + IF y1 > y2 THEN SWAP y1, y2 + FOR y = y1 TO y2 + colorBuffer(x, y) = c + NEXT y + CASE 4 - IF RND * 100 < 20 THEN - FOR y = 1 TO 25 - FOR x = 1 TO 40 - IF col(x, y) > 1 THEN col(x, y) = col(x, y) - 1 - NEXT x - NEXT y - END IF + ' Decrease the intensity of colors on the screen + IF RND * 100 < 20 THEN + FOR y = 1 TO 25 + FOR x = 1 TO 40 + IF colorBuffer(x, y) > 1 THEN colorBuffer(x, y) = colorBuffer(x, y) - 1 + NEXT x + NEXT y + END IF + CASE 5 - IF RND * 100 < 5 THEN - FOR y = 1 TO 25 STEP 2 - FOR x = 1 TO 40 - col(x, y) = 1 - NEXT x - NEXT y - END IF + ' Reset every second row to default color + IF RND * 100 < 5 THEN + FOR y = 1 TO 25 STEP 2 + FOR x = 1 TO 40 + colorBuffer(x, y) = 1 + NEXT x + NEXT y + END IF + CASE 6 - IF RND * 100 < 5 THEN - FOR x = 1 TO 40 STEP 2 - FOR y = 1 TO 25 - col(x, y) = 1 - NEXT y - NEXT x - END IF + ' Reset every second column to default color + IF RND * 100 < 5 THEN + FOR x = 1 TO 40 STEP 2 + FOR y = 1 TO 25 + colorBuffer(x, y) = 1 + NEXT y + NEXT x + END IF + CASE 7 - FOR a = 1 TO 30 - col(RND * 39 + 1, RND * 23 + 1) = RND * 4 + 1 - NEXT a + ' Randomly set some characters to random colors + FOR a = 1 TO 30 + colorBuffer(INT(RND * 39 + 1), INT(RND * 23 + 1)) = INT(RND * 4 + 1) + NEXT a + CASE 8 - IF INKEY$ <> "" THEN SYSTEM - act = 0 + ' Check for user input to exit the game + IF INKEY$ <> "" THEN SYSTEM + actionCounter = 0 END SELECT -GOTO 1 -SYSTEM +GOTO 10 + +SYSTEM: +' Exit the game +END -SUB disp +SUB displayScreen -mks +makeSound LOCATE 1, 1 + +' Display the top half of the screen FOR y = 1 TO 10 -FOR x = 1 TO 40 -COLOR col(x, y), 0 -PRINT CHR$(buf(x, y)); -NEXT x + FOR x = 1 TO 40 + COLOR colorBuffer(x, y), 0 + PRINT CHR$(screenBuffer(x, y)); + NEXT x NEXT y -mks + +makeSound +' Display the middle part of the screen FOR y = 11 TO 20 -FOR x = 1 TO 40 -COLOR col(x, y), 0 -PRINT CHR$(buf(x, y)); -NEXT x + FOR x = 1 TO 40 + COLOR colorBuffer(x, y), 0 + PRINT CHR$(screenBuffer(x, y)); + NEXT x NEXT y -mks + +makeSound +' Display the bottom half of the screen FOR y = 21 TO 25 -FOR x = 1 TO 40 -COLOR col(x, y), 0 -PRINT CHR$(buf(x, y)); -NEXT x + FOR x = 1 TO 40 + COLOR colorBuffer(x, y), 0 + PRINT CHR$(screenBuffer(x, y)); + NEXT x NEXT y -mks + +makeSound END SUB -FUNCTION getc +FUNCTION getCharacter +' Generate a random character based on probability IF RND * 100 > 50 THEN - a = RND * 9 + 48 + getCharacter = INT(RND * 9 + 48) ELSE - a = RND * 25 + 65 + getCharacter = INT(RND * 25 + 65) END IF -IF RND * 100 < 15 THEN a = 32 -getc = a +IF RND * 100 < 15 THEN getCharacter = 32 ' 15% chance to return a space END FUNCTION -SUB mks -sndp = sndp + 1 -IF sndp > 20 THEN sndp = 1 -SOUND snd(sndp), .07 -'SOUND 0, .07 -END SUB - -SUB shpal - -FOR a = 0 TO 16 -COLOR a -PRINT a, "Palette test" -NEXT a -a$ = INPUT$(1) -END SUB +SUB initializeGame -SUB start -RANDOMIZE TIMER -CLS -WIDTH 40, 25 -VIEW PRINT 1 TO 25 +RANDOMIZE TIMER ' Seed the random number generator +CLS ' Clear the screen +WIDTH 40, 25 ' Set screen dimensions +VIEW PRINT 1 TO 25 ' Set the print area - OUT &H3C8, 0 - OUT &H3C9, 0 - OUT &H3C9, 0 - OUT &H3C9, 0 +' Initialize palette registers for color text mode +OUT &H3C8, 0 +OUT &H3C9, 0 +OUT &H3C9, 0 +OUT &H3C9, 0 +' Set up color palettes FOR a = 1 TO 5 OUT &H3C8, a @@ -182,6 +211,7 @@ FOR a = 1 TO 5 g = a * 10 + 20 r = a * 0 + ' Ensure RGB values do not exceed the maximum value IF r > 63 THEN r = 63 IF g > 63 THEN g = 63 IF b > 63 THEN b = 63 @@ -192,3 +222,22 @@ NEXT a END SUB +SUB makeSound +' Update the sound pointer and play a sound +soundPointer = soundPointer + 1 +IF soundPointer > 20 THEN soundPointer = 1 +SOUND soundArray(soundPointer), .07 +' SOUND 0, .07 ' Uncomment to play a continuous tone +END SUB + +SUB showPalette + +' Display a palette test on the screen +FOR a = 0 TO 15 + COLOR a + PRINT a; " Palette test" +NEXT a +a$ = INPUT$(1) ' Wait for user input + +END SUB + diff --git a/Graphics/Animations/movdna.bas b/Graphics/Animations/movdna.bas index 51d02be..70c45d6 100755 --- a/Graphics/Animations/movdna.bas +++ b/Graphics/Animations/movdna.bas @@ -1,4 +1,8 @@ -' Svjatoslav Agejenko +' Program to render animated DNA. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' DEFINT A-Y @@ -9,42 +13,41 @@ DIM SHARED cc(1 TO 100) SCREEN 7, , , 1 f = 0 -1 + +1: b = 0 -zf = zf + .1 +zf = zf + 0.1 FOR a = 1 TO 20 - -b = b + 1 -cx(b) = SIN(a / 2 + zf) * 30 + 150 -cz(b) = SIN(a / 2 + zf + 1.6) * 2 + 2 -cy(b) = a * 8 + cz(b) -cc(b) = 3 -b = b + 1 -cx(b) = SIN(a / 2 + zf + 2.5) * 30 + 150 -cz(b) = SIN(a / 2 + zf + 1.6 + 2.5) * 2 + 2 -cy(b) = a * 8 + cz(b) -cc(b) = 4 + b = b + 1 + cx(b) = SIN(a / 2 + zf) * 30 + 150 + cz(b) = SIN(a / 2 + zf + 1.6) * 2 + 2 + cy(b) = a * 8 + cz(b) + cc(b) = 3 + + b = b + 1 + cx(b) = SIN(a / 2 + zf + 2.5) * 30 + 150 + cz(b) = SIN(a / 2 + zf + 1.6 + 2.5) * 2 + 2 + cy(b) = a * 8 + cz(b) + cc(b) = 4 NEXT a CLS FOR b = 0 TO 4 - -IF b = 1 THEN -FOR a = 1 TO 40 STEP 2 -LINE (cx(a), cy(a))-(cx(a + 1), cy(a + 1)), 15 -NEXT a -END IF - -FOR a = 1 TO 40 -IF cz(a) = b THEN -CIRCLE (cx(a), cy(a)), b + 5, cc(a) -PAINT (cx(a), cy(a)), cc(a) -CIRCLE (cx(a), cy(a)), b + 5, 0 -END IF -NEXT a + IF b = 1 THEN + FOR a = 1 TO 40 STEP 2 + LINE (cx(a), cy(a))-(cx(a + 1), cy(a + 1)), 15 + NEXT a + END IF + + FOR a = 1 TO 40 + IF cz(a) = b THEN + CIRCLE (cx(a), cy(a)), b + 5, cc(a) + PAINT (cx(a), cy(a)), cc(a) + CIRCLE (cx(a), cy(a)), b + 5, 0 + END IF + NEXT a NEXT b PCOPY 0, 1 CLS IF INKEY$ = "" THEN GOTO 1 SYSTEM - diff --git a/Graphics/Animations/ssaver.bas b/Graphics/Animations/ssaver.bas index 4d8daaf..aca7239 100755 --- a/Graphics/Animations/ssaver.bas +++ b/Graphics/Animations/ssaver.bas @@ -1,28 +1,45 @@ ' Mystery screensaver -' made by Svjatoslav Agejenko -' last edit 2004.01 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2004.01, Initial version +' 2024.08, Improved program readability using AI + SCREEN 7, , , 1 +' Main loop for animation +DO + ' Increment frame counter + frameCounter = frameCounter + 1 + + ' Calculate scaling factor based on frame counter + scaleFactor = (SIN(frameCounter / 100) + 1.1) * 2 + + ' Draw lines to create animation effect + FOR s = 1 TO 20 STEP .1 + ' Calculate x and y coordinates for the first point + x = SIN(s / 1 + frameCounter / 7) * 100 + y = COS(s / 1 + frameCounter / 10) * 100 + + ' Calculate x and y coordinates for the second point + x1 = SIN(s / 1 - frameCounter / 8) * 100 + y1 = COS(s / 1 + frameCounter / 15) * 100 -1 -frm = frm + 1 + ' Draw a line between the two points with varying thickness + LINE (x + 160, y + 100)-(x1 + 160, y1 + 100), s MOD 15 + NEXT s -f = (SIN(frm / 100) + 1.1) * 2 -FOR s = 1 TO 20 STEP .1 - x = SIN(s / 1 + frm / 7) * 100 - y = COS(s / 1 + frm / 10) * 100 - x1 = SIN(s / 1 - frm / 8) * 100 - y1 = COS(s / 1 + frm / 15) * 100 - LINE (x + 160, y + 100)-(x1 + 160, y1 + 100), s MOD 15 -NEXT s + ' Copy screen buffer to display the animation + PCOPY 0, 1 + ' Generate a sound effect + SOUND 0, 1 -PCOPY 0, 1 -SOUND 0, 1 -CLS -IF INKEY$ <> "" THEN SYSTEM -GOTO 1 + ' Clear the screen for the next frame + CLS + ' Check if a key is pressed and exit if so + IF INKEY$ <> "" THEN SYSTEM +LOOP diff --git a/Graphics/Animations/ssaver2.bas b/Graphics/Animations/ssaver2.bas index 5cd885b..ac8cac9 100755 --- a/Graphics/Animations/ssaver2.bas +++ b/Graphics/Animations/ssaver2.bas @@ -1,31 +1,62 @@ -' Svjatoslav Agejenko 2003.04 +' Screensaver +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.04, Initial version +' 2024.08, Improved program readability using AI + SCREEN 7, , , 1 -1 -IF frm > 10000 THEN frm = -10000 -FOR c = 1 TO 6 - OUT &H3C8, c - OUT &H3C9, SIN(c + frm * 3) * 30 + 31 - OUT &H3C9, COS(c * 1 + frm * 5) * 30 + 31 - OUT &H3C9, SIN(c * .7 + frm * 2.23) * 30 + 31 -NEXT c - -frm = frm + .01 - -FOR b = 1 TO 10 - c = (b MOD 6) + 1 - x = SIN(b + frm) * 100 + 150 - y = COS(b * 1.2 + frm * 1.81) * 80 + 100 - xs = SIN(b * frm * 2.3) - FOR xp = -50 TO 50 STEP 10 - ys = COS(xp / 60 + frm * 1 + b) * 50 - LINE (x, y)-(x + xp * xs, y - ys), c - NEXT xp -NEXT b -PCOPY 0, 1 -CLS -SOUND 0, .4 -IF INKEY$ <> "" THEN SYSTEM +' Main animation loop +1 : + ' Adjust frame counter if it exceeds a certain threshold + IF frameCounter > 10000 THEN frameCounter = -10000 + + ' Update the positions of six points based on the frame counter + FOR pointIndex = 1 TO 6 + OUT &H3C8, pointIndex + OUT &H3C9, SIN(pointIndex + frameCounter * 3) * 30 + 31 + OUT &H3C9, COS(pointIndex * 1 + frameCounter * 5) * 30 + 31 + OUT &H3C9, SIN(pointIndex * .7 + frameCounter * 2.23) * 30 + 31 + NEXT pointIndex + + ' Increment the frame counter for the next iteration + frameCounter = frameCounter + .01 + + ' Render lines connecting points + FOR objectIndex = 1 TO 10 + ' Determine the color based on the remainder of objectIndex divided by 6 + pointColor = (objectIndex MOD 6) + 1 + ' Calculate the X coordinate for the starting point of the line + xCoordinate = SIN(objectIndex + frameCounter) * 100 + 150 + ' Calculate the Y coordinate for the starting point of the line + yCoordinate = COS(objectIndex * 1.2 + frameCounter * 1.81) * 80 + 100 + ' Calculate the sine component for the line's angle + xSineComponent = SIN(objectIndex * frameCounter * 2.3) + ' Draw lines from the starting point with varying lengths and angles + FOR xPositionOffset = -50 TO 50 STEP 10 + ' Calculate the cosine component for the line's angle based on xPositionOffset + yCosineComponent = COS(xPositionOffset / 60 + frameCounter * 1 + objectIndex) * 50 + ' Draw a line segment with varying thickness and color + LINE (xCoordinate, yCoordinate)-(xCoordinate + xPositionOffset * xSineComponent, yCoordinate - yCosineComponent), pointColor + NEXT xPositionOffset + NEXT objectIndex + + ' Copy the graphics from the hidden page to the visible page + PCOPY 0, 1 + + ' Clear the screen for the next frame + CLS + + ' Play a sound at a specific frequency and duration + SOUND 0, .4 + + ' Check if any key is pressed; if so, exit the program + IF INKEY$ <> "" THEN SYSTEM + + ' Loop back to the start of the animation GOTO 1 diff --git a/Graphics/Animations/strange.bas b/Graphics/Animations/strange.bas index 6d7f2bd..abdfebb 100755 --- a/Graphics/Animations/strange.bas +++ b/Graphics/Animations/strange.bas @@ -1,27 +1,51 @@ -' Svjatoslav Agejenko 2000 +' Yin and yang animation +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2000, Initial version +' 2024.08, Improved program readability using AI -DECLARE SUB cir (x!, y!, r!, c!) +DECLARE SUB Cir (x!, y!, r!, c!) SCREEN 13 pi = 3.1415926# PAINT (1, 1), 1 -1 -x = SIN(a) * 40 + 160 -x1 = SIN(a + pi) * 40 + 160 -y = COS(a) * 34 + 100 -y1 = COS(a + pi) * 34 + 100 -cir x, y, 40, 0 -cir x1, y1, 40, 1 -a = a + .05 -IF INKEY$ <> "" THEN SYSTEM -GOTO 1 - -SUB cir (x, y, r, c) -cc1 = 0 -cc2 = 15 -IF c = 1 THEN SWAP cc1, cc2 -FOR a = 1 TO r -IF a < r / 2 THEN c1 = cc1 ELSE c1 = cc2 -CIRCLE (x, y), a, c1 -NEXT a -END SUB +' Main animation loop +DO + ' Calculate the x and y positions for both circles using sine and cosine functions + x = SIN(a) * 40 + 160 + x1 = SIN(a + pi) * 40 + 160 + y = COS(a) * 34 + 100 + y1 = COS(a + pi) * 34 + 100 + + ' Draw the first circle with color 0 (black) + Cir x, y, 40, 0 + ' Draw the second circle with color 1 (blue) + Cir x1, y1, 40, 1 + + ' Increment the angle to animate the circles + a = a + .05 + + ' Check for user input to exit the program + IF INKEY$ <> "" THEN SYSTEM +LOOP + +' Subroutine to draw a circle with specified center (x, y), radius r, and color c +SUB Cir (x, y, r, c) + ' Define colors for the circle outline + cc1 = 0 ' Black + cc2 = 15 ' White + + ' Swap colors if the second color is desired for the inner part of the circle + IF c = 1 THEN SWAP cc1, cc2 + + ' Draw the circle from radius 1 to r + FOR a = 1 TO r + ' Determine the color for the current circle segment + IF a < r / 2 THEN c1 = cc1 ELSE c1 = cc2 + ' Draw the circle segment with the determined color + CIRCLE (x, y), a, c1 + NEXT a +END SUB -- 2.20.1