小白求助pygame中纵向移动

将Xue类中update()方法中self.rect.y += 1 改成下方代码就只会出现一行雪花下落

import pygame
import sys
from xue import Xue

def check_events():
    """检查事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


def get_num_x(screen):
    """计算一行放置的雪花个数"""
    xue = Xue(screen)
    space_x = screen.get_rect().width - 2 * xue.rect.width
    num_x = int(space_x / (2 * xue.rect.width))
    return num_x


def get_num_y(screen):
    """
    计算放置雪花的列数
    大概是屏幕的一半
    """
    xue = Xue(screen)
    space_y = (screen.get_rect().height / 2) - (2 * xue.rect.height)
    num_y = int(space_y / (2 * xue.rect.height))
    return num_y


def create_xuehuas(screen, xues):
    """创建一大堆雪花"""
    for n_y in range(get_num_y(screen)):
        for n_x in range(get_num_x(screen)):
            xue = Xue(screen)
            xue.rect.y = xue.rect.height + 2 * xue.rect.height * n_y
            xue.rect.x = xue.rect.width + 2 * xue.rect.width * n_x
            xues.add(xue)


def update_screen(screen, xues):
    """更新屏幕"""
    screen.fill((245, 245, 245))
    xues.draw(screen)
    pygame.display.flip()

```import pygame
from pygame.sprite import Sprite

class Xue(Sprite):
    """关于雪的类"""

    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        self.image = pygame.image.load('xuehua/xh.bmp')
        self.rect = self.image.get_rect()
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height
        self.y = float(self.rect.y)

    def update(self):
        """向下移动雪花"""
        self.y += 1
        self.rect.y = self.y


```import pygame
import sys
from xue import Xue
from pygame.sprite import Group
import xia_xue as xx

def run_game():
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("下雪")
xues = Group()
xx.create_xuehuas(screen, xues)
while True:

    xx.check_events()
    xues.update()
    xx.update_screen(screen, xues)

run_game()

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!