diff --git a/act1_cowabunga.py b/act1_cowabunga.py new file mode 100644 index 0000000..ba370f6 --- /dev/null +++ b/act1_cowabunga.py @@ -0,0 +1,25 @@ +# CHANGEME +# turn angle 90 +replacementRules = { + "x": "x+yf+", + "y": "-fx-y", + "+": "+", + "-": "-", + "f": "f" +} + +def generateCowabunga(generations): + print("initialized and running") + # CHANGEME + axiom = "fx" + structure = "" + for _ in range(generations): + print(f"gen {_} starting") + if (structure): + axiom = structure + structure = "" + for _ in axiom: + print(f"{structure}") + structure += replacementRules[_] + axiom = structure + return structure \ No newline at end of file diff --git a/carl.png b/carl.png new file mode 100644 index 0000000..68a59db Binary files /dev/null and b/carl.png differ diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..fa6a443 --- /dev/null +++ b/demo.py @@ -0,0 +1,50 @@ + + + + + +actions = { + "f" : "go forward", + "+" : "turn right", + "-": "turn left" +} + + + + +# import turtle + +# t = turtle.Turtle() + + +# def forward(): +# return t.forward(50) +# def left(): +# return t.left(90) +# def right(): +# return t.right(90) + +# actions = { +# "f" : forward, +# "+": right, +# "-": left +# } + +# for letter in "f+f+f+f": + +# actions[letter]() + + + + + + + + + + + + + + + diff --git a/driver.py b/driver.py new file mode 100644 index 0000000..b555cf8 --- /dev/null +++ b/driver.py @@ -0,0 +1,37 @@ +import turtle +import code +import sys + +import pathlib +import importlib.util +import argparse + +parser = argparse.ArgumentParser(description='Process some integers.') +parser.add_argument('files', metavar='FILES', type=pathlib.Path, nargs='*') +args = parser.parse_args() + +screen = turtle.Screen() +canvas = screen.getcanvas() +tk = canvas.winfo_toplevel() +tk.attributes('-fullscreen', True) + +screen.title("Python Turtle on Replit") +screen.setup(1.0, 1.0) + +print("Python turtle on Replit") +modules = [] +for path in args.files: + spec = importlib.util.spec_from_file_location(path.stem, path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + modules.append(module) + +locals = {} +if len(modules) > 0: + main = modules[0] + locals = {attr: getattr(main, attr) for attr in dir(main)} + +sys.ps1 = "\u001b[33m\uEEA7\u001b[00m " +code.interact(local=locals, banner='') + +screen.mainloop() diff --git a/ex_arrow.py b/ex_arrow.py new file mode 100644 index 0000000..eac84a1 --- /dev/null +++ b/ex_arrow.py @@ -0,0 +1,29 @@ +""" + FX Axiom + X -> YF+XF+Y Rule 1 + Y -> XF-YF-X Rule 2 + angle = 60 +""" + +replacementRules = { + "x": "yf+xf+y", + "y": "xf-yf-x", + "+": "+", + "-": "-", + "f": "f" +} + +def generateArrow(generations): + print("initialized and running") + axiom = "fx" + structure = "" + for _ in range(generations): + print(f"gen {_} starting") + if (structure): + axiom = structure + structure = "" + for _ in axiom: + print(f"{structure}") + structure += replacementRules[_] + axiom = structure + return structure \ No newline at end of file diff --git a/ex_dragon.py b/ex_dragon.py new file mode 100644 index 0000000..95c3a23 --- /dev/null +++ b/ex_dragon.py @@ -0,0 +1,29 @@ +""" +AXIOM: fx +RULES: + +x -> x+yf+ +y -> -fx-y + +""" + + +replacementRules = { + "x": "x+yf+", + "y": "-fx-y", + "+": "+", + "-": "-", + "f": "f" +} + +def generateDragon(generations): + axiom = "fx" + structure = "" + for _ in range(generations): + if (structure): + axiom = structure + structure = "" + for _ in axiom: + structure += replacementRules[_] + axiom = structure + return structure diff --git a/ex_fern.py b/ex_fern.py new file mode 100644 index 0000000..fbb1edc --- /dev/null +++ b/ex_fern.py @@ -0,0 +1,30 @@ +""" + Axiom X + F --> FF + X --> x[-fff][+fff]fx + ø = 22.5 +""" + +replacementRules = { + "x": "y[-fff][+fff]fy", + "y": "yfx[+y][-y]", + "+": "+", + "-": "-", + "f": "f", + "[": "[", + "]": "]" +} + +def generateFern(generations): + axiom = "yy" + structure = "" + for _ in range(generations): + print(f"gen {_}") + if (structure): + axiom = structure + structure = "" + for _ in axiom: + structure += replacementRules[_] + print(structure) + axiom = structure + return structure diff --git a/ex_weave.py b/ex_weave.py new file mode 100644 index 0000000..f40420a --- /dev/null +++ b/ex_weave.py @@ -0,0 +1,28 @@ +""" + Axiom F+F+F+F + F --> FF+F-F+F+FF + ø = 90 +""" + +replacementRules = { + "x": "", + "y": "", + "+": "+", + "-": "-", + "f": "ff+f-f+f+ff" +} + +def generateWeave(generations): + print("initialized and running") + axiom = "f+f+f+f" + structure = "" + for _ in range(generations): + print(f"gen {_} starting") + if (structure): + axiom = structure + structure = "" + for _ in axiom: + print(f"{structure}") + structure += replacementRules[_] + axiom = structure + return structure \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..36f7005 --- /dev/null +++ b/main.py @@ -0,0 +1,84 @@ +# File for running all turtle code and generating structure +import turtle +from act1_cowabunga import generateCowabunga +from ex_dragon import generateDragon +from ex_fern import generateFern +from ex_arrow import generateArrow +from ex_weave import generateWeave + +# initialization +t = turtle.Turtle() +t.shape("turtle") +t.speed(10) +t.left(90) + +# This variable determines how far the turtle +# moves everytime we call the goForward function +moveDistance = 10 +# The angle at which the turtle runs +turnAmount = 22.5 +# Fancy Computer-Sciency data structure that helps us +# keep track of all the coordinates we visit while generating structures with turtle +stack = [] + + +#Enables the turle to go forward by the distance, moveDistance +def goForward(): + t.forward(moveDistance) + + +# Turns the turtle left by the turn amount +def goLeft(): + t.left(turnAmount) + + +# Turns the turtle right by the turn amount +def goRight(): + t.right(turnAmount) + + +# 10 points if you can guess what this does! +def doNothing(): + pass + + +# Put stuff onto the stack (fancy CS data strucutre) +def pushStack(): + coor = t.position() + stack.append(coor) + # print(f"pushed {coor}") + + +# Get stuff out of the stack (fancy CS data strucutre) +def popStack(): + coor = stack.pop() + t.penup() + t.goto(coor) + t.pendown() + # print(f"popped {coor}") + + +# Map stuff from our replacement rules to the functions above +actions = { + "f": goForward, + "+": goLeft, + "-": goRight, + "x": doNothing, + "y": doNothing, + "[": pushStack, + "]": popStack +} + +# Generates the structure according to the given code +def run(structure): + for i in structure: + actions[i]() + + +# # Run this! +# run(generateCowabunga(5)) +# examples +# run(generateDragon(10)) #angle = 90 +run(generateFern(5)) #angle = 22.5 +# run(generateArrow(7)) #angle = 60 +# run(generateWeave(3)) #angle = 90 \ No newline at end of file diff --git a/panAndZoom.py b/panAndZoom.py index edab31b..0b11165 100644 --- a/panAndZoom.py +++ b/panAndZoom.py @@ -45,4 +45,4 @@ def setListens(): def initialize(): setListens() - listen() + listen() \ No newline at end of file