I am making an analog clock with python, using turtle module.
I could configue a window size and a circle, But i can't finish outliner of a circle.
I didn't give any integer to get the complete ouliner. But it doesn't surround a circle entire.
Please, tell me if there is a something missing.
window = turtle.Screen()
window.setup(1000, 800)
window.bgcolor("black")
window.title("Analog Clock")
window.tracer(0)
t = turtle.Turtle()
t.color("#FFD700")
t.penup()
t.speed(0)
t.pensize(25)
t.hideturtle()
t.goto(0, -290)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(300, 360)
t.end_fill()
thank you
The problem is your use of tracer() without any call to update(). You can either remove the window.tracer(0) line, or add a window.update() line after your t.end_fill() line.
I usually recommend avoiding tracer() and update() until you have your code basically working, just to avoid bugs like this.
from turtle import Screen, Turtle
screen = Screen()
screen.setup(1000, 800)
screen.bgcolor('black')
screen.title("Analog Clock")
screen.tracer(0)
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest') # no-op if `tracer(0)`
turtle.color('#FFD700')
turtle.pensize(25)
turtle.penup()
turtle.sety(-290)
turtle.pendown()
turtle.fillcolor('white')
turtle.begin_fill()
turtle.circle(300)
turtle.end_fill()
screen.update()
screen.mainloop()
Here is the solution to your problem with your posted code working properly :)
window = turtle.Screen()
window.setup(1000, 800)
window.bgcolor("black")
window.title("Analog Clock")
t = turtle.Turtle()
t.color("#FFD700")
t.penup()
t.speed(0)
t.pensize(25)
t.hideturtle()
t.goto(0, -290)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(300, 360)
t.end_fill()
window.update()
Related
# Game creation
import turtle
wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("Black")
wn.setup(width=800, height=800)
wn.tracer(0)
# paddle a
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.penup()
paddle_a.goto(0, 0)
# Functions
def paddle_a_right():
turtle.forward(100)
wn.onkeypress(paddle_a_right, 'd')
while True:
wn.update()
Want the square to move to the right or left using 'a' or 'd' I don't know very much about turtle, I just want to program a simple game.
There are three major issues with your code. First, you need to call wn.listen() to allow the window to receive keyboard input. Second, you do turtle.forward(100) when you mean paddle_a.forward(100). Finally, since you did tracer(0), you now need to call wn.update() anytime a change is made that you want your user to see.
Here's a simplified example:
from turtle import Screen, Turtle
def paddle_right():
paddle.forward(10)
screen.update()
screen = Screen()
screen.title("Pong")
screen.bgcolor("Black")
screen.setup(width=800, height=800)
screen.tracer(0)
paddle = Turtle()
paddle.shape("square")
paddle.color("white")
paddle.penup()
screen.onkeypress(paddle_right, 'd')
screen.listen()
screen.update()
screen.mainloop()
When I attempt to put text over a turtle in the python turtle module, it flashes. Any solutions?
import turtle
s = turtle.Screen()
s.setup(width=500,height=600)
c = turtle.Turtle()
c.shapesize(stretch_len=5,stretch_wid=5)
c.goto(0,0)
c.shape("square")
pen = turtle.Turtle()
pen.hideturtle()
pen.goto(0,0)
pen.color("red")
while True:
pen.write("hello!")
s.update()
Although I don't see the flashing on my screen, my guess is your problem is related to this bad idiom:
while True:
# ...
s.update()
We need neither the while True: (which has no place in an event-driven environment like turtle) nor the call to update() (which is not needed given no previous call to tracer()). Let's rewrite this as turtle code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=500, height=600)
turtle = Turtle()
turtle.hideturtle()
turtle.shapesize(5)
turtle.shape('square')
turtle.stamp() # stamp a square so we can reuse turtle
pen = Turtle()
pen.hideturtle()
pen.color("red")
pen.write("Hello!", align='center', font=('Arial', 16, 'normal'))
screen.exitonclick()
Does that solve your problem?
I am trying to move a drawing slowly across the screen as I move another turtle with the arrow keys. The problem is that my code only makes the drawing move across the screen, not allowing me to move the other turtle with the keys. I have tried defining the keys inside of the while true statement and it still came out with the same outcome.
Here is my code:
from turtle import *
setup(500, 500)
Screen()
screen = Screen()
title("Turtle Keys")
turtle1 = Turtle()
turtle2 = Turtle()
def moving_square():
turtle1.fillcolor("Red")
turtle1.begin_fill()
for i in range(4):
turtle1.forward(50)
turtle1.right(90)
turtle1.end_fill()
turtle1.goto(-350, 0)
turtle1.pendown()
turtle1.hideturtle()
def k1():
turtle2.forward(50)
def k2():
turtle2.left(45)
def k3():
turtle2.right(45)
def k4():
turtle2.back(25)
onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")
while True:
moving_square()
screen.update()
turtle1.forward(5)
It needs listen() to react on pressed keys.
And you have this information even in documentation for onkey()
from turtle import *
# --- functions ---
def moving_square():
turtle1.fillcolor("Red")
turtle1.begin_fill()
for i in range(4):
turtle1.forward(50)
turtle1.right(90)
turtle1.end_fill()
def k1():
turtle2.forward(50)
def k2():
turtle2.left(45)
def k3():
turtle2.right(45)
def k4():
turtle2.back(25)
# -- main ---
setup(500, 500)
Screen()
screen = Screen()
title("Turtle Keys")
turtle1 = Turtle()
turtle2 = Turtle()
turtle1.goto(-350, 0)
turtle1.pendown()
turtle1.hideturtle()
onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")
listen()
while True:
moving_square()
screen.update()
turtle1.forward(5)
For more game-like action, I would go about this a different way. First, by not drawing a red square but by making the first turtle into a red square. Second, by using a timed event instead of a while True: loop:
from turtle import Screen, Turtle
WIDTH, HEIGHT = 500, 500
BLOCK_SIZE = 50
CURSOR_SIZE = 20
def k1():
turtle.forward(50)
def k2():
turtle.left(45)
def k3():
turtle.right(45)
def k4():
turtle.back(25)
def move():
block.forward(1)
if block.xcor() < WIDTH/2 + BLOCK_SIZE/2:
screen.ontimer(move, 75) # milliseconds
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.title("Turtle Keys")
block = Turtle()
block.hideturtle()
block.shape('square')
block.fillcolor('red')
block.shapesize(BLOCK_SIZE / CURSOR_SIZE)
block.penup()
block.setx(-WIDTH/2 - BLOCK_SIZE/2)
block.showturtle()
turtle = Turtle()
turtle.shape('turtle')
turtle.penup()
screen.onkey(k1, 'Up')
screen.onkey(k2, 'Left')
screen.onkey(k3, 'Right')
screen.onkey(k4, 'Down')
screen.listen()
move()
screen.mainloop()
You can increase the speed of the block by adjusting block.forward(1) to higher value. And if you need a trailing line, you can put the pen down.
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 have been trying to design the below image using Python turtle graphics.
So far, this is what I have come up with.
Here is my code snippet.
from turtle import Turtle
t=Turtle()
t.hideturtle()
t.speed(0)
def text():
t.color("blue")
t.write("AOL",align="center",font=("Adobe Gothic Std B",100,"bold"))
def circle_with_color_fill():
t.up()
t.setpos(200,50)
t.down()
t.color("blue")
t.begin_fill()
t.circle(40)
t.end_fill()
def circle():
t.up()
t.setpos(200,40)
t.down()
t.color("blue")
t.circle(50)
text()
circle_with_color_fill()
circle()
t.screen.mainloop()
Can anyone suggest how I can get those triangles?
Since you have the text worked out, here's a rough approximation of the logo using stamping:
from turtle import Turtle, Screen
STAMP_UNIT = 20
screen = Screen()
aol = Turtle(shape='triangle', visible=False)
aol.color('blue')
aol.turtlesize(140 / STAMP_UNIT)
aol.stamp()
aol.shape('circle')
aol.turtlesize(80 / STAMP_UNIT, outline=12)
aol.color(screen.bgcolor(), 'blue')
aol.stamp()
screen.exitonclick()
OUTPUT