python圣诞雪人

简介: python圣诞雪人

一、前言


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


二、创意名


Lebron James It’s for you!


三、效果展示

b669bddfd1c04cb5911dfa0d83fc4c9e.png

四、实现步骤


创建画布,设置背景,图片,设置雪球参数,风参数

创建画布中所有圆形对象

创建雪球

创建雪球移动函数

创建雪人

创建地面


五、编码实现


创建画布,设置背景,图片,设置雪球参数,风参数


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月前
|
Python
Python玫瑰花完整代码
Python玫瑰花完整代码
197 0
|
10月前
|
Python 容器
萌妹子Python入门指北(二)
有一天我写了一个很复杂的计算式子得出一个结果,然后我要拿这个结果去和别的数做计算,比如我 (((123*2421+111)/16)+15)*179 然后把得出的结果再来计算一次,难道我要把这个结果记下来然后再输入吗??我们最好有个容器把结果保存下来,接下来我们直接拿来用就可以了,那么在python这个容器叫什么?这里引出本文最后一个专业术语 变量
52 0
|
10月前
|
Python
萌妹子Python入门指北(三)
其中if和else之间可以加多个else if语句,如果代码执行到这一个判断,你们就执行本行后面有相同缩进的代码,负责就跳到下一个else if,或者else。
31 0
|
10月前
|
运维 Linux C++
|
10月前
|
程序员 Python
萌妹子Python入门指北(四)
1.if可以嵌套for吗?可以嵌套while循环吗? 2.难道只能嵌套两层吗?3层 4层 5层可不可以? 一般的文章在这个时候会告诉大家『这里我就不公布答案了,大家可以去尝试下』,然而我这里我偏要告诉大家答案很明显都是yes yes,你试不试就和我无关了。
35 0
|
10月前
|
程序员 Python
萌妹子Python入门指北(五)
def isprime(n): //isprime是函数名,n是需要判断的数 for i in range(2, n): if n%i == 0: //这里如果被i整除了看到不是素数了,素数的定义就是因子只有1和它本身 return False return True
35 0
|
4月前
|
前端开发 Python
Python玫瑰花
Python玫瑰花
92 0
|
Python
python制作代码舞视频来啦~
python制作代码舞视频来啦~
161 0
python制作代码舞视频来啦~
|
数据可视化 Python
中秋节——Python恶作剧
中秋节——Python恶作剧
93 0
中秋节——Python恶作剧
|
存储 索引 Python
Python八个自动化办公的技巧续集
5、Excel文件批量合并 5.1 工具包 1 2 3 # 导入工具包 import pandas as pd import os 5.2 获取文件列表 1 2 3 4 5 6 7 8 9 10 # 设置文件路径 path = 'C:/Users/yyz/Desktop/python办公技巧/data/数据合并/' # 空列表, 用于存放文件路径 files = [] for file in os.listdir(path): if file.endswith(".xlsx"): files.append(path+fil
89 0