Код, который вы указали, не может быть запущен из-за того, что goto(-59)
и endfill()
не являются допустимыми вызовами функций. В целом в вашем коде отсутствует слой для организации проблемы, которую вы пытаетесь решить. (Например, вам нужно определить по коду, что означают «Слева внизу» или «Восток».) В малом, ваш логотип YouTube использует абсолютные координаты, а не относительные, предотвращая его отрисовку в любом месте.
Ниже приведена скелетная реализация того, что вы описываете. Он рисует сетку для целей отладки, чтобы показать, что логотипы оказались в правильных местах. Он заменяет цветные круги на все, кроме логотипа YouTube:
from turtle import Turtle, Screen
path = [('Start', 'Centre', 4), ('North', 2, 3), ('East', 2, 2), ('South', 4, 1), ('West', 2, 0)]
GRID_CELL_SIZE = 100 # pixels
NUMBER_SQUARES = 7 # this for creating the 7x7 grid map
ABSOLUTE_OFFSETS = {
'Centre': (NUMBER_SQUARES // 2, NUMBER_SQUARES // 2),
'Bottom left': (0, NUMBER_SQUARES - 1),
# etc.
}
COMPASS_OFFSETS = {
'North': (0, 1),
'East': (1, 0),
'South': (0, -1),
'West': (-1, 0),
'Start': (1, 1), # Special case, assumes absolute offset
}
# YouTube Variables
YOUTUBE_RED = '#ff0000'
YOUTUBE_WHITE = 'White'
YOUTUBE_RADIUS = 10
YOUTUBE_WIDTH = 80
YOUTUBE_HEIGHT = 60
YOUTUBE_TRIANGLE_EDGE = 28
def draw_grid(): # for debugging
grid = Turtle(visible=False)
grid.speed('fastest')
grid.dot() # visualize origin
grid.penup()
grid.goto(-GRID_CELL_SIZE * NUMBER_SQUARES / 2, GRID_CELL_SIZE * (NUMBER_SQUARES / 2 - 1))
for _ in range(NUMBER_SQUARES - 1):
grid.pendown()
grid.forward(NUMBER_SQUARES * GRID_CELL_SIZE)
grid.penup()
grid.goto(-GRID_CELL_SIZE * NUMBER_SQUARES / 2, grid.ycor() - GRID_CELL_SIZE)
grid.goto(-GRID_CELL_SIZE * (NUMBER_SQUARES / 2 - 1), GRID_CELL_SIZE * NUMBER_SQUARES / 2)
grid.setheading(270)
for _ in range(NUMBER_SQUARES - 1):
grid.pendown()
grid.forward(NUMBER_SQUARES * GRID_CELL_SIZE)
grid.penup()
grid.goto(grid.xcor() + GRID_CELL_SIZE, GRID_CELL_SIZE * NUMBER_SQUARES / 2)
def follow_path(path_selection):
turtle = Turtle(visible=False)
x, y = ABSOLUTE_OFFSETS['Centre'] # relative to grid, not screen!
for direction, offset, marker in path_selection:
if direction in COMPASS_OFFSETS:
dx, dy = COMPASS_OFFSETS[direction]
if offset in ABSOLUTE_OFFSETS:
x, y = ABSOLUTE_OFFSETS[offset]
else:
x += dx * offset
y += dy * offset
turtle.penup()
# new virtual drawing origin, convert to screen coordinates
turtle.goto((x - NUMBER_SQUARES // 2) * GRID_CELL_SIZE, (y - NUMBER_SQUARES // 2) * GRID_CELL_SIZE)
MARKERS[marker](turtle)
turtle.penup()
def YouTube(turtle):
diameter = YOUTUBE_RADIUS * 2
x, y = turtle.position()
# Draws YouTube logo marker
turtle.goto(x + YOUTUBE_WIDTH/2 + YOUTUBE_RADIUS, y + YOUTUBE_HEIGHT/2 - YOUTUBE_RADIUS)
turtle.setheading(90)
# Draw the rounded rectangle (should really be a superellipse)
turtle.color(YOUTUBE_RED)
turtle.begin_fill()
for _ in range(2):
turtle.circle(YOUTUBE_RADIUS, 90)
turtle.forward(YOUTUBE_WIDTH)
turtle.circle(YOUTUBE_RADIUS, 90)
turtle.forward(YOUTUBE_HEIGHT - diameter)
# Finish up
turtle.end_fill()
# Return turtle position towards the centre of the rounded rectangle
turtle.goto(x - YOUTUBE_TRIANGLE_EDGE/4, y + YOUTUBE_TRIANGLE_EDGE/2)
turtle.setheading(90)
# Drawing the white 'play icon' triangle
turtle.fillcolor(YOUTUBE_WHITE)
turtle.begin_fill()
for _ in range(2):
turtle.right(120)
turtle.forward(YOUTUBE_TRIANGLE_EDGE)
# Finish up
turtle.end_fill()
def RedDot(turtle):
turtle.dot(GRID_CELL_SIZE / 2, 'Red')
def BlueDot(turtle):
turtle.dot(GRID_CELL_SIZE / 2, 'Blue')
def GreenDot(turtle):
turtle.dot(GRID_CELL_SIZE / 2, 'Green')
def OrangeDot(turtle):
turtle.dot(GRID_CELL_SIZE / 2, 'Orange')
MARKERS = [RedDot, BlueDot, GreenDot, YouTube, OrangeDot]
screen = Screen()
draw_grid() # for debugging
follow_path(path)
screen.mainloop()
![enter image description here](https://i.stack.imgur.com/YU01E.png)