Inksaver

turtle template

Jan 18th, 2022 (edited)
332
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import turtle library
  2. import turtle
  3.  
  4. ''' global Constants and variables'''
  5.  
  6. XOFFSET = 12 # Do not change margin offsets
  7. YOFFSET = 14 # Do not change margin offsets
  8. NORTH = 90
  9. EAST = 0
  10. SOUTH = 270
  11. WEST = 180
  12. width = 300  # Width of window drawing area
  13. height = 300 # height of window drawing area
  14.  
  15. '''
  16. example: turtle.setheading(NORTH)
  17. '''
  18.  
  19. def reposition(pen, position):
  20.     ''' Moves the turtle to one of the window corners '''
  21.     pen.pu()
  22.     if position == "topright":
  23.         pen.setpos(width * 0.5 - XOFFSET * 0.2, height * 0.5 + YOFFSET * 0.2)
  24.     elif position == "topleft":
  25.         pen.setpos(-width * 0.5 - XOFFSET * 0.2, height * 0.5 + YOFFSET * 0.2)
  26.     elif position == "bottomright":
  27.         pen.setpos(width * 0.5 - XOFFSET * 0.2, -height * 0.5 + YOFFSET * 0.2) 
  28.     elif position == "bottomleft":
  29.         pen.setpos(-width * 0.5 - XOFFSET * 0.2, -height * 0.5 + YOFFSET * 0.2)
  30.     pen.pd()
  31.  
  32. def main():
  33.     global width, height        # allow changing value of width and height
  34.     '''  Set Window size and background colour '''
  35.     width = 400                 # set window width here
  36.     height = 400                # set window height here
  37.     window = turtle.Screen()    # create a window
  38.     window.setup(width + XOFFSET, height + YOFFSET) # allow for title and borders
  39.     turtle.colormode(255)
  40.     turtle.bgcolor((200, 255, 255))     # change background colour
  41.    
  42.     ''' example for green arrow shaped turtle facing east top left corner '''
  43.     fred = turtle.Pen()         # create a new turtle called 'fred'
  44.     fred.shape('arrow')         # choose 'arrow', 'turtle', 'circle', 'square'. 'triangle', 'classic'
  45.     fred.color('green')         # set colour to green
  46.    
  47.     ''' add your code here '''
  48.     # reposition(fred, 'topleft')   # set position to top left of the window
  49.     # fred.setheading(EAST)     # set to face east 
  50.    
  51.     turtle.done()               # MUST BE LAST STATEMENT!
  52. main()
RAW Paste Data Copied