Python Turtle Opacity? - python

Just wondering, is it possible to make a turtle draw/fill with semi-transparent ink?
Something like:
turtle.setfillopacity(50) # Would set it to 50% transparency
Running python 2.7

You can by doing
import turtle
turtle = turtle.Turtle()
r = 100
g = 100
b = 100
a = 0.5
turtle.color(r,g,b,a)
(well, maybe it only works for repl.it)

Well, you can use RGBA.
First, put in the normal statements:
import turtle
t = turtle.Turtle()
Then, use t.color(), but use RGBA.
The first portion of RGBA is the same as RGB, and the last value is the percentage of opacity (where 0 is transparent, 1 is opaque.)
t.color(0,0,0,.5)
will get you black with 50% opacity.

It's not possible to do that, but you could define your colors, and then a light equivalent, and use those.
Red = (255,0,0,0)
LRed = (100,0,0)
I think that would achieve similar effects. You could then just use a lighter color when you want it semi-transparent.

This python turtle example fades out the text while keeping the original turtle stamps unmodified:
import turtle
import time
alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.pencolor((0, 0, 0)) #black
alex_text.write("hello")
time.sleep(1)
alex_text.clear()
alex_text.pencolor((.1, .1, .1)) #dark grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.5, .5, .5)) #Grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.8, .8, .8)) #Light grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((1, 1, 1)) #white
alex_text.write("hello")
time.sleep(1)
alex_text.clear() #gone
time.sleep(1)
The text simulates an opacity increase to maximum. Alex's stamps are unmodified.

you can do this by using turtle.hideturtle() if you want full opacity.
Like used here in the last line:
import turtle
t = turtle.Turtle()
t.speed(1)
t.color("blue")
t.begin_fill()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("red")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("green")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("yellow")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.hideturtle()

Related

Repeating function in Python turtle

I am working on an assignment for school and want to draw a large building with repeating windows. I now you would probably use a for loop for the windows to repeat, but I don't know how to set it up. Any help would be great! Here is my code:
import turtle
def draw_rectangle(t, x, y, width, height, tilt, pen, fill):
t.hideturtle()
t.up()
t.goto(x,y)
t.setheading(0 + tilt)
t.fillcolor(fill)
t.pencolor(pen)
t.down()
t.begin_fill()
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)
t.end_fill()
t.up()
t.down()
t.begin_fill()
draw_rectangle(t, -80, 100, 20, 30, 0, "white", "white")
t.end_fill()
t.up()
By making the window for loop into a function, it will be much easier to call and won't be as long. Declare this function outside of the draw_rectangle function and call it inside the draw_rectangle function with draw_window() whenever you need to draw a window.
def draw_window():
for i in range (2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)

Fill color is not working for my function (Python turtle graphics)

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()

Use a while loop to control game in Python turtle

I am trying to make a random game in which whichever character reaches the end first wins. I have basically the entire code made, but I need help with the end to determine who crosses the finish line first. So how would I do that? My code is:
from turtle import Turtle
from random import randint
t = Turtle()
t.speed(0)
t.up()
t.goto(-200,0)
t.down()
t.forward(900)
t.up()
t.goto(-200,100)
t.down()
t.forward(900)
t.up()
t.goto(-200,200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,-100)
t.down()
t.forward(900)
t.up()
t.goto(-200,-200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,200)
t.down()
t.right(90)
t.forward(400)
t.left(90)
t.up()
t.goto(-100, -200)
t.left(90)
t.down()
t.forward(400)
t.up()
t.goto(800,-200)
t.down()
t.forward(400)
t.up()
t.goto(700,-200)
t.down()
t.forward(400)
d = Turtle()
d.speed(5)
d.color('red')
d.shape('arrow')
d.up()
d.goto(-155,150)
d.right(360)
c = Turtle()
c.speed(5)
c.color('blue')
c.shape('turtle')
c.up()
c.goto(-155,50)
c.right(360)
b = Turtle()
b.speed(5)
b.color('yellow')
b.shape('arrow')
b.up()
b.goto(-155,-50)
b.right(360)
a = Turtle()
a.speed(5)
a.color('green')
a.shape('turtle')
a.up()
a.goto(-155,-150)
a.right(360)
for i in range(350):
a.forward(randint(1,6))
b.forward(randint(1,6))
d.forward(randint(1,6))
c.forward(randint(1,6))
Any suggestions to make the code smaller would also be appreciated. I am trying to use a while loop so that I can stop the game as soon as the finish line is crossed.
We can test if a turtle's .xcor() value is greater than the x coordinate of the finish line to see if someone has won the race. I've reworked your code accordingly below along with some code compacting (but not as much as I could do, just to keep things nicely simple):
from turtle import Screen, Turtle
from random import randint
START_LINE = -300
FINISH_LINE = 300
screen = Screen()
screen.setup(1000, 600)
t = Turtle(visible=False)
t.speed('fastest')
# Race Lanes
for y in range(-200, 300, 100):
t.up()
t.goto(START_LINE - 100, y)
t.down()
t.forward((FINISH_LINE + 90) - (START_LINE - 100))
# Starting and Finishing Gates
for x in [START_LINE - 100, FINISH_LINE - 10]:
t.up()
t.goto(x, 200)
t.right(90)
t.down()
t.forward(400)
t.left(90)
t.forward(100)
t.left(90)
t.forward(400)
t.right(90)
d = Turtle('arrow')
d.color('red')
d.speed(5)
d.up()
d.goto(START_LINE - 50, 150)
d.right(360)
c = Turtle('turtle')
c.color('blue')
c.speed(5)
c.up()
c.goto(START_LINE - 50, 50)
c.right(360)
b = Turtle('arrow')
b.color('yellow')
b.speed(5)
b.up()
b.goto(START_LINE - 50, -50)
b.right(360)
a = Turtle('turtle')
a.color('green')
a.speed(5)
a.up()
a.goto(START_LINE - 50, -150)
a.right(360)
while a.xcor() < FINISH_LINE and b.xcor() < FINISH_LINE and c.xcor() < FINISH_LINE and d.xcor() < FINISH_LINE:
a.forward(randint(1, 6))
b.forward(randint(1, 6))
c.forward(randint(1, 6))
d.forward(randint(1, 6))
# The race is over
screen.mainloop()
It's good to define key locations with variables so that you don't have to work out numbers for every step in the code -- you can instead calculate and adjust as needed.

Changing bar graph colors in Python

I am new to Python and I am struggling with a lesson in our book where we have to change the colors in a bar graph. I am not sure what I am doing wrong. There is no error message the colors are just printing black.
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
if xs is 48:
tess.color("blue")
if xs is 117:
tess.color("yellow")
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
This is what I have so far.
Thanks for the help!
I think you need to have a color assigned before you begin_fill(). Because the computer is like: "Well, I'll fill the box, but I don't have a color", so it uses the default of black.
The if statement you provided doesn't make sense, so I moved it up in the function and modified it where you can examine the height within the function. Instead of using if/else, I'd recommend dictionaries.
This new code works:
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
# Select Color
if height is 48:
color = "blue"
elif height is 117:
color = "yellow"
else:
color = "black"
# Assign color
t.color(color)
# The rest of your code
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()

Drawing a indian flag chakra in python turtlee

I am drawing the Indian flag using turtle and python. I have got the rectangles and colours down so far, but am struggling to make the chakra in the middle.
It has 24 spokes, and is surrounded by a closed circle. Any tips on how to accomplish this?
This is my code now:
import turtle
def drawRectangle (t, w, h, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(h)
t.left(90)
t.forward(w)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
chloe = turtle.Turtle()
drawRectangle(chloe,50,200, "chartreuse3")
chloe.up()
chloe.goto(0,-100)
chloe.down()
drawRectangle(chloe,50,200, "orange1")
chloe.up()
chloe.goto(100,-25)
chloe.down()
chloe.pencolor("blue4")
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
chloe.up()
chloe.goto(300,300)
main()
You can use the circle to draw the circle:
chloe.pencolor("blue4")
# draw the spokes
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
# raise pen
chloe.up()
# head down
chloe.setheading(270)
# go forward 20
chloe.forward(20)
# reset heading
chloe.setheading(0)
# pen down
chloe.down()
# draw the circle
chloe.circle(20)
import turtle
def drawRectangle (t, l, b, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(b)
t.left(90)
t.forward(l)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
india = turtle.Turtle()
drawRectangle(india,50,200, "orange1")
india.up()
india.goto(0,-100)
india.down()
drawRectangle(india,50,200, "green")
india.up()
india.goto(100,-20)
india.down()
india.pencolor("blue3")
for i in range(24):
india.forward(20)
india.backward(20)
india.left(15)
india.up()
india.goto(400,400)
main()

Categories

Resources