How to draw the beauty of the moon with Python code?

I wrote a piece of code about the Mid-Autumn Festival in my spare time after get off work, which can automatically draw mountains and rivers, transport poison and the moon, and write beautiful poems, so that you can be deeply intoxicated in the festive atmosphere of the Mid-Autumn Festival. This article mainly uses the turtle library, time library and pygame library in Python, which can better achieve dynamic effects. 0b822a2c79a0472690b5fc08527d69e4.pngWe can use the turtle library to draw the moon, clouds, mountains and ancient poems, and the game module uses the pygame library with beautiful background music.

1. Import the required libraries (turtle library, time library, pygame library)

1 '''2 Function: 中秋赏月3 Author: 向阳逐梦4 '''5 import turtle6 import time7 import pygame
Use import to import the function library to be used to realize the required function. The function realized in this article is to draw patterns of the moon, clouds and mountains and display ancient poems.                                       
2. Drawing the Moon and Clouds
1def drawMoon():    #绘制月亮 2 turtle.penup()   #画笔拿起  3turtle.goto(-150, 0)  4turtle.fillcolor((255, 215, 0))   #圆月的颜色 5 turtle.pendown()   #画笔放下  6turtle.begin_fill() 7 turtle.circle(112) 8 turtle.end_fill()  #turtle.begin_fill()到turtle.end_fill() 颜色填充9def drawCloud(): #绘制云朵   10turtle.penup()   11turtle.goto(-500, 200)  12 turtle.fillcolor((245, 245, 245))   13turtle.pencolor((255, 255, 255))   14turtle.pensize(5)   15turtle.pendown()  16 turtle.forward(250)  17 def cloud(mode='right'):      18for i in range(90):         19turtle.pensize((i+1)*0.2+5)       20  turtle.right(1) if mode == 'right' else turtle.left(1)         21turtle.forward(0.5)     22for i in range(90):        23turtle.pensize(90*0.2+5-0.2*(i+1))        24turtle.right(1) if mode == 'right' else turtle.left(1)         25turtle.forward(0.5)  26 cloud()   27turtle.forward(100)   28cloud('left')   29  turtle.forward(600)3. Drawing mountains and rivers​​​​
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()4. Add suitable background music​​​​​​​​
def initTurtle():   pygame.mixer.init()   pygame.mixer.music.load('zqbg.mp3')   pygame.mixer.music.play(-1, 20.0)   turtle.hideturtle()   turtle.setup(1000, 600)   turtle.title('中秋赏月')   turtle.colormode(255)   turtle.bgcolor((193, 210, 240))   turtle.speed(10)

 

​​​​​​

5. Add appropriate ancient verses

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圆\n缺","\n但\n愿\n人\n长\n久\n", "千\n里\n共\n婵\n娟\n"]  #诗句的位置(可自行设计添加), 最好2/4句五言诗  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])

6. Call the main function​​​​​​​​

def main():    initTurtle()    drawMoon()          #绘制月亮    drawCloud()         #绘制云朵    drawMountain()      #绘制山    writePoetry()       #写诗    turtle.done()
if __name__ == '__main__':    main()

7. Background music download address

Link: pan.baidu.com/s/1VI5WIDeO... Extraction code: 4ry4

  • The software used to write the code is pycharm, and the environment configuration of the software is required before using the software. You also need to download the required library functions to implement the required functions.

8. Compilation software screenshot

58ea3818edc64565aec7a6895d564451.png

 9. Code running screenshot

4e2a6ebc7a00431088505e9bada6bcb2.png

 10. Import pygame library in pycharm

(1) Open pycharm and find the file;

381a7bdc07ec4ad0921c3fb7a090652c.png

 (2) Find the setting option;

85b49f5230de4fc791f2687829de6600.png

 (3) After opening the setting, search for project in the search bar of the window, find your corresponding project, and click the python interpreter below; 

d8985d56358d40f8b222bd1153d91312.png

 (4) After clicking python Interpreter, you can see the library imported by the project, find the + sign and click;

1d255515c3b444e889a5fc5f700e690e.png

 (5) After clicking +, you can see the installable library in the pop-up window, search for pygame, install the corresponding module, generally only install the first one, click install package to install, wait for a while, the installation is complete, in the project The pygame library can be used normally. If there is no pygame, it may be that your pycharm does not have this library and needs to be installed;

e3c8f473539548f7bfe7388953e1273a.png

 (6) Verify whether the import is successful, import pygame in the corresponding project, if there is no error, the import is successful;

8f3a320a1cf64e01ac42e49f0bb323af.png

6870817e418246f6bfd685095fcdda3e.png 

     Finally, your project can be compiled normally and run to see the actual effect.

 

 

 

              

 

Guess you like

Origin blog.csdn.net/m0_74773803/article/details/127560876