Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import *
- import random
- from pprint import pprint
- settings = { #TODO: Get settings from file.
- 'DELAY': 0}
- def main():
- #Instruction for the main loop.
- global settings
- home()
- generator()
- delay(settings['DELAY'])
- gotostart(map)
- debuginfo()
- drawmap(map)
- done()
- #Define cardinal directions.
- direct = {
- 'NORTH': 90,
- 'EAST': 0,
- 'SOUTH': 270,
- 'WEST': 180}
- #Define tile size (tiles are squares).
- tilesize = 50
- #Define tile colors.
- tileset = {
- 'GRASS': 0,
- 'WATER': 1,
- 'SAND': 2,
- 'ROCK': 3}
- #Define our map to draw.
- #Example:
- #map = [3, #Set row width.
- # 3, 0, 0, #Row data.
- # 0, 2, 1,
- # 2, 1, 1]
- map = [4, #Default map.
- 99, 0, 0, 0,
- 0, 1, 1, 0,
- 0, 1, 1, 3,
- 0, 2, 2, 2]
- #Used to keep track of the last tile drawn.
- lasttile = 0
- def draw_tile(tile):
- #Draws a singular tile.
- #Set color of tile
- if tile == tileset['GRASS']:
- color('brown', 'green')
- elif tile == tileset['WATER']:
- color('blue', 'aqua')
- elif tile == tileset['SAND']:
- color('brown', 'yellow')
- elif tile == tileset['ROCK']:
- color('gray', 'gray')
- else:
- color('purple', 'purple')
- #Draw a tile.
- down()
- begin_fill()
- seth(direct['NORTH'])
- forward(tilesize)
- seth(direct['WEST'])
- forward(tilesize)
- seth(direct['SOUTH'])
- forward(tilesize)
- seth(direct['EAST'])
- forward(tilesize)
- end_fill()
- up()
- #Draw a ? in unknown tiles.
- if tile not in tileset.values():
- color('white')
- write("? ", False, "right", ("Arial", int(tilesize / 1.5), "bold"))
- def move(m, lasttile):
- #Determines where to begin drawing the next tile.
- w = m[0] #Make a copy of the map's width value.
- forward(tilesize) #Always start by moving forward one tile.
- if lasttile < (w + 1): #If the tile is in the first row...
- pass #Do nothing.
- if lasttile % w == 0: #If the last tile was the last in the row...
- right(90) #Turn toward the bottom of the current tile.
- forward(tilesize) #Go to the bottom of the current tile.
- right(90) #Turn to the left of the current tile.
- forward(tilesize * w) #Move all the way down te row.
- right(90) #Get in position for next row.
- def drawmap(m):
- #Print the entire map.
- for x in range (0, len(m)):
- if x == 0:
- print("Reading map data....") #Not really, just skipping index 0.
- else:
- draw_tile(map[x])
- lasttile = x
- tileinfo(x)
- move(m, lasttile)
- hideturtle()
- print("Map drawn.")
- def gotostart(m):
- #Go to the top left of the map.
- #Makes maps more or less centered.
- up()
- topleft = ((m[0] * tilesize) / 2) #Row width * tile size in px, halfed.
- setpos (-topleft, topleft)
- down()
- def debuginfo():
- #Numbers and stats that help me develop.
- #Printed into sysout.
- print ("Animation delay: ", end='')
- print (delay())
- print ("Tile size: ", end='')
- print (tilesize)
- print ("Tileset: ", end='')
- pprint (tileset)
- print ("Map Width: " , end='')
- print (map[0])
- print ("Map Data: ", end='')
- print (map[1:])
- print ("Turtle Start Point: ", end=' ')
- print (position(), end = ' ')
- print (heading())
- def tileinfo(index):
- #Print info on the last drawn tile.
- print ("Drew tile ", end='')
- print (index, end='')
- getcoords(index)
- print (" as color ", end='')
- print (map[index])
- def getcoords(index):
- #Determine and print the coords of the last tile based on its index.
- print (" (", end='')
- if (index % map[0]) == 0:
- print ((index // map[0]), end='')
- else:
- print ((index // map[0] + 1), end='')
- print (",", end='')
- if (index % map[0]) == 0:
- print (map[0], end='')
- else:
- print (index % map[0], end='')
- print (")", end='')
- def generator():
- #Determine which random generator to use.
- gen = numinput('MapGen', 'Generator:')
- if gen == 0: #Use the built in map.
- pass
- elif gen == 1:
- gen1()
- else:
- print ('ERROR: Invalid generator.')
- generator()
- def gen1():
- #First random generator
- global map #Load in the map so we can modify it.
- mapwidth = 9
- rocks = []
- lakes = []
- map = [None]*( (mapwidth ** 2) + 1) #Define a map full of Nones.
- print ('Generating random map of type 1.')
- #Fill the map with grassy terrain.
- for i in range(mapwidth ** 2 + 1):
- if i == 0:
- map[i] = mapwidth
- else:
- map[i] = tileset['GRASS']
- #Add rocks to the map.
- for j in range(mapwidth ** 2 + 1):
- if j == 0:
- pass #Don't overwrite the map width.
- else:
- mayberock = random.randint(0, 9)
- if mayberock == (1): #10% chance of rock.
- map[j] = tileset['ROCK']
- rocks.append(j)
- #Add lake points to the map.
- for k in range(30): #Try 30 times to add water to a tile.
- tileselect = random.randint(1, mapwidth ** 2)
- maybewater = random.randint(0, 9)
- if maybewater == (0 or 1): #20% chance of water.
- map[tileselect] = tileset['WATER']
- lakes.append(tileselect)
- for l in lakes:
- #Set up boolean variables.
- lakeontopedge = False
- lakeonbottomedge = False
- lakeonleftedge = False
- lakeonrightedge = False
- didspreadup = False
- didspreaddown = False
- didspreadleft= False
- didspreadright = False
- didspreadtopleft = False
- didspreadtopright = False
- didspreadbottomleft = False
- didspreadbootomright = False
- #Determine if the lake is on any edges.
- if l % mapwidth == 0:
- lakeonrightedge = True
- if l % mapwidth == 1:
- lakeonleftedge = True
- if l in range(1, mapwidth):
- lakeontopedge = True
- if l in range(len(map) - mapwidth - 1, len(map) - 1):
- lakeonbottomedge = True
- #Spread the water.
- #Spread up 1?
- if lakeontopedge:
- #Don't spread up if we're already on the top edge.
- print ('Lake ' + str(l) + ' is on the top edge.')
- else:
- spreadupchance = random.randint(0, 9)
- if spreadupchance == (0 or 1 or 2 or 3 or 4 or 5): #60% chance to spread up.
- didspreadup = True
- print ('Spread lake ' + str(l) + ' up.')
- map[(l - mapwidth)] = tileset['WATER']
- else:
- pass
- #Spread down 1?
- if lakeonbottomedge:
- #Don't spread down if we're already on the bottom edge.
- print ('Lake ' + str(l) + ' is on the bottom edge.')
- else:
- spreaddownchance = random.randint(0, 9)
- if spreaddownchance == (0 or 1 or 2 or 3 or 4 or 5): #60% chance to spread down.
- didspreaddown = True
- print ('Spread lake ' + str(l) + ' down.')
- map[(l + mapwidth)] = tileset['WATER']
- else:
- pass
- #Spread left 1?
- if lakeonleftedge:
- #Don't spread left if we're already on the left edge.
- print ('Lake ' + str(l) + ' is on the left edge.')
- else:
- spreadleftchance = random.randint(0, 9)
- if spreadleftchance == (0 or 1 or 2 or 3 or 4 or 5): #60% chance to spread left.
- didspreadleft = True
- print ('Spread lake ' + str(l) + ' left.')
- map[(l - 1)] = tileset['WATER']
- else:
- pass
- #Spread right 1?
- if lakeonrightedge:
- #Don't spread right if we're already on the right edge.
- print ('Lake ' + str(l) + ' is on the right edge.')
- else:
- spreadrightchance = random.randint(0, 9)
- if spreadrightchance == (0 or 1 or 2 or 3 or 4 or 5): #60% chance to spread right.
- didspreadright = True
- print ('Spread lake ' + str(l) + ' right.')
- map[(l + 1)] = tileset['WATER']
- else:
- pass
- #Spread to corners sometimes.
- if random.choice([True, False]) and didspreadup and didspreadleft:
- print ('Spread lake ' + str(l) + ' top-left.')
- map[l - mapwidth - 1] = tileset['WATER']
- if random.choice([True, False]) and didspreadup and didspreadright:
- print ('Spread lake ' + str(l) + ' top-right.')
- map[l - mapwidth + 1] = tileset['WATER']
- if random.choice([True, False]) and didspreaddown and didspreadleft:
- print ('Spread lake ' + str(l) + ' bottom-left.')
- map[l + mapwidth - 1] = tileset['WATER']
- if random.choice([True, False]) and didspreaddown and didspreadright:
- print ('Spread lake ' + str(l) + ' bottom-right.')
- map[l + mapwidth + 1] = tileset['WATER']
- #Add sand around lakes.
- #Try to place upper snad tile.
- if random.choice([True, True, False]): #67% chance.
- if didspreadup: #Don't overwrite existing water.
- map[l - mapwidth * 2] = tileset['SAND'] #Put sand two tiles up.
- else: #If we can,
- map[l - mapwidth] = tileset['SAND'] #Put sand one tile up.
- #Try to place lower sand tile.
- if random.choice([True, True, False]):
- if didspreaddown:
- map[l + mapwidth * 2] = tileset['SAND']
- else:
- map[l + mapwidth] = tileset['SAND']
- #Try to place left sand tile.
- if random.choice([True, True, False]):
- if didspreadleft:
- map[l - 2] = tileset['SAND']
- else:
- map[l - 1] = tileset['SAND']
- #Try to place right sand tile.
- if random.choice([True, True, False]):
- if didspreadright:
- map[l + 2] = tileset['SAND']
- else:
- map[l + 1] = tileset['SAND']
- #print ( 'Lake ' + str(l) + ': ' + str(lakeontopedge) + str(lakeonbottomedge)
- #+ str(lakeonleftedge) + str(lakeonrightedge))
- print ('Added ' + str(len(rocks)) + ' rocks at tiles ' + str(rocks) + '.')
- print ('Added ' + str(len(lakes)) + ' lakes at tiles ' + str(lakes) + '.')
- #Run the main program loop.
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement