Python Forum
Divide a Python Turtle program into subprograms
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide a Python Turtle program into subprograms
#1
Hello everyone,

I want to draw a simple house with several windows and a door by using the python turtle inferface.
The window(s) and door(s) have to be implemented by modular programming. In principle I am successful in it (see program code below).

 
import turtle

def contour():
	turtle.left(90)
	turtle.forward(200)
	turtle.right(45)
	turtle.forward(142)
	turtle.right(90)
	turtle.forward(142)
	turtle.right(45)
	turtle.forward(200)
	turtle.right(90)
	turtle.forward(200)

def window():
	turtle.seth(90)
	for a in range (4):
		turtle.forward(30)
		turtle.right(90)

def door():
	turtle.seth(90)
	turtle.forward(60)
	turtle.right(90)
	turtle.forward(30)
	turtle.right(90)
	turtle.forward(60)
	turtle.right(90)
	turtle.forward(30)


contour()
turtle.pu()
turtle.goto(25,150)
turtle.pd()
window()
turtle.pu()
turtle.goto(85,150)
turtle.pd()
window()
turtle.pu()
turtle.goto(85,0)
turtle.pd()
door()

turtle.done()
But instead of defining CONTOUR, WINDOW and DOOR at the beginning of the main program HOUSE.py, I would like to have the contour, windows and doors as seperate independent programs CONTOUR.py, DOOR.py and WINDOW.py and then "import" them in the main program HOUSE.py.
So something like this:

import turtle

contour.py		#Which command is needed to execute the program "contour.py"
turtle.pu()
turtle.goto(25,150)
turtle.pd()

window.py
turtle.pu()
turtle.goto(85,150)
turtle.pd()

window.py
turtle.pu()
turtle.goto(85,0)
turtle.pd()

door.py
turtle.done()
I have tried to solve the problem with the command import [NAME OF THE PROGRAM] but then I have to create a new turtle in each program. This then results in several turtles with its own different positions, which is not wanted, since modularity is not given than any more.
So in the end I do want to create modularity by using seperate programs (*py) rather than using seperate def-commands within the mnain program.

I hope somebody can help me out
Waiting for your response
Reply
#2
pass the turtle as argument to your functions. then you can have one turtle in the main file and pass it when calling the imported functions.
also you need to import the other scripts, e.g. import countour or from contour import contour, not just write contour.py
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
How exactly does that work? I am just starting to learn the Python language. Would you be able to provide me with an example or could you perhaps rewrite my program in a way it works with your proposal?
Reply
#4
for example, in contour.py you will have

def contour(turtle):
    turtle.left(90)
    turtle.forward(200)
    turtle.right(45)
    turtle.forward(142)
    turtle.right(90)
    turtle.forward(142)
    turtle.right(45)
    turtle.forward(200)
    turtle.right(90)
    turtle.forward(200)
then in main.py you will have

import turtle
from contour import contour # import contour function from contour.py

contour(turtle)
turtle.done()
obviously it works, but if you have learned that it's better to create an instance of Turtle class, e.g.
my_turtle = turtle.Turtle()
and use it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
WOW, Many, many thanks
It now works!!! You saved my day!
Reply
#6
note that you make your functions more universal, e.g. pass optional arguments for x, y position of the turtle to start from. If no x, y are passed - used the current position, but if user pass x and y - move the turtle to that position in the beginning of the function.
That will allow to use same function to draw same shape at different locations - e.g. 2 windows at different positions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How Can I Draw Circle With Turtle İn Python ? mdm 2 2,751 Jun-03-2021, 02:07 PM
Last Post: DPaul
  divide a number iin two marciotos 2 1,932 Jan-20-2020, 12:25 PM
Last Post: marciotos
  Turtle Polygon drawing program tp_oz 3 3,187 Jul-23-2019, 01:01 PM
Last Post: ichabod801
  python idle turtle graphics help jojo 5 4,019 Apr-14-2019, 12:28 PM
Last Post: ichabod801
  Python Turtle Help patrick_oneal_4 5 3,636 Jan-27-2019, 04:39 AM
Last Post: ichabod801
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,576 May-17-2018, 05:39 PM
Last Post: j.crater
  Divide a vector Langosmon 1 2,705 May-13-2018, 09:09 AM
Last Post: ThiefOfTime
  Divide by 0 error kethyar 1 3,154 Jul-16-2017, 05:55 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020