python编程代码画哆啦a梦

大家好,本文将围绕哆啦a梦pythonturtle库画法展开说明,用python画哆啦a梦的代码是一个很多人都想弄明白的事情,想搞清楚编程哆啦a梦图片程序需要先了解以下几个事情。

大家好,小编来为大家解答以下问题,python画图代码大全画雪人,用python画雪人编程代码,现在让我们一起来看看吧!

一、前言

🎄为了庆祝我们副山羊勒布朗詹姆斯,连续刷分6场30+,特此画一幅圣诞雪人,也祝福湖人12月26日圣诞大战,再我詹的带领下,刷分60+,圣诞之夜历史第一人。

二、创意名

Lebron James It’s for you!

三、效果展示

四、实现步骤

创建画布,设置背景,图片,设置雪球参数,风参数创建画布中所有圆形对象创建雪球创建雪球移动函数创建雪人创建地面

五、编码实现
创建画布,设置背景,图片,设置雪球参数,风参数
import turtle
import random
import time

width = height = 500

window = turtle.Screen()
window.setup(width, height)
window.bgcolor("sky blue")
window.bgpic('')
window.title("Happy Holidays")

snowball_rate = 1, 3
snowball_size = 5, 15
wind_change = 1, 5
max_wind = 3

turtle.bgpic函数只接受gif格式的图片

创建画布中所有圆形对象
def make_circle(turtle_name, x, y, size, colour):
    turtle_name.color(colour)
    turtle_name.penup()
    turtle_name.setposition(x, y)
    (size)
创建雪球
list_of_snowballs = []
def make_snowball():
    snowball = turtle.Turtle()
    snowball.color("white")
    snowball.penup()
    snowball.setposition(random.randint(-2*width, width/2), height/2)
    snowball.hideturtle()
     = random.randint(*snowball_size)
    list_of_snowballs.append(snowball)
创建雪球移动函数
def move_snowball(turtle_name, falling_speed=1, wind=0):
    turtle_name.clear()
    (() - falling_speed)
    if wind:
        (() + wind)
    ()
创建雪人
# 雪人:身体
snowman = turtle.Turtle()
x_position = 100
y_positions = 75, 0, -100
size = 75
for y in y_positions:
    make_circle(snowman, x_position, y, size, "white")
    size = size *1.5

# 雪人:按钮
button_seperation = 25
button_y_positions = [y_positions[1]-button_seperation,
                      y_positions[1],
                      y_positions[1]+button_seperation]
for y in button_y_positions:
    make_circle(snowman, x_position, y, 10, "black")

# 雪人:眼睛
y_offset = 10
x_seperation = 15
for x in x_position-x_seperation, x_position+x_seperation:
    make_circle(snowman, x, y_positions[0] + y_offset, 20, "green")
    make_circle(snowman, x, y_positions[0] + y_offset, 10, "black")

# 雪人:鼻子
snowman.color("orange")
snowman.setposition(x_position - 10, y_positions[0]-y_offset)
snowman.shape("triangle")
snowman.setheading(200)
snowman.turtlesize(0.5, 2.5)

window.tracer(0)
创建地面
grass = turtle.Turtle()
grass.fillcolor("forest green")
grass.penup()
grass.setposition(-width/2, -height/2)
grass.begin_fill()
for _ in range(2):
    grass.forward(width)
    (90)
    grass.forward(70)
    (90)
grass.end_fill()

ground = turtle.Turtle()
for x in range(int(-width/2), int(width/2), int(width/200)):
    make_circle(ground, x, -180, random.randint(5, 20), "white")
给画布添加文字
text = turtle.Turtle()
text.color("red")
text.penup()
text.setposition(-100,170)
text.write("Happy Holidays", font=("Apple Chancery", 30, "bold"), align="center")
text.setposition(130, 140)
text.color("dark green")
text.write("Lebron", font=("Avenir", 30, "normal"), align="right")
text.color("black")
text.write("James", font=("Avenir", 30, "normal"), align="left")
(50)
text.write("It's for you!", font=("Apple Chancery", 20, "bold"), align="right")
text.hideturtle()
定义画布的时间函数
time_delay = 0
start_time = ()
wind = 0
wind_delay = 5
wind_timer = ()
wind_step = 0.1
while True:
    if () - start_time > time_delay:
        make_snowball()
        start_time = ()
        time_delay = random.randint(*snowball_rate)/10

    for snowball in list_of_snowballs:
        move_snowball(snowball, wind=wind)
        if () < -height/2:
            snowball.clear()
            list_of_snowballs.remove(snowball)

    if () - wind_timer > wind_delay:
        wind += wind_step
        if wind >= max_wind:
            wind_step = -wind_step
        elif wind <= 0:
            wind_step = abs(wind_step)

        wind_timer = ()
        wind_delay = random.randint(*wind_change)/10


    window.update()

()


原文地址1:https://blog.csdn.net/2301_81900439/article/details/135581602
python手册 http://www.78tp.com/python/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值