又是一年中秋,作为一位Python爱好者,又怎能不做点什么呢?使用turtle绘制月亮、云朵、山再加上诗,游戏模块pygame模块照旧,再配上美美的音乐,这个中秋很NICE,程序运行截图:程序运行所需的背景音乐请在文末下载,也可自行替换 完整程序代码:
import turtle
import time
import pygame
def drawMoon():
turtle.penup()
turtle.goto(-150, 0)
turtle.fillcolor((255, 215, 0))
turtle.pendown()
turtle.begin_fill()
turtle.circle(112)
turtle.end_fill()
def drawCloud():
turtle.penup()
turtle.goto(-500, 200)
turtle.fillcolor((245, 245, 245))
turtle.pencolor((255, 255, 255))
turtle.pensize(5)
turtle.pendown()
turtle.forward(250)
def cloud(mode='right'):
for i in range(90):
turtle.pensize((i+1)*0.2+5)
turtle.right(1) if mode == 'right' else turtle.left(1)
turtle.forward(0.5)
for i in range(90):
turtle.pensize(90*0.2+5-0.2*(i+1))
turtle.right(1) if mode == 'right' else turtle.left(1)
turtle.forward(0.5)
cloud()
turtle.forward(100)
cloud('left')
turtle.forward(600)
def drawMountain():
turtle.penup()
turtle.goto(-500, -250)
turtle.pensize(4)
turtle.fillcolor((36, 36, 36))
turtle.pencolor((31, 28, 24))
turtle.pendown()
turtle.begin_fill()
turtle.left(20)
turtle.forward(400)
turtle.right(45)
turtle.forward(200)
turtle.left(60)
turtle.forward(300)
turtle.right(70)
turtle.forward(300)
turtle.goto(500, -300)
turtle.goto(-500, -300)
turtle.end_fill()
def initTurtle():
pygame.mixer.init()
pygame.mixer.music.load('ZXbg.mp3')
pygame.mixer.music.play(-1, 20.0)
turtle.hideturtle()
turtle.setup(1000, 600)
turtle.title('中秋赏月-Python代码大全')
turtle.colormode(255)
turtle.bgcolor((193, 210, 240))
turtle.speed(10)
def writePoetry():
turtle.penup()
turtle.goto(400, -150)
turtle.pencolor((250, 240, 230))
potery = ["\n明\n月\n几\n时\n有\n", "把\n酒\n问\n青\n天\n"]
coordinates = [(300, -150), (200, -150), (100, -150)]
for i, p in enumerate(potery):
turtle.write(p, align="center", font=("STXingkai", 50, "bold"))
if (i + 1) != len(potery):
time.sleep(2)
turtle.goto(coordinates[i])
def main():
initTurtle()
drawMoon()
drawCloud()
drawMountain()
writePoetry()
turtle.done()
if __name__ == '__main__':
main()
程序运行所需的背景音乐下载地址:https://pan.baidu.com/s/1Xc_Zx7v8EUIHFwv1cAZyaw,网盘下载提取码请关注“Python代码大全”公众号后回复:中秋背景音乐,更多好玩python代码请微信关注:Python代码大全。
|