Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. from turtle import *
  2.  
  3. def main():
  4. #Instruction for the main loop.
  5.     home()
  6.     delay(25)
  7.     gotostart(map)
  8.     debuginfo()
  9.     drawmap(map)
  10.     done()
  11.  
  12. #Define cardinal directions.
  13. direct = {
  14.     'NORTH': 90,
  15.     'EAST': 0,
  16.     'SOUTH': 270,
  17.     'WEST': 180}
  18.  
  19. #Define box size (boxes are squares).
  20. boxsize = 50
  21.  
  22. #Define box colors.
  23. tileset = {
  24.     'GRASS': 0,
  25.     'WATER': 1,
  26.     'SAND': 2,
  27.     'ROCK': 3}
  28.  
  29. #Define our map to draw.
  30. map = [3,           #Set row width.
  31.         3, 0, 0,    #Row data.
  32.         0, 2, 1,
  33.         2, 1, 1]
  34.  
  35. #Used to keep track of the last box drawn.
  36. lastbox = 0
  37.  
  38. def draw_tile(tile):
  39. #Draws a singular tile.
  40.  
  41.     #Set color of tile
  42.     if tile == tileset['GRASS']:
  43.         color('brown', 'green')
  44.     elif tile == tileset['WATER']:
  45.         color('blue', 'aqua')
  46.     elif tile == tileset['SAND']:
  47.         color('brown', 'yellow')
  48.     elif tile == tileset['ROCK']:
  49.         color('gray', 'gray')
  50.     else:
  51.         unknown_tile()
  52.  
  53.     #Draw a box.
  54.     down()
  55.     begin_fill()
  56.     seth(direct['NORTH'])
  57.     forward(boxsize)
  58.     seth(direct['WEST'])
  59.     forward(boxsize)
  60.     seth(direct['SOUTH'])
  61.     forward(boxsize)
  62.     seth(direct['EAST'])
  63.     forward(boxsize)
  64.     end_fill()
  65.     up()
  66.  
  67. def move(m, lastbox):
  68. #Determines where to begin drawing the next box.
  69.     w = m[0]                    #Make a copy of the map's width value.
  70.     forward(boxsize)            #Always start by moving forward one box.
  71.     if lastbox < (w + 1):       #If the box is in the first row...
  72.         pass                    #Do nothing.
  73.     if lastbox % w == 0:        #If the last box was the last in the row...
  74.         right(90)               #Turn toward the bottom of the current box.
  75.         forward(boxsize)        #Go to the bottom of the current box.
  76.         right(90)               #Turn to the left of the current box.
  77.         forward(boxsize * w)    #Move all the way down te row.
  78.         right(90)               #Get in position for next row.
  79.  
  80. def drawmap(m):
  81. #Print the entire map.
  82.     for x in range (0, len(m)):
  83.         if x == 0:
  84.             print("Reading map data....") #Not really, just skipping index 0.
  85.         else:
  86.             draw_tile(map[x])
  87.             lastbox = x
  88.             tileinfo(x)
  89.             move(m, lastbox)
  90.  
  91. def gotostart(m):
  92. #Go to the top left of the map.
  93. #Makes maps more or less centered.
  94.     up()
  95.     topleft = ((m[0] * boxsize) / 2) #Row width * box size in px, halfed.
  96.     setpos (-topleft, topleft)
  97.     down()
  98.  
  99. def debuginfo():
  100. #Numbers and stats that help me develop.
  101. #Printed into sysout.
  102.     print ("Animation delay: ", end='')
  103.     print (delay())
  104.     print ("Tile size: ", end='')
  105.     print (boxsize)
  106.     print ("Tileset: ", end='')
  107.     print (tileset)
  108.     print ("Map Width: " , end='')
  109.     print (map[0])
  110.     print ("Map Data: ", end='')
  111.     print (map[1:])
  112.     print ("Turtle Start Point: ", end='')
  113.     print (position(), end = '')
  114.     print (heading())
  115.  
  116. def tileinfo(index):
  117.     print ("Drew tile ", end='')
  118.     print (index, end='')
  119.     print (getcoords(index), end='')
  120.     print (" as color ", end='')
  121.     print (map[index])
  122.  
  123. def getcoords(index):
  124.     print (" (", end='')
  125.     print ((index // map[0]) + 1, end='')
  126.     print (",", end='')
  127.     if (index % map[0]) == 0:
  128.         print (map[0], end='')
  129.     else:
  130.         print (index % map[0], end='')
  131.     print (")")
  132.  
  133. #Run the main program loop.
  134. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement