I'm trying to create a code using Python to create a STOP sign.
Here is my code:
wn = Screen()
sarah = Turtle()
sarah.penup()
sarah.left(180)
sarah.forward(50)
sarah.left(90)
sarah.forward(50)
sarah.left(90)
sarah.pendown()
for i in range(1,9):
#repeat four times
sarah.color("red")
sarah.fillcolor("red")
sarah.fill(True)
sarah.forward(100)
sarah.left(45)
sarah.fill(False)
sarah.penup()
sarah.color("white")
sarah.left(115)
sarah.forward(125)
sarah.right(100)
sarah.forward(25)
sarah.left(15)
sarah.pendown()
sarah.pendown()
sarah.forward(25)
sarah.left(15)
sarah.right(25)
sarah.left(80)
sarah.left(80)
sarah.forward(1)
sarah.left(15)
sarah.forward(35)
sarah.forward(25)
sarah.left(45)
sarah.left(15)
sarah.forward(15)
sarah.forward(10)
sarah.penup()
wn.exitonclick()
I am not clear on everything you're trying to do, but this might help you get started:
import turtle
wn = turtle.Screen()
sarah = turtle.Turtle()
turtle.setup(1000,1000)
sarah.fillcolor("red")
sarah.begin_fill()
sarah.color("red")
# Now draw an octagon
for i in range(8):
sarah.left(45)
sarah.forward(50)
sarah.end_fill()
wn.exitonclick()
Unfortunately, here's how you won't be taught to draw a stop sign using turtle graphics:
from turtle import Turtle, Screen
RADIUS = 100
FONTSIZE = int(RADIUS / 2)
FONT = ("Arial", FONTSIZE, "bold")
sarah = Turtle(visible=False)
sarah.penup()
screen = Screen()
sarah.sety(-RADIUS)
sarah.begin_poly()
sarah.circle(RADIUS, steps=8)
sarah.end_poly()
screen.register_shape("octagon", sarah.get_poly())
sarah.shape("octagon")
sarah.fillcolor("red")
sarah.home()
sarah.setheading(360 / 8 / 2)
sarah.stamp()
sarah.pencolor("white")
sarah.shapesize(0.9)
sarah.stamp()
sarah.shapesize(1.0)
sarah.sety(-FONTSIZE / 2)
sarah.write("STOP", align="center", font=FONT)
screen.exitonclick()
Most turtle examples emphasize drawing and only rarely stamping. But stamping has many advantages and can solve many problems more easily than drawing:
I probably need to write a "Joy of Stamp" tutorial...
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()
How to make draw like this with turtle?
right now my code looks like that:
class OperationsOnSets():
def __init__(self):
self.font_style = ("Times New Roman", 40, "bold")
def move_turtle_pos(self, x, y, turtle):
turtle.up()
turtle.setpos(x, y)
turtle.down()
def set_d(self):
turtle = Turtle()
turtle.pensize(2)
turtle.speed(2)
self.move_turtle_pos(-100, -50, turtle)
turtle.circle(200)
self.move_turtle_pos(100, -50, turtle)
turtle.circle(200)
sleep(5)
turtle.mainloop()
example = OperationsOnSets()
example.set_d()
and here is result
I though about pasting image, or make algorithm that would draw a lines, but I don`t know how to realize it. So I hope that someobody of you will help me with it...
My philosophy of turtles, is avoid the math by getting the turtle to do the hard work for you. Not quite perfect, but pretty close:
from turtle import Screen, Turtle
RADIUS = 200
NUMBER_LINES = 14
class OperationsOnSets():
def __init__(self):
self.turtle = Turtle()
self.turtle.pensize(2)
self.turtle.fillcolor(screen.bgcolor())
def move_turtle_pos(self, x, y):
self.turtle.penup()
self.turtle.setpos(x, y)
self.turtle.pendown()
def set_d(self):
self.move_turtle_pos(-RADIUS/2, -RADIUS)
points = []
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
points.append(self.turtle.position())
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
position = self.turtle.position()
self.turtle.goto(points.pop())
self.turtle.goto(position)
self.move_turtle_pos(RADIUS/2, -RADIUS)
self.turtle.begin_fill()
self.turtle.circle(RADIUS)
self.turtle.end_fill()
self.move_turtle_pos(-RADIUS/2, -RADIUS)
self.turtle.circle(RADIUS, 180)
self.turtle.hideturtle()
screen = Screen()
example = OperationsOnSets()
example.set_d()
screen.mainloop()
We make the turtle stop along its way around the circle to record points we'll need to finish the drawing.
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.
I'm trying to draw on a small 200x200 screen using turtle, however the drawing doesn't pop up as full size, it opens a smaller window and I have to scroll up/down, left/right (just a bit) to see the whole drawing. I don't have this problem with larger windows. How do I prevent this?
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
edit: screenshot, I want the actual size window instead of the scrolls
You could resize the window to 420×420.
If you don't want to resize your window, I suggest modifying the values for the keys "canvwidth" and "canvheight" keys in the turtle._CFG dictionary:
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
turtle._CFG.update({"canvwidth": width-20, "canvheight": height-20}) # Removing the scroll bars
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
screen.exitonclick()
Using small windows in turtle is a can of worms. If #TheOneMusic's simple solution (+1) is good enough for your purposes, go for it! On my system, your setworldcoordinates() call gets rid of the scroll bars, so I don't even see the issue. So, another approximate solution might be to upgrade to current Python and tkinter.
However, neither is an exact solution. If we add code to draw a 200 x 200 box around our drawing area:
t.penup()
t.color('red')
t.goto(0, 0) # because of setworldcoordinates()
t.pendown()
for _ in range(4):
t.forward(200)
t.left(90)
We get the box skewed:
To solve this problem more precisely, involves uglier code:
from turtle import Screen, Turtle
from random import randint
TRUE_WIDTH, TRUE_HEIGHT = 200, 200
CURSOR_SIZE = 20 # for drawing frame around edge
RADIUS = 20
CHROME = 14 # magic number possibly derivable from tkinter
width, height = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # needs to be slightly larger than 200 target
offset_x = CHROME / -2 + 2
offset_y = CHROME / 2 - 2
screen = Screen()
screen.setup(width, height)
screen.screensize(width/2, height/2) # backing store needs to be smaller than window
screen.setworldcoordinates(0, 0, TRUE_WIDTH, TRUE_HEIGHT)
# Draw red frame around edge to "prove" drawing area
frame = Turtle(shape='square', visible=False)
frame.shapesize(TRUE_HEIGHT / CURSOR_SIZE, TRUE_WIDTH / CURSOR_SIZE) # 200 x 200 frame
frame.color('red', 'white')
frame.penup()
frame.goto(TRUE_WIDTH/2 + offset_x, TRUE_HEIGHT/2 + offset_y)
frame.stamp()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
for _ in range(5):
turtle.penup()
turtle.goto(randint(RADIUS, TRUE_WIDTH - RADIUS) + offset_x, randint(0, TRUE_HEIGHT - RADIUS*2) + offset_y)
turtle.pendown()
turtle.circle(RADIUS)
screen.exitonclick()
But this sort of detail work could easily be undone by a future release of turtle and/or tkinter. If you can live with turtle's default window, life gets easier.
I am attempting to make a game in python with the turtle module, I have the square moving towards the player (the circle) and the aim is for the circle to jump over the square and not get hit.
The player can jump by pressing the spacebar,
but every time you hit the space bar to jump the player jumps, but the square stops moving and you are unable to jump over.
here is my code:
import turtle
import time
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("dinosaur run")
wn.tracer(1,20)
floor = turtle.Turtle()
floor.fd(370)
floor.bk(370*2)
floor.ht()
player = turtle.Turtle()
player.shape("circle")
player.penup()
player.setpos(-370,14)
def jump():
player.lt(90)
player.fd(40)
time.sleep(0.5)
player.bk(40)
player.rt(90)
turtle.listen()
turtle.onkey(jump, "space")
class cactus(turtle.Turtle):
turtle.shape("square")
turtle.penup()
turtle.speed(0)
turtle.setpos(370,14)
cactusspeed = 2
while True:
x = turtle.xcor()
x -= cactusspeed
turtle.setx(x)
Thanks a lot,
all ideas welcome,
I've tried wn.update() at the end
As provided above, your code doesn't run at all as cactusspeed never gets defined. And your class cactus doesn't have a hope of working as currently laid out (reread about Python classes.) Finally, your while True: has no business in an event driven world like turtle.
Below is my rework of your code to use an ontimer() event to control the cactus independent of the player. I also eliminated the sleep() and simply made the player move slower and jump higher. I believe this should give you the dynamic you're looking for:
from turtle import Turtle, Screen
def jump():
player.forward(100)
player.backward(100)
def move():
if cactus.xcor() < -screen.window_width()/2:
cactus.hideturtle()
cactus.setx(370)
cactus.showturtle()
else:
cactus.forward(cactusspeed)
screen.ontimer(move, 40)
screen = Screen()
floor = Turtle(visible=False)
floor.speed('fastest')
floor.fd(370)
floor.bk(370 * 2)
player = Turtle("circle", visible=False)
player.penup()
player.setpos(-370, 14)
player.setheading(90)
player.speed('slowest')
player.showturtle()
cactusspeed = 4
cactus = Turtle("square", visible=False)
cactus.speed('fastest')
cactus.penup()
cactus.setpos(370, 14)
cactus.setheading(180)
cactus.showturtle()
screen.onkey(jump, "space")
screen.listen()
move()
screen.mainloop()