Author Topic: Maze Generator  (Read 2943 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Maze Generator
« on: June 09, 2020, 01:00:27 am »
This Maze Generator is much better than the one I made last year, although I don't know if I posted that one. It doesn't automatically make a finished path always but almost all of them do have ways to enter and exit. You can make unlimited random mazes with it and it's your decision on where to enter and where to exit. You can also save them as .bmp pictures and also print them.

Note: Please skip this one if you haven't seen it yet and use the next one below on this forum. This is nowhere near as good.

Code: QB64: [Select]
  1. 'Maze Generator 3 - By SierraKen - June 8, 2020.
  2. _TITLE "Maze Generator 3 - Space Bar To Make Another Maze, (S)ave, (P)rint, Esc ends."
  3. picture& = _NEWIMAGE(800, 600, 32)
  4. start:
  5. _LIMIT 200
  6. s& = _NEWIMAGE(800, 600, 32)
  7. SCREEN s&, picture&
  8. PAINT (1, 1), _RGB32(255, 255, 255)
  9. FOR x = 50 TO 750 STEP 25
  10.     LINE (x, 50)-(x, 550), _RGB32(0, 0, 0)
  11. FOR y = 50 TO 550 STEP 25
  12.     LINE (50, y)-(750, y), _RGB32(0, 0, 0)
  13. FOR tt = 1 TO 100000
  14.     xx = INT(RND * 750) + 50
  15.     yy = INT(RND * 550) + 50
  16.     LINE (xx, yy)-(xx + 25, yy + 25), _RGB32(255, 255, 255), BF
  17.     xx = INT(RND * 750) + 50
  18.     yy = INT(RND * 550) + 50
  19.     LINE (xx, yy)-(xx + 25, yy + 25), _RGB32(255, 255, 255), BF
  20. NEXT tt
  21. _PUTIMAGE , s&, picture&
  22. IF sav = 1 THEN SaveImage 0, nm$: sav = 0
  23.     a$ = INKEY$
  24.     IF a$ = " " THEN GOTO start:
  25.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  26.     IF a$ = CHR$(27) THEN END
  27.     IF a$ = "p" OR a$ = "P" THEN
  28.         j& = _COPYIMAGE(0)
  29.         _DELAY .25
  30.         INPUT "Print on printer (Y/N)?", i$ 'print screen page on printer
  31.         CLS
  32.         SCREEN j&
  33.         _DELAY .25
  34.         IF LEFT$(i$, 1) = "y" OR LEFT$(i$, 1) = "Y" THEN
  35.             'printer prep (code copied and pasted from bplus Free Calendar Program)
  36.             YMAX = _HEIGHT: XMAX = _WIDTH
  37.             landscape& = _NEWIMAGE(YMAX, XMAX, 32)
  38.             _MAPTRIANGLE (XMAX, 0)-(0, 0)-(0, YMAX), 0 TO(0, 0)-(0, XMAX)-(YMAX, XMAX), landscape&
  39.             _MAPTRIANGLE (XMAX, 0)-(XMAX, YMAX)-(0, YMAX), 0 TO(0, 0)-(YMAX, 0)-(YMAX, XMAX), landscape&
  40.             _PRINTIMAGE landscape&
  41.             _DELAY 2
  42.             landscape& = 0
  43.             s& = j&
  44.         END IF
  45.     END IF
  46.  
  47. saving:
  48. PRINT "                       Saving"
  49. PRINT "         Your bmp file will be saved in the"
  50. PRINT "         same directory as this program is."
  51. PRINT "         It can be used with almost any"
  52. PRINT "         other graphics program or website."
  53. PRINT "         It is saved using:"
  54. PRINT "         width: 800  height: 600 pixels."
  55. PRINT "         Type a name to save your picture"
  56. PRINT "         and press the Enter key. Do not"
  57. PRINT "         add .bmp at the end, the program"
  58. PRINT "         will do it automatically."
  59. PRINT "         Example: MyPic"
  60. PRINT "         Quit and Enter key ends program."
  61. INPUT "         ->"; nm$
  62. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  63. nm$ = nm$ + ".bmp"
  64. 'Checking to see if the file already exists on your computer.
  65. theFileExists = _FILEEXISTS(nm$)
  66. IF theFileExists = -1 THEN
  67.     PRINT
  68.     PRINT "       File Already Exists"
  69.     PRINT "       Saving will delete your old"
  70.     PRINT "       bmp picture."
  71.     PRINT "       Would you like to still do it?"
  72.     PRINT "      (Y/N)."
  73.     PRINT "      Esc goes to start screen."
  74.     llloop:
  75.     _LIMIT 100
  76.     ag2$ = INKEY$
  77.     IF ag2$ = CHR$(27) THEN GOTO start:
  78.     IF ag2$ = "" THEN GOTO llloop:
  79.     IF ag2$ = "y" OR ag$ = "Y" THEN
  80.         SHELL _HIDE "DEL " + nm$
  81.         GOTO saving2:
  82.     END IF
  83.     GOTO llloop:
  84. saving2:
  85. sav = 1
  86. FOR snd = 100 TO 700 STEP 100
  87.     SOUND snd, 2
  88. NEXT snd
  89. GOTO start:
  90.  
  91. 'Here is the SUB needed to save the image to BMP.
  92. SUB SaveImage (image AS LONG, filename AS STRING)
  93.     bytesperpixel& = _PIXELSIZE(image&)
  94.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  95.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  96.     x& = _WIDTH(image&)
  97.     y& = _HEIGHT(image&)
  98.     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)
  99.     IF bytesperpixel& = 1 THEN
  100.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  101.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  102.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  103.         NEXT
  104.     END IF
  105.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  106.     lastsource& = _SOURCE
  107.     _SOURCE image&
  108.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  109.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  110.         r$ = ""
  111.         FOR px& = 0 TO x& - 1
  112.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  113.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  114.         NEXT px&
  115.         d$ = d$ + r$ + padder$
  116.     NEXT py&
  117.     _SOURCE lastsource&
  118.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  119.     b$ = b$ + d$ ' total file data bytes to create file
  120.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  121.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  122.     f& = FREEFILE
  123.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  124.     OPEN filename$ + ext$ FOR BINARY AS #f&
  125.     PUT #f&, , b$
  126.     CLOSE #f&
  127.  
