I want to have a race with these four turtles where each turtle has a different button to move it two pixels forward but i can't get it to work and i was wondering in anyone here could help?
from turtle import *
from random import randint
speed(0)
penup()
goto(-140,140)
for step in range(16):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
ada = Turtle()
ada.color('red')
ada.shape("turtle")
ada.penup()
ada.goto(-160, 100)
ada.right(360)
ada.pendown()
bmb = Turtle()
bmb.color('cyan')
bmb.shape("turtle")
bmb.penup()
bmb.goto(-160, 70)
bmb.right(360)
bmb.pendown()
cbc = Turtle()
cbc.color('lawn green')
cbc.shape("turtle")
cbc.penup()
cbc.goto(-160, 40)
cbc.right(360)
cbc.pendown()
dgd = Turtle()
dgd.color('dark violet')
dgd.shape("turtle")
dgd.penup()
dgd.goto(-160, 10)
dgd.right(360)
dgd.pendown()
You can rely on the turtle library itself for this. For example with the onkey function. Just as an example:
from turtle import *
def up():
# ...handle...
onkey(up, 'Up')
You can find much more about this here
Related
So I'm making a game where the two turtles race each other, but I want them to wait a bit before they start (to display a 3,2,1) screen. But I can't figure it out! I used time.sleep, and turtle.delay, but both did not work. What can I do? Here is the code :)
import turtle
import random
import time
turt = turtle.Turtle()
turt2 = turtle.Turtle()
turtle.Screen() .bgcolor("green")
turt.speed(10)
turt.penup()
turt.goto(200,100)
turt.pendown()
turt.right(90)
turt.width(10)
turt.forward(450)
turt.right(180)
turt.forward(800)
#position 1
turt.penup()
turt.goto(-400,200)
turt.pendown()
turt.right(90)
turt.forward(100)
#position 2
turt2.width(10)
turt2.penup()
turt2.goto(-400,-200)
turt2.pendown()
turt2.forward(100)
Contrary to the advice so far, I'd avoid sleep() in my turtle program and use ontimer() instead to countdown the start of the race:
from turtle import Screen, Turtle
from random import choice
FONT = ('Arial', 36, 'bold')
def race():
while turtle_1.xcor() < 200 > turtle_2.xcor():
choice([turtle_1, turtle_2]).forward(10)
def countdown(seconds=3):
pen.clear()
if seconds < 1:
screen.ontimer(race)
else:
pen.write(seconds, align='center', font=FONT)
screen.ontimer(lambda: countdown(seconds - 1), 1000)
screen = Screen()
screen.bgcolor('green')
marker = Turtle()
marker.hideturtle()
marker.speed('fastest')
marker.color('white')
marker.width(5)
marker.penup()
marker.goto(200, 300)
marker.pendown()
marker.right(90)
marker.forward(600)
pen = Turtle()
pen.hideturtle()
turtle_1 = Turtle()
turtle_1.shape('turtle')
turtle_1.color('red')
turtle_1.penup()
turtle_1.goto(-400, 200)
turtle_2 = turtle_1.clone()
turtle_1.color('blue')
turtle_2.goto(-400, -200)
countdown()
screen.exitonclick()
from turtle import *
o=Turtle()
d=Screen()
o.begin_fill()
o.width("2")
o.color("yellow")
extent1=150**2*3.14
print(extent1)
o.circle(radius=150,extent=extent1,steps=10)
o.end_fill()
#jasonharper's comment is spot-on (+1). To make the filled star appear the same on both architectures, draw (then fill) just the perimeter of the star you want:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtle.color("yellow")
turtle.begin_fill()
for _ in range(11):
turtle.forward(64)
turtle.right(98.2)
turtle.forward(64)
turtle.left(130.91)
turtle.end_fill()
turtle.hideturtle()
screen.exitonclick()
I need some help. I want to center the hexagon into the larger hexagon but I don't now how to do it. Below I have the source code and an image link to the output.
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 20
move_left = 60
polygon.pensize(2)
polygon.pencolor((245, 176, 66))
for turtle_move in range(num_sides):
polygon.forward(side_length)
polygon.left(move_left)
polygon.penup()
polygon.left(2)
polygon.pendown()
side_length2 = 40
move_left2 = 60
I want to center the hexagon inside the larger hexagons, but I don't know what to do.
for turtle_move in range(num_sides):
polygon.forward(side_length2)
polygon.left(move_left2)
Here is the output:
There are any number of ways to do this if you read about the geometry of hexagons, eg. on Wikipedia:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
ANGLE_LEFT = 60
screen = Screen()
turtle = Turtle()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH)
turtle.left(ANGLE_LEFT)
turtle.penup()
turtle.backward(SIDE_LENGTH / 2)
turtle.sety(-SIDE_LENGTH * 3**0.5/2)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH*2)
turtle.left(ANGLE_LEFT)
turtle.hideturtle()
screen.exitonclick()
One alternate approach is to use the turtle circle() method to draw the hexagons, then it becomes a matter of centering two circles:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
circumradius = SIDE_LENGTH
screen = Screen()
turtle = Turtle()
for _ in range(2):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
turtle.circle(circumradius, steps=NUM_SIDES)
circumradius *= 2
turtle.hideturtle()
screen.exitonclick()
I'm assuming by your use of pencolor((245, 176, 66)) you're using a site like Repl.it or some other non-standard Python turtle implementation, so you may need to adjust the examples above slightly to suit your environment.
pink square is the taskI am trying to draw a square inside a square using turtle graphics. I couldn't draw exactly like the original one.
Here is my code
import turtle
win = turtle.Screen()
tess = turtle.Turtle()
for two in range(12):
for _ in range(4):
tess.forward(20)
tess.left(90)
tess.penup()
tess.goto(-7,-4)
tess.pendown()
for _ in range(4):
tess.forward(35)
tess.left(90)
tess.penup()
tess.goto(-14,-8)
tess.pendown()
for _ in range(4):
tess.forward(50)
tess.left(90)
tess.penup()
tess.goto(-21,-16)
tess.pendown()
for _ in range(4):
tess.forward(70)
tess.left(90)
A good example for better living through stamping instead of drawing:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
screen.bgcolor('lightgreen')
turtle = Turtle()
turtle.shape('square')
turtle.color('pink', 'lightgreen')
for size in range(100, 0, -20):
turtle.shapesize(size / CURSOR_SIZE, outline=3)
turtle.stamp()
screen.exitonclick()
I think, my code solution your problem.
from turtle import *
def draw_square(a,color,x,y):
penup()
goto(x,y)
setheading(90)
backward(a//2)
setheading(0)
backward(a//2)
pendown()
pencolor(color)
for _ in range(4):
forward(a)
left(90)
draw_square(20,"pink",0,0)
draw_square(50,"pink",0,0)
draw_square(70,"pink",0,0)
draw_square(90,"pink",0,0)
draw_square(110,"pink",0,0)
The fillcolor() is not working at all in this function -- I can not figure out why. It worked in all of my other functions:
from turtle import Turtle
from random import randint
t = Turtle()
def rocks():
for i in range(5):
t.penup()
t.goto(randint(-300,0), randint(-200,0))
for x in range(40):
t.pendown()
t.fillcolor("gray")
t.fillcolor()
t.begin_fill()
t.forward(5)
t.left(25)
t.right(27)
t.forward(5)
t.right(20)
t.end_fill()
t.speed("fastest")
rocks()
The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments. You need them around your loop to fill the entire shape:
from turtle import Turtle, Screen
from random import randint
def rocks(t):
t.fillcolor('gray')
for _ in range(5):
t.penup()
t.goto(randint(-300, 0), randint(-200, 0))
t.begin_fill()
for _ in range(15):
t.pendown()
t.forward(5)
t.right(2)
t.forward(5)
t.right(22)
t.end_fill()
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
rocks(turtle)
turtle.hideturtle()
screen.exitonclick()