The problem is that circle_locs remains an empty list.
Modify handle_mouse_click: depending on current_shape, append to either square_locs, circle_locs, or path_verts (use if-elif-else):
if current_shape == 's': square_locs.append( [x, y, current_size] ) elif current_shape == 'p': circle_locs.append( [x, y, current_size] ) else: (do this one)Still no circles! You need to implement draw_circles (very similar to draw_squares). Also, implement draw_circle. It’s similar to draw_square, but instead of the forloop, just call t.circle(radius). The radius is half the current_size.
Now, circles and squares get drawn. Are you having problems? Add some print statements in every function, and run the program. Look in the IDLE console for your debug print output.
I am aware that I have to implement that code but I am struggling with where to put the code. The work that I have currently changes the size of a square but I need to be able to change the pen to be able to do that with circles now. This is for a homework.
import turtle
"""
This program lets the user draw squares, circles, and polygonal paths.
"""
# The locations of the squares. Each entry is a list, with [x, y, size]
square_locs= []
# The locations of the circles. Each entry is a list, with [x, y, size]
circle_locs = []
# The vertexes of the path. Each entry is a list, with [x, y]
path_verts = []
"""
When the user clicks, either a square, circle, or vertex
gets added. Which one, depends on the current shape:
's' : add a square
'c' : add a circle
'p' : add a vertex to the path
"""
current_shape = 's'
"""
Each added shape has a size. It's whatever the current size
was at the time of the click. This gets changed with the
+ and - keys.
"""
current_size = 15
def draw_square(x, y, size):
"""
Draw a square, at the given x, y position, with the given size
"""
t = turtle
t.up()
t.goto(x, y)
t.down()
for side in range(4):
t.fd(size)
t.left(90)
def draw_circle(x, y, diameter):
"""
Draw a circle at the given x y position, with the given diameter
"""
t = turtle
t.penup()
t.goto(x, y)
t.down()
t.circle()
# ******************************
# You must implement this function.
# ******************************
pass
def draw_squares():
"""
Draw all the existing squares
"""
for point in square_locs:
x, y, size = point
draw_square(x, y, size)
def draw_circles():
"""
Draw all the existing circles
"""
for point in circle_locs:
x, y, size = point
draw_square(x, y, size)
# ******************************
# You must implement this function
# ******************************
pass
def draw_path():
"""
Draw the current path, from its existing vertices
"""
# ******************************
# You must change this function;
# it doesn't quite work.
# ******************************
t = turtle
t.down()
for x, y, size in path_verts:
t.goto(x, y)
def redraw_scene():
"""
Draw all the objects, squares, circles, and path
"""
# Prepare to do fast drawing
turtle.tracer(False)
turtle.hideturtle()
# erase the window
turtle.clear()
# Draw all the object
draw_squares()
draw_circles()
draw_path()
# show the window again
turtle.update()
def handle_mouse_click(x, y):
"""
This is called when the user clicks somewhere
"""
# Add an [x, y, size] list to the list of square locations.
square_locs.append( [x, y, current_size] )
if current_shape == 's':
square_locs.append( [x, y, current_size] )
elif current_shape == 'p':
circle_locs.append( [x, y, current_size] )
# ******************************
# You must modify this function:
# Instead of just adding to square_locs, check current_shape
# and then either add to square_locs, circle_locs, or path_verts
# ******************************
pass
# Don't remove this call to redraw_scene().
# We just added something, so we need to re-draw everything.
redraw_scene()
def handle_key_plus():
"""
This is called when the user presses the + key
"""
# ******************************
# You must change this function
# ******************************
global current_size
current_size += 5
def handle_key_minus():
"""
This is called when the user presses the - key
"""
# ******************************
# You must change this function
# ******************************
global current_size
current_size -= 5
if current_size < 5:
current_size==5
pass
def handle_key_S():
"""
This is called when the user presses the S key
"""
# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 's'
def handle_key_C():
"""
This is called when the user presses the C key
"""
# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 'c'
pass
def handle_key_P():
"""
This is called when the user presses the P key
"""
# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 'p'
pass
def main():
t = turtle
# Create the turtle drawing window
t.setup(500, 500)
# register callbacks for all the user actions
t.onscreenclick(handle_mouse_click)
t.onkey(handle_key_S, 's')
t.onkey(handle_key_C, 'c')
t.onkey(handle_key_P, 'p')
t.onkey(handle_key_plus, '+')
t.onkey(handle_key_minus, '-')
# get keyboard focus (so that keyboard events will trigger onkey() calls)
t.listen()
# wait for keyboard, mouse, and timer events.
t.mainloop()
if __name__ == '__main__':
# Run the main() function when we run this module,
# but not when we "import" this module.
main()