Skip to content

Instantly share code, notes, and snippets.

@matdombrock
Created February 8, 2018 09:56
Show Gist options
  • Save matdombrock/5500778315bf838b2a2ac81ce9d3eb07 to your computer and use it in GitHub Desktop.
Save matdombrock/5500778315bf838b2a2ac81ce9d3eb07 to your computer and use it in GitHub Desktop.
import numpy as np
import random
render_chars = {}
render_chars[99]="X"
render_chars[12]="^"
render_chars[11]="~"
render_chars[10]="o"
render_chars[9]="#"
render_chars[0]=" "
def CreateWorld(col,row):
shape = (row, col)
e = np.zeros(shape)
return e,shape
#draw
def RenderWorld(world,world_shape):
world_data = ""
for x in range(world_shape[0]):
line = ""
for y in range(world_shape[1]):
val =world[x][y]
#print(val)
line+=render_chars[val]
'''
if world[x][y]==12:
line += render_chars[12]
elif world[x][y]==11:
line += render_chars[11]
elif world[x][y]==10:
line += render_chars[10]
elif world[x][y]==9:
line += render_chars[9]
else:
line += " "
'''
world_data += line+"\n"
return world_data
def GenMountians(world,world_shape,density):
for i in range(density):
cursor = [0,0]#(x,y)
cursor[0] = random.randint(0, world_shape[0]-1)
cursor[1] = random.randint(0, world_shape[1]-1)
choice = random.choice (["vert","horz"])
#print(choice)
for ii in range(10):
if(choice == "vert"):
direction = 0
opdir =1
else:
direction = 1
opdir = 0
world[cursor[0]][cursor[1]] = 12
if random.randint(0,1)>0:
cursor[direction]=cursor[direction]+1
else:
cursor[opdir]=cursor[opdir]+1
if cursor[direction]>=world_shape[direction]:
cursor[direction]=0
if cursor[opdir]>=world_shape[opdir]:
cursor[opdir]=0
return world
def GenRivers(world,world_shape,density):
for i in range(density):
cursor = [0,0]#(x,y)
while world[cursor[0]][cursor[1]]!=12:
cursor[0] = random.randint(0, world_shape[0]-1)
cursor[1] = random.randint(0, world_shape[1]-1)
choice = random.choice (["vert","horz"])
#print(choice)
for ii in range(10):
if(choice == "vert"):
direction = 0
opdir =1
else:
direction = 1
opdir = 0
world[cursor[0]][cursor[1]] = 11
if random.randint(0,1)>0:
cursor[direction]=cursor[direction]+1
else:
cursor[opdir]=cursor[opdir]+1
if cursor[direction]>=world_shape[direction]:
cursor[direction]=0
if cursor[opdir]>=world_shape[opdir]:
cursor[opdir]=0
return world
def GenCaves(world,world_shape,density):
for i in range(density):
cursor = [0,0]#(x,y)
cursor[0] = random.randint(0, world_shape[0]-1)
cursor[1] = random.randint(0, world_shape[1]-1)
world[cursor[0]][cursor[1]] = 10
return world
def GenVillages(world,world_shape,density):
for i in range(density):
cursor = [0,0]#(x,y)
cursor[0] = random.randint(0, world_shape[0]-1)
cursor[1] = random.randint(0, world_shape[1]-1)
world[cursor[0]][cursor[1]] = 9
return world
def DebugP(text):
print(text)
world, world_shape = CreateWorld(32,16)
world[0][0] = 12
world = GenMountians(world,world_shape,5)
world = GenRivers(world,world_shape,5)
world = GenCaves(world,world_shape,5)
world = GenVillages(world,world_shape,5)
render = RenderWorld(world,world_shape)
print(render)
cursor = [0,0]#(x,y)
def Check():
while True:
x = input(">")
y = input(">")
#print(render_chars[world[int(x)][int(y)]])
cache = world[int(x)][int(y)]
world[int(x)][int(y)]=99
render = RenderWorld(world,world_shape)
print(render)
world[int(x)][int(y)]=cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment