A little exploration of proc gen.
This commit is contained in:
parent
3b99a65499
commit
4c905e3aa2
|
|
@ -0,0 +1,63 @@
|
|||
'''
|
||||
Cellular Automaton-based terrain generation adapted from:
|
||||
https://codewithgolu.com/python/procedural-generation-of-game-content-with-python-a-comprehensive-guide/
|
||||
Produces goo like this:
|
||||
################### ####### ###### ########### ########### ###### ### ####### >
|
||||
####################### ####### # ###### ################### #### ####### ############# >
|
||||
############################### ### ####### ################## ## ###### ############### >
|
||||
################# ########### #### ######## ################ ## #### #### ########## >
|
||||
##### #### ### ######## ####### ######### ############# ## ## ### ######### >
|
||||
#### ####### ################### ########### #### #### ####### >
|
||||
### ####### #################### ########## ###### ## ##### ###### >
|
||||
## ######## ########### #### ##### ##### #### ###### ##### >
|
||||
#################### ### #### #### ###### ######## #### >
|
||||
### #################### ### #### ### ######### ############ #### >
|
||||
##### ################### ### #### ## ########### ############# #### >
|
||||
###### #################### ### #### ### ############# ############## #########>
|
||||
############ #################### ### ###### #### ############################### ########>
|
||||
################ ######### ####### #### ###### ##### ############################### #######>
|
||||
################ ####### ###### #### #### ##### ############################### #######>
|
||||
##### ###### ###### ##### #### #### ####################### #### ######>
|
||||
## #### #### #### ##### #### ############ ######## ### #>
|
||||
### ## ###### #### ##### ##### #### ### >
|
||||
### ## ###### ## ## #### ### ## #### >
|
||||
## #### ## ##### #### ## ##### #### #### #### ###### #>
|
||||
#### ###################### ### #### ###### ### ############# #########>
|
||||
################################## #### #### ##### ## ############## ########>
|
||||
'''
|
||||
import random
|
||||
|
||||
# Set the size of the terrain
|
||||
w, h = size = (256, 256)
|
||||
|
||||
# Set the initial terrain state to random values
|
||||
terrain = [[random.randint(0, 1) for _ in range(w)] for _ in range(h)]
|
||||
|
||||
# Define the cellular automata rules
|
||||
def cellular_automata_step(terrain):
|
||||
new_terrain = [[0] * w for _ in range(h)]
|
||||
|
||||
for x in range(1, w - 1):
|
||||
for y in range(1, h - 1):
|
||||
if sum(neighbors(terrain, x, y)) >= 5:
|
||||
new_terrain[y][x] = 1
|
||||
|
||||
return new_terrain
|
||||
|
||||
|
||||
def neighbors(terrain, x, y):
|
||||
for i in range(y - 1, y + 2):
|
||||
for j in range(x - 1, x + 2):
|
||||
try:
|
||||
n = terrain[i][j]
|
||||
except IndexError:
|
||||
continue
|
||||
yield n
|
||||
|
||||
|
||||
# Apply the cellular automata rules
|
||||
for i in range(10):
|
||||
terrain = cellular_automata_step(terrain)
|
||||
|
||||
for row in terrain:
|
||||
print(''.join(' #'[n] for n in row)
|
||||
Loading…
Reference in New Issue