turtle_repl.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "Christian Heider Nielsen"
  4. __doc__ = r"""
  5. Created on 31-10-2020
  6. """
  7. from turtle import *
  8. from warg import PlaybackShell, str_to_tuple
  9. class TurtleShell(PlaybackShell):
  10. intro = "Welcome to the turtle shell. Type help or ? to list commands.\n"
  11. prompt = "(turtle) "
  12. # ----- basic turtle commands -----
  13. def do_forward(self, arg):
  14. """Move the turtle forward by the specified distance: FORWARD 10"""
  15. forward(*str_to_tuple(arg))
  16. def do_right(self, arg):
  17. """Turn turtle right by given number of degrees: RIGHT 20"""
  18. right(*str_to_tuple(arg))
  19. def do_left(self, arg):
  20. """Turn turtle left by given number of degrees: LEFT 90"""
  21. left(*str_to_tuple(arg))
  22. def do_goto(self, arg):
  23. """Move turtle to an absolute position with changing orientation. GOTO 100 200"""
  24. goto(*str_to_tuple(arg))
  25. def do_home(self, arg):
  26. """Return turtle to the home position: HOME"""
  27. home()
  28. def do_circle(self, arg):
  29. """Draw circle with given radius an options extent and steps: CIRCLE 50"""
  30. circle(*str_to_tuple(arg))
  31. def do_position(self, arg):
  32. """Print the current turtle position: POSITION"""
  33. print(f"Current position is {position()}\n")
  34. def do_heading(self, arg):
  35. """Print the current turtle heading in degrees: HEADING"""
  36. print(f"Current heading is {heading()}\n")
  37. def do_color(self, arg):
  38. """Set the color: COLOR BLUE"""
  39. color(arg.lower())
  40. def do_undo(self, arg):
  41. """Undo (repeatedly) the last turtle action(s): UNDO"""
  42. def do_reset(self, arg):
  43. """Clear the screen and return turtle to center: RESET"""
  44. reset()
  45. def do_bye(self, arg):
  46. """Stop recording, close the turtle window, and exit: BYE"""
  47. print("Thank you for using Turtle")
  48. self.close()
  49. bye()
  50. return True
  51. if __name__ == "__main__":
  52. TurtleShell().cmdloop()