Python Turtle Graphics Simplification - python

just reviewing for my upcoming midterm. We were given Past midterm problems but no solutions. I am trying to grasp the knowledge best i can.
For this problem, it asks to define a function named equalSigns, pass it values t and length. So, i just need to make my program in turtle graphics, create two parellel line, simple enough i suppose. this is my code that i wrote just for it to correctly output an equal sign of x length. (then of course i would convert it to a function) My question, is there any better way to create this?
import turtle
t=turtle.Turtle()
s=turtle.Screen()
t.forward(200)
t.penup()
t.home()
t.right(90)
t.forward(50)
t.pendown()
t.left(90)
t.forward(200)
'''i suppose i dont have to go home and then down.
instead just continue and go down and forward left.
but either way, is this the best approach to take?
'''

Yes, I think there's a better way. Most of all, I think you turned the wrong way: you need to make a second right turn to come back along the lower line.
You could make a routine that does a half-equals, and then all it twice to get the two lines. Think of this as drawing a rectangle, except that the short sides are invisible.
# Draw long side
t.pendown()
t.forward(x)
t.penup()
t.right(90)
# Move along short side without drawing
t.forward(x/4)
t.right(90)
That gets you to the opposite corner of the rectangle. Call this twice, and you're done ... and back at the starting point.

Perhaps you could have your turtle think outside the shell:
import turtle
import tkinter as _
_.ROUND = _.BUTT
turtle.width(50)
turtle.forward(200)
turtle.color("white")
turtle.width(48)
turtle.backward(200)
turtle.done()
(The vertical gray bars at the two ends are artifacts of GIF conversion and not present when the program is run.)

Related

Why is there a second Turtle?

I am learning turtle graphics in python and for some reason there is a second turtle on the screen and I haven't even created a second turtle. How can I get rid of the second turtle?
import turtle
s = turtle.getscreen()
t = turtle.Turtle()
for i in range(4):
t.fd(100)
t.rt(90)
turtle.exitonclick()
The second turtle at the starting location appears because of the line s = turtle.getscreen().
This line is not needed (you do not use s), and if you remove it this turtle disappears but the rest of the code seems to work as before.
The turtle library exposes two interfaces, a functional one (for beginners) and an object-oriented one. You got that extra turtle because you mixed the two interfaces (and #mkrieger1's solution doesn't fix that completely).
I always recommend an import like:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
for _ in range(4):
turtle.forward(100)
turtle.right(90)
screen.exitonclick()
This gives you access to the object-oriented interface and blocks the functional one. Mixing the two leads to all sorts of bugs and artifacts.
To combine the answer from mkrieger1 and cdlane, you could replace
s = turtle.getscreen()
with
s = turtle.Screen()
You've still got a variable holding the screen (in case you should ever need it), and it doesn't generate that extra turtle in the center.

Randomly chose an action (definition) with random.choice()

Code doesn't work like it should. It is just a small thing i would like to learn as a new coder:
import turtle
import random
me=turtle.Turtle()
def up_right():
me.right(90)
me.forward(100)
def down_right():
me.right(90)
me.forward(100)
choose = (up_right(), down_right)
random.choice(chose)
It should pick one and do it but it picks them both.
I've tried random.sample and random.choice but cant get them to work.
Besides the typo of choose... My suggestion is that after you create a tuple of functions and choose the function using random.choice(), you should make a call to the function chosen by the random.choice().
# Notice I removed the () after up_right so it doesn't make the function call on this line
choose = (up_right, down_right)
# random.choice will return one of the two, and then the () will call whatever function was chosen
random.choice(choose)()
I see three issues with your code. The first two folks have pointed out already, the choose vs chose typo and leaving the parens () on upright when refering to it as a function (up_right(), down_right).
The third is that up_right and down_right both implement the same motion, so even if the rest of your code worked, you wouldn't see any difference! Below's a rewrite that fixes this issue:
from turtle import Screen, Turtle
from random import choice
def up_right(turtle):
turtle.setheading(90)
turtle.forward(100)
def down_right(turtle):
turtle.setheading(270)
turtle.forward(100)
choices = [up_right, down_right]
screen = Screen()
me = Turtle('turtle')
choice(choices)(me)
screen.mainloop()
Run it several times and you'll see sometimes the turtle heads up the screen, sometimes it heads down.

Python turtle drawing updated for turtle.tracer(0,0)

I am a newbie using python 3.2.3
When I ran the module in the python IDLE, the turtle drew a square without any update on the screen, so the window appeared blank, and after I input any key, the turtle and the square appeared due to the call of turtle.update().
However, when I double-clicked the .py file storing the below code in my document and executed it directly, the square always showed up before I input any key.
The interesting part is that the turtle was not shown but only the square was shown.
It seems that there was a turtle update only for the square after drawing even if I had already set turtle tracer to (0,0).
Is this considered a bug and how can I solve it? Thanks for the help.
import turtle
def drawSquare():
turtle.down();
turtle.begin_fill();
turtle.goto(10, 0);
turtle.goto(10, 10);
turtle.goto(0, 10);
turtle.goto(0, 0);
turtle.end_fill();
turtle.up();
def tUpdate():
turtle.update();
turtle.tracer(0,0);
drawSquare();
input("Not updated. Press any key.");
tUpdate();
print("Updated");
turtle.mainloop();
You've got a couple of things working against you: the poor documenation provided for tracer() and the fact that end_fill() and up() cause updates to occur. tracer() is not really meant to hide things from the user until you're ready to display them -- it's a speed optimization so that the user doesn't have to see every drawing step in a complicated image. You don't have full control over when updates will occur.
Here's a rework of your example that displays the behaviour you want at the cost of it no longer being a filled square. I've swapped your input() trigger for a mouse click on the window instead but your approach will work just as well here too:
from turtle import Turtle, Screen
def drawSquare(turtle):
turtle.goto(100, 0)
turtle.goto(100, 100)
turtle.goto(0, 100)
turtle.goto(0, 0)
screen = Screen()
screen.tracer(0, 0)
screen.onclick(lambda x, y: screen.update())
turtle = Turtle()
drawSquare(turtle)
screen.mainloop()
I also made which are turtle methods, and which are screen methods, more explicit. The beginning programmer friendly design of the turtle library tends to blur these in the interest of ease of use.

Python Turtle Graphics - Bring A Turtle To The Front

I have two turtles in my program. An animation happened where they collide together, but I would like one turtle to be on top of the other like this:
So, my question is - how can I make this happen - is there a simple line of code such as: turtle.front(), if not what is it?
I discuss this briefly in my response to Make one turtle object always above another where the rule of thumb for this simple situation is:
last to arrive is on top
Since Python turtle doesn't optimize away zero motion, a simple approach is to move the turtle you want on top by a zero amount:
import turtle
def tofront(t):
t.forward(0)
gold = turtle.Turtle("square")
gold.turtlesize(5)
gold.color("gold")
gold.setx(-10)
blue = turtle.Turtle("square")
blue.turtlesize(5)
blue.color("blue")
blue.setx(10)
turtle.onscreenclick(lambda x, y: tofront(gold))
turtle.done()
The blue turtle overlaps the gold one. Click anywhere and the situation will be reversed.
Although #Bally's solution works, my issue with it is that it creates a new turtle everytime you adjust the layering. And these turtles don't go away. Watch turtle.turtles() grow. Even my solution leaves footprints in the turtle undo buffer but I have to believe it consumes less resources.
Just add this to turtle.py file
def tofront(self, tobring):
newfront = tobring.clone()
tobring.ht()
return newfront
works correct,
so method - sould return you new clone of turtle, on the top of all other turtles.
parameter tobring - it's your current turtle (the one you want to bring to front)
you can use this def in your program or put it into turtle.py file
if so, don't forget to add it to list of commands, - _tg_turtle_functions
_tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'tofront', 'color',
I did it after clone command
Hope this will help you

Draw faster circles with Python turtle

I have an exercise wherein I have to draw a lot of circles with Python turtle. I have set speed(0) and I am using:
from turtle import*
speed(0)
i=0
while i < 360:
forward(1)
left(1)
i+=1
to draw circles. It takes so long. Is there any faster way?
Have you tried turtle.delay() or turtle.tracer() ? See documentation here and here. These set options for screen refreshing which is responsible for most of the delays.
Turtle has a function for drawing circles and the speed of that is much faster than going in a circle one step at a time.
import turtle
tina=turtle.Turtle()
tina.circle(70)
It will look like this
If you want to draw your circle even faster, you can try adding a delay block as well.
import turtle
tina=turtle.Turtle()
tina.delay(1)
tina.speed(0)
You could draw fewer segments, so rather than 360 you go for 120:
while i < 360:
forward(3)
left(3)
i+=3
That will make your circle less smooth, but three times faster to draw.
The circle() method might not be faster, but may be easier to manage:
turtle.circle()
Use Multithread to draw two semi circles simultaneously.
Initially the turtle will be at (0,0) so just clone the turtle and make them both face in opposite direction by 180° then draw semicircles. The code is below:
from threading import Thread
import turtle
t = turtle.Turtle()
t.speed(0)
def semi1(r):
r.circle(50,180)
def semi2(t):
t.circle(50,180)
r = t.clone()
r.rt(180)
a = Thread(target=semi1).start()
b = Thread(target=semi2).start()
This may draw the circle fast.

Categories

Resources