Python绘画:蛋糕


🌵🌲🌳🌴🌿🍀☘️🌱🍃🎋🌾🎍🌺🌸🌼🍁🥀💐🌷🌹🌻🏵️🐾🐿️🐇🦚🦜🦢🕊️🦃🐓🐈🐩🐎🐖🐏🐑🦙🐐🦌🐕🐄🐃🐂🐃🦘🦒🐫🐫🐪🦏🦏🦛🐘🦍🦓🐆🐅🐊🦈🦞🦀🐡🐠🐟🐬🐳🐋🦐🦑🐙🦕🦖🦎🐍🐉🐞🐜🦟🦗🕷️🕸️🦂🐢🐌🦋🐛🐝🦄🐴🐗🐺🐦🐤🐣🐥🦆🦅🦉🦇🐧🐔😾😿🙀😽😼😻🐵🙊🙉🙈🐒😺😸😹🐸🐽🐷🐮🦁🐯🐨🐼🐶🐱🐱🐭🐹🐰🐻🐻🦊🐰⛄☃️🛶🐲🎉🎊✨🎈🎁🎃🎄🍀🍀🍀🍀

🐸 前言 🐸

欢迎来到 《Python绘画:蛋糕》!在这个博客里,我们将一起用 Python 编程语言,绘制出一款美味的蛋糕。蛋糕不仅是许多节日和庆典中不可或缺的美食,它还代表着甜美与祝福。今天,我们将通过代码,创造出一款充满甜美气息的蛋糕图案,带给你编程与艺术相结合的乐趣。
在本教程中,我们将从简单的几何图形开始,逐步构建蛋糕的底部、蛋糕层和顶部装饰。通过巧妙地使用 turtle 库的绘图功能,我们将能够绘制出蛋糕的轮廓、涂上色彩,并为它添加一些可爱的装饰,比如樱桃、巧克力和奶油,最终呈现出一款完美的蛋糕。

🐋 效果图 🐋

在这里插入图片描述

🐉 代码 🐉

# Layer Cake Challenge - www.101computing.net/layer-cake/
from turtle import *
from shapes import *
import shapes
import time

myPen = Turtle()
myPen.shape("turtle")
myPen.speed(10)
myPen.hideturtle()
window = turtle.Screen()
window.bgcolor("#69D9FF")
y = -140

# Inititalise the dictionary
ingredients = {}
# Add items to the dictionary
ingredients["strawberry"] = "pink"
ingredients["milk chocolate"] = "#BF671F"
ingredients["matcha"] = "#93c572"
ingredients["icing sugar"] = "#FFFFFF"

### Now let's preview the layer cake

# let's draw the plate
draw_rectangle(turtle, "white", -150, y - 10, +300, 10)

# Iterate through each layer of the list
draw_rectangle(myPen, ingredients["milk chocolate"], -120, y, 240, 30)
y += 30
draw_rectangle(myPen, ingredients["strawberry"], -120, y, 240, 35)
y += 35
addIcing(myPen, ingredients["icing sugar"], 120, y)
y += 10
draw_rectangle(myPen, ingredients["milk chocolate"], -100, y, 200, 20)
y += 20
draw_rectangle(myPen, ingredients["strawberry"], -100, y, 200, 40)
y += 40
addIcing(myPen, ingredients["icing sugar"], 100, y)
y += 10
draw_rectangle(myPen, ingredients["milk chocolate"], -70, y, 140, 24)
y += 24
draw_rectangle(myPen, ingredients["strawberry"], -70, y, 140, 36)
y += 36
addIcing(myPen, ingredients["icing sugar"], 70, y)
y += 10
draw_rectangle(myPen, ingredients["matcha"], -4, y, 8, 60)
y += 65
draw_star(myPen, "white", 2, y, 10)

time.sleep(30)

shapes.py

# This is a custom module we've made.
# Modules are files full of code that you can import into your programs.
# This one teaches our turtle to draw various shapes.

import turtle
import math


def draw_circle(turtle, color, x, y, radius):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius)
    turtle.end_fill()


def draw_rectangle(turtle, color, x, y, width, height):
    turtle.hideturtle()
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)

    turtle.end_fill()
    turtle.setheading(0)


def draw_star(turtle, color, x, y, size):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.right(144)
    for i in range(5):
        turtle.forward(size)
        turtle.right(144)
        turtle.forward(size)
    turtle.end_fill()
    turtle.setheading(0)


def addIcing(turtle, color, x, y):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(-x - 2, y + 10)
    turtle.pendown()
    turtle.begin_fill()

    for z in range(-x - 3, x + 3):
        turtle.goto(z, y - 10 - 10 * math.cos((z / 100) * 2 * math.pi))

    turtle.goto(x + 3, y + 10)
    turtle.goto(-x - 3, y + 10)
    turtle.end_fill()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栗子风暴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值