Author Topic: Random Scene Generator  (Read 3808 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Random Scene Generator
« on: December 20, 2019, 10:18:24 pm »
This makes random hills, random grass texture on the land, and a random creek coming from the hills to you. I used radians to make the hills this time instead of full circles like I did before. I also used radians to make the creek. :) Plus I added the .bmp save code that I originally got from the Wiki pages to save the pictures. The directions are on the TITLE bar. That _PUTIMAGE took me awhile again to do exactly right to capture the picture. But hopefully I'm learning. It took around 20 tests to finally get it.

Edit: I just added a picture of what it can make.

Code: QB64: [Select]
  1. 'Made on Dec. 20, 2019 by Ken G.
  2. _TITLE "Scene Generator - by Ken G. - Space Bar for new scene, (S)ave Picture, Esc to quit."
  3. start:
  4. picture& = _NEWIMAGE(800, 600, 32)
  5. SCREEN picture&
  6. 'Draw sky.
  7. sky = INT(RND * 154) + 100
  8. FOR sk = 0 TO 299
  9.     sky = sky + .25
  10.     IF sky > 255 THEN sky = 255
  11.     LINE (0, sk)-(800, sk), _RGB32(0, 0, sky)
  12. NEXT sk
  13. 'Draw ground.
  14. cc2 = INT(RND * 50) + 20
  15. FOR cc = 300 TO 600
  16.     cc2 = cc2 + .5
  17.     LINE (0, cc)-(800, cc), _RGB32(116, cc2, 0)
  18. NEXT cc
  19. y = 300
  20. c1 = INT(RND * 100) + 20
  21. c2 = INT(RND * 150) + 60
  22. c4 = c2
  23. c3 = INT(RND * 100) + 20
  24. t = 0
  25. tt = 0
  26. si = 0
  27. 'Draw hills.
  28. amount = INT(RND * 40) + 5
  29. FOR t = 1 TO amount
  30.     _LIMIT 100
  31.     x = (RND * 800) + 1
  32.     s = (RND * 250) + 1
  33.     aspect = RND * 2
  34.     FOR sz = .25 TO s STEP .25
  35.         c2 = c2 + .05
  36.         IF c2 > 255 THEN c2 = 255
  37.         CIRCLE (x, y), sz, _RGB32(c1, c2, c3), 2 * _PI, _PI, aspect
  38.     NEXT sz
  39.     c2 = c4
  40.     l = .5
  41. 'Draw grass.
  42. FOR grassy = 300 TO 600 STEP 2.5
  43.     FOR grassx = 0 TO 800 STEP 2
  44.         RANDOMIZE TIMER
  45.         sst = RND * 2
  46.         sst2 = RND * 1
  47.         LINE (grassx + sst, grassy + sst2)-(grassx + sst, (grassy + sst2) - l), _RGB32(50, 255, 127)
  48.     NEXT grassx
  49.     l = l + .025
  50.     IF l > 4 THEN l = 4
  51. NEXT grassy
  52. 'Draw random river.
  53. yy = 305
  54. xx = (RND * 650) + 50
  55. rw = 2
  56.     IF yy > 600 THEN GOTO again:
  57.     _DELAY .1
  58.     tt = tt + 1
  59.     si = si + 1
  60.     yy = yy + (si * 2)
  61.     IF tt = 1 THEN
  62.         FOR sz = 1 TO 5 STEP .25
  63.             CIRCLE (xx, yy), si + sz, _RGB32(6, 128, 255), _PI / 2, (3 / 2) * _PI
  64.         NEXT sz
  65.     END IF
  66.     IF tt = 2 THEN
  67.         FOR sz = 1 TO 5 STEP .25
  68.             CIRCLE (xx, yy), si - sz, _RGB32(6, 128, 255), (3 / 2) * _PI, _PI / 2
  69.         NEXT sz
  70.         tt = 0
  71.     END IF
  72. again:
  73. ag$ = INKEY$
  74. IF ag$ = "s" OR ag$ = "S" THEN GOTO saving:
  75. IF ag$ = " " THEN GOTO start:
  76. IF ag$ = CHR$(27) THEN END
  77. GOTO again:
  78. saving:
  79. picture2& = _COPYIMAGE(picture&)
  80. PRINT "                       Saving"
  81. PRINT "         Your bmp file will be saved in the"
  82. PRINT "         same directory as this program is."
  83. PRINT "         It can be used with almost any"
  84. PRINT "         other graphics program or website."
  85. PRINT "         It is saved using:"
  86. PRINT "         width: 800  height: 600 pixels."
  87. PRINT "         Type a name to save your picture"
  88. PRINT "         and press the Enter key. Do not"
  89. PRINT "         add .bmp at the end, the program"
  90. PRINT "         will do it automatically."
  91. PRINT "         Example: MyPic"
  92. PRINT "         Quit and Enter key ends program."
  93. INPUT "         ->"; nm$
  94. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  95. nm$ = nm$ + ".bmp"
  96. 'Checking to see if the file already exists on your computer.
  97. theFileExists = _FILEEXISTS(nm$)
  98. IF theFileExists = -1 THEN
  99.     PRINT
  100.     PRINT "       File Already Exists"
  101.     PRINT "       Saving will delete your old"
  102.     PRINT "       bmp picture."
  103.     PRINT "       Would you like to still do it?"
  104.     PRINT "      (Y/N)."
  105.     PRINT "      Esc goes to start screen."
  106.     llloop:
  107.     _LIMIT 100
  108.     ag2$ = INKEY$
  109.     IF ag2$ = CHR$(27) THEN GOTO start:
  110.     IF ag2$ = "" THEN GOTO llloop:
  111.     IF ag2$ = "y" OR ag$ = "Y" THEN
  112.         SHELL _HIDE "DEL " + nm$
  113.         GOTO saving2:
  114.     END IF
  115.     GOTO llloop:
  116. saving2:
  117. SCREEN picture&
  118. _PUTIMAGE , picture2&
  119. SaveImage 0, nm$
  120. nm$ = ""
  121. FOR snd = 100 TO 700 STEP 100
  122.     SOUND snd, 2
  123. NEXT snd
  124. picture& = 0
  125. picture2& = 0
  126. GOTO start:
  127.  
  128. 'Here is the SUB needed to save the image to BMP.
  129. SUB SaveImage (image AS LONG, filename AS STRING)
  130.     bytesperpixel& = _PIXELSIZE(image&)
  131.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  132.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  133.     x& = _WIDTH(image&)
  134.     y& = _HEIGHT(image&)
  135.     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)
  136.     IF bytesperpixel& = 1 THEN
  137.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  138.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  139.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  140.         NEXT
  141.     END IF
  142.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  143.     lastsource& = _SOURCE
  144.     _SOURCE image&
  145.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  146.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  147.         r$ = ""
  148.         FOR px& = 0 TO x& - 1
  149.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  150.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  151.         NEXT px&
  152.         d$ = d$ + r$ + padder$
  153.     NEXT py&
  154.     _SOURCE lastsource&
  155.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  156.     b$ = b$ + d$ ' total file data bytes to create file
  157.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  158.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  159.     f& = FREEFILE
  160.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  161.     OPEN filename$ + ext$ FOR BINARY AS #f&
  162.     PUT #f&, , b$
  163.     CLOSE #f&
  164.  
Scenetest 30.bmp
* Scenetest 30.bmp (Filesize: 1.37 MB, Dimensions: 800x600, Views: 250)
« Last Edit: December 21, 2019, 12:00:41 am by SierraKen »

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Random Scene Generator
« Reply #1 on: December 20, 2019, 10:37:19 pm »
Nice surreal concept!  Have you thought about using an ASC2 reprogrammed character set to create terrain. Just asking. It is something I have thought about but never got around to trying.
My name is Michael, but you can call me Mike :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Random Scene Generator
« Reply #2 on: December 20, 2019, 11:46:51 pm »
Thanks! Hm, not sure what you mean by that. I did make a picture to ASCII text converter that turns binary pictures into text, if you want to check it out. It's on my website here:
http://www.kensprograms.com/#jpg2txt

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Random Scene Generator
« Reply #3 on: December 21, 2019, 09:46:34 am »
Hi Ken.

That effect you achieved with the grass is really interesting.

Quote
Hm, not sure what you mean by that.

Surreal? When I think of surreal I think of Salvador Dali a Surrealist Spanish Painter, particularly his 1931 painting, Persistence of Memory, you might enjoy checking him out on Internet specially after we just did clocks!




Quote
Have you thought about using an ASC2 reprogrammed character set to create terrain.

Hi romichess,

Here is discussion on reprogramming characters:
https://www.qb64.org/forum/index.php?topic=498.msg3498#msg3498
points to my character editor but whole thread discusses subject.

PMACKAY has mastered and used extensively in his works.

« Last Edit: December 21, 2019, 10:17:10 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Random Scene Generator
« Reply #4 on: December 21, 2019, 12:15:15 pm »
Awesome artwork of his. Yeah I know what surreal meant, I just wasn't sure what he meant by characters. So I went to your forum link and I see it now. The link I posted to my website has a program that changes a JPG to ASCII characters using the level of brightness for each one. I found a list online of least bright to most bright of the characters and used that to make the picture. It can't use a regular photo though since it's too detailed, instead it can use a less detailed graphic. Like you can make a happy face with Windows Paint, save it as JPG and change it with that to ASCII characters text.

