I haven't been able to find anything online for this but I need to create a program that:
If the left button is pressed, the turtle should move to that location and draw a small square.
If the right button is pressed, the turtle should move that that location and draw a small circle.
If the middle button is pressed, the turtle should change to a different random color.
You should also change the color if the use presses the space bar.
Any suggestions on how to start?
Here is some code I have tried so far:
def k2(x,y): turtle.penup() turtle.setposition(x,y) turtle.pendown() turtle.circle(radius)
This is the top from turtle
import * setup(500, 500) Screen() title("Turtle Keys") move = Turtle() showturtle()
This is the bottom
onkey(k1, "Up") onkey(k2, "Left") onkey(k3, "Right") onkey(k4, "Down") listen() mainloop()
Below is my guess as to what you and your code snippets describe. Note that I changed the circle and square functions from the left and right keyboard buttons to the left and right mouse buttons which seemed to make more sense in the context of, "move to that location":
import turtle
import random
colors = ["red", "orange", "yellow", "green", "blue", "violet"]
radius = 10
width = 20
LEFT, MIDDLE, RIGHT = 1, 2, 3
def k1(x=None, y=None): # dummy arguments so can be a click or key
turtle.color(random.choice(colors))
def k2(x, y):
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
for _ in range(4):
turtle.forward(width)
turtle.right(90)
def k3(x, y):
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
turtle.circle(radius)
turtle.setup(500, 500)
turtle.Screen().title("Turtle Keys")
turtle.onkey(k1, " ")
turtle.onscreenclick(k2, btn=LEFT)
turtle.onscreenclick(k1, btn=MIDDLE)
turtle.onscreenclick(k3, btn=RIGHT)
turtle.listen()
turtle.mainloop()
Related
I am trying to move the object around the screen with the arrow keys. I have left and right how I want them, but when I am trying to go up it doesn't work how I want.
I tried to use player.left(90) then move forward but then I have to use the right arrow key to move up. I would prefer to not even have the object rotate 90deg
I just want to easily move the object around the window.
python
import turtle
window = turtle.Screen()
window.bgcolor('black')
player = turtle.Turtle()
player.shape('square')
player.color('white')
player.penup()
def move_left():
player.color('light green')
player.backward(10)
def move_right():
player.color('light green')
player.forward(10)
def move_up():
player.color('light green')
player.left(90)
player.forward(10)
window.onkeypress(move_left, "Left")
window.onkeypress(move_right, "Right")
window.onkeypress(move_up, "Up")
window.listen()
turtle.done()
I am trying to draw a square in turtle without any thing filled in it. I would manually draw it with a loop, but I am trying to write a program where if the square is touched, something would happen. If I drew it with a loop, even if I touch the border, it would not work, because the actual turtle isn't there.
We can do this by dedicating one turtle as the button. We need to change its shape, size and fill color and assign it a button event handler:
from turtle import Screen, Turtle
def thanks(x, y):
marker.write("Thank you!", align='center', font=('Arial', 24, 'bold'))
marker = Turtle()
marker.hideturtle()
marker.penup()
marker.sety(-200)
button = Turtle()
button.shape('square')
button.fillcolor('white')
button.shapesize(3)
button.onclick(thanks)
screen = Screen()
screen.mainloop()
Click on the square in the middle of the screen and it will thank you for doing so.
I am trying to draw a square in turtle without any thing filled in it.
The above solution does use a filled turtle, it just happens to be white fill. We can't draw within the square that is this button. If that's the need, then we can draw the square with a loop, but move our onclick() from the turtle to the screen. We'll need to make the onclick() itself determine if it's in the right place:
from turtle import Screen, Turtle
BUTTON_POSITION = (100, 100)
BUTTON_SIZE = 60
def thanks(x, y):
bx, by = BUTTON_POSITION
if bx < x < bx + BUTTON_SIZE and by < y < by + BUTTON_SIZE:
turtle.write("Thank you!", align='center', font=('Arial', 24, 'bold'))
turtle = Turtle()
turtle.hideturtle()
# draw the button
turtle.penup()
turtle.goto(BUTTON_POSITION)
turtle.pendown()
for _ in range(4):
turtle.forward(BUTTON_SIZE)
turtle.left(90)
# decorate our button a bit
turtle.penup()
turtle.forward(BUTTON_SIZE/2)
turtle.left(90)
turtle.forward(BUTTON_SIZE/2)
turtle.dot()
# position turtle for writing later
turtle.goto(0, -200)
screen = Screen()
screen.onclick(thanks)
screen.mainloop()
I want to start drawing with the mouse, and I had the idea to do onclick and then the turtle comes to the mouse, and then ondrag turtle I start the freehand drawing, although turtle doesn't detect the dragging. Here is the code. Can you help?
import turtle
def go_to_mouse(x, y):
my_pen.penup()
my_pen.goto(x, y)
my_pen.pendown()
def click_right(x, y):
my_pen.clear()
def drag_handler(x, y):
my_pen.ondrag(None)
my_pen.goto(x, y)
my_pen.ondrag(drag_handler)
window = turtle.Screen()
window.setup(width=1000, height=1000)
window.title('drawing')
window.delay(0)
my_pen = turtle.Turtle()
my_pen.speed(0)
my_pen.color("red")
my_pen.penup()
my_pen.goto(0, 0)
my_pen.pendown()
my_pen.ondrag(drag_handler)
window.onclick(go_to_mouse, 1)
turtle.onscreenclick(click_right, 3)
window.listen()
turtle.mainloop()
Let's first be clear that your code works if you "click again" after the turtle moves to your position. The issue is you don't want to "click again", but rather continue to hold the screen click and start dragging.
The way I was able to figure out to do this was to install and import pyautogui to simulate the missing click:
from turtle import Screen, Turtle
from pyautogui import mouseDown
def go_to_mouse(x, y):
screen.onclick(None, 1) # disable until turtle.release()
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.onrelease(release_handler)
screen.ontimer(mouseDown)
def click_right(x, y):
turtle.clear()
def drag_handler(x, y):
turtle.ondrag(None)
turtle.goto(x, y)
turtle.ondrag(drag_handler)
def release_handler(x, y):
turtle.onrelease(None)
screen.onclick(go_to_mouse, 1)
screen = Screen()
screen.setup(width=1000, height=1000)
screen.title('drawing')
turtle = Turtle('turtle')
turtle.speed('fastest')
turtle.color('red')
turtle.ondrag(drag_handler)
screen.onclick(go_to_mouse, 1)
screen.onclick(click_right, 3)
screen.mainloop()
I'm using a larger cursor to make sure we don't missposition the "click again". I can't test the right click functionality on my system.
I am trying to make a program that will repeatedly draw two lines. my code is
import turtle
screen = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
screen.tracer(0,0)
while True:
screen.clear()
t.penup()
t.goto(1,12)
t.pendown()
t.goto(4,67)
t.penup()
t.goto(50, 3)
t.pendown()
t.goto(4, 73)
screen.update()
I expect this to show two lines in turtle which do not flicker. however it is drawing one line and that line is flickering. the lines do need to be redrawn every frame so i can do some other stuff with the lines. Why is this happening?
Short answer: don't do screen.clear(), instead do t.clear().
When you clear the screen, you reset a number of its properties to the default values, including the tracer() setting. You simply want to clear whatever the turtle drew in the last iteration so clear the turtle instead.
In the long term, you don't want while True: in an event-driven environment like turtle. I would write this code more like:
from turtle import Screen, Turtle
def one_step():
turtle.clear()
turtle.penup()
turtle.goto(1, 12)
turtle.pendown()
turtle.goto(4, 67)
turtle.penup()
turtle.goto(50, 3)
turtle.pendown()
turtle.goto(4, 73)
screen.update()
screen.ontimer(one_step, 50)
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
one_step()
screen.mainloop()
I am trying to move the turtle to my cursor to draw something so that every time I click, the turtle will go there and draw something.
I have already tried onscreenclick(), onclick, and many combinations of both, I feel like I am doing something wrong, but I don't know what.
from turtle import*
import random
turtle = Turtle()
turtle.speed(0)
col = ["red","green","blue","orange","purple","pink","yellow"]
a = random.randint(0,4)
siz = random.randint(100,300)
def draw():
for i in range(75):
turtle.color(col[a])
turtle.forward(siz)
turtle.left(175)
TurtleScreen.onclick(turtle.goto)
Any help would be great, thank you for your time ( If you help me! ;)
It's not so much what method you're invoking, but on what object you're invoking it:
TurtleScreen.onclick(turtle.goto)
TurtleScreen is a class, you need to call it on a screen instance. And since you want to call draw in addition to turtle.goto you need to define your own function that calls both:
screen = Screen()
screen.onclick(my_event_handler)
Here's a rework of your code with the above fixes and other tweaks:
from turtle import Screen, Turtle, mainloop
from random import choice, randint
COLORS = ["red", "green", "blue", "orange", "purple", "pink", "yellow"]
def draw():
size = randint(100, 300)
# make turtle position center of drawing
turtle.setheading(0)
turtle.setx(turtle.xcor() - size/2)
turtle.pendown()
for _ in range(75):
turtle.color(choice(COLORS))
turtle.forward(size)
turtle.left(175)
turtle.penup()
def event_handler(x, y):
screen.onclick(None) # disable event handler inside event handler
turtle.goto(x, y)
draw()
screen.onclick(event_handler)
turtle = Turtle()
turtle.speed('fastest')
turtle.penup()
screen = Screen()
screen.onclick(event_handler)
mainloop()