Advertisement
DigiDuncan

map.py 1

Nov 6th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.01 KB | None | 0 0
  1. from turtle import *
  2. import random
  3. from pprint import pprint
  4.  
  5. settings = {    #TODO: Get settings from file.
  6.     'DELAY': 0}
  7.  
  8. def main():
  9. #Instruction for the main loop.
  10.     global settings
  11.     home()
  12.     generator()
  13.     delay(settings['DELAY'])
  14.     gotostart(map)
  15.     debuginfo()
  16.     drawmap(map)
  17.     done()
  18.  
  19. #Define cardinal directions.
  20. direct = {
  21.     'NORTH': 90,
  22.     'EAST': 0,
  23.     'SOUTH': 270,
  24.     'WEST': 180}
  25.  
  26. #Define tile size (tiles are squares).
  27. tilesize = 50
  28.  
  29. #Define tile colors.
  30. tileset = {
  31.     'GRASS': 0,
  32.     'WATER': 1,
  33.     'SAND': 2,
  34.     'ROCK': 3}
  35.  
  36. #Define our map to draw.
  37. #Example:
  38. #map = [3,          #Set row width.
  39. #       3, 0, 0,    #Row data.
  40. #       0, 2, 1,
  41. #       2, 1, 1]
  42.  
  43. map = [4,           #Default map.
  44.         99, 0, 0, 0,   
  45.         0, 1, 1, 0,
  46.         0, 1, 1, 3,
  47.         0, 2, 2, 2]
  48.  
  49. #Used to keep track of the last tile drawn.
  50. lasttile = 0
  51.  
  52. def draw_tile(tile):
  53. #Draws a singular tile.
  54.  
  55.     #Set color of tile
  56.     if tile == tileset['GRASS']:
  57.         color('brown', 'green')
  58.     elif tile == tileset['WATER']:
  59.         color('blue', 'aqua')
  60.     elif tile == tileset['SAND']:
  61.         color('brown', 'yellow')
  62.     elif tile == tileset['ROCK']:
  63.         color('gray', 'gray')
  64.     else:
  65.         color('purple', 'purple')
  66.  
  67.     #Draw a tile.
  68.     down()
  69.     begin_fill()
  70.     seth(direct['NORTH'])
  71.     forward(tilesize)
  72.     seth(direct['WEST'])
  73.     forward(tilesize)
  74.     seth(direct['SOUTH'])
  75.     forward(tilesize)
  76.     seth(direct['EAST'])
  77.     forward(tilesize)
  78.     end_fill()
  79.     up()
  80.  
  81.     #Draw a ? in unknown tiles.
  82.     if tile not in tileset.values():
  83.         color('white')
  84.         write("? ", False, "right", ("Arial", int(tilesize / 1.5), "bold"))
  85.  
  86. def move(m, lasttile):
  87. #Determines where to begin drawing the next tile.
  88.     w = m[0]                    #Make a copy of the map's width value.
  89.     forward(tilesize)           #Always start by moving forward one tile.
  90.     if lasttile < (w + 1):      #If the tile is in the first row...
  91.         pass                    #Do nothing.
  92.     if lasttile % w == 0:       #If the last tile was the last in the row...
  93.         right(90)               #Turn toward the bottom of the current tile.
  94.         forward(tilesize)       #Go to the bottom of the current tile.
  95.         right(90)               #Turn to the left of the current tile.
  96.         forward(tilesize * w)   #Move all the way down te row.
  97.         right(90)               #Get in position for next row.
  98.  
  99. def drawmap(m):
  100. #Print the entire map.
  101.     for x in range (0, len(m)):
  102.         if x == 0:
  103.             print("Reading map data....") #Not really, just skipping index 0.
  104.         else:
  105.             draw_tile(map[x])
  106.             lasttile = x
  107.             tileinfo(x)
  108.             move(m, lasttile)
  109.     hideturtle()
  110.     print("Map drawn.")
  111.  
  112. def gotostart(m):
  113. #Go to the top left of the map.
  114. #Makes maps more or less centered.
  115.     up()
  116.     topleft = ((m[0] * tilesize) / 2) #Row width * tile size in px, halfed.
  117.     setpos (-topleft, topleft)
  118.     down()
  119.  
  120. def debuginfo():
  121. #Numbers and stats that help me develop.
  122. #Printed into sysout.
  123.     print ("Animation delay: ", end='')
  124.     print (delay())
  125.     print ("Tile size: ", end='')
  126.     print (tilesize)
  127.     print ("Tileset: ", end='')
  128.     pprint (tileset)
  129.     print ("Map Width: " , end='')
  130.     print (map[0])
  131.     print ("Map Data: ", end='')
  132.     print (map[1:])
  133.     print ("Turtle Start Point: ", end=' ')
  134.     print (position(), end = ' ')
  135.     print (heading())
  136.  
  137. def tileinfo(index):
  138. #Print info on the last drawn tile.
  139.     print ("Drew tile ", end='')
  140.     print (index, end='')
  141.     getcoords(index)
  142.     print (" as color ", end='')
  143.     print (map[index])
  144.  
  145. def getcoords(index):
  146. #Determine and print the coords of the last tile based on its index.
  147.     print (" (", end='')
  148.     if (index % map[0]) == 0:
  149.         print ((index // map[0]), end='')
  150.     else:
  151.         print ((index // map[0] + 1), end='')
  152.     print (",", end='')
  153.     if (index % map[0]) == 0:
  154.         print (map[0], end='')
  155.     else:
  156.         print (index % map[0], end='')
  157.     print (")", end='')
  158.  
  159. def generator():
  160. #Determine which random generator to use.
  161.     gen = numinput('MapGen', 'Generator:')
  162.     if gen == 0:    #Use the built in map.
  163.         pass
  164.     elif gen == 1:
  165.         gen1()
  166.     else:
  167.         print ('ERROR: Invalid generator.')
  168.         generator()
  169.  
  170. def gen1():
  171. #First random generator
  172.     global map  #Load in the map so we can modify it.
  173.     mapwidth = 9
  174.     rocks = []
  175.     lakes = []
  176.  
  177.     map = [None]*( (mapwidth ** 2) + 1) #Define a map full of Nones.
  178.     print ('Generating random map of type 1.')
  179.  
  180.     #Fill the map with grassy terrain.
  181.     for i in range(mapwidth ** 2 + 1):
  182.         if i == 0:
  183.             map[i] = mapwidth
  184.         else:
  185.             map[i] = tileset['GRASS']
  186.  
  187.     #Add rocks to the map.
  188.     for j in range(mapwidth ** 2 + 1):
  189.         if j == 0:
  190.             pass    #Don't overwrite the map width.
  191.         else:
  192.             mayberock = random.randint(0, 9)
  193.             if mayberock == (1):    #10% chance of rock.
  194.                 map[j] = tileset['ROCK']
  195.                 rocks.append(j)
  196.  
  197.     #Add lake points to the map.
  198.     for k in range(30): #Try 30 times to add water to a tile.
  199.         tileselect = random.randint(1, mapwidth ** 2)
  200.         maybewater = random.randint(0, 9)
  201.         if maybewater == (0 or 1)#20% chance of water.
  202.             map[tileselect] = tileset['WATER']
  203.             lakes.append(tileselect)
  204.  
  205.     for l in lakes:
  206.  
  207.         #Set up boolean variables.
  208.         lakeontopedge = False
  209.         lakeonbottomedge = False
  210.         lakeonleftedge = False
  211.         lakeonrightedge = False
  212.         didspreadup = False
  213.         didspreaddown = False
  214.         didspreadleft= False
  215.         didspreadright = False
  216.         didspreadtopleft = False
  217.         didspreadtopright = False
  218.         didspreadbottomleft = False
  219.         didspreadbootomright = False
  220.  
  221.         #Determine if the lake is on any edges.
  222.         if l % mapwidth == 0:
  223.             lakeonrightedge = True
  224.         if l % mapwidth == 1:
  225.             lakeonleftedge = True
  226.         if l in range(1, mapwidth):
  227.             lakeontopedge = True
  228.         if l in range(len(map) - mapwidth - 1, len(map) - 1):
  229.             lakeonbottomedge = True
  230.  
  231.         #Spread the water.
  232.         #Spread up 1?
  233.         if lakeontopedge:
  234.             #Don't spread up if we're already on the top edge.
  235.             print ('Lake ' + str(l) + ' is on the top edge.')
  236.         else:
  237.             spreadupchance = random.randint(0, 9)
  238.             if spreadupchance == (0 or 1 or 2 or 3 or 4 or 5)#60% chance to spread up.
  239.                 didspreadup = True
  240.                 print ('Spread lake ' + str(l) + ' up.')
  241.                 map[(l - mapwidth)] = tileset['WATER']
  242.             else:
  243.                 pass
  244.  
  245.         #Spread down 1?
  246.         if lakeonbottomedge:
  247.             #Don't spread down if we're already on the bottom edge.
  248.             print ('Lake ' + str(l) + ' is on the bottom edge.')
  249.         else:
  250.             spreaddownchance = random.randint(0, 9)
  251.             if spreaddownchance == (0 or 1 or 2 or 3 or 4 or 5):    #60% chance to spread down.
  252.                 didspreaddown = True
  253.                 print ('Spread lake ' + str(l) + ' down.')
  254.                 map[(l + mapwidth)] = tileset['WATER']
  255.             else:
  256.                 pass
  257.  
  258.         #Spread left 1?
  259.         if lakeonleftedge:
  260.             #Don't spread left if we're already on the left edge.
  261.             print ('Lake ' + str(l) + ' is on the left edge.')
  262.         else:
  263.             spreadleftchance = random.randint(0, 9)
  264.             if spreadleftchance == (0 or 1 or 2 or 3 or 4 or 5):    #60% chance to spread left.
  265.                 didspreadleft = True
  266.                 print ('Spread lake ' + str(l) + ' left.')
  267.                 map[(l - 1)] = tileset['WATER']
  268.             else:
  269.                 pass
  270.  
  271.         #Spread right 1?
  272.         if lakeonrightedge:
  273.             #Don't spread right if we're already on the right edge.
  274.             print ('Lake ' + str(l) + ' is on the right edge.')
  275.         else:
  276.             spreadrightchance = random.randint(0, 9)
  277.             if spreadrightchance == (0 or 1 or 2 or 3 or 4 or 5):   #60% chance to spread right.
  278.                 didspreadright = True
  279.                 print ('Spread lake ' + str(l) + ' right.')
  280.                 map[(l + 1)] = tileset['WATER']
  281.             else:
  282.                 pass
  283.  
  284.         #Spread to corners sometimes.
  285.         if random.choice([True, False]) and didspreadup and didspreadleft:
  286.             print ('Spread lake ' + str(l) + ' top-left.')
  287.             map[l - mapwidth - 1] = tileset['WATER']
  288.         if random.choice([True, False]) and didspreadup and didspreadright:
  289.             print ('Spread lake ' + str(l) + ' top-right.')
  290.             map[l - mapwidth + 1] = tileset['WATER']
  291.         if random.choice([True, False]) and didspreaddown and didspreadleft:
  292.             print ('Spread lake ' + str(l) + ' bottom-left.')
  293.             map[l + mapwidth - 1] = tileset['WATER']
  294.         if random.choice([True, False]) and didspreaddown and didspreadright:
  295.             print ('Spread lake ' + str(l) + ' bottom-right.')
  296.             map[l + mapwidth + 1] = tileset['WATER']
  297.  
  298.         #Add sand around lakes.
  299.             #Try to place upper snad tile.
  300.             if random.choice([True, True, False]):          #67% chance.
  301.                 if didspreadup:                             #Don't overwrite existing water.
  302.                     map[l - mapwidth * 2] = tileset['SAND'] #Put sand two tiles up.
  303.                 else:                                       #If we can,
  304.                     map[l - mapwidth] = tileset['SAND']     #Put sand one tile up.
  305.             #Try to place lower sand tile.
  306.             if random.choice([True, True, False]):
  307.                 if didspreaddown:
  308.                     map[l + mapwidth * 2] = tileset['SAND']
  309.                 else:
  310.                     map[l + mapwidth] = tileset['SAND']
  311.             #Try to place left sand tile.
  312.             if random.choice([True, True, False]):
  313.                 if didspreadleft:
  314.                     map[l - 2] = tileset['SAND']
  315.                 else:
  316.                     map[l - 1] = tileset['SAND']
  317.             #Try to place right sand tile.
  318.             if random.choice([True, True, False]):
  319.                 if didspreadright:
  320.                     map[l + 2] = tileset['SAND']
  321.                 else:
  322.                     map[l + 1] = tileset['SAND']
  323.  
  324.  
  325.  
  326.         #print ( 'Lake ' + str(l) + ': ' + str(lakeontopedge) + str(lakeonbottomedge)
  327.         #+ str(lakeonleftedge) + str(lakeonrightedge))
  328.  
  329.     print ('Added ' + str(len(rocks)) + ' rocks at tiles ' + str(rocks) + '.')
  330.     print ('Added ' + str(len(lakes)) + ' lakes at tiles ' + str(lakes) + '.')
  331.  
  332.    
  333. #Run the main program loop.
  334. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement