python代码画樱花带图片_python编程——pygame画樱花树

先来看看效果:

732bae536722

是不是很好看呢?现在我们来编程吧。

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()

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

喜欢的话请关注微信公众号:迷你恐龙的博客。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值