About my scene generator, I might work on it a little more. I thought about adding trees or plants but that will take a lot of thinking because it would have to make smaller ones farther ahead and larger ones up close. Thanks for the compliment B+. Feel free to use any of the code on anything. I use longer grass up close and shorter grass further away. That is why you see more brown dirt farther away. Before the grass, I make a gradual change of color from darker farther away, to brighter up close, and then the grass is put on top of that. 

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Random Scene Generator
« Reply #5 on: December 21, 2019, 04:00:53 pm »
I added random trees. I don't love math, but I'm learning how to use it pretty good. The trees get a little smaller and a little thinner the further away they are.
Here is a randomly made photo also.

Code: QB64: [Select]
  1. 'Made on Dec. 20, 2019 by Ken G.
  2. _TITLE "Scene Generator - by Ken G. - Space Bar for new scene, (S)ave Picture, Esc to quit."
  3. start:
  4. _LIMIT 500
  5. picture& = _NEWIMAGE(800, 600, 32)
  6. SCREEN picture&
  7. 'Draw sky.
  8. sky = INT(RND * 154) + 100
  9. FOR sk = 0 TO 299
  10.     sky = sky + .25
  11.     IF sky > 255 THEN sky = 255
  12.     LINE (0, sk)-(800, sk), _RGB32(0, 0, sky)
  13. NEXT sk
  14. 'Draw ground.
  15. cc2 = INT(RND * 50) + 20
  16. FOR cc = 300 TO 600
  17.     cc2 = cc2 + .5
  18.     LINE (0, cc)-(800, cc), _RGB32(116, cc2, 0)
  19. NEXT cc
  20. y = 300
  21. c1 = INT(RND * 100) + 20
  22. c2 = INT(RND * 150) + 60
  23. c4 = c2
  24. c3 = INT(RND * 100) + 20
  25. t = 0
  26. tt = 0
  27. si = 0
  28. 'Draw hills.
  29. amount = INT(RND * 40) + 5
  30. FOR t = 1 TO amount
  31.     _LIMIT 100
  32.     x = (RND * 800) + 1
  33.     s = (RND * 250) + 1
  34.     aspect = RND * 2
  35.     FOR sz = .25 TO s STEP .25
  36.         c2 = c2 + .05
  37.         IF c2 > 255 THEN c2 = 255
  38.         CIRCLE (x, y), sz, _RGB32(c1, c2, c3), 2 * _PI, _PI, aspect
  39.     NEXT sz
  40.     c2 = c4
  41.     l = .5
  42. 'Draw grass.
  43. FOR grassy = 300 TO 600 STEP 2.5
  44.     FOR grassx = 0 TO 800 STEP 2
  45.         RANDOMIZE TIMER
  46.         sst = RND * 2
  47.         sst2 = RND * 1
  48.         LINE (grassx + sst, grassy + sst2)-(grassx + sst, (grassy + sst2) - l), _RGB32(50, 255, 127)
  49.     NEXT grassx
  50.     l = l + .025
  51.     IF l > 4 THEN l = 4
  52. NEXT grassy
  53. 'Draw random river.
  54. yy = 305
  55. xx = (RND * 650) + 50
  56. rw = 2
  57.     IF yy > 600 THEN GOTO trees:
  58.     _DELAY .1
  59.     tt = tt + 1
  60.     si = si + 1
  61.     yy = yy + (si * 2)
  62.     IF tt = 1 THEN
  63.         FOR sz = 1 TO 5 STEP .25
  64.             CIRCLE (xx, yy), si + sz, _RGB32(6, 128, 255), _PI / 2, (3 / 2) * _PI
  65.         NEXT sz
  66.     END IF
  67.     IF tt = 2 THEN
  68.         FOR sz = 1 TO 5 STEP .25
  69.             CIRCLE (xx, yy), si - sz, _RGB32(6, 128, 255), (3 / 2) * _PI, _PI / 2
  70.         NEXT sz
  71.         tt = 0
  72.     END IF
  73. 'Trees
  74. trees:
  75. trees = INT(RND * 50) + 3
  76. FOR tree = 1 TO trees
  77.     xt = RND * 800
  78.     yt = (RND * 300) + 300
  79.     length = yt * 8 / 160
  80.     th = yt / 100
  81.     FOR thick = 1 TO th
  82.         LINE (xt + thick, yt)-(xt + thick, yt - length), _RGB32(211, 83, 6)
  83.         'left side
  84.         LINE (xt + thick, yt - length)-((xt - length) + thick, yt - length * 2), _RGB32(211, 83, 6)
  85.         LINE ((xt - length) + thick, yt - (length * 2))-((xt - (length * 2)) + thick, yt - (length * 3)), _RGB32(211, 83, 6)
  86.         LINE ((xt - length) + thick, yt - (length * 2))-((xt - (length * 1.5) + thick), yt - (length * 3)), _RGB32(211, 83, 6)
  87.         'right side
  88.         LINE ((xt + thick), yt - length)-((xt + length) + thick, yt - (length * 2)), _RGB32(211, 83, 6)
  89.         LINE ((xt + length) + thick, yt - (length * 2))-((xt + (length * 2) + thick), yt - (length * 3)), _RGB32(211, 83, 6)
  90.         LINE ((xt + length) + thick, yt - (length * 2))-((xt + (length * 1.5) + thick), yt - (length * 3)), _RGB32(211, 83, 6)
  91.     NEXT thick
  92. NEXT tree
  93.  
  94. again:
  95. ag$ = INKEY$
  96. IF ag$ = "s" OR ag$ = "S" THEN GOTO saving:
  97. IF ag$ = " " THEN GOTO start:
  98. IF ag$ = CHR$(27) THEN END
  99. GOTO again:
  100. saving:
  101. picture2& = _COPYIMAGE(picture&)
  102. PRINT "                       Saving"
  103. PRINT "         Your bmp file will be saved in the"
  104. PRINT "         same directory as this program is."
  105. PRINT "         It can be used with almost any"
  106. PRINT "         other graphics program or website."
  107. PRINT "         It is saved using:"
  108. PRINT "         width: 800  height: 600 pixels."
  109. PRINT "         Type a name to save your picture"
  110. PRINT "         and press the Enter key. Do not"
  111. PRINT "         add .bmp at the end, the program"
  112. PRINT "         will do it automatically."
  113. PRINT "         Example: MyPic"
  114. PRINT "         Quit and Enter key ends program."
  115. INPUT "         ->"; nm$
  116. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  117. nm$ = nm$ + ".bmp"
  118. 'Checking to see if the file already exists on your computer.
  119. theFileExists = _FILEEXISTS(nm$)
  120. IF theFileExists = -1 THEN
  121.     PRINT
  122.     PRINT "       File Already Exists"
  123.     PRINT "       Saving will delete your old"
  124.     PRINT "       bmp picture."
  125.     PRINT "       Would you like to still do it?"
  126.     PRINT "      (Y/N)."
  127.     PRINT "      Esc goes to start screen."
  128.     llloop:
  129.     _LIMIT 100
  130.     ag2$ = INKEY$
  131.     IF ag2$ = CHR$(27) THEN GOTO start:
  132.     IF ag2$ = "" THEN GOTO llloop:
  133.     IF ag2$ = "y" OR ag$ = "Y" THEN
  134.         SHELL _HIDE "DEL " + nm$
  135.         GOTO saving2:
  136.     END IF
  137.     GOTO llloop:
  138. saving2:
  139. SCREEN picture&
  140. _PUTIMAGE , picture2&
  141. SaveImage 0, nm$
  142. nm$ = ""
  143. FOR snd = 100 TO 700 STEP 100
  144.     SOUND snd, 2
  145. NEXT snd
  146. picture& = 0
  147. picture2& = 0
  148. GOTO start:
  149.  
  150. 'Here is the SUB needed to save the image to BMP.
  151. SUB SaveImage (image AS LONG, filename AS STRING)
  152.     bytesperpixel& = _PIXELSIZE(image&)
  153.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  154.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  155.     x& = _WIDTH(image&)
  156.     y& = _HEIGHT(image&)
  157.     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)
  158.     IF bytesperpixel& = 1 THEN
  159.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  160.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  161.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  162.         NEXT
  163.     END IF
  164.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  165.     lastsource& = _SOURCE
  166.     _SOURCE image&
  167.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  168.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  169.         r$ = ""
  170.         FOR px& = 0 TO x& - 1
  171.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  172.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  173.         NEXT px&
  174.         d$ = d$ + r$ + padder$
  175.     NEXT py&
  176.     _SOURCE lastsource&
  177.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  178.     b$ = b$ + d$ ' total file data bytes to create file
  179.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  180.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  181.     f& = FREEFILE
  182.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  183.     OPEN filename$ + ext$ FOR BINARY AS #f&
  184.     PUT #f&, , b$
  185.     CLOSE #f&
  186.  
Scene with trees.bmp
* Scene with trees.bmp (Filesize: 1.37 MB, Dimensions: 800x600, Views: 225)