« Last Edit: June 09, 2020, 08:52:09 pm by SierraKen »

Offline Virtusoroca

  • Newbie
  • Posts: 24
    • View Profile
Re: Maze Generator
« Reply #1 on: June 09, 2020, 03:28:38 am »
I was just working in some maze algorithms! Definetly gona check this one :D

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #2 on: June 09, 2020, 11:42:27 am »
I am amazed how Ken finds simple work-a-rounds to usually more complex processes, this one is good case in point.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #3 on: June 09, 2020, 11:47:15 am »
:) Thanks Bplus. When I don't have the math skills or programming experience, it's pretty much all I can do. lol But originality is what makes inventions too so that's what keeps me chugging along. :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #4 on: June 09, 2020, 11:48:39 am »
Virtusoroca, I would love to see your algorithms. Mine has none, just a grid and then random places it erases. I figure that if someone doesn't like the maze, they can keep pressing the Space Bar until they find one they like.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #5 on: June 09, 2020, 08:48:00 pm »
I spent all afternoon again on a new Maze Generator that is much better. All, or almost all of them are solvable and this one looks more like a maze than the others I've made. It doesn't calculate a solved path but what it does is first it makes neat looking random walls. Then it randomly opens up more walls so it can be solved. The funny thing is, I spent a lot more time on a different version that I had actually try to "crawl" around from bottom to top making paths, but that turned out to be a mess. lol I've read some of a Wikipedia page about making mazes with algorithms but that is a bit beyond my comprehension. So I hope you all enjoy this. Put it in its own folder so you can save them as .bmp pictures and look at anytime. This program can also print them on your printer. Tell me what you think. I made it look hard with many walls, but the mazes itself are fairly easy to finish. Start at any opening and end at any other opening. I suggest going from bottom to top. I included an example maze as an attachment. Oh also, if you send any maze made with this to someone else, or put online, please make sure it's solvable first, thanks. You can quickly make a new one by pressing the Space Bar. 

Code: QB64: [Select]
  1. 'Maze Generator 5 - By SierraKen - June 9, 2020.
  2. 'This Maze Generator does not calculate a solvable path to take but all or almost all of them are solvable.
  3. 'So if you give one of these maze pictures to someone else, please make sure you can solve it first.
  4. 'You can save them as .bmp pictures and/or print them on your printer.
  5. 'Freeware.
  6.  
  7. _TITLE "Maze Generator 5 - Space Bar To Make Another Maze, (S)ave, (P)rint, Esc ends."
  8. picture& = _NEWIMAGE(800, 600, 32)
  9. start:
  10. _LIMIT 200
  11. s& = _NEWIMAGE(800, 600, 32)
  12. SCREEN s&, picture&
  13. PAINT (1, 1), _RGB32(255, 255, 255)
  14. LINE (50, 50)-(725, 575), _RGB32(0, 0, 0), B
  15. FOR x = 50 TO 710 STEP 10
  16.     FOR y = 50 TO 560 STEP 10
  17.         RANDOMIZE TIMER
  18.         d = INT(RND * 2) + 1
  19.         IF d = 1 THEN GOTO skip:
  20.         LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0)
  21.         GOTO skip2:
  22.         skip:
  23.         LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  24.         skip2:
  25.     NEXT y
  26. yy = 550
  27. again:
  28. xx = INT(RND * 675) + 75
  29. xx2 = INT(RND * 675) + 75
  30. IF xx / 10 <> INT(xx / 10) THEN GOTO again:
  31. LINE (xx, 575)-(xx + 10, 575), _RGB32(255, 255, 255)
  32. LINE (xx2, 575)-(xx2 + 10, 575), _RGB32(255, 255, 255)
  33. tt = INT(RND * 250) + 375
  34. tt2 = INT(RND * 250) + 375
  35. FOR xx3 = 1 TO tt
  36.     more:
  37.     xx4 = INT(RND * 650) + 50
  38.     yy4 = INT(RND * 500) + 50
  39.     IF xx4 / 10 <> INT(xx4 / 10) OR yy4 / 10 <> INT(yy4 / 10) THEN GOTO more:
  40.     LINE (xx4, yy4)-(xx4 + 10, yy4), _RGB32(255, 255, 255)
  41. NEXT xx3
  42. FOR xx3 = 1 TO tt2
  43.     more2:
  44.     xx5 = INT(RND * 650) + 50
  45.     yy5 = INT(RND * 500) + 50
  46.     IF xx5 / 10 <> INT(xx5 / 10) OR yy5 / 10 <> INT(yy5 / 10) THEN GOTO more2:
  47.     LINE (xx5, yy5)-(xx5, yy5 + 10), _RGB32(255, 255, 255)
  48. NEXT xx3
  49.  
  50. _PUTIMAGE , s&, picture&
  51. IF sav = 1 THEN SaveImage 0, nm$: sav = 0
  52.     a$ = INKEY$
  53.     IF a$ = " " THEN GOTO start:
  54.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  55.     IF a$ = CHR$(27) THEN END
  56.     IF a$ = "p" OR a$ = "P" THEN
  57.         j& = _COPYIMAGE(0)
  58.         _DELAY .25
  59.         INPUT "Print on printer (Y/N)?", i$ 'print screen page on printer
  60.         CLS
  61.         SCREEN j&
  62.         _DELAY .25
  63.         IF LEFT$(i$, 1) = "y" OR LEFT$(i$, 1) = "Y" THEN
  64.             'printer prep (code copied and pasted from bplus Free Calendar Program)
  65.             YMAX = _HEIGHT: XMAX = _WIDTH
  66.             landscape& = _NEWIMAGE(YMAX, XMAX, 32)
  67.             _MAPTRIANGLE (XMAX, 0)-(0, 0)-(0, YMAX), 0 TO(0, 0)-(0, XMAX)-(YMAX, XMAX), landscape&
  68.             _MAPTRIANGLE (XMAX, 0)-(XMAX, YMAX)-(0, YMAX), 0 TO(0, 0)-(YMAX, 0)-(YMAX, XMAX), landscape&
  69.             _PRINTIMAGE landscape&
  70.             _DELAY 2
  71.             landscape& = 0
  72.             s& = j&
  73.         END IF
  74.     END IF
  75.  
  76. saving:
  77. PRINT "                       Saving"
  78. PRINT "         Your bmp file will be saved in the"
  79. PRINT "         same directory as this program is."
  80. PRINT "         It can be used with almost any"
  81. PRINT "         other graphics program or website."
  82. PRINT "         It is saved using:"
  83. PRINT "         width: 800  height: 600 pixels."
  84. PRINT "         Type a name to save your picture"
  85. PRINT "         and press the Enter key. Do not"
  86. PRINT "         add .bmp at the end, the program"
  87. PRINT "         will do it automatically."
  88. PRINT "         Example: MyPic"
  89. PRINT "         Quit and Enter key ends program."
  90. INPUT "         ->"; nm$
  91. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  92. nm$ = nm$ + ".bmp"
  93. 'Checking to see if the file already exists on your computer.
  94. theFileExists = _FILEEXISTS(nm$)
  95. IF theFileExists = -1 THEN
  96.     PRINT
  97.     PRINT "       File Already Exists"
  98.     PRINT "       Saving will delete your old"
  99.     PRINT "       bmp picture."
  100.     PRINT "       Would you like to still do it?"
  101.     PRINT "      (Y/N)."
  102.     PRINT "      Esc goes to start screen."
  103.     llloop:
  104.     _LIMIT 100
  105.     ag2$ = INKEY$
  106.     IF ag2$ = CHR$(27) THEN GOTO start:
  107.     IF ag2$ = "" THEN GOTO llloop:
  108.     IF ag2$ = "y" OR ag$ = "Y" THEN
  109.         SHELL _HIDE "DEL " + nm$
  110.         GOTO saving2:
  111.     END IF
  112.     GOTO llloop:
  113. saving2:
  114. sav = 1
  115. FOR snd = 100 TO 700 STEP 100
  116.     SOUND snd, 2
  117. NEXT snd
  118. GOTO start:
  119.  
  120. 'Here is the SUB needed to save the image to BMP.
  121. SUB SaveImage (image AS LONG, filename AS STRING)
  122.     bytesperpixel& = _PIXELSIZE(image&)
  123.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  124.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  125.     x& = _WIDTH(image&)
  126.     y& = _HEIGHT(image&)
  127.     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)
  128.     IF bytesperpixel& = 1 THEN
  129.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  130.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  131.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  132.         NEXT
  133.     END IF
  134.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  135.     lastsource& = _SOURCE
  136.     _SOURCE image&
  137.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  138.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  139.         r$ = ""
  140.         FOR px& = 0 TO x& - 1
  141.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  142.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  143.         NEXT px&
  144.         d$ = d$ + r$ + padder$
  145.     NEXT py&
  146.     _SOURCE lastsource&
  147.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  148.     b$ = b$ + d$ ' total file data bytes to create file
  149.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  150.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  151.     f& = FREEFILE
  152.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  153.     OPEN filename$ + ext$ FOR BINARY AS #f&
  154.     PUT #f&, , b$
  155.     CLOSE #f&
  156.  






* MazeExample.bmp (Filesize: 1.37 MB, Dimensions: 800x600, Views: 448)
« Last Edit: June 09, 2020, 08:56:21 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #6 on: June 09, 2020, 09:02:39 pm »
Wow looking good.

Here is a tip:
Code: QB64: [Select]
  1.         d = INT(RND * 2) + 1
  2.         IF d = 1 THEN GOTO skip:
  3.         LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0)
  4.         GOTO skip2:
  5.         skip:
  6.         LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  7.         skip2:


is this:
Code: QB64: [Select]
  1. d = INT(RND * 2) + 1
  2. IF d = 1 THEN
  3.     LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0)
  4.     LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  5.  
  6. 'or just this
  7.  
  8. d = INT(RND * 2) + 1
  9. IF d = 1 THEN LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0) ELSE LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  10.  

Oh try making lines a pixel longer to be rid of those tiny gaps. I assume you will be using POINT to detect wall collision in games?
« Last Edit: June 09, 2020, 09:14:07 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #7 on: June 09, 2020, 09:13:49 pm »
Thanks Bplus! I changed it to what you said, it sure reduces some lines, thank you. I also fixed 2 minor problems in that the deleted vertical walls were making 1 pixel holes on the horizontal walls so I fixed that. I also made the maze line up in the center of the page (and bmp file and paper printout) better.
Here is the better version. Also included is another attachment maze.

Code: QB64: [Select]
  1. 'Maze Generator 5 - By SierraKen - June 9, 2020.
  2. 'This Maze Generator does not calculate a solvable path to take but all or almost all of them are solvable.
  3. 'So if you give one of these maze pictures to someone else, please make sure you can solve it first.
  4. 'You can save them as .bmp pictures and/or print them on your printer.
  5. 'Freeware.
  6.  
  7. _TITLE "Maze Generator 5 - Space Bar To Make Another Maze, (S)ave, (P)rint, Esc ends."
  8. picture& = _NEWIMAGE(800, 600, 32)
  9. start:
  10. _LIMIT 200
  11. s& = _NEWIMAGE(800, 600, 32)
  12. SCREEN s&, picture&
  13. PAINT (1, 1), _RGB32(255, 255, 255)
  14. LINE (50, 50)-(750, 575), _RGB32(0, 0, 0), B
  15. FOR x = 50 TO 740 STEP 10
  16.     FOR y = 50 TO 560 STEP 10
  17.         RANDOMIZE TIMER
  18.         d = INT(RND * 2) + 1
  19.         IF d = 1 THEN LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0) ELSE LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  20.     NEXT y
  21. yy = 550
  22. again:
  23. xx = INT(RND * 675) + 75
  24. xx2 = INT(RND * 675) + 75
  25. IF xx / 10 <> INT(xx / 10) THEN GOTO again:
  26. LINE (xx, 575)-(xx + 10, 575), _RGB32(255, 255, 255)
  27. LINE (xx2, 575)-(xx2 + 10, 575), _RGB32(255, 255, 255)
  28. tt = INT(RND * 250) + 375
  29. tt2 = INT(RND * 250) + 375
  30. FOR xx3 = 1 TO tt
  31.     more:
  32.     xx4 = INT(RND * 700) + 50
  33.     yy4 = INT(RND * 500) + 50
  34.     IF xx4 / 10 <> INT(xx4 / 10) OR yy4 / 10 <> INT(yy4 / 10) THEN GOTO more:
  35.     LINE (xx4 + 1, yy4)-(xx4 + 9, yy4), _RGB32(255, 255, 255)
  36. NEXT xx3
  37. FOR xx3 = 1 TO tt2
  38.     more2:
  39.     xx5 = INT(RND * 700) + 50
  40.     yy5 = INT(RND * 500) + 50
  41.     IF xx5 / 10 <> INT(xx5 / 10) OR yy5 / 10 <> INT(yy5 / 10) THEN GOTO more2:
  42.     LINE (xx5, yy5 + 1)-(xx5, yy5 + 9), _RGB32(255, 255, 255)
  43. NEXT xx3
  44.  
  45. _PUTIMAGE , s&, picture&
  46. IF sav = 1 THEN SaveImage 0, nm$: sav = 0
  47.     a$ = INKEY$
  48.     IF a$ = " " THEN GOTO start:
  49.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  50.     IF a$ = CHR$(27) THEN END
  51.     IF a$ = "p" OR a$ = "P" THEN
  52.         j& = _COPYIMAGE(0)
  53.         _DELAY .25
  54.         INPUT "Print on printer (Y/N)?", i$ 'print screen page on printer
  55.         CLS
  56.         SCREEN j&
  57.         _DELAY .25
  58.         IF LEFT$(i$, 1) = "y" OR LEFT$(i$, 1) = "Y" THEN
  59.             'printer prep (code copied and pasted from bplus Free Calendar Program)
  60.             YMAX = _HEIGHT: XMAX = _WIDTH
  61.             landscape& = _NEWIMAGE(YMAX, XMAX, 32)
  62.             _MAPTRIANGLE (XMAX, 0)-(0, 0)-(0, YMAX), 0 TO(0, 0)-(0, XMAX)-(YMAX, XMAX), landscape&
  63.             _MAPTRIANGLE (XMAX, 0)-(XMAX, YMAX)-(0, YMAX), 0 TO(0, 0)-(YMAX, 0)-(YMAX, XMAX), landscape&
  64.             _PRINTIMAGE landscape&
  65.             _DELAY 2
  66.             landscape& = 0
  67.             s& = j&
  68.         END IF
  69.     END IF
  70.  
  71. saving:
  72. PRINT "                       Saving"
  73. PRINT "         Your bmp file will be saved in the"
  74. PRINT "         same directory as this program is."
  75. PRINT "         It can be used with almost any"
  76. PRINT "         other graphics program or website."
  77. PRINT "         It is saved using:"
  78. PRINT "         width: 800  height: 600 pixels."
  79. PRINT "         Type a name to save your picture"
  80. PRINT "         and press the Enter key. Do not"
  81. PRINT "         add .bmp at the end, the program"
  82. PRINT "         will do it automatically."
  83. PRINT "         Example: MyPic"
  84. PRINT "         Quit and Enter key ends program."
  85. INPUT "         ->"; nm$
  86. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  87. nm$ = nm$ + ".bmp"
  88. 'Checking to see if the file already exists on your computer.
  89. theFileExists = _FILEEXISTS(nm$)
  90. IF theFileExists = -1 THEN
  91.     PRINT
  92.     PRINT "       File Already Exists"
  93.     PRINT "       Saving will delete your old"
  94.     PRINT "       bmp picture."
  95.     PRINT "       Would you like to still do it?"
  96.     PRINT "      (Y/N)."
  97.     PRINT "      Esc goes to start screen."
  98.     llloop:
  99.     _LIMIT 100
  100.     ag2$ = INKEY$
  101.     IF ag2$ = CHR$(27) THEN GOTO start:
  102.     IF ag2$ = "" THEN GOTO llloop:
  103.     IF ag2$ = "y" OR ag$ = "Y" THEN
  104.         SHELL _HIDE "DEL " + nm$
  105.         GOTO saving2:
  106.     END IF
  107.     GOTO llloop:
  108. saving2:
  109. sav = 1
  110. FOR snd = 100 TO 700 STEP 100
  111.     SOUND snd, 2
  112. NEXT snd
  113. GOTO start:
  114.  
  115. 'Here is the SUB needed to save the image to BMP.
  116. SUB SaveImage (image AS LONG, filename AS STRING)
  117.     bytesperpixel& = _PIXELSIZE(image&)
  118.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  119.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  120.     x& = _WIDTH(image&)
  121.     y& = _HEIGHT(image&)
  122.     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)
  123.     IF bytesperpixel& = 1 THEN
  124.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  125.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  126.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  127.         NEXT
  128.     END IF
  129.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  130.     lastsource& = _SOURCE
  131.     _SOURCE image&
  132.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  133.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  134.         r$ = ""
  135.         FOR px& = 0 TO x& - 1
  136.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  137.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  138.         NEXT px&
  139.         d$ = d$ + r$ + padder$
  140.     NEXT py&
  141.     _SOURCE lastsource&
  142.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  143.     b$ = b$ + d$ ' total file data bytes to create file
  144.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  145.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  146.     f& = FREEFILE
  147.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  148.     OPEN filename$ + ext$ FOR BINARY AS #f&
  149.     PUT #f&, , b$
  150.     CLOSE #f&
  151.  

* MazeExample3.bmp (Filesize: 1.37 MB, Dimensions: 800x600, Views: 421)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #8 on: June 09, 2020, 09:16:59 pm »
Huh? where are dots coming from?

Oh another tip, a repeated one, RANDOMIZE TIMER only needed once at the start of your code, really :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #9 on: June 09, 2020, 09:21:36 pm »
Oh I bet you missed reading this:
Quote
Oh try making lines a pixel longer to be rid of those tiny gaps. I assume you will be using POINT to detect wall collision in games?

I added it on in edit, that might get rid of the dots.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #10 on: June 09, 2020, 09:25:23 pm »
Making the maze, I make a lot of random walls first. Then I delete a bunch of random ones. Deleting vertical ones were too long of white which were making 1 pixel holes on the horizontal ones, so I just reduced the size of the white ones. Yeah, I've always had a bad habit of making many Randomize Timers. I guess I thought that as the program went on, it needed to be more random LOL. I just took your advice and removed them and put one up near the top. Thanks, I'll try to remember that.

Code: QB64: [Select]
  1. 'Maze Generator 5 - By SierraKen - June 9, 2020.
  2. 'This Maze Generator does not calculate a solvable path to take but all or almost all of them are solvable.
  3. 'So if you give one of these maze pictures to someone else, please make sure you can solve it first.
  4. 'You can save them as .bmp pictures and/or print them on your printer.
  5. 'Freeware.
  6.  
  7. _TITLE "Maze Generator 5 - Space Bar To Make Another Maze, (S)ave, (P)rint, Esc ends."
  8. picture& = _NEWIMAGE(800, 600, 32)
  9. start:
  10. _LIMIT 200
  11. s& = _NEWIMAGE(800, 600, 32)
  12. SCREEN s&, picture&
  13. PAINT (1, 1), _RGB32(255, 255, 255)
  14. LINE (50, 50)-(750, 575), _RGB32(0, 0, 0), B
  15. FOR x = 50 TO 740 STEP 10
  16.     FOR y = 50 TO 560 STEP 10
  17.         d = INT(RND * 2) + 1
  18.         IF d = 1 THEN LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0) ELSE LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  19.     NEXT y
  20. yy = 550
  21. again:
  22. xx = INT(RND * 675) + 75
  23. xx2 = INT(RND * 675) + 75
  24. IF xx / 10 <> INT(xx / 10) THEN GOTO again:
  25. LINE (xx, 575)-(xx + 10, 575), _RGB32(255, 255, 255)
  26. LINE (xx2, 575)-(xx2 + 10, 575), _RGB32(255, 255, 255)
  27. tt = INT(RND * 250) + 375
  28. tt2 = INT(RND * 250) + 375
  29. FOR xx3 = 1 TO tt
  30.     more:
  31.     xx4 = INT(RND * 700) + 50
  32.     yy4 = INT(RND * 500) + 50
  33.     IF xx4 / 10 <> INT(xx4 / 10) OR yy4 / 10 <> INT(yy4 / 10) THEN GOTO more:
  34.     LINE (xx4 + 1, yy4)-(xx4 + 9, yy4), _RGB32(255, 255, 255)
  35. NEXT xx3
  36. FOR xx3 = 1 TO tt2
  37.     more2:
  38.     xx5 = INT(RND * 700) + 50
  39.     yy5 = INT(RND * 500) + 50
  40.     IF xx5 / 10 <> INT(xx5 / 10) OR yy5 / 10 <> INT(yy5 / 10) THEN GOTO more2:
  41.     LINE (xx5, yy5 + 1)-(xx5, yy5 + 9), _RGB32(255, 255, 255)
  42. NEXT xx3
  43.  
  44. _PUTIMAGE , s&, picture&
  45. IF sav = 1 THEN SaveImage 0, nm$: sav = 0
  46.     a$ = INKEY$
  47.     IF a$ = " " THEN GOTO start:
  48.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  49.     IF a$ = CHR$(27) THEN END
  50.     IF a$ = "p" OR a$ = "P" THEN
  51.         j& = _COPYIMAGE(0)
  52.         _DELAY .25
  53.         INPUT "Print on printer (Y/N)?", i$ 'print screen page on printer
  54.         CLS
  55.         SCREEN j&
  56.         _DELAY .25
  57.         IF LEFT$(i$, 1) = "y" OR LEFT$(i$, 1) = "Y" THEN
  58.             'printer prep (code copied and pasted from bplus Free Calendar Program)
  59.             YMAX = _HEIGHT: XMAX = _WIDTH
  60.             landscape& = _NEWIMAGE(YMAX, XMAX, 32)
  61.             _MAPTRIANGLE (XMAX, 0)-(0, 0)-(0, YMAX), 0 TO(0, 0)-(0, XMAX)-(YMAX, XMAX), landscape&
  62.             _MAPTRIANGLE (XMAX, 0)-(XMAX, YMAX)-(0, YMAX), 0 TO(0, 0)-(YMAX, 0)-(YMAX, XMAX), landscape&
  63.             _PRINTIMAGE landscape&
  64.             _DELAY 2
  65.             landscape& = 0
  66.             s& = j&
  67.         END IF
  68.     END IF
  69.  
  70. saving:
  71. PRINT "                       Saving"
  72. PRINT "         Your bmp file will be saved in the"
  73. PRINT "         same directory as this program is."
  74. PRINT "         It can be used with almost any"
  75. PRINT "         other graphics program or website."
  76. PRINT "         It is saved using:"
  77. PRINT "         width: 800  height: 600 pixels."
  78. PRINT "         Type a name to save your picture"
  79. PRINT "         and press the Enter key. Do not"
  80. PRINT "         add .bmp at the end, the program"
  81. PRINT "         will do it automatically."
  82. PRINT "         Example: MyPic"
  83. PRINT "         Quit and Enter key ends program."
  84. INPUT "         ->"; nm$
  85. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  86. nm$ = nm$ + ".bmp"
  87. 'Checking to see if the file already exists on your computer.
  88. theFileExists = _FILEEXISTS(nm$)
  89. IF theFileExists = -1 THEN
  90.     PRINT
  91.     PRINT "       File Already Exists"
  92.     PRINT "       Saving will delete your old"
  93.     PRINT "       bmp picture."
  94.     PRINT "       Would you like to still do it?"
  95.     PRINT "      (Y/N)."
  96.     PRINT "      Esc goes to start screen."
  97.     llloop:
  98.     _LIMIT 100
  99.     ag2$ = INKEY$
  100.     IF ag2$ = CHR$(27) THEN GOTO start:
  101.     IF ag2$ = "" THEN GOTO llloop:
  102.     IF ag2$ = "y" OR ag$ = "Y" THEN
  103.         SHELL _HIDE "DEL " + nm$
  104.         GOTO saving2:
  105.     END IF
  106.     GOTO llloop:
  107. saving2:
  108. sav = 1
  109. FOR snd = 100 TO 700 STEP 100
  110.     SOUND snd, 2
  111. NEXT snd
  112. GOTO start:
  113.  
  114. 'Here is the SUB needed to save the image to BMP.
  115. SUB SaveImage (image AS LONG, filename AS STRING)
  116.     bytesperpixel& = _PIXELSIZE(image&)
  117.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  118.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  119.     x& = _WIDTH(image&)
  120.     y& = _HEIGHT(image&)
  121.     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)
  122.     IF bytesperpixel& = 1 THEN
  123.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  124.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  125.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  126.         NEXT
  127.     END IF
  128.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  129.     lastsource& = _SOURCE
  130.     _SOURCE image&
  131.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  132.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  133.         r$ = ""
  134.         FOR px& = 0 TO x& - 1
  135.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  136.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  137.         NEXT px&
  138.         d$ = d$ + r$ + padder$
  139.     NEXT py&
  140.     _SOURCE lastsource&
  141.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  142.     b$ = b$ + d$ ' total file data bytes to create file
  143.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  144.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  145.     f& = FREEFILE
  146.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  147.     OPEN filename$ + ext$ FOR BINARY AS #f&
  148.     PUT #f&, , b$
  149.     CLOSE #f&
  150.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #11 on: June 09, 2020, 09:55:18 pm »
Oh hey, I was walking dog and realized making wall lines longer wasn't going to work.

So you already figured to make wall erasers shorter good! But that still leaves us with the dots when walls erased all the way around.

There is a way to make a dot eraser: check every place walls intersect and use point() 1 pixel above&below, 1 pixel left&right of those places, if they are all white then make the center location white.

Offline Virtusoroca

  • Newbie
  • Posts: 24
    • View Profile
Re: Maze Generator
« Reply #12 on: June 09, 2020, 09:57:51 pm »
@SierraKen um trying to code the backtrack algorithm. I have little training in math but quite a bit in data arrays. If yoi want to give it a try, look at this video:
. The guy works in C but the best part is in the logical steps. As soon as I finsh my experiment I get back to you.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Maze Generator
« Reply #13 on: June 09, 2020, 10:20:36 pm »
Oh man I ran into that guy explaining 3D code engines, 3D something or other.

Heck I can give you the algorithm, I have already posted with notes on using for Ken, but Ken sorta has to do things his own way :)

https://www.qb64.org/forum/index.php?topic=1680.msg109160#msg109160
« Last Edit: June 09, 2020, 10:25:51 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Maze Generator
« Reply #14 on: June 09, 2020, 10:42:52 pm »
Thanks :) Here's another update. I now have it delete the dots for this version.
I'll check out what you posted before Bplus. But for now here is this one.

Code: QB64: [Select]
  1. 'Maze Generator 5 - By SierraKen - June 9, 2020.
  2. 'This Maze Generator does not calculate a solvable path to take but all or almost all of them are solvable.
  3. 'So if you give one of these maze pictures to someone else, please make sure you can solve it first.
  4. 'You can save them as .bmp pictures and/or print them on your printer.
  5. 'Freeware.
  6.  
  7. _TITLE "Maze Generator 5 - Space Bar To Make Another Maze, (S)ave, (P)rint, Esc ends."
  8. picture& = _NEWIMAGE(800, 600, 32)
  9. start:
  10. _LIMIT 200
  11. s& = _NEWIMAGE(800, 600, 32)
  12. SCREEN s&, picture&
  13. PAINT (1, 1), _RGB32(255, 255, 255)
  14. LINE (50, 50)-(750, 575), _RGB32(0, 0, 0), B
  15. FOR x = 50 TO 740 STEP 10
  16.     FOR y = 50 TO 560 STEP 10
  17.         d = INT(RND * 2) + 1
  18.         IF d = 1 THEN LINE (x, y)-(x + 10, y), _RGB32(0, 0, 0) ELSE LINE (x, y)-(x, y + 10), _RGB32(0, 0, 0)
  19.     NEXT y
  20. yy = 550
  21. again:
  22. xx = INT(RND * 675) + 75
  23. xx2 = INT(RND * 675) + 75
  24. IF xx / 10 <> INT(xx / 10) THEN GOTO again:
  25. LINE (xx, 575)-(xx + 10, 575), _RGB32(255, 255, 255)
  26. LINE (xx2, 575)-(xx2 + 10, 575), _RGB32(255, 255, 255)
  27. tt = INT(RND * 250) + 375
  28. tt2 = INT(RND * 250) + 375
  29. FOR xx3 = 1 TO tt
  30.     more:
  31.     xx4 = INT(RND * 700) + 50
  32.     yy4 = INT(RND * 500) + 50
  33.     IF xx4 / 10 <> INT(xx4 / 10) OR yy4 / 10 <> INT(yy4 / 10) THEN GOTO more:
  34.     LINE (xx4 + 1, yy4)-(xx4 + 9, yy4), _RGB32(255, 255, 255)
  35. NEXT xx3
  36. FOR xx3 = 1 TO tt2
  37.     more2:
  38.     xx5 = INT(RND * 700) + 50
  39.     yy5 = INT(RND * 500) + 50
  40.     IF xx5 / 10 <> INT(xx5 / 10) OR yy5 / 10 <> INT(yy5 / 10) THEN GOTO more2:
  41.     LINE (xx5, yy5 + 1)-(xx5, yy5 + 9), _RGB32(255, 255, 255)
  42. NEXT xx3
  43. FOR checkx = 50 TO 750
  44.     FOR checky = 50 TO 575
  45.         IF POINT(checkx, checky) = _RGB32(0, 0, 0) AND POINT(checkx + 1, checky) = _RGB32(255, 255, 255) AND POINT(checkx, checky + 1) = _RGB32(255, 255, 255) AND POINT(checkx - 1, checky) = _RGB32(255, 255, 255) AND POINT(checkx, checky - 1) = _RGB32(255, 255, 255) THEN PSET (checkx, checky), _RGB32(255, 255, 255)
  46.     NEXT checky
  47. NEXT checkx
  48.  
  49.  
  50. _PUTIMAGE , s&, picture&
  51. IF sav = 1 THEN SaveImage 0, nm$: sav = 0
  52.     a$ = INKEY$
  53.     IF a$ = " " THEN GOTO start:
  54.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  55.     IF a$ = CHR$(27) THEN END
  56.     IF a$ = "p" OR a$ = "P" THEN
  57.         j& = _COPYIMAGE(0)
  58.         _DELAY .25
  59.         INPUT "Print on printer (Y/N)?", i$ 'print screen page on printer
  60.         CLS
  61.         SCREEN j&
  62.         _DELAY .25
  63.         IF LEFT$(i$, 1) = "y" OR LEFT$(i$, 1) = "Y" THEN
  64.             'printer prep (code copied and pasted from bplus Free Calendar Program)
  65.             YMAX = _HEIGHT: XMAX = _WIDTH
  66.             landscape& = _NEWIMAGE(YMAX, XMAX, 32)
  67.             _MAPTRIANGLE (XMAX, 0)-(0, 0)-(0, YMAX), 0 TO(0, 0)-(0, XMAX)-(YMAX, XMAX), landscape&
  68.             _MAPTRIANGLE (XMAX, 0)-(XMAX, YMAX)-(0, YMAX), 0 TO(0, 0)-(YMAX, 0)-(YMAX, XMAX), landscape&
  69.             _PRINTIMAGE landscape&
  70.             _DELAY 2
  71.             landscape& = 0
  72.             s& = j&
  73.         END IF
  74.     END IF
  75.  
  76. saving:
  77. PRINT "                       Saving"
  78. PRINT "         Your bmp file will be saved in the"
  79. PRINT "         same directory as this program is."
  80. PRINT "         It can be used with almost any"
  81. PRINT "         other graphics program or website."
  82. PRINT "         It is saved using:"
  83. PRINT "         width: 800  height: 600 pixels."
  84. PRINT "         Type a name to save your picture"
  85. PRINT "         and press the Enter key. Do not"
  86. PRINT "         add .bmp at the end, the program"
  87. PRINT "         will do it automatically."
  88. PRINT "         Example: MyPic"
  89. PRINT "         Quit and Enter key ends program."
  90. INPUT "         ->"; nm$
  91. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  92. nm$ = nm$ + ".bmp"
  93. 'Checking to see if the file already exists on your computer.
  94. theFileExists = _FILEEXISTS(nm$)
  95. IF theFileExists = -1 THEN
  96.     PRINT
  97.     PRINT "       File Already Exists"
  98.     PRINT "       Saving will delete your old"
  99.     PRINT "       bmp picture."
  100.     PRINT "       Would you like to still do it?"
  101.     PRINT "      (Y/N)."
  102.     PRINT "      Esc goes to start screen."
  103.     llloop:
  104.     _LIMIT 100
  105.     ag2$ = INKEY$
  106.     IF ag2$ = CHR$(27) THEN GOTO start:
  107.     IF ag2$ = "" THEN GOTO llloop:
  108.     IF ag2$ = "y" OR ag$ = "Y" THEN
  109.         SHELL _HIDE "DEL " + nm$
  110.         GOTO saving2:
  111.     END IF
  112.     GOTO llloop:
  113. saving2:
  114. sav = 1
  115. FOR snd = 100 TO 700 STEP 100
  116.     SOUND snd, 2
  117. NEXT snd
  118. GOTO start:
  119.  
  120. 'Here is the SUB needed to save the image to BMP.
  121. SUB SaveImage (image AS LONG, filename AS STRING)
  122.     bytesperpixel& = _PIXELSIZE(image&)
  123.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  124.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  125.     x& = _WIDTH(image&)
  126.     y& = _HEIGHT(image&)
  127.     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)
  128.     IF bytesperpixel& = 1 THEN
  129.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  130.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  131.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  132.         NEXT
  133.     END IF
  134.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  135.     lastsource& = _SOURCE
  136.     _SOURCE image&
  137.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  138.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  139.         r$ = ""
  140.         FOR px& = 0 TO x& - 1
  141.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  142.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  143.         NEXT px&
  144.         d$ = d$ + r$ + padder$
  145.     NEXT py&
  146.     _SOURCE lastsource&
  147.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  148.     b$ = b$ + d$ ' total file data bytes to create file
  149.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  150.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  151.     f& = FREEFILE
  152.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  153.     OPEN filename$ + ext$ FOR BINARY AS #f&
  154.     PUT #f&, , b$
  155.     CLOSE #f&
  156.