简体   繁体   中英

How can I simply this function?

I want to shorten and simplify my code, but I don't seem to get how to do it.

def draw_sky(canvas, width, height, interval=30):
    sky_width = width
    sky_height = height
    sky_bottom = height / 4
    color_sky_list = ["skyBlue4", "skyBlue3", "skyBlue2", "lightSkyBlue"]

    draw_rectangle(canvas, 0, sky_bottom + (interval*3), sky_width, sky_height, fill = color_sky_list[0], width=0)
    draw_rectangle(canvas, 0, sky_bottom + (interval*2), sky_width, sky_height-(interval*1), fill = color_sky_list[1], width=0)
    draw_rectangle(canvas, 0, sky_bottom + interval, sky_width, sky_height-(interval*2), fill = color_sky_list[2], width=0)
    draw_rectangle(canvas, 0, sky_bottom, sky_width, sky_height-(interval*3), fill = color_sky_list[3], width=0)

Replace tedious drawing with loop:

for i, color in enumerate(color_sky_list):
    draw_rectangle(canvas, 0, sky_bottom + interval * (3 - i), sky_width, sky_height - interval * i, fill=color, width=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM