Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw a lovely birthday cake with Python+Turtle

Shulou Source: shulou.com Published: 2022-05-31 17:18:50 09月21日 Update

In this article, the editor introduces in detail "how to use Python+Turtle to draw a lovely birthday cake". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Python+Turtle to draw a lovely birthday cake" can help you solve your doubts.

The Python libraries we use in this article include: turtle, math, and random.

The main code of the implementation is as follows:

Import math as mimport random as rimport turtle as tt.speed (0) t.delay (0) # set background color and window t.bgcolor ("# FFFFFF") t.setup (800,600) t.penup () t.goto (150,0) t.pendown () t.pencolor ("white") t.begin_fill () for i in range (x = drawX (150,I) y = drawY (60,I) t.goto (x) Y) t.fillcolor ("# fef5f7") t.end_fill () t.begin_fill () for i in range (180): X = drawX (150,-I) y = drawY (70,-I) t.goto (x, y) for i in range (180,360): X = drawX (150,I) y = drawY (60, I) t.goto (x Y) t.fillcolor ("# f2d7dd") t.end_fill () t.pu () t.goto (120,0) t.pd () t.begin_fill () for i in range: X = drawX (120,I) y = drawY (48,I) t.goto (x, y) t.fillcolor ("# 33CCFF") t.end_fill () t.begin_fill () t.pencolor ("# fee48c") for i in range I) y = drawY (48, I) + 70 t.goto (x, y) t.goto (- 120,0) t.fillcolor ("# 99FFFF") t.end_fill () t.pu () t.goto (120,70) t.pd () t.pencolor ("# fff0f3") t.begin_fill () for i in range (x = drawX (120,I) y = drawY (48, I) + 70 t.goto (x) Y) t.fillcolor ("# fff0f3") t.end_fill () t.pu () t.goto (110,70) t.pd () t.pencolor ("# fff9fb") t.begin_fill () for i in range: X = drawX (110,I) y = drawY (44, I) + 70 t.goto (x, y) t.fillcolor ("# FFCCCC") t.end_fill () t.goto 0) t.pd () t.begin_fill () t.pencolor ("# ffa79d") for i in range (180): X = drawX (120,I) y = drawY (48,- I) + 10 t.goto (x, y) t.goto (- 120,0) for i in range (180,360): X = drawX (120,I) y = drawY (48,I) t.goto (x) Y) t.fillcolor ("# ffa79d") t.end_fill () for i in range (50): t.pu () x = r.randint (- 500,500) y = r.randint (120,300) t.goto (x, y) t.pd () t.dot (r.randint (3,5), color [r.randint (0,7)]) t.penup () t.goto T.pencolor ("# FF0000") t.write ("Happy Birthday", font= ("Curlz MT", 30)) t.hideturtle () t.done ()

Achieve results:

Read here, this "how to use Python+Turtle to draw a lovely birthday cake" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

Tags: Lovely birthday cake cake articles content proper code ideas effects new knowledge more steps knowledge knowledge points articles details background industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno MySQL Huawei Shulou Information