python画图代码大全画雪人,用python画雪人编程代码

本篇文章给大家谈谈python画图代码大全画雪人,以及用python画雪人编程代码,希望对各位有所帮助,不要忘了收藏本站喔。

本篇文章给大家谈谈python画图代码大全画雪人,以及用python画雪人的编程,希望对各位有所帮助,不要忘了收藏本站喔。

一、前言

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

二、创意名

Lebron James It’s for you!

三、效果展示

在这里插入图片描述

四、实现步骤
  1. 创建画布,设置背景,图片,设置雪球参数,风参数
  2. 创建画布中所有圆形对象
  3. 创建雪球
  4. 创建雪球移动函数
  5. 创建雪人
  6. 创建地面
五、编码实现
创建画布,设置背景,图片,设置雪球参数,风参数
import turtle
import random
import time

width = height = 500

window = turtle.Screen()
window.setup(width, height)
window.bgcolor("sky blue")
window.bgpic('jm.gif')
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)
    turtle_name.dot(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()
    snowball.size = random.randint(*snowball_size)
    list_of_snowballs.append(snowball)
创建雪球移动函数
def move_snowball(turtle_name, falling_speed=1, wind=0):
    turtle_name.clear()
    turtle_name.sety(turtle_name.ycor() - falling_speed)
    if wind:
        turtle_name.setx(turtle_name.xcor() + wind)
    turtle_name.dot(turtle_name.size)
创建雪人
# 雪人:身体
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)
    grass.left(90)
    grass.forward(70)
    grass.left(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")
text.setx(50)
text.write("It's for you!", font=("Apple Chancery", 20, "bold"), align="right")
text.hideturtle()
定义画布的时间函数
time_delay = 0
start_time = time.time()
wind = 0
wind_delay = 5
wind_timer = time.time()
wind_step = 0.1
while True:
    if time.time() - start_time > time_delay:
        make_snowball()
        start_time = time.time()
        time_delay = random.randint(*snowball_rate)/10

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

    if time.time() - 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 = time.time()
        wind_delay = random.randint(*wind_change)/10


    window.update()

turtle.done()
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值