python编程——pygame画樱花树

最终的效果是这样的:
在这里插入图片描述
很好看吧?现在我们就来看看是怎么编的吧!

import pygame,random#首先导入pygame、random模块
pygame.init()
screen = pygame.display.set_mode([640,480])#绘制一个长640,宽480的窗口
screen.fill([0,0,0])#以黑色填充屏幕

x = 320
y = 530
size = 50

for tree in range(200):
    y = y - 1
    x = x - 0.2
    size = size - 0.1
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)#画出主树干

size = 30
for tree in range(180):
    y = y - 1
    x = x + 0.5
    size = size - 0.15
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)#剩下的画出不同的树枝

x = 280
y = 330
size = 15
for tree in range(180):
    y = y - 1
    x = x - 0.4
    size = size - 0.05
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)

x = 270
y = 180
size = 10
for tree in range(50):
    y = y - 1
    x = x + 1.5
    size = size - 0.05
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)

x = 280
y = 310
size = 15
for tree in range(150):
    y = y - 1
    x = x - 1
    size = size - 0.05
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)

x = 180
y = 210
size = 10
for tree in range(50):
    y = y + 0.3
    x = x - 1
    size = size - 0.05
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)

x = 240
y = 230
size = 12
for tree in range(80):
    y = y - 1.5
    x = x + 1
    size = size - 0.1
    my_tree = pygame.draw.rect(screen,[134,78,2],[x,y,size,size],0)

f_x = 0
f_y = 0
colour_R = 0
colour_G = 0
colour_B = 0
for flower in range(250):#画出樱花
    f_x = random.randint(250,350)
    f_y = random.randint(100,300)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)

for flower in range(130):
    f_x = random.randint(350,450)
    f_y = random.randint(130,250)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)


for flower in range(130):
    f_x = random.randint(150,250)
    f_y = random.randint(130,250)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)
       
for flower in range(80):
    f_x = random.randint(200,250)
    f_y = random.randint(200,300)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)

for flower in range(80):
    f_x = random.randint(400,500)
    f_y = random.randint(100,200)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)

for flower in range(80):
    f_x = random.randint(100,200)
    f_y = random.randint(150,250)
    colour_R = random.randint(254,255)
    colour_G = random.randint(103,255)
    colour_B = random.randint(205,255)
    my_flower = pygame.draw.circle(screen,[colour_R,colour_G,colour_B],[f_x,f_y],5,1)
#此部分樱花使用那么多的for循环是为了增强樱花排列的自然感,否则就是一大块方形樱花
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

运行程序后就能看到美丽的樱花了,快去试试吧!

喜欢的话请点赞收藏关注及微信公众号:
在这里插入图片描述
注:本文章为作者原创,未经允许禁止转载!!!

猜你喜欢

转载自blog.csdn.net/minikonglongz/article/details/106605979