系列文章?
序號(hào) | 文章目錄 | 直達(dá)鏈接 |
1 | 浪漫520表白代碼 | https://want595.blog.csdn.net/article/details/130666881 |
2 | 滿屏表白代碼 | https://want595.blog.csdn.net/article/details/129794518 |
3 | 跳動(dòng)的愛心 | https://want595.blog.csdn.net/article/details/129503123 |
4 | 漂浮愛心 | https://want595.blog.csdn.net/article/details/128808630 |
5 | 愛心光波 | https://want595.blog.csdn.net/article/details/132311588 |
6 | 流星雨 | https://want595.blog.csdn.net/article/details/129395465 |
7 | 滿天星 | https://want595.blog.csdn.net/article/details/129572082 |
8 | 煙花秀 | https://want595.blog.csdn.net/article/details/128746664 |
9 | 圣誕樹 | https://want595.blog.csdn.net/article/details/128213770 |
10 | 雪花代碼 | https://want595.blog.csdn.net/article/details/129038108 |
11 | 模擬星空 | https://want595.blog.csdn.net/article/details/129948882 |
12 | 生日蛋糕 | https://want595.blog.csdn.net/article/details/129694998 |
13 | 櫻花樹 | https://want595.blog.csdn.net/article/details/130350743 |
14 | 五彩氣球 | https://want595.blog.csdn.net/article/details/130950744 |
15 | 七彩花朵 | https://want595.blog.csdn.net/article/details/130897838 |
16 | 惡搞代碼 | https://want595.blog.csdn.net/article/details/131274862 |
17 | 代碼雨 | https://want595.blog.csdn.net/article/details/132574687 |
18 | 中秋星空 | https://want595.blog.csdn.net/article/details/132910075 |
19 | 國慶祝福 | https://want595.blog.csdn.net/article/details/133427031 |
20 | 皮卡丘 | https://want595.blog.csdn.net/article/details/133783136 |
21 | 玫瑰花 | https://want595.blog.csdn.net/article/details/133851128 |
22 | 名偵探柯南 | https://want595.blog.csdn.net/article/details/133903847 |
23 | 蝙蝠 | https://want595.blog.csdn.net/article/details/133935474 |
24 | 南瓜頭 | https://want595.blog.csdn.net/article/details/133973340 |
25 | 萬圣節(jié)禮物 | https://want595.blog.csdn.net/article/details/134011397 |
前言
中秋佳節(jié)即將來臨,博主在此獻(xiàn)上一個(gè)美麗的夜空,祝大家中秋快樂吖!?
Python繪圖基礎(chǔ)
Turtle入門
Python的turtle是一個(gè)基于tkinter的Python圖形庫,可以幫助初學(xué)者輕松地理解和繪制圖形。它模仿了一個(gè)烏龜,與繪圖窗口交互,畫筆隨烏龜移動(dòng)而移動(dòng),完成特定的繪圖任務(wù)。
turtle庫是Python標(biāo)準(zhǔn)庫中的一部分,所以在使用前不需要進(jìn)行任何安裝和下載。通過import turtle語句引入turtle庫,就可以使用它提供的一些函數(shù)和方法了。
turtle庫中最基本的函數(shù)包括:
- turtle.forward(distance):向當(dāng)前方向移動(dòng)distance個(gè)像素的距離
- turtle.backward(distance):向相反方向移動(dòng)distance個(gè)像素的距離
- turtle.right(angle):向右旋轉(zhuǎn)angle度
- turtle.left(angle):向左旋轉(zhuǎn)angle度
- turtle.penup():抬起畫筆
- turtle.pendown():放下畫筆
- turtle.color(color):設(shè)置畫筆顏色
- turtle.pensize(size):設(shè)置畫筆大小
- turtle.speed(speed):設(shè)置畫筆速度
使用turtle庫繪制圖形時(shí),需要注意的是:
- 繪圖的起點(diǎn)在屏幕中心點(diǎn),即坐標(biāo)(0,0)處;
- turtle.forward()和turtle.backward()函數(shù)移動(dòng)的距離單位是像素;
- turtle.right()和turtle.left()函數(shù)旋轉(zhuǎn)的角度單位是度數(shù);
- turtle.color()函數(shù)可以接收顏色名稱或RGB值作為參數(shù),例如“red”、“#FF0000”;
- turtle.speed()函數(shù)可以設(shè)置畫筆速度,速度越快越接近實(shí)時(shí);
- 當(dāng)需要繪制完多個(gè)圖形后,可以使用turtle.done()函數(shù)停止turtle工作。
總之,Python的turtle庫是一個(gè)非常好的圖形庫,可以幫助初學(xué)者理解和掌握?qǐng)D形繪制的基礎(chǔ)知識(shí)。從簡單的繪制幾何圖形開始,逐漸掌握函數(shù)使用方法,最終能夠繪制出復(fù)雜的圖形。由于turtle庫的易學(xué)、易用,所以它也成為了Python繪圖庫中的一個(gè)入門品種。
Turtle繪圖
以下是turtle庫中的一些簡單例子,您可以通過運(yùn)行這些代碼來了解turtle庫的使用方法:
1. 繪制一個(gè)正方形
import turtle
for i in range(4):
? ? turtle.forward(100)
? ? turtle.right(90)
turtle.done()
2. 繪制一個(gè)五角星
import turtle
turtle.left(72)
for i in range(5):
? ? turtle.forward(100)
? ? turtle.right(144)
? ? turtle.forward(100)
? ? turtle.left(72)
turtle.done()
3. 繪制一個(gè)彩虹
import turtle
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
turtle.pensize(10)
for i in range(6):
? ? turtle.pencolor(colors[i])
? ? turtle.circle(50)
? ? turtle.right(60)
turtle.done()
4. 繪制一個(gè)螺旋線
import turtle
turtle.pensize(2)
for i in range(100):
? ? turtle.forward(i*2)
? ? turtle.right(90)
turtle.done()
這些示例只是turtle庫中的一小部分,通過學(xué)習(xí)這些例子,您可以開始熟悉turtle庫的使用方法,并且可以通過修改代碼來創(chuàng)造自己的圖形。
Python浪漫星空
程序設(shè)計(jì)
import math
import turtle as tu
import random as ra
tu.setup(1.0, 1.0)
tu.screensize(1.0, 1.0) # 設(shè)置畫布大小
tu.bgcolor('black') # 設(shè)置畫布顏色
t = tu.Pen()
t.ht() # 隱藏畫筆
colors2 = ['yellow', 'gold', 'orange'] # 星星的顏色列表
colors3 = ['skyblue', 'white', 'cyan', 'aqua'] # 流星的顏色列表
class Star(): # 流星類
def __init__(self):
self.x1 = -850
self.y1 = 300
self.x2 = ra.randint(-1500, 1000) # 星星的橫坐標(biāo)
self.y2 = ra.randint(-500, 500) # 星星的縱坐標(biāo)
self.r2 = ra.randint(1, 5)
self.x3 = ra.randint(-1500, 1000) # 流星的橫坐標(biāo)
self.y3 = ra.randint(-500, 500) # 流星的縱坐標(biāo)
self.r3 = ra.randint(50, 100) # 流星的半徑
self.t = ra.randint(1, 3)
self.speed2 = ra.randint(1, 3) # 星星流星移動(dòng)速度
self.speed3 = ra.randint(10, 15) # 流星的移動(dòng)速度
self.color2 = ra.choice(colors2) # 星星的顏色
self.color3 = ra.choice(colors3) # 流星的顏色
def moon(self): # 畫月亮
t.penup()
t.goto(self.x1, self.y1)
t.pendown()
t.pencolor("yellow")
t.begin_fill()
t.fillcolor("gold")
t.circle(66)
t.end_fill()
def star1(self): # 畫星星的函數(shù)
t.pensize(1) # 設(shè)置畫筆大小
t.penup() # 提筆
t.goto(self.x2, self.y2) # 設(shè)置星星在畫布中的初始坐標(biāo)
t.pendown() # 落筆
t.speed(0) # 畫星星的速度,范圍為0~10(0最快)
t.color(self.color2) # 設(shè)置星星的外框顏色
t.begin_fill() # 開始填色
t.fillcolor(self.color2) # 星星的內(nèi)部顏色
for i in range(5): # 循環(huán)畫星星
t.forward(self.r2)
t.right(144)
t.forward(self.r2)
t.left(72)
t.end_fill() # 結(jié)束填充顏色
def star2(self): # 畫流星函數(shù)
t.pensize(1) # 流星的大小
t.penup() # 提筆
t.goto(self.x3, self.y3) # 隨機(jī)位置
t.pendown() # 落筆
t.color(self.color3)
t.begin_fill()
t.fillcolor(self.color3)
t.setheading(-30)
t.right(self.t)
t.forward(self.r3)
t.left(self.t)
t.circle(self.r3 * math.sin(math.radians(self.t)), 180)
t.left(self.t)
t.forward(self.r3)
t.end_fill()
def move(self): # 移動(dòng)函數(shù)
if self.x1 <= 850: # 當(dāng)月亮還在畫布中時(shí)
self.x1 += 1 # 設(shè)置左右移動(dòng)速度
else:
self.x1 = -850
if self.x2 <= 1000: # 當(dāng)星星還在畫布中時(shí)
self.x2 += 2 * self.speed2 # 設(shè)置左右移動(dòng)速度
else:
self.r2 = ra.randint(1, 5)
self.x2 = ra.randint(-1500, -1000)
self.speed2 = ra.randint(1, 3)
self.color2 = ra.choice(colors2)
if self.y3 >= -500: # 當(dāng)流星還在畫布中時(shí)
self.y3 -= self.speed3 # 設(shè)置上下移動(dòng)速度
self.x3 += 2 * self.speed3 # 設(shè)置左右移動(dòng)速度
else:
self.r3 = ra.randint(50, 100)
self.t = ra.randint(1, 3)
self.x3 = ra.randint(-1500, -750)
self.y3 = ra.randint(-500,1000)
self.speed3 = ra.randint(10, 15)
self.color3 = ra.choice(colors3)
……完整代碼見文末公眾號(hào)喔
程序分析
此代碼實(shí)現(xiàn)了一個(gè)星空夜景的動(dòng)畫效果,包括月亮、星星和流星。整個(gè)代碼分為四個(gè)部分:初始化,定義類Star,創(chuàng)建星星和流星對(duì)象,繪制動(dòng)畫。
首先,在初始化部分,引入了必要的庫和模塊,設(shè)置了畫布大小和背景顏色,并創(chuàng)建了畫筆對(duì)象t。接著定義了兩個(gè)顏色列表,colors2存儲(chǔ)星星顏色,colors3存儲(chǔ)流星顏色。
在定義Star類部分,首先初始化了各個(gè)變量,包括星星和流星的坐標(biāo)、半徑和顏色等,其中坐標(biāo)、半徑、速度和顏色都是隨機(jī)生成的。然后定義了三個(gè)函數(shù),分別用于繪制月亮、星星和流星。繪制月亮使用了begin_fill()和end_fill()來填充黃色和金色,繪制星星使用了for循環(huán)來繪制五角星,繪制流星使用了circle()和setheading()方法來繪制弧線和轉(zhuǎn)向。最后定義了move()函數(shù)來控制各個(gè)星星和流星的移動(dòng),當(dāng)它們移動(dòng)出畫布范圍后,重新賦值坐標(biāo)和速度等參數(shù),使得它們能夠持續(xù)移動(dòng)。
在創(chuàng)建Star對(duì)象部分,首先創(chuàng)建了一個(gè)空的列表Stars,然后使用for循環(huán)創(chuàng)建100個(gè)Star對(duì)象,并將它們依次添加到列表Stars中。
最后,在繪制動(dòng)畫部分,使用while循環(huán)來不斷更新畫布,并在其中使用tu.tracer(0)來關(guān)閉圖形繪制時(shí)的延遲效果,從而使得繪制流暢。在每次循環(huán)時(shí),首先清空畫布,然后繪制月亮和所有的星星,最后繪制所有的流星,最后使用tu.update()方法將畫布更新。
總的來說,此代碼實(shí)現(xiàn)了一個(gè)星空夜景的動(dòng)畫效果,通過隨機(jī)生成參數(shù)來使得星星和流星的位置、速度和顏色等變得多樣化,使得動(dòng)畫看起來更加真實(shí)和生動(dòng)。同時(shí),通過類的封裝和函數(shù)的定義,使得代碼結(jié)構(gòu)更加模塊化和易于維護(hù)。文章來源:http://www.zghlxwxcb.cn/news/detail-713132.html
尾聲
祝大家中秋節(jié)快樂!文章來源地址http://www.zghlxwxcb.cn/news/detail-713132.html
到了這里,關(guān)于Python浪漫星空的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!