Python turtle 库 自学1

Turtle (海龟库)

  • 1、打开海龟壳工具箱及创建一支画笔
  • 2、让画笔前进和转弯
  • 3、改变画笔的颜色
  • 4、让画笔画一个圆圈

1、打开海龟壳工具箱及创建一支画笔

# 方法一
import turtle

pen = turtle.Turtle() 
# 创建一支画笔 pen

# 方法二
from turtle import *

pen = Turtle()
# 创建一支画笔 pen

2、让画笔前进和转弯

import turtle

pen = turtle.Turtle()
# 创建一支画笔 pen
pen.forward(100)
# 让画笔前进 100 个像素
pen.left(90)
# 让画笔逆时针转 90°
pen.forward(200)
# 让画笔前进 200 个像素

turtle.done()
# 让屏幕暂停

3、改变画笔的颜色

import turtle

turtle.colormode(255)
# 设置 colormode 选项
pen = turtle.Turtle()
# 创建一支画笔 pen
pen.forward(100)
# 让画笔前进 100 个像素
pen.left(90)
# 让画笔逆时针转 90°
pen.color(255, 0, 0)
# RGB 颜色
# 将画笔的颜色改为红色
pen.forward(200)
# 让画笔前进 200 个像素

turtle.done()
# 让屏幕暂停

4、让画笔画一个圆圈

import turtle

turtle.colormode(255)
# 设置 colormode 选项
pen = turtle.Turtle()
# 创建一支画笔 pen
pen.forward(100)
# 让画笔前进 100 个像素
pen.left(90)
# 让画笔逆时针转 90°
pen.color(255, 0, 0)
# RGB 颜色
# 将画笔的颜色改为红色
pen.forward(200)
# 让画笔前进 200 个像素
pen.circle(90)
# 以 90 像素为半径画圆

turtle.done()
# 让屏幕暂停

你可能感兴趣的:(Python,turtle,库,python,turtle)