Python Turtle如何在7x7网格中的单元格内绘制标记
我对在Python3中使用海龟图形还不熟悉,我不知道该怎么做。我的问题之一是,我不知道从何处开始创建一个函数,该函数将基于一个名为“path”的数据集(包含3个变量)在网格单元内绘制一个标记。栅格地图本身是7x7,总共有5个标记(范围从0到4)。每个标记都绘制了自己的社交媒体徽标,并且彼此完全不同Python Turtle如何在7x7网格中的单元格内绘制标记,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我对在Python3中使用海龟图形还不熟悉,我不知道该怎么做。我的问题之一是,我不知道从何处开始创建一个函数,该函数将基于一个名为“path”的数据集(包含3个变量)在网格单元内绘制一个标记。栅格地图本身是7x7,总共有5个标记(范围从0到4)。每个标记都绘制了自己的社交媒体徽标,并且彼此完全不同 grid_cell_size = 100 # px num_squares = 7 # this for creating the 7x7 grid map # for clarification:
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个标记。我不确定如何将这些标记指定给它们自己的标记值。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栅格地图中的位置
如果有人有任何想法或能够帮助我,我将不胜感激
TL;博士:
由于
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()