Author Topic: Kaleidoscope  (Read 3762 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Kaleidoscope
« on: December 16, 2019, 04:22:08 pm »
Today I was experimenting more with graphics and equations and figured out a way to make a kaleidoscope. I know I made one back in October which is more of funky, dance floor looking lights. Which can be found here if you are interested. It is called Black Hole. https://www.qb64.org/forum/index.php?topic=1756.msg110029#msg110029

But here is the much better one I made today. On this one you use the Mouse Wheel to change patterns. There's unlimited amount of different patterns and it will display the number of it in the Title bar. It starts at 0 so you can go into the negative numbers or positive, it doesn't matter. You also can save anything to .bmp by pressing S if you wish. The colors also change automatically without doing anything so if you want to save an image, do it quickly before it changes, or wait until something else catches your eye. Tell me what you think, thanks.

I included 3 pictures.

Code: QB64: [Select]
  1. start:
  2. ka& = _NEWIMAGE(600, 600, 32)
  3. SCREEN ka&
  4. _DEST ka&
  5. c1 = 50: c2 = 10: c3 = 50
  6. s = RND * 10000
  7.  
  8.     _LIMIT 500
  9.     clr = INT(RND * 300) + 1
  10.     IF clr = 1 THEN c1 = c1 + 25
  11.     IF c1 > 255 THEN c1 = 50
  12.     IF clr = 2 THEN c3 = c3 + 50
  13.     IF c3 > 255 THEN c3 = 0
  14.     IF clr > 298 THEN c3 = c1: c2 = c1
  15.     IF c3 > 255 THEN c3 = 5
  16.  
  17.  
  18.         mouseX = _MOUSEX
  19.         mouseY = _MOUSEY
  20.         mouseLeftButton = _MOUSEBUTTON(1)
  21.         mouseRightButton = _MOUSEBUTTON(2)
  22.         mouseMiddleButton = _MOUSEBUTTON(3)
  23.         mouseWheel = mouseWheel + _MOUSEWHEEL
  24.     LOOP
  25.  
  26.     s = 0
  27.     FOR m = 1 TO 3
  28.         RANDOMIZE TIMER
  29.         mx = (RND * 4) - 2
  30.         my = (RND * 4) - 2
  31.         FOR d = 320 TO 0 STEP -.125
  32.             k = mouseWheel
  33.             s = s + k
  34.             x = COS(s * 3.141592 / 180) * d
  35.             x = x + mx
  36.             y = SIN(s * 3.151492 / 180) * d
  37.             y = y + my
  38.             PSET (x + 300, y + 300), _RGB32(c1, c2, c3)
  39.         NEXT d
  40.     NEXT m
  41.     a$ = INKEY$
  42.     IF a$ = CHR$(27) THEN END
  43.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  44.     k$ = STR$(k)
  45.     _TITLE "Use Mouse Wheel To Change - Pattern " + k$ + "   Press S to save picture. Esc to end."
  46.  
  47. 'The rest of this is code to save a picture.
  48. '-------------------------------------------
  49. saving:
  50. ka2& = _COPYIMAGE(ka&)
  51.  
  52. 'Saving
  53. _TITLE "Saving Picture"
  54. _DELAY .25
  55. PRINT "                                  Saving"
  56. PRINT "                     Your bmp file will be saved in the"
  57. PRINT "                     same directory as this program is."
  58. PRINT "                     It can be used with almost any"
  59. PRINT "                     other graphics program or website."
  60. PRINT "                     It is saved using:"
  61. PRINT "                     width: 600  height: 600 pixels."
  62. PRINT "                     Type a name to save your picture"
  63. PRINT "                     and press the Enter key. Do not"
  64. PRINT "                     add .bmp at the end, the program"
  65. PRINT "                     will do it automatically."
  66. PRINT "                     Example: MyPic"
  67. PRINT "                     Quit and Enter key ends program."
  68. INPUT "                     ->"; nm$
  69. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  70. nm$ = nm$ + ".bmp"
  71. 'Checking to see if the file already exists on your computer.
  72. theFileExists = _FILEEXISTS(nm$)
  73. IF theFileExists = -1 THEN
  74.     PRINT
  75.     PRINT "                File Already Exists"
  76.     PRINT "                Saving will delete your old"
  77.     PRINT "                bmp picture."
  78.     PRINT "                Would you like to still do it?"
  79.     PRINT "                (Y/N)."
  80.     PRINT "               Esc goes to start screen."
  81.     llloop:
  82.     _LIMIT 500
  83.     ag2$ = INKEY$
  84.     IF ag2$ = CHR$(27) THEN GOTO start:
  85.     IF ag2$ = "" THEN GOTO llloop:
  86.     IF ag2$ = "y" OR ag$ = "Y" THEN
  87.         SHELL _HIDE "DEL " + nm$
  88.         GOTO saving2:
  89.     END IF
  90.     GOTO llloop:
  91. saving2:
  92. SCREEN ka2&
  93. SaveImage 0, nm$
  94. FOR snd = 100 TO 800 STEP 50
  95.     SOUND snd, 1
  96. NEXT snd
  97. nm$ = ""
  98. ka& = 0
  99. ka2& = 0
  100. GOTO start:
  101.  
  102.  
  103. 'This section saves the .bmp picture file.
  104. SUB SaveImage (image AS LONG, filename AS STRING)
  105.     bytesperpixel& = _PIXELSIZE(image&)
  106.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  107.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  108.     x& = _WIDTH(image&)
  109.     y& = _HEIGHT(image&)
  110.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  111.     IF bytesperpixel& = 1 THEN
  112.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  113.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  114.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  115.         NEXT
  116.     END IF
  117.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  118.     lastsource& = _SOURCE
  119.     _SOURCE image&
  120.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  121.     FOR py& = y& - 1 TO 0 STEP -1 ' read image pixel color data
  122.         r$ = ""
  123.         FOR px& = 0 TO x& - 1
  124.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  125.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  126.         NEXT px&
  127.         d$ = d$ + r$ + padder$
  128.     NEXT py&
  129.     _SOURCE lastsource&
  130.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  131.     b$ = b$ + d$ ' total file data bytes to create file
  132.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  133.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  134.     f& = FREEFILE
  135.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  136.     OPEN filename$ + ext$ FOR BINARY AS #f&
  137.     PUT #f&, , b$
  138.     CLOSE #f&
  139.  
K1.bmp
* K1.bmp (Filesize: 1.03 MB, Dimensions: 600x600, Views: 281)
K3.bmp
* K3.bmp (Filesize: 1.03 MB, Dimensions: 600x600, Views: 269)
K4.bmp
* K4.bmp (Filesize: 1.03 MB, Dimensions: 600x600, Views: 297)
« Last Edit: December 16, 2019, 07:58:39 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Kaleidoscope
« Reply #1 on: December 16, 2019, 04:59:02 pm »
Ken make sure to tell people to use the scroll wheel on mouse, took me awhile to figure that out.
(Update: Oh he did do that! Another great reading job by bplus.)

How did you get the pixels to look like little squares?
« Last Edit: December 16, 2019, 07:25:15 pm by bplus »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Kaleidoscope
« Reply #2 on: December 16, 2019, 06:31:06 pm »
That's amazing, I saved it and opened it in GIMP. Nice job.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Kaleidoscope
« Reply #3 on: December 16, 2019, 07:56:07 pm »
Thanks guys!

Bplus yeah I actually did forget to add that in the TITLE so users would know to use the mouse wheel, so I just did.
It's this code (I think) that makes the squares:

        RANDOMIZE TIMER
        mx = (RND * 4) - 2
        my = (RND * 4) - 2

and then it's added to the pixel as it loops.