我是在 Python 3 中使用 Turtle 图形的新手,我不知道该怎么做。我的问题之一是我不知道从哪里开始创建一个函数,该函数将根据包含 3 个变量的名为“路径”的数据集在网格单元格内绘制标记。网格 map 本身是 7x7,总共有 5 个标记(范围从 0 到 4)。每个标记都绘制了自己的社交媒体 Logo ,并且彼此完全不同。
grid_cell_size = 100 # px
num_squares = 7 # this for creating the 7x7 grid map
# for clarification: path = ['Start', location, marker_value]
path = [['Start', 'Centre', 4], ['North', 2, 3],
['East', 2, 2], ['South', 4, 1], ['West', 2, 0]]
我的主要目标是能够使用上述数据集在它们自己的位置坐标中同时绘制所有 5 个标记。我不确定我应该如何将这些标记分配给它们自己的 marker_value。 if/elif/else 语句是否适用于此?
尝试一次实现 5 个标记对我来说太过分了,所以我尝试使用这个名为“path_var_3”的非常简单的数据集,它只会绘制 1 个标记。
path_var_3 = [['Start', 'Bottom left', 3]]
def follow_path(path_selection):
# Draws YouTube logo marker
if 3 in path_var_3[0]:
penup()
# !!!
goto(0, -32)
setheading(90)
# Variables
youtube_red = '#ff0000'
youtube_white = 'White'
radius = 10
diameter = radius * 2
# Prep to draw the superellipse
pencolor(youtube_red)
fillcolor(youtube_red)
# Drawing the superellipse
begin_fill()
pendown()
for superellipse in range(2):
circle(radius, 90)
forward(80)
circle(radius, 90)
forward(60 - diameter)
# Finish up
end_fill()
penup()
# Move turtle position towards the centre of the superellipse
# !!!
goto(-59)
backward(16)
setheading(90)
fillcolor(youtube_white)
# Drawing the white 'play icon' triangle
begin_fill()
pendown()
for play_triangle in range(2):
right(120)
forward(28)
right(120)
forward(28)
# Finish up
endfill()
penup()
else: return print('ERROR')
follow_path(path_var_3)
到目前为止,我能够在程序中绘制标记,但我立即遇到了第一个问题:我意识到我已经硬编码了超椭圆和三角形开始绘制的坐标,如“!!!”所示注释。所以当我运行程序时,标记被绘制在网格单元之外。无论单元格位于 7x7 网格 map 中的哪个位置,如何让标记绘制在单元格内?
如果有人有任何想法或能够提供帮助,我将不胜感激。
长话短说:
- 如何在 7x7 网格 map 的 100x100 单元格内绘制由各种形状组成的标记?
- 如何根据数据集中的位置变量在任何单元格中绘制标记?
- 我应该如何为 0-4 范围内的整数分配标记? if/elif/else 语句?
最佳答案
由于 goto(-59)
和 endfill()
不是有效的函数调用,您提供的代码无法运行。总的来说,您的代码缺少一层来组织您要解决的问题。 (例如,您需要在代码方面定义“左下”或“东”的含义。)简而言之,您的 YouTube Logo 绘图使用的是绝对坐标而不是相对坐标,因此无法在任何地方绘制。
下面是您所描述内容的框架实现。它出于调试目的绘制了一个网格,以显示 Logo 最终位于正确的位置。它用彩色圆圈代替 YouTube Logo 以外的所有内容:
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()
关于Python Turtle 如何在 7x7 网格的单元格内绘制标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172078/