Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import *
- def main():
- #Instruction for the main loop.
- home()
- delay(25)
- gotostart(map)
- debuginfo()
- drawmap(map)
- done()
- #Define cardinal directions.
- direct = {
- 'NORTH': 90,
- 'EAST': 0,
- 'SOUTH': 270,
- 'WEST': 180}
- #Define box size (boxes are squares).
- boxsize = 50
- #Define box colors.
- tileset = {
- 'GRASS': 0,
- 'WATER': 1,
- 'SAND': 2,
- 'ROCK': 3}
- #Define our map to draw.
- map = [3, #Set row width.
- 3, 0, 0, #Row data.
- 0, 2, 1,
- 2, 1, 1]
- #Used to keep track of the last box drawn.
- lastbox = 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:
- unknown_tile()
- #Draw a box.
- down()
- begin_fill()
- seth(direct['NORTH'])
- forward(boxsize)
- seth(direct['WEST'])
- forward(boxsize)
- seth(direct['SOUTH'])
- forward(boxsize)
- seth(direct['EAST'])
- forward(boxsize)
- end_fill()
- up()
- def move(m, lastbox):
- #Determines where to begin drawing the next box.
- w = m[0] #Make a copy of the map's width value.
- forward(boxsize) #Always start by moving forward one box.
- if lastbox < (w + 1): #If the box is in the first row...
- pass #Do nothing.
- if lastbox % w == 0: #If the last box was the last in the row...
- right(90) #Turn toward the bottom of the current box.
- forward(boxsize) #Go to the bottom of the current box.
- right(90) #Turn to the left of the current box.
- forward(boxsize * 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])
- lastbox = x
- tileinfo(x)
- move(m, lastbox)
- def gotostart(m):
- #Go to the top left of the map.
- #Makes maps more or less centered.
- up()
- topleft = ((m[0] * boxsize) / 2) #Row width * box 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 (boxsize)
- print ("Tileset: ", end='')
- print (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 ("Drew tile ", end='')
- print (index, end='')
- print (getcoords(index), end='')
- print (" as color ", end='')
- print (map[index])
- def getcoords(index):
- print (" (", end='')
- print ((index // map[0]) + 1, end='')
- print (",", end='')
- if (index % map[0]) == 0:
- print (map[0], end='')
- else:
- print (index % map[0], end='')
- print (")")
- #Run the main program loop.
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement