📜  在Python中使用 Pygame 显示降雪

📅  最后修改于: 2022-05-13 01:55:19.741000             🧑  作者: Mango

在Python中使用 Pygame 显示降雪

不是每个人都必须亲自目睹降雪,但等一下,如果您只需几行创意和编程就可以在屏幕上看到降雪,那会怎样。

在开始这个话题之前,强烈建议复习一下 Pygame 的基础知识。

创建降雪的步骤

1. 导入模块

首先,我们需要使用命令导入 Pygame 模块。

此外,除了 Pygame,我们还需要 random 模块。 Python有一个内置模块,您可以通过导入 random 模块来使用它来生成随机数。

2. 初始化游戏引擎

它只是意味着选择您想要使用的颜色。在编程的世界里,你能想到的都能做出来。在文章的最后,你会发现白色背景上的绿色降雪。

Python3
# initialize
pygame.init()
 
# chosen colours will be used
# to display the output
WHITE = [255, 255, 255]
GREEN = [0, 255, 0]


Python3
# specify the size
 
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)


Python3
# caption for output window
 
pygame.display.set_caption("Programming World of GFG")


Python3
snowFall = []


Python3
for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])


Python3
# object to track time
 
clock = pygame.time.Clock()


Python3
# loop till the close button is pressed
done = False
 
while not done:
 
  # User did something
    for event in pygame.event.get():
 
       # If user clicked close
        if event.type == pygame.QUIT:
 
          # Flag that we are done so
          # we exit this loop
            done = True


Python3
screen.fill(WHITE)


Python3
for i in range(len(snowFall)):


Python3
pygame.draw.circle(screen, GREEN, snowFall[i], 2)


Python3
# Move the snowFall down one pixel
        snowFall[i][1] += 1
 
        # If the snowFall has moved off the bottom of the screen
        if snowFall[i][1] > 400:
         
            # Reset it just above the top
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
             
            # Give it a new x position
            x = random.randrange(0, 400)
            snowFall[i][0] = x
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    clock.tick(20)
pygame.quit()


Python3
import pygame
import random
pygame.init()
 
WHITE = [255, 255, 255]
GREEN  = [0,255,0]
SIZE = [400, 400]
 
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Programming World of GFG")
 
snowFall = []
for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])
 
clock = pygame.time.Clock()
done = False
while not done:
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True
    screen.fill(WHITE)
    for i in range(len(snowFall)):
        pygame.draw.circle(screen, GREEN, snowFall[i], 2)
 
        snowFall[i][1] += 1
        if snowFall[i][1] > 400:
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
        
            x = random.randrange(0, 400)
            snowFall[i][0] = x
 
    pygame.display.flip()
    clock.tick(20)
pygame.quit()


3. 指定屏幕大小

 它可以是一个新数字,具体取决于您系统的分辨率。

蟒蛇3

# specify the size
 
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)

4. 为你的降雪窗口屏幕指定一个名称

给出的名称可以在输出窗口的左上角看到。

蟒蛇3

# caption for output window
 
pygame.display.set_caption("Programming World of GFG")

5. 为你的降雪创建一个空数组

蟒蛇3

snowFall = []


6. 循环获取降雪位置

循环并运行 50 次,然后使用随机模块在随机 x,y 位置添加降雪。

蟒蛇3

for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])

7. 追踪时间

创建一个对象来帮助跟踪时间

蟒蛇3

# object to track time
 
clock = pygame.time.Clock()

8. 设置降雪发生的标准

降雪应该发生,直到用户按下关闭按钮,为此在while循环内,使用for循环。

蟒蛇3

# loop till the close button is pressed
done = False
 
while not done:
 
  # User did something
    for event in pygame.event.get():
 
       # If user clicked close
        if event.type == pygame.QUIT:
 
          # Flag that we are done so
          # we exit this loop
            done = True

9.设置屏幕背景:

蟒蛇3

screen.fill(WHITE)


10.处理降雪

现在使用 for 循环来处理列表中的每个 Snowfall:

蟒蛇3

for i in range(len(snowFall)):

11.绘制降雪

蟒蛇3

pygame.draw.circle(screen, GREEN, snowFall[i], 2)

12. 添加运动

蟒蛇3

# Move the snowFall down one pixel
        snowFall[i][1] += 1
 
        # If the snowFall has moved off the bottom of the screen
        if snowFall[i][1] > 400:
         
            # Reset it just above the top
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
             
            # Give it a new x position
            x = random.randrange(0, 400)
            snowFall[i][0] = x
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    clock.tick(20)
pygame.quit()

是的,绿色降雪开始了!!

完整的程序

蟒蛇3

import pygame
import random
pygame.init()
 
WHITE = [255, 255, 255]
GREEN  = [0,255,0]
SIZE = [400, 400]
 
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Programming World of GFG")
 
snowFall = []
for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])
 
clock = pygame.time.Clock()
done = False
while not done:
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True
    screen.fill(WHITE)
    for i in range(len(snowFall)):
        pygame.draw.circle(screen, GREEN, snowFall[i], 2)
 
        snowFall[i][1] += 1
        if snowFall[i][1] > 400:
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
        
            x = random.randrange(0, 400)
            snowFall[i][0] = x
 
    pygame.display.flip()
    clock.tick(20)
pygame.quit()

输出