前言:
1.在開始前需要引入turtle庫,若沒有下載這個庫可以復制下面這條語句在PyCharm終端下載
?
pip install turtle
2.turtle庫,它這個畫筆是在這個窗口橫軸x軸,縱軸y軸從原點(0,0)處開始,以函數(shù)指令使其移動繪圖。
正文:
一:開始前引入所需的庫
# turtle as t 是將turtle替換為t
import turtle as t
# 隨機庫
import random
# 引入turtle庫中的所有用法
from turtle import *
?二:設置窗口屬性和RGB顏色屬性以及繪圖數(shù)度
# 設置窗口大小和背景顏色
t.screensize(800,700,'black')
# RGB默認范圍是0~1,通過Screen().colormode設置成0~255
Screen().colormode(255)
# 加快作圖數(shù)度
t.speed(speed='fastest')
三:畫圣誕樹
# 畫樹
t.left(90)
t.forward(-300)
t.pensize(5)
# 設置畫筆顏色
t.color('green')
# 開始畫樹
def tree(d, s):
if d <= 0:
return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
# 調用畫樹函數(shù)
tree(15,100)
backward(50)
四:畫五角星
# 畫五角星
def xin():
t.right(90)
# 設置畫筆尺寸
t.pensize(3)
# t.circle(40)
# 抬筆
t.penup()
# 將畫筆移動到該位置
t.goto(-25,190)
# 落筆
pendown()
# 開始填充
t.begin_fill()
# 設置畫筆顏色
t.color('yellow')
# 畫五角星,一共需要五筆
for i in range(5):
t.forward(55)
t.right(144)
# 結束填充
t.end_fill()
# 調用畫五角星函數(shù)
xin()
五:畫天上的星星
# 滿天繁星
# 用for循環(huán)畫17顆星星
for i in range(17):
# 設置隨機坐標
x=random.randint(-400,400)
y=random.randint(240,330)
# 設置星星隨機大小
a=random.randint(5,12)
# 抬筆
t.penup()
# 將畫筆移動到隨機位置
t.goto(x,y)
# 落筆
t.pendown()
t.pensize(5)
t.color('yellow')
t.begin_fill()
for i in range(4):
t.forward(a)
t.left(30)
t.forward(a)
t.right(120)
# 每畫完一顆星星將畫筆方向向左改變30度,使星星看起來更加生動
t.left(30)
t.end_fill()
六:畫彩色氣球
#彩色氣球
# 利用循環(huán)畫20個氣球
for i in range(20):
# 設置隨機位置
X=random.randint(-400,400)
Y=random.randint(-170,150)
# 抬筆
t.penup()
# 將畫筆位置移動到初始位置
t.home()
# 將畫筆移動到隨機位置
t.goto(X, Y)
# 設置RGB顏色范圍
red=random.randint(100,255)
green=random.randint(50,155)
blue=random.randint(100,255)
# 畫筆尺寸
t.pensize(2)
# 落筆
t.pendown()
# 設置氣球線顏色
t.color('white')
t.left(90)
t.circle(80,15)
t.circle(-80,15)
t.right(90)
t.pensize(5)
# 利用RGB顏色設置氣球顏色
t.color(red,green,blue)
t.begin_fill()
t.circle(15)
t.end_fill()
七:畫彩燈
# 彩燈
def light():
# 隱藏筆頭,ht=hideturtle
t.hideturtle()
# 利用for循環(huán)畫100個彩燈
for i in range(100):
t.penup()
# 設置彩燈隨機位置
x=random.randint(-300,300)
y=random.randint(-350,-300)
# 設置RGB顏色
red=random.randint(100,255)
green=random.randint(50,155)
blue=random.randint(100,255)
# 將畫筆移動到隨機位置
t.goto(x,y)
t.pendown()
t.pensize(5)
t.color(red,green,blue)
t.begin_fill()
t.circle(10)
t.end_fill()
# 調用彩燈函數(shù)
light()
八:畫滿天飛雪
# 雪花
def drawsnow():
# 隱藏筆頭,ht=hideturtle
t.hideturtle()
t.pensize(2)
for i in range(200):
t.pencolor("white")
t.penup()
# 設置雪花隨機位置坐標
x=random.randint(-400,400)
y=random.randint(-250,300)
# 將畫筆移動到隨機位置
t.goto(x,y)
t.pendown()
# 雪花花瓣數(shù)
petal = 6
# 設置雪花隨機大小
snowsize = random.randint(1, 10)
# print(type(snowsize))
for j in range(petal):
t.forward(snowsize)
t.backward(snowsize)
# 轉動角度
t.right(int(360 / petal))
# 調用雪花函數(shù)
drawsnow()
九:畫祝福話語
# 添加文字
t.penup()
t.goto(-300,-250)
t.color('red')
t.pendown()
t.write('祝 劉曉云 Merry Christmas!',font=('Mistral',42,'bold italic'))
十:防止繪圖完成后窗口秒關
# 防止繪圖完成后窗口秒關
t.done()
最后完整代碼雙手奉上!
import turtle as t
# 隨機庫
import random
# 引入turtle庫中的所有用法
from turtle import *
# 跳過繪圖過程
t.tracer(False)
# 設置窗口大小和背景顏色
t.screensize(800,700,'black')
# RGB默認范圍是0~1,通過Screen().colormode設置成0~255
Screen().colormode(255)
# 加快作圖數(shù)度
t.speed(speed='fastest')
# 畫樹
t.left(90)
t.forward(-300)
t.pensize(5)
# 設置畫筆顏色
t.color('green')
# 開始畫樹
def tree(d, s):
if d <= 0:
return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
# 調用畫樹函數(shù)
tree(15,100)
backward(50)
# 畫五角星
def xin():
t.right(90)
# 設置畫筆尺寸
t.pensize(3)
# t.circle(40)
# 抬筆
t.penup()
# 將畫筆移動到該位置
t.goto(-25,190)
# 落筆
pendown()
# 開始填充
t.begin_fill()
# 設置畫筆顏色
t.color('yellow')
# 畫五角星,一共需要五筆
for i in range(5):
t.forward(55)
t.right(144)
# 結束填充
t.end_fill()
# 調用畫五角星函數(shù)
xin()
# 滿天繁星
# 用for循環(huán)畫17顆星星
for i in range(17):
# 設置隨機坐標
x=random.randint(-400,400)
y=random.randint(240,330)
# 設置星星隨機大小
a=random.randint(5,12)
# 抬筆
t.penup()
# 將畫筆移動到隨機位置
t.goto(x,y)
# 落筆
t.pendown()
t.pensize(5)
t.color('yellow')
t.begin_fill()
for i in range(4):
t.forward(a)
t.left(30)
t.forward(a)
t.right(120)
# 每畫完一顆星星將畫筆方向向左改變30度,使星星看起來更加生動
t.left(30)
t.end_fill()
#彩色氣球
# 利用循環(huán)畫20個氣球
for i in range(20):
# 設置隨機位置
X=random.randint(-400,400)
Y=random.randint(-170,150)
# 抬筆
t.penup()
# 將畫筆位置移動到初始位置
t.home()
# 將畫筆移動到隨機位置
t.goto(X, Y)
# 設置RGB顏色范圍
red=random.randint(100,255)
green=random.randint(50,155)
blue=random.randint(100,255)
# 畫筆尺寸
t.pensize(2)
# 落筆
t.pendown()
# 設置氣球線顏色
t.color('white')
t.left(90)
t.circle(80,15)
t.circle(-80,15)
t.right(90)
t.pensize(5)
# 利用RGB顏色設置氣球顏色
t.color(red,green,blue)
t.begin_fill()
t.circle(15)
t.end_fill()
# 彩燈
def light():
# 隱藏筆頭,ht=hideturtle
t.hideturtle()
# 利用for循環(huán)畫100個彩燈
for i in range(100):
t.penup()
# 設置彩燈隨機位置
x=random.randint(-300,300)
y=random.randint(-350,-300)
# 設置RGB顏色
red=random.randint(100,255)
green=random.randint(50,155)
blue=random.randint(100,255)
# 將畫筆移動到隨機位置
t.goto(x,y)
t.pendown()
t.pensize(5)
t.color(red,green,blue)
t.begin_fill()
t.circle(10)
t.end_fill()
# 調用彩燈函數(shù)
light()
# 雪花
def drawsnow():
# 隱藏筆頭,ht=hideturtle
t.hideturtle()
t.pensize(2)
for i in range(200):
t.pencolor("white")
t.penup()
# 設置雪花隨機位置坐標
x=random.randint(-400,400)
y=random.randint(-250,300)
# 將畫筆移動到隨機位置
t.goto(x,y)
t.pendown()
# 雪花花瓣數(shù)
petal = 6
# 設置雪花隨機大小
snowsize = random.randint(1, 10)
# print(type(snowsize))
for j in range(petal):
t.forward(snowsize)
t.backward(snowsize)
# 轉動角度
t.right(int(360 / petal))
# 調用雪花函數(shù)
drawsnow()
# 添加文字
t.penup()
t.goto(-300,-250)
t.color('red')
t.pendown()
t.write('祝 劉曉云 Merry Christmas!',font=('Mistral',42,'bold italic'))
# 防止繪圖完成后窗口秒關
t.done()
效果如下
文章來源:http://www.zghlxwxcb.cn/news/detail-779424.html
?最近今天持續(xù)發(fā)燒,沒來得急上傳,趕一波末班車嘻嘻文章來源地址http://www.zghlxwxcb.cn/news/detail-779424.html
到了這里,關于python-turtle(海龜繪圖)圣誕樹的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!