在特定的 x,y 坐标处绘制一个像素

Draw a pixel at specific x,y coordinates

我正在使用 Turtle Graphics,我想在特定的 x,y 位置绘制一个像素

类似于:

pixel = turtle.Turtle()
pixel.draw(x, y)

可以吗?

  • turtle模块的goto, setpos and setposition方法可以 用于将位置设置为乌龟的给定 x、y。

  • 那么可以用dot的方法在该点画一个像素点


# STEP-1: GOING TO (X, Y)
# Any one of these methods can be used
# pixel.goto(x, y) # or
# pixel.setpos(x, y) # or
pixel.setposition(x, y)

# STEP-2: DRAWING A PIXEL
pixel.dot(1, "black") # drawing the pixel.

此外,可以定义一个函数,以防同一个函数像这样被多次使用 -:

def draw_pixel(turtle, x, y, color) :
    # Draws a pixel of given color using given turtle at (x, y)
    
    # Any one of these methods can be used
    # turtle.goto(x, y) # or
    # turtle.setpos(x, y) # or
    turtle.setposition(x, y)
    turtle.dot(1, color) # drawing the pixel.
    return