I have a triangle and want to split it into 2 color, left side color red and the right side color yellow.
I can not.
If you can see my code, run it you see 2 triangles and I want to first want to split. See the picture example:
Here is my code:
import turtle
def dreieck(x, y, direction, size, color):
turtle.penup()
turtle.setpos(x, y)
turtle.setheading(direction)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for i in range(3):
turtle.forward(size)
turtle.left(120)
turtle.end_fill()
dreieck(100, 200, 180, 200, "red")
turtle.left(120)
turtle.penup()
turtle.setpos(-50, -50)
turtle.setheading(180)
turtle.pendown()
dreieck(-100, -150, 0, 200, "blue")
The disconnect I see from the provided illustration is that the problem involves three isosceles right triangles but your code draws equilateral triangles! First, we need to change your code to draw the correct type of triangle and then apply it three times from the correct position and orientation. There are many ways to do this, here's an example:
import turtle
MY_HYPOTENUSE = 200
MY_LEG = MY_HYPOTENUSE * 2**0.5/2
def dreieck(hypotenuse, color):
leg = hypotenuse * 2**0.5/2
turtle.fillcolor(color)
turtle.begin_fill()
turtle.forward(hypotenuse)
turtle.left(135)
turtle.forward(leg)
turtle.left(90)
turtle.forward(leg)
turtle.left(135)
turtle.end_fill()
dreieck(MY_HYPOTENUSE, 'blue')
turtle.left(45)
turtle.penup()
turtle.forward(2 * MY_LEG)
turtle.pendown()
turtle.left(135)
dreieck(MY_HYPOTENUSE, 'green')
turtle.right(135)
turtle.backward(MY_LEG)
dreieck(MY_LEG, 'red')
turtle.hideturtle()
turtle.done()
There are likely more clever and efficient ways to do this.
Related
I am making a tic tac toe game and when the user presses 'o' a circle is printed but the circle is always on the left of the turtle. i would like the turtle to be in the center of a box and draw the circle around itself.
You have to move turtle on your own - using left,right,forward, penup, pendowm.
Example
import turtle
radius = 100
# move
turtle.penup()
turtle.right(90)
turtle.forward(radius)
turtle.left(90)
turtle.pendown()
# circle
turtle.circle(radius)
# move back
turtle.penup()
turtle.right(-90)
turtle.forward(radius)
turtle.left(-90)
turtle.pendown()
Result:
There are a couple of ways to draw a circle centered around a Python turtle without moving the turtle. The first is the dot() method. It takes a diameter, rather than a radius, and optionally allows you to specify the color at the same time:
import turtle
RADIUS = 100
turtle.dot(RADIUS * 2)
turtle.dot(RADIUS * 1.6, turtle.bgcolor()) # "unfill" the circle
turtle.done()
Another way to do it is via stamping, that is, make the turtle cursor itself a circle, size it, and then call stamp():
import turtle
RADIUS = 100
CURSOR_RADIUS = 10
turtle.hideturtle()
turtle.shape('circle')
turtle.fillcolor(turtle.bgcolor())
turtle.shapesize(RADIUS / CURSOR_RADIUS, outline=RADIUS/5)
turtle.stamp()
turtle.done()
Both of the above have the side effect of overwritting anything that the circle surrounds, which doesn't seem like a problem for tic-tac-toe. To avoid this, you can, of course, temporarily shift the turtle's position, as #furas suggests:
import turtle
RADIUS = 100
turtle.width(RADIUS/5)
turtle.penup()
turtle.sety(turtle.ycor() - RADIUS)
turtle.pendown()
turtle.circle(RADIUS)
turtle.penup()
turtle.sety(turtle.ycor() + RADIUS)
turtle.pendown()
turtle.done()
Has anyone ever done this? Using multiple colors at the save time.
I have a list of colors:
colors = ["#880000",\
"#884400",\
"#888800",\
"#008800",\
"#008888",\
"#000088",\
"#440088",\
"#880088"]
My aim is to take that list of colors and pass it to turtle so it can draw a colorful circle in one line of turn.
my functions is as follows:
def drawImage(colorList, radius):
for color in colorList:
turtle.color(color)
turtle.penup()
turtle.setpos(0, -radius)
xpos=turtle.xcor()
ypos=turtle.ycor()
turtle.begin_fill()
turtle.pendown()
turtle.home()
turtle.setpos(xpos,ypos)
turtle.circle(radius)
turtle.end_fill()
turtle.color('black')
turtle.width(2)
turtle.circle(radius)
return
The problem with the above function is that it draws using only one color instead of little arcs of different colors from the list. can anyone help me solve this or point to what I'm doing wrong?
the function is called like drawImage(colors,200) this will draw a colorful circle with a radius of 200
Do you mean this circle ?
import turtle
colors = [
"#880000",
"#884400",
"#888800",
"#008800",
"#008888",
"#000088",
"#440088",
"#880088"
]
#--------------------
#turtle.reset()
angle = 360/len(colors)
turtle.width(10)
for color in colors:
turtle.color(color)
turtle.circle(100, angle)
There will be more work with filled circle because you have to draw filled "triangle" (arc) one by one.
EDIT:
import turtle
colors = [
"#880000",
"#884400",
"#888800",
"#008800",
"#008888",
"#000088",
"#440088",
"#880088"
]
#--------------------
def filled_arc(radius, angle, color):
turtle.color(color)
turtle.begin_fill()
turtle.forward(radius)
turtle.left(90)
turtle.circle(radius, angle)
turtle.left(90)
turtle.forward(radius)
turtle.end_fill()
turtle.left(180-angle)
#--------------------
angle = 360/len(colors)
for color in colors:
filled_arc(100, angle, color)
turtle.left(angle)
I have the following code which is supposed to draw a ring of colors around a circle but only one color if printed and changed 8 times before moving to the next
import turtle
def drawCircle(colorList, radius):
for color in colorList:
turtle.color(color)
for i in range(len(colorList)):
turtle.penup()
turtle.setpos(0, -radius)
xpos=turtle.xcor()
ypos=turtle.ycor()
head=turtle.heading()
turtle.begin_fill()
turtle.pendown()
turtle.home()
turtle.setpos(xpos,ypos)
turtle.setheading(head)
turtle.circle(radius)
turtle.end_fill()
turtle.penup()
return
colorList=["#880000","#884400","#888800","#008800",\
"#008888","#000088","#440088","#880088"]
drawCircle(colorList,200)
How would I make it that each arc around the circle is a different color. here is an example
you will need something like this
def drawSegment(color,x, y, r, angleStart, angleEnd, step=1):
#More efficient to work in radians
radianStart = angleStart*pi / 180
radianEnd = angleEnd*pi / 180
radianStep=step *pi/180
#Draw the segment
turtle.penup()
turtle.setpos(x,y)
turtle.color(color)
turtle.begin_fill()
turtle.pendown()
for theta in arange(radianStart,radianEnd,radianStep):
turtle.setpos(x + r * cos(theta), y + r * sin(theta))
turtle.setpos(x + r * cos(radianEnd), y + r * sin(radianEnd))
turtle.setpos(x, y);
turtle.end_fill()
def drawCircle(colorList,radius):
#do something to draw an equal segment for each color advancing it around 360 degree's
I'm trying to draw a checkerboard.
I drew the board, but now I have to define a function (a loop) that fills in every other square with black. I've been trying to write a loop to do this for a while, can someone help?
Here's my code:
import turtle
def drawGrid():
turtle.penup()
turtle.goto(-300, 250)
turtle.pendown()
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
def drawColumns():
for i in range(4):
turtle.right(90)
turtle.forward(37.5)
turtle.right(90)
turtle.forward(300)
turtle.left(90)
turtle.forward(37.5)
turtle.left(90)
turtle.forward(300)
def drawRows():
turtle.left(180)
rows = 0
while rows <= 3:
rows += 1
turtle.forward(37.5)
turtle.right(90)
turtle.forward(300)
turtle.left(90)
turtle.forward(37.5)
turtle.left(90)
turtle.forward(300)
turtle.right(90)
def main():
drawGrid()
drawColumns()
drawRows()
if __name__ == "__main__":
main()
Turtles fill method works on shapes, i.e. a completely bounded area. So rather than drawing a grid you need to think in terms of drawing a series of squares.
So lets start by defining a simple function to draw a filled in square. It takes a turtle object and a size which is the length of the side.
import turtle
def draw_filled_square(this_turtle, size):
"""Draw a square by drawing a line and turning through 90 degrees 4 times"""
this_turtle.pendown()
this_turtle.fill(True)
for _ in range(4):
this_turtle.forward(size)
this_turtle.right(90)
this_turtle.fill(False)
this_turtle.penup()
we can call it like this:
window = turtle.Screen()
myturtle = turtle.Turtle()
square_size = 90
myturtle.goto(-300, 200)
draw__filled_square(myturtle, square_size)
Which draws a single square. Note that it puts it back at the starting place, so we need to move it before drawing the next square.
Now, in practice, as long as we draw the outline of the box we only need to draw filled squares, unfilled squares can be represented negative space. But for the purposes of explanation I'm going to also draw them.
Defining a function for an unfilled square is easy -- just duplicate the existing function but set this_turtle.fill(False) at the beginning instead.
Anytime something needs to count in a repeating sequence (1, 2, 3, 4, 1, 2, 3, 4, ...) it calls for use of modulo (remainder). Modulo means remainder so if x modulo y is 0 it means x is exactly divisible by y. This translates into code as if x % y == 0:
Here's a simple drum machine to demonstrate:
def drum_loop(x):
# go bang on the fourth beat
if x % 4 == 0:
print("bang!")
else:
print("tish")
# prints tish, tish, tish, bang! tish, tish, tish, bang!
for i in range(1,9):
drum_loop(i)
Alternating is just like counting 0, 1 , 0, 1 repeatedly.
So we can draw a row like this:
for i in range(8):
if i % 2 == 0:
draw_filled_square(myturtle, square_size)
else:
draw_unfilled_square(myturtle, square_size)
# move to start of next square
myturtle.forward(square_size)
Now just repeating this isn't going to do the trick, but it should be clear you can use modulo 2 again to make the rows alternate properly.
Do this by defining a row function that will alternate between starting with a black and starting with a white square, then calling this from within another loop. (Don't forget to go back to the beginning and move down every time you start a row).
Here's another example where drawing creates more work for the poor turtle than simply stamping. Rather than thinking about drawing and filling boxes, think about the board itself as a filled square onto which other filled squares are stamped:
from turtle import Turtle, Screen
NUMBER_SQUARES = 8
SQUARE_SIZE = 40
BOARD_SIZE = SQUARE_SIZE * NUMBER_SQUARES
BORDER_FRACTION = 1.025 # add a slight edge to board
STAMP_SIZE = 20 # size of turtle square image
turtle = Turtle(shape='square', visible=False)
turtle.shapesize(BOARD_SIZE / STAMP_SIZE * BORDER_FRACTION)
turtle.color('black')
turtle.stamp()
turtle.shapesize(SQUARE_SIZE / STAMP_SIZE)
turtle.color('white')
turtle.penup()
for y in range(-NUMBER_SQUARES//2, NUMBER_SQUARES//2):
parity = y % 2 == 0
for x in range(-NUMBER_SQUARES//2, NUMBER_SQUARES//2):
if parity:
turtle.goto(x * SQUARE_SIZE + SQUARE_SIZE//2, y * SQUARE_SIZE + SQUARE_SIZE//2)
turtle.stamp()
parity = not parity
Screen().exitonclick()
This solution can print a board in any two colors (e.g. black and red), it doesn't assume a white background. Just another example of better living through stamping.
import turtle
turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("black")
for i in range(4):
turtle.forward(320)
turtle.left(90)
for y in range(-160,160,80):
for x in range(-160,160,80):
turtle.begin_fill()
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
for y in range(-120,160,80):
for x in range(-120,160,80):
turtle.begin_fill()
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
turtle.done()
Let's have a look at the zig-zag approach (the explanation of the code is commented inside):
import turtle
def drawGrid(rows, cols, size):
turtle.sety(turtle.ycor() + size * rows) # Draw the initial line
f = size * 2
turtle.begin_fill()
for j in range(cols): # Make the columns
if j % 2:
turtle.backward(f) # Make room for the zig-zag up and zig-zag down
for i in range(rows * 2): # rows * 2 because there will be both zig-zag down and a zig-zag up for each row
turtle.forward(size)
turtle.right(90)
if i % 2:
turtle.left, turtle.right = turtle.right, turtle.left # Switch the zig-zag direction
turtle.left(180)
turtle.end_fill()
turtle.sety(turtle.ycor() - size * rows) # Draw the final line
turtle.penup()
turtle.goto(-300, -50) # Set the bottom-left position of the grid
turtle.pendown()
turtle.speed("fastest") # Because I have no patience
drawGrid(8, 8, 40)
Output:
Note that the number of rows and columns must be even for this to work.
I am trying to draw a target like this:
(source: newscientist.com)
using turtle.
The problem is turtle includes the origin as part of the graph, as opposed to the origin being in the center. My question is, how do I get turtle draw a circle AROUND the origin rather than include it?
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.circle(radius)
radius += 10
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
radius += 10
It's nicer to use radius as the loop variable
import turtle
turtle.speed(0)
for radius in range(100, 200, 10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
Then you might wish to define a function
import turtle
turtle.speed(0)
def origin_circle(turtle, radius):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
for radius in range(100, 200, 10):
origin_circle(turtle, radius)