How do you trace on the graphics window where the user clicks?
I have to create a circle at the point they clicked and am not sure where to begin.
I tried;
win.getMouse()
center = Point(win.getMouse(),win.getMouse())
circ = Circle(center, 30)
circ.draw(win)
but obviously that doesn't work
Your initial code:
win.getMouse()
center = Point(win.getMouse(),win.getMouse())
circ = Circle(center, 30)
circ.draw(win)
can be edited to work and condensed to:
Circle(win.getMouse(), 30).draw(win)
That assumes win is an instance of the GraphWin class and has already been declared. win.getMouse returns a Point object, so there is no need to change a point into coordinate pairs just to change it back into a point...
Related
I want to create yet another clone of Pong with Python and Turtle. My goal is to let my pupils (that begin to code in Python) to practise a bit further.
I'd like to create a Turtle whose shape is an horizontal filled rectangle, like a stylized paddle. But when I create a shape I suppose to be convenient, I get a rotated (vertical) paddle instead of the horizontal one I hoped for.
Here is a code that demonstrates this odd behaviour.
from turtle import *
begin_poly()
fd(200)
left(90)
fd(40)
left(90)
fd(200)
left(90)
fd(40)
left(90)
end_poly()
shape = get_poly()
register_shape("drawn", shape)
polyNotOk = ( (0,0), (100, 0), (100, 20), (0, 20) )
register_shape("polyNotOk", polyNotOk)
polyOk = ( (0,0), (0, 100), (20, 100), (20, 0) )
register_shape("polyOk", polyOk)
t1 = Turtle(shape="drawn")
t2 = Turtle(shape="polyNotOk")
t3 = Turtle(shape="polyOk")
t1.color("black")
t2.color("red")
t3.color("blue")
t1.stamp()
t2.stamp()
t3.stamp()
t1.goto(100,200)
t2.goto(100,-50)
t3.goto(100,-150)
t1.forward(100)
t2.forward(100)
t3.forward(100)
mainloop()
So, as you can see if you run the code, the first drawing is OK, with an horizontal shape. But when I stamp the Turtle t1, the shape is vertical.
Same problem with the 2nd shape, defined through polyNotOk (with values for x and y coords which allow to get a horizontal paddle). I need to create a "vertical" poly to get a horizontal paddle.
So I'm able to find a workaround. Yet I'm still not satisfied with this solution, so I'm asking for brilliant explanations ;-) Thanks in advance.
I hope to illuminate this odd behavior, not defend it. First thing to remember about drawn cursors is that where ever (0, 0) falls in your cursor drawing, that's the center point about which your cursor rotates and the pixel in your cursor that lands on any point you goto().
Some insight might be found in the shapesize() method documentation:
shapesize(stretch_wid=None, stretch_len=None, outline=None)
stretch_wid is stretchfactor perpendicular to orientation
stretch_len is stretchfactor in direction of turtles orientation.
That is, if the cursor is in the default (East) orientation, this reverses the sense of X and Y. I believe that's what you're seeing in your drawing. The X plane is perpendicular to orientation (vertical) and the Y plane is in the direction of orientation (horizontal). The opposite of what we normally expect.
This doesn't appear to be the fault of the Shape() class, but buried in the cursor logic. It may be a historical artifact--if we change to mode("logo") and run your code, we get:
More what we might expect, given that "logo" mode's default orientation is North, and more consistent than before.
Regardless, I would make my paddles a different way. Instead of a custom cursor, I'd use turtle's square cursor and reshape it as needed using shapesize():
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
t0 = Turtle("square")
t0.shapesize(20 / CURSOR_SIZE, 100 / CURSOR_SIZE)
t0.color("green")
screen.exitonclick()
Still rotated logic (not graphic) from what you might expect, but at least the documentation told us that. But, what I really tend to do is make the paddle in the wrong orientation, and use settiltangle(), but not as a workaround as you did, but to make my paddle face in one direction, but move in the other:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
t0 = Turtle("triangle")
t0.shapesize(100 / CURSOR_SIZE, 20 / CURSOR_SIZE)
t0.settiltangle(90)
t0.penup()
t0.color("green")
t0.speed("slowest") # for demonstration purposes
t0.forward(300)
t0.backward(600)
t0.forward(300)
screen.exitonclick()
Notice that I can use forward(10) and backward(10) to move my paddle and not have to do awful things like t0.setx(t0.xcor() + 10). Works great for Space Invader type games where the player faces upwards but moves sideways.
With Python 3.6 and the Turtle module, I would like to know how to print the coordinates of the turtle on screen.
My questions are: How do I print text on the screen, and how do I make that text the player's coordinates?
Here is my code.
from turtle import Turtle, Screen
play = Screen()
play.bgcolor("black")
play.screensize(250, 250)
play.title("Turtle Keys")
def position():
coord = follow.coor()
coord.color("white")
coord.setposition(130, 100)
run = Turtle("triangle")
run.speed("fastest")
run.color("white")
run.penup()
run.setposition(250, 250)
print(position)
Thank you.
Edit I tried write, but it throws a name not defined error.
Here is how you can write the turtle position on screen: the write method from the turtle module, must be called on a Turtle object.
import turtle
playground = turtle.Screen() # use nouns for objects, play is a verb
playground.bgcolor("black")
playground.screensize(250, 250)
playground.title("Turtle Keys")
tom = turtle.Turtle() # use nouns for objects, run is a verb
tom.color("white")
tom.setposition(130, 100)
tom.speed("fastest")
tom.color("white")
p = tom.pos() # here you get the coordinates of the turtle
tom.write(str(p), True) # and you print a string representation on screen
tom.penup()
print(p) # this prints to the console
playground.exitonclick()
The way I approach this type of problem is to dedicate a turtle to each fixed label on the screen. That way you can permanentely locate a turtle at that position and simply do undo() to clear the old text followed by write() to write the new.
Here's a dynamic example where you can drag the turtle around the screen with your mouse and the current position will be written to the center of the screen:
from turtle import Turtle, Screen
FONT = ('Arial', 24, 'normal')
playground = Screen()
playground.setup(500, 500)
playground.bgcolor("black")
runner = Turtle("triangle")
runner.color("white")
runner.speed("fastest")
runner.penup()
runner.setposition(200, 200)
marker = Turtle(visible=False) # our virtual magic marker
marker.penup()
marker.color("yellow")
marker.write(runner.position(), align='center', font=FONT) # so we can undo it
def drag_handler(x, y):
runner.ondrag(None) # disable handler while in handler
marker.undo() # erase previous position
marker.write((x, y), align='center', font=FONT)
runner.setheading(runner.towards(x, y)) # head towards new location
runner.setposition(x, y)
runner.ondrag(drag_handler)
runner.ondrag(drag_handler)
playground.mainloop()
Use the functions xcor() and ycor() to get coordinates. Use the function write() to write on the screen.
See documentation: https://docs.python.org/3/library/turtle.html
Maybe this will help:
def write_pos(self, position):
self.pu()
self.bk(len(str(position))-len(str(position))/2)
self.pd()
self.write(str(position))
self.pu()
self.fd(len(str(position))-len(str(position))/2)
self.pd()
write_pos(run, run.position())
Ok. Here's the explanation.
So I defined a function writing the position of something (in this case, the turtle).
When I do:
self.pu()
self.bk(len(str(position))-len(str(position))/2)
I:
Lift the pen up and
Move backward some steps, so I can center the text.
I then do:
self.pd()
self.write(str(position))
Which:
Puts the pen down and
Writes (or outputs) the position
After that, when I do:
self.pu()
self.fd(len(str(position))-len(str(position))/2)
self.pd()
I:
Pick the pen up
Move it back to the original position and
Put the pen down again
EDIT:
I just realized that you can do it a much more simple way of using write like this:
write(run.position(), False, center)
It writes the turtles position, it doesn't move, and it still writes the text but with the canter as the place where it aligns.
EDIT #2:
I already figured out the problem.
Your using write directly and not actually calling it from the object. You should call write like this:
run.write(run.position(), align="center")
I am trying to create a simulator. (referring to John Zelle's graphics.py)
Basically, my object will make use of graphics.py to display the object as a circle. Then, using the .move method in the class in graphics.py, the object will move in the x direction and y direction. If the object is currently drawn, the circle is adjusted to the new position.
Moving just one object can easily be done with the following codes:
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
for i in range(40):
c.move(30, 0) #speed=30
time.sleep(1)
win.close()
However, I want the program to display multiple circles at once that moves at different speed. I've created a Circle object class which takes speed as an input, and a list with 3 Circle objects in it
circle = []
circle1 = Car(40)
circle2= Car(50)
circle3 = Car(60)
In summary, my question is, how do make use of this list such that I am able to display and move multiple circles in one window at once using the methods available in graphics.py?
That all depends on how you create your Car class, but nothing stops you from using the same code to move multiple circles in the same refresh cycle, e.g.:
win = GraphWin("My Circle", 1024, 400)
speeds = [40, 50, 60] # we'll create a circle for each 'speed'
circles = [] # hold our circles
for speed in speeds:
c = Circle(Point(50, speed), 10) # use speed as y position, too
c.draw(win) # add it to the window
circles.append((c, speed)) # add it to our circle list as (circle, speed) pair
for i in range(40): # main animation loop
for circle in circles: # loop through the circles list
circle[0].move(circle[1], 0) # move the circle on the x axis by the defined speed
time.sleep(1) # wait a second...
win.close()
Of course, if you're already going to use classes, you might as well implement move() in it so your Car instances can remember their speed and then just apply it when you call move() on them in a loop.
I was showing a grandson patterns drawn with Python's Turtle module,
and he asked to see concentric circles.
I thought it would be faster to use the turtle's circle() to draw them
than to write my own code for generating a circle. Ha! I am stuck.
I see that the circle produced begins its circumference at the turtle's
current location and its direction of drawing depends on turtle's current
direction of motion, but I can't figure out what I need to do to get
concentric circles.
I am not at this point interested in an efficient way of producing
concentric circles: I want to see what I have to do to get
this way to work:
def turtle_pos(art,posxy,lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape,tcolor,pen_color,pen_thick,scolor,radius,mv):
window=turtle.Screen() #Request a screen
window.bgcolor(scolor) #Set its color
#...code that defines the turtle trl
for j in range(1,11):
turtle_pos(trl,[trl.xcor()+mv,trl.ycor()-mv],1)
trl.circle(j*radius)
drawit("turtle","purple","green",4,"black",20,30)
You can do it like this:
import turtle
turtle.penup()
for i in range(1, 500, 50):
turtle.right(90) # Face South
turtle.forward(i) # Move one radius
turtle.right(270) # Back to start heading
turtle.pendown() # Put the pen back down
turtle.circle(i) # Draw a circle
turtle.penup() # Pen up while we go home
turtle.home() # Head back to the start pos
Which creates the picture below:
Basically it moves the turtle down one radius lenght to keep the center point for all the circles in the same spot.
From the documentation:
The center is radius units left of the turtle.
So wherever the turtle is when you start to draw a circle, the center of that circle is some distance to the right. After each circle, just move left or right some number of pixels and draw another circle whose radius is adjusted for the distance the turtle moved. For example, if you draw a circle with a radius of 50 pixels, then move right 10 pixels, you would draw another circle with a radius of 40, and the two circles should be concentric.
I am not at this point interested in an efficient way of producing
concentric circles: I want to see what I have to do to get this way to
work
To address the OP's question, the change to their original code to make it work is trivial:
turtle_pos(trl, [trl.xcor() + mv, trl.ycor() - mv], 1)
trl.circle(j * radius)
becomes:
turtle_pos(trl, [trl.xcor(), trl.ycor() - mv], 1)
trl.circle(j * mv + radius)
The complete code with the above fix and some style changes:
import turtle
def turtle_pos(art, posxy, lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape, tcolor, pen_color, pen_thick, scolor, radius, mv):
window = turtle.Screen() # Request a screen
window.bgcolor(scolor) # Set its color
#...code that defines the turtle trl
trl = turtle.Turtle(tshape)
trl.pencolor(pen_color)
trl.fillcolor(tcolor) # not filling but makes body of turtle this color
trl.width(pen_thick)
for j in range(10):
turtle_pos(trl, (trl.xcor(), trl.ycor() - mv), True)
trl.circle(j * mv + radius)
window.mainloop()
drawit("turtle", "purple", "green", 4, "black", 20, 30)
So now i am giving you the exact code that can draw concentric circles.
import turtle
t=turtle.Turtle()
for i in range(5):
t.circle(i*10)
t.penup()
t.setposition(0,-(i*10))
t.pendown()
turtle.done()
I've been trying to create a graph using a create_line and a list of (x,y) points.
import Tkinter
Screen = [a list of screen coordinates]
World = []
for x,y in screen:
World.append(somefunctiontochange(x,y))
if len(World) >= 2:
Canvas.create_line(World)
The line doesn't show in my canvas though, and no error was given. Any help?
Took me a while but this is how you draw to a canvas in the way you want:
import Tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Drawing lines to a canvas")
cv = tk.Canvas(root,height="500",width="500",bg="white")
cv.pack()
def linemaker(screen_points):
""" Function to take list of points and make them into lines
"""
is_first = True
# Set up some variables to hold x,y coods
x0 = y0 = 0
# Grab each pair of points from the input list
for (x,y) in screen_points:
# If its the first point in a set, set x0,y0 to the values
if is_first:
x0 = x
y0 = y
is_first = False
else:
# If its not the fist point yeild previous pair and current pair
yield x0,y0,x,y
# Set current x,y to start coords of next line
x0,y0 = x,y
list_of_screen_coods = [(50,250),(150,100),(250,250),(350,100)]
for (x0,y0,x1,y1) in linemaker(list_of_screen_coods):
cv.create_line(x0,y0,x1,y1, width=1,fill="red")
root.mainloop()
You need to supply create_line with the x,y positions at the start and end point of the line, in the example code above (works) I'm drawing four lines connecting points (50,250),(150,100),(250,250),(350,100) in a zigzag line
Its worth pointing out also that the x,y coords on a canvas start at the top left rather than the bottom left, think of it less like a graph with the x,y = 0,0 in the bottom left of the canvas and more how you would print to a page starting in top left corner moving to the right in the x and with the y incrementing as you move down the page.
I used:
http://www.tutorialspoint.com/python/tk_canvas.htm as reference.
If you aren't getting errors and you're certain your function is being called, you probably have one of three problems:
Is your canvas visible? A common mistake for beginners is to either forget to pack/grid/place the canvas, or to neglect to do that for all of its containers. An easy way to verify is to temporarily give your canvas a really bright background so that it stands out from the rest of the GUI.
Have you set the scroll region? The other explanation is that the drawing is happening, but it's happening in an area that is outside the viewable portion of the canvas. You should be setting the scrollregion attribute of the canvas after creating your drawings, to make sure everything you're drawing can be made visible.
Does your canvas and canvas objects have an appropriate color? It's possible that you've changed the background of the canvas to black (since you don't show that code in your question), and you're using a default color of black when creating your lines.