Python makes snow scene pictures, if you can’t see the snow, you can do it yourself~

foreword

In winter in the south, there are only strong winds, or heavy rain. Fortunately, there were several snowfalls in Changsha last year. It was really the year before last. I have never seen snow in Changsha.

I've started to learn Python, it's time to change my hands and make a snow scene picture by myself, the technology is not good, don't spray me

Specific introduction

The realization of the dynamic version mainly relies on the pygame module.

From drawing to realizing dynamic movement,

The original idea was to replace the plane with the Koch snowflake drawn last time according to the idea of ​​the plane war.

I tried it myself and the effect was very poor, just pictures flying around on the screen.

I found the pygame.draw module through Baidu, which is similar to turtle.

Of course, random is indispensable
Please add a picture description

Code

Original code. Click to receive [Remarks: Su]

Because the first step to use pygame is to initialize:

import pygame
import random
#初始化
pygame.init()

Load the background image and set the screen length and width according to the size of the background image:

SIZE = (1000, 500)
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("下雪了")
#加载位图
background = pygame.image.load('snow.jpg')

Next, we need to define a list of snowflakes and initialize the snowflakes. Here we need to use random numbers to set the coordinates and speed of the xy axis.

random.randrange

random.randrange([start],stop[, step]): Obtain a random number from the set increasing by the specified base within the specified range.

random.randint(a,b): used to generate an integer within the specified range. Among them, the parameter a is the lower limit, the parameter b is the upper limit, and the generated random number n: a<=n<=b.

# 定义一个雪花列表
snow = []
# 初始化雪花
for i in range(300):
   x = random.randrange(0, SIZE[0])
    y = random.randrange(0, SIZE[1])
    speedx = random.randint(-1, 2)
    speedy = random.randint(3,8)
    snow.append([x, y, speedx, speedy])

Friends who have done airplane wars or are familiar with pygame should know that the next thing to do is to set the game loop and draw the previously loaded background image.

Surface objects have a method called blit() which draws bitmaps

screen.blit(space, (0,0))

The first parameter is the loaded bitmap, and the second parameter is the starting coordinates of drawing.

done = False
while not done:
    # 消息事件循环,判断退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    #绘制位图
    screen.blit(background, (0,0))  

This step is the most important cycle of drawing snowflakes and setting the snowflake list.

The pygame.draw module is used to draw snowflakes, which is used to draw some simple graphics on the Surface, such as points, lines, rectangles, circles, arcs, etc. What we use to draw snowflakes is:

pygame.draw.circle

原型:pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect

Purpose: Used to draw circles. The third parameter pos is the position coordinates of the center of the circle, and radius specifies the radius of the circle.

The width parameter indicates the width of the line (brush). If the value is set to 0, it means to fill the entire graphic. Our drawn snowflakes are filled with white. The color parameter is usually an RGB triplet (R, G, B).

The snowflake list cycle mainly depends on the length of the snowflake list. The position of the moving snowflake is also set. The program also makes a judgment that if the snowflake falls out of the screen when it moves from the top down, the position will be reset.

 # 雪花列表循环
    for i in range(len(snow)):
        # 绘制雪花,颜色、位置、大小
        pygame.draw.circle(screen, (255, 255, 255), snow[i][:2], snow[i][3])

        # 移动雪花位置(下一次循环起效)
        snow[i][0] += snow[i][2]
        snow[i][1] += snow[i][3]

        # 如果雪花落出屏幕,重设位置
        if snow[i][1] > SIZE[1]:
            snow[i][1] = random.randrange(-50, -10)
            snow[i][0] = random.randrange(0, SIZE[0])  

At this point, the program is basically finished. You only need to add the time to refresh the screen and the game exit statement.

Alright, today's sharing is over here~

If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you think the
article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/

Guess you like

Origin blog.csdn.net/sunanpython/article/details/128331878