I am programming in python 3.2.2 turtle. I am able to make the turtle follow my cursor when I clack but now I have trouble changing the appearance of the turtle. In my code you will see a tank, and I want the image of the tank to be my turtle.
Here is my code:
#importing modules
from turtle import Turtle
from turtle import *
#Setting up variables
unVar1 = 25
unVar2 = 100
unVar3 = 90
unVar4 = 150
unVar5 = -30
unVar6 = 75
unVar7 = 50
screen = Screen() # create the screen
#first part in making the turtle move
turtle = Turtle()
t = Turtle() # create the first turtle
t2 = Turtle() # create the second turtle
screen.onscreenclick(turtle.goto) # set up the callback for moving the first turtle
#defining shapes and objects
def drawSquare(t , xPrime, yPrime, sideLength):
t.up()
t.hideturtle()
t.goto(xPrime, yPrime)
t.setheading(270)
t.down()
for count in range(4):
t.forward(sideLength)
t.left(90)
t.end_fill()
def drawRectangle(t, x2, y2, sideLength1, sideLength2):
t.up()
t.hideturtle()
t.goto(x2, y2)
t.setheading(270)
t.down()
for count in range(2):
t.forward(sideLength1)
t.left(90)
t.forward(sideLength2)
t.left(90)
t.end_fill()
def drawTank():
t.pencolor("black")
t.fillcolor("gray")
t.begin_fill()
tire1 = drawRectangle(t, int("10"), unVar1, unVar6, int("30")) #Tire
t.begin_fill()
tire2 = drawRectangle(t, int("110"), unVar1, unVar6, int("30")) #Tire
t.begin_fill()
tire3 = drawRectangle(t, int("110"), unVar2, unVar6, int("30")) #Tire
t.begin_fill()
tire4 = drawRectangle(t, int("10"), unVar2, unVar6, int("30")) #Tire
t.pencolor("gray")
t.fillcolor("black")
t.begin_fill()
bodyTank = drawRectangle(t, int("20"), unVar3, int("130"), int("110"))
t.begin_fill()
gunTank = drawRectangle(t, int("65"), unVar4, int("100"), int("20")) #Gun
t.begin_fill()
exhaustTank = drawRectangle(t, int("50"), unVar5, int("20"), int("10"))
t.fillcolor("red")
t.begin_fill()
turretTank = drawSquare(t, int("50"), unVar7, int("50")) #Turret
t.end_fill()
drawTank()
screen.mainloop() # start everything running
You can create a tank cursor with the code you have, you just need to rearrange it. The drawing functions need to create polygons rather than draw directly to the screen. The following will create your tank cursor and drive it around the screen a bit. See the comments on tankCursor() about how you can also used fixed polygons instead -- you don't have to make a bitmap image to do this:
import turtle
unVar1 = 25
unVar2 = 100
unVar3 = 90
unVar4 = 150
unVar5 = -30
unVar6 = 75
unVar7 = 50
def polySquare(t, x, y, length):
t.goto(x, y)
t.setheading(270)
t.begin_poly()
for count in range(4):
t.forward(length)
t.left(90)
t.end_poly()
return t.get_poly()
def polyRectangle(t, x, y, length1, length2):
t.goto(x, y)
t.setheading(270)
t.begin_poly()
for count in range(2):
t.forward(length1)
t.left(90)
t.forward(length2)
t.left(90)
t.end_poly()
return t.get_poly()
def tankCursor():
"""
Create the tank cursor. An alternate solution is to toss the temporary turtle
and use the commented out polygon assignments instead of the poly* function calls
"""
temporary = turtle.Turtle()
screen = turtle.getscreen()
delay = screen.delay()
screen.delay(0)
temporary.hideturtle()
temporary.penup()
tank = turtle.Shape("compound")
# tire1 = ((10, unVar1), (10, unVar1 - unVar6), (10 + 30, unVar1 - unVar6), (10 + 30, unVar1))
tire1 = polyRectangle(temporary, 10, unVar1, unVar6, 30) # Tire #1
tank.addcomponent(tire1, "gray", "black")
# tire2 = ((110, unVar1), (110, unVar1 - unVar6), (110 + 30, unVar1 - unVar6), (110 + 30, unVar1))
tire2 = polyRectangle(temporary, 110, unVar1, unVar6, 30) # Tire #2
tank.addcomponent(tire2, "gray", "black")
# tire3 = ((110, unVar2), (110, unVar2 - unVar6), (110 + 30, unVar2 - unVar6), (110 + 30, unVar2))
tire3 = polyRectangle(temporary, 110, unVar2, unVar6, 30) # Tire #3
tank.addcomponent(tire3, "gray", "black")
# tire4 = ((10, unVar2), (10, unVar2 - unVar6), (10 + 30, unVar2 - unVar6), (10 + 30, unVar2))
tire4 = polyRectangle(temporary, 10, unVar2, unVar6, 30) # Tire #4
tank.addcomponent(tire4, "gray", "black")
# bodyTank = ((20, unVar3), (20, unVar3 - 130), (20 + 110, unVar3 - 130), (20 + 110, unVar3))
bodyTank = polyRectangle(temporary, 20, unVar3, 130, 110)
tank.addcomponent(bodyTank, "black", "gray")
# gunTank = ((65, unVar4), (65, unVar4 - 100), (65 + 20, unVar4 - 100), (65 + 20, unVar4))
gunTank = polyRectangle(temporary, 65, unVar4, 100, 20) # Gun
tank.addcomponent(gunTank, "black", "gray")
# exhaustTank = ((50, unVar5), (50, unVar5 - 20), (50 + 10, unVar5 - 20), (50 + 10, unVar5))
exhaustTank = polyRectangle(temporary, 50, unVar5, 20, 10)
tank.addcomponent(exhaustTank, "black", "gray")
# turretTank = ((50, unVar7), (50, unVar7 - 50), (50 + 50, unVar7 - 50), (50 + 50, unVar7))
turretTank = polySquare(temporary, 50, unVar7, 50) # Turret
tank.addcomponent(turretTank, "red", "gray")
turtle.addshape("tank", shape=tank)
del temporary
screen.delay(delay)
tankCursor() # creates and registers the "tank" cursor shape
turtle.shape("tank")
turtle.up() # get rid of the ink
# show our tank in motion
turtle.setheading(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(45)
turtle.forward(100)
turtle.done()
Since the tank is a shape you created using turtle, you can always simply take a screenshot of your tank and set that screenshot as the shape of the turtle using the turtle module's register_shape and shape functions like so:
turtle.register_shape("INSERT PATH OF SCREENSHOT HERE")
t.shape("INSERT PATH OF SCREENSHOT HERE")
However, if you want to do something harder, you can use the turtle.register_shape() method shown above, but instead of supplying the path of some image, you will instead have to supply a name, and then some coordinates to create the shape. In short, the syntax for that would be:
turtle.register_shape(name, (coordinates))
t.shape(name)
However, that would be very tedious seeing as how complex your tank already is, so I would recommend going on with the method above, where you just take a screenshot of the tank and use that screenshot as a shape for the turtle.
Also, some issues I see with your code:
The following:
from turtle import Turtle
from turtle import *
You are importing the turtle module twice. There is no need for that! from turtle import * already imports the Turtle() function, so the first import command is not needed. Therefore, change the import command to just:
from turtle import *
# The rest of your code...
When the user clicks on the screen to go somewhere, your turtle creates a line wherever it goes. There is a way to avoid this. However, I don't know if you want this, but if you do and decide to keep it, it does result in a lot of the lines going to places they were not supposed to and could eventually start hogging up a lot of memory until your program crashes. So I do recommend taking this step, but whether or not you do is up to you. However, if you do decide to take this step, then to start, define the following function after t2 = Turtle():
def Go(x, y):
# Turn the animation completely off
screen.tracer(0, 0)
# Pick the pen up so no lines are created as the turtle moves
turtle.penup()
# Go to the clicked position on the canvas
turtle.goto(x, y)
# Put the pen back down
turtle.pendown()
# Turn animation back on
screen.tracer(1, 1)
And then change:
screen.onscreenclick(turtle.goto)
To:
screen.onscreenclick(Go)
And that's it! I hope this helps! :)
Related
I'm currently making a 5x5 grid for a snakes and ladder but can't figure out how to display the numbers on the grid in this formation;
21,22,23,24,25
20,19,18,17,16
11,12,13,14,15
10, 9, 8, 7, 6
1 , 2, 3, 4, 5
The code I've wrote makes the 5x5 grid, I'm juts note sure how to add the labels of if I should take a
different approach with the code.
thanks for any help.
import turtle
t = turtle.Turtle()
speed = turtle.speed()
t.speed(10)
x=250
y=250
t.penup()
t.goto(-x,y)
t.pendown()
turtle.hideturtle()
turtle.ht()
for a in range(5):
t.penup()
t.goto(-x,-y)
t.pendown()
y=y-100
for b in range(5):
for n in range(5):
t.speed(10)
t.fd(100)
if n!=4:
t.right(90) here
Here's a solution that uses much of your existing logic, tweaking it a bit, and adds the numbers:
from turtle import Screen, Turtle
FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'bold')
BOXES = 5
BOX_SIZE = 100
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
x = -BOX_SIZE * BOXES / 2
y = -BOX_SIZE * BOXES / 2
for i in range(BOXES):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for j in range(BOXES):
for _ in range(4):
turtle.forward(BOX_SIZE)
turtle.left(90)
dx, dy = turtle.position()
turtle.penup()
turtle.goto(dx + BOX_SIZE/2, dy + BOX_SIZE/2 - FONT_SIZE / 2)
turtle.write(j + BOXES * i + 1, align='center', font=FONT)
turtle.setposition(dx, dy)
turtle.pendown()
turtle.forward(BOX_SIZE)
y += BOX_SIZE
screen.exitonclick()
Centering-wise, the vertical font correction (FONT_SIZE / 2) is an approximation that works fine on my system but if you want an accurate solution for all systems, see this answer
I was just wondering how can I make a bar graph that uses the key-pair from a dictionary to use it for the x-axis and the key-value to draw the graph.
For example: dic = {'0-10': 24, '10-20': 20, '20-30': 22, '30-40': 27, '40-50': 150, '50-60': 0, '60-70': 231, '70-80': 467, '80-90': 443, '90-100': 86}
I want the '0-10' as the x-axis and the number as the height of the bar. Here's my my code so bar but I can't find a find to draw the x-axis for it. Also the y-axis will always be 100.
import turtle
from main import value_pairs, key_values
# Basic function to draw "bar graph", it takes the height as prameter
def drawBar(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.left(90)
t.end_fill()
xs = value_pairs
maxheight = max(xs)
numbars = len(xs)
border = 5
wn = turtle.Screen()
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("white")
t = turtle.Turtle()
t.color("black")
t.fillcolor("white")
t.pensize(3)
for x in value_pairs:
drawBar(t, x)
I've reworked your attempt to roughly do what you describe, filling in missing pieces from your posted code:
from turtle import Screen, Turtle
FONT_HEIGHT = 18
FONT = ('Arial', FONT_HEIGHT, 'normal')
BORDER = FONT_HEIGHT
def drawBar(t, datum):
label, height = datum
t.left(90)
t.begin_fill()
t.forward(height)
t.right(90)
t.forward(20)
if height > FONT_HEIGHT:
t.write(height, align="center", font=FONT)
t.forward(20)
t.right(90)
t.forward(height)
t.end_fill()
t.left(90)
t.backward(40)
t.forward(20)
t.write(label, align="center", font=FONT)
t.forward(20)
data = {
'0-10': 24,
'10-20': 20,
'20-30': 22,
'30-40': 27,
'40-50': 150,
'50-60': 0,
'60-70': 231,
'70-80': 467,
'80-90': 443,
'90-100': 86
}
maxheight = max(data.values())
numbars = len(data)
screen = Screen()
screen.setworldcoordinates(-BORDER, -BORDER, 40 * numbars + BORDER, maxheight + BORDER)
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtle.fillcolor('white')
turtle.pensize(3)
for datum in data.items():
drawBar(turtle, datum)
turtle.hideturtle()
screen.exitonclick()
Matplotlib is the gold standard for data visualization with Python, but it's always fun to try these things with turtle to get a better understanding of what's involved in drawing plots.
Warning: this approach assumes ordered dictionaries -- not a historically safe assumption.
Not originally my code:
import turtle
wn = turtle.Screen()
wn.bgcolor('Black')
wn.setup( width = 250, height = 250)
turtle = turtle.Turtle()
def snowflake (size, pensize, x, y):
""" function that draws a snowflake """
turtle.speed(100)
turtle.penup()
turtle.goto(x, y)
turtle.forward(10*size)
turtle.left(45)
turtle.pendown()
turtle.color('white')
for x in range (8):
branch(size)
turtle.left(45)
def branch (size):
for z in range (3):
for z in range (3):
turtle.forward(10.0*size/3)
turtle.backward(10.0*size/3)
turtle.right(45)
turtle.left(90)
turtle.backward(10.0*size/3)
turtle.left(45)
turtle.right(90)
turtle.forward(10.0*size)
snowflake(8, 6, 0, 0)
wn.exitonclick()
If it's not originally your code, your should provide proper blame, I mean credit for it. The issue with this code is that the base angle 45 is being used to generate the 8-sided snowflake as well as the angle of the small branches. So when we go to use 60 for a 6-sided snowflake, it's hard to know which 45's (or 90's) to replace and which to keep. The author of the original code didn't help any by starting the branch logic in the snowflake() function, before calling the branch() function. So let's tease out the two different uses of 45 degrees, make the code more explicit, and switch just the sides to 60 degrees:
from turtle import Screen, Turtle
SIDES = 6
BRANCH_ANGLE = 45
def snowflake(size, x, y):
""" function that draws a snowflake """
turtle.penup()
turtle.goto(x, y)
turtle.forward(size)
turtle.left(BRANCH_ANGLE)
turtle.pendown()
for _ in range(SIDES):
branch(size)
turtle.left(BRANCH_ANGLE)
def branch(size):
for _ in range(3):
for _ in range(3):
turtle.forward(size / 3)
turtle.backward(size / 3)
turtle.right(BRANCH_ANGLE)
turtle.left(2 * BRANCH_ANGLE)
turtle.backward(size / 3)
turtle.left(BRANCH_ANGLE)
turtle.right(BRANCH_ANGLE + 360 / SIDES)
turtle.forward(size)
screen = Screen()
screen.bgcolor('black')
screen.setup(width=250, height=250)
turtle = Turtle()
turtle.color('white')
turtle.speed('fastest')
snowflake(80, 0, 0)
turtle.hideturtle()
screen.exitonclick()
I am new to python animation, although have managed to use animation to make the snowflake rotate but it is not rotating so smoothly...
I have attached the code below, can anyone please suggest me what do I do to make it rotate more smoothly?
import turtle
import time
bob = turtle.Turtle()
bob.ht()
screen = turtle.Screen()
bob.speed(100)
screen.tracer(0, 0)
n = 0
bob.pencolor("blue")
bob.pensize(5)
def vshape():
bob.rt(25)
bob.fd(50)
bob.backward(50)
bob.lt(50)
bob.fd(50)
bob.backward(50)
bob.rt(25)
def snowflakeArm():
for x in range(4):
bob.fd(30)
vshape()
bob.backward(120)
def snowflake(n, x, y):
bob.seth(n)
bob.pu()
bob.setpos(x ,y)
bob.pd()
for x in range(6):
snowflakeArm()
bob.rt(60)
while True:
time.sleep(0.02)
bob.update()
bob.clear()
snowflake(n, 0, 0)
snowflake(n, 350, 0)
snowflake(n, -350, 0)
snowflake(n, 0, 350)
snowflake(n, 0, -350)
n += 10
I have tried to decrease the sleep time as well as decrease the amount of rotation but still no result...
It concerns me that you're able to call bob.update() successfully as update() isn't a method of Turtle in the standard turtle.py released with Python 3 nor Python 2. You may be using an older, or non-standard, turtle implementation so some of the below may need to be adjusted.
Rather than introduce time.sleep(), which is out of sync with turtle's own event handler, let's use a turtle ontimer() event:
from turtle import Screen, Turtle
def vshape():
turtle.right(25)
turtle.forward(50)
turtle.backward(50)
turtle.left(50)
turtle.forward(50)
turtle.backward(50)
turtle.right(25)
def snowflakeArm():
for _ in range(4):
turtle.forward(30)
vshape()
turtle.backward(120)
def snowflake(angle, position):
turtle.setheading(angle)
turtle.penup()
turtle.setposition(position)
turtle.pendown()
for _ in range(360 // 60):
snowflakeArm()
turtle.right(60)
angle = 0
def snowflakes():
global angle
turtle.clear()
snowflake(angle, (0, 0))
snowflake(angle, (350, 0))
snowflake(angle, (-350, 0))
snowflake(angle, (0, 350))
snowflake(angle, (0, -350))
screen.update()
angle = (angle + 10) % 360
screen.ontimer(snowflakes, 25) # repeat 25 milliseconds from now
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.pencolor('blue')
turtle.pensize(5)
snowflakes()
screen.exitonclick()
This should also allow us to exit the program cleanly, by clicking on the window, without generating all those warning messages.
I have created the wheel, but when I try to make code to spin it will not work.
I have already tried to make it using a loop but that was near impossible for me. I am basically drawing the wheel over. Here is some code from the spinning wheel part:
turtle.listen()
if turtle.onkeypress("space"):
colors = ['#880000','#884400','#884400','#888800',
'#888800','#008800','#008800','#008800',
'#008800','#008800','#008888','#008888',
'#008888','#008888','#008888','#000088',
'#000088','#000088','#000088','#000088']
for color in colors:
slice_angle = 360 / len(colors)
heading, position = 90, (center[0] + radius, center[1])
turtle.color(color, color)
turtle.speed(0)
turtle.penup()
turtle.goto(position)
turtle.setheading(heading)
turtle.pendown()
turtle.begin_fill()
turtle.circle(radius, extent=slice_angle)
heading, position = turtle.heading(), turtle.position()
turtle.penup()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
time.sleep(0.2)
colors = ['#884400','#884400','#888800',
'#888800','#008800','#008800','#008800',
'#008800','#008800','#008888','#008888',
'#008888','#008888','#008888','#000088',
'#000088','#000088','#000088','#000088','#880000']
for color in colors:
slice_angle = 360 / len(colors)
heading, position = 90, (center[0] + radius, center[1])
turtle.color(color, color)
turtle.speed(0)
turtle.penup()
turtle.goto(position)
turtle.setheading(heading)
turtle.pendown()
turtle.begin_fill()
turtle.circle(radius, extent=slice_angle)
heading, position = turtle.heading(), turtle.position()
turtle.penup()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
time.sleep(0.2)
The code keeps on going to make the wheel 'spin'.
This is what I get:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 701, in eventfun
fun()
TypeError: 'str' object is not callable
My impression is that you're not trying to make the wheel spin but rather make it look like it's spinning by cycling its colors. Here's my example of doing such which uses tracer() and update() to turn off drawing while remaking the circle with the colors shifted. It uses a timer event to trigger redraws. Rather than your fixed list of colors, I'm going to use continuous hues, but you should be able to use any colors you wish:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
RADIUS = 100
NUMBER_OF_WEDGES = 20
SLICE_ANGLE = 360 / NUMBER_OF_WEDGES
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.penup()
center = turtle.position()
turtle.sety(turtle.ycor() - RADIUS)
hues = [color / NUMBER_OF_WEDGES for color in range(NUMBER_OF_WEDGES)] # precompute hues
index = 0
def draw_circle():
global index
for hue in range(NUMBER_OF_WEDGES):
turtle.color(hsv_to_rgb(hues[(hue + index) % NUMBER_OF_WEDGES], 1.0, 1.0))
turtle.pendown()
turtle.begin_fill()
turtle.circle(RADIUS, extent=SLICE_ANGLE)
position = turtle.position()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
turtle.goto(position)
screen.update()
index = (index + 1) % NUMBER_OF_WEDGES
screen.ontimer(draw_circle, 40)
draw_circle()
screen.exitonclick()
This works, but, on my system, inexplicably slows down over time.
Let's try a different approach that doesn't redraw anything at the Python level during the cycling of colors. We're going to design a new cursor shape, "wedge" and build our circle out of turtles! All the wedges will be prepositioned and not move nor be redrawn. The timer event handler will simply ask each turtle to take on the color of its neighbor:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
RADIUS = 100
NUMBER_OF_WEDGES = 20
SLICE_ANGLE = 360 / NUMBER_OF_WEDGES
screen = Screen()
screen.tracer(False)
# create a pie wedge-shaped cursor
turtle = Turtle(visible=False)
turtle.begin_poly()
turtle.sety(turtle.ycor() - RADIUS)
turtle.circle(RADIUS, extent=SLICE_ANGLE)
turtle.home()
turtle.end_poly()
screen.register_shape("wedge", turtle.get_poly())
# create a turtle for each wedge in the pie
turtles = []
for hue in range(NUMBER_OF_WEDGES):
turtle = Turtle("wedge")
turtle.color(hsv_to_rgb(hue / NUMBER_OF_WEDGES, 1.0, 1.0))
turtle.setheading(hue * SLICE_ANGLE)
turtles.append(turtle)
def draw_circle():
# have each turtle take on the color of its neighbor
for index, turtle in enumerate(turtles):
turtle.color(*turtles[(index + 1) % NUMBER_OF_WEDGES].color())
screen.update()
screen.ontimer(draw_circle, 40)
draw_circle()
screen.exitonclick()
Notice how much simpler our main loop, draw_circle(), is and that the spinning doesn't slow down.