Ich habe Probleme damit, Kreise in meinem This-Turtle-Programm auszudrucken. Wie kann ich herausfinden, was mein ProfessPython

Python-Programme
Anonymous
 Ich habe Probleme damit, Kreise in meinem This-Turtle-Programm auszudrucken. Wie kann ich herausfinden, was mein Profess

Post by Anonymous »

Das Problem besteht darin, dass „circle_locs“ eine leere Liste bleibt.

Ändern Sie handle_mouse_click: abhängig von current_shape, hängen Sie es entweder an „square_locs“, „circle_locs“ oder „path_verts“ an (verwenden Sie if-elif-else):

Code: Select all

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)
Immer noch keine Kreise! Sie müssen draw_circles implementieren (sehr ähnlich zu draw_squares). Implementieren Sie außerdem draw_circle. Es ist ähnlich wie draw_square, aber statt der forloop rufen Sie einfach t.circle(radius) auf. Der Radius beträgt die Hälfte der aktuellen_Größe.
Jetzt werden Kreise und Quadrate gezeichnet. Haben Sie Probleme? Fügen Sie in jeder Funktion einige Druckanweisungen hinzu und führen Sie das Programm aus. Suchen Sie in der IDLE-Konsole nach Ihrer Debug-Druckausgabe.

Mir ist bewusst, dass ich diesen Code implementieren muss, aber ich habe Probleme damit, wo ich den Code ablegen soll. Die Arbeit, die ich derzeit habe, verändert die Größe eines Quadrats, aber ich muss in der Lage sein, den Stift zu wechseln, um das jetzt mit Kreisen machen zu können. Das ist für eine Hausaufgabe.

Code: Select all

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post