Я пытаюсь сделать так, чтобы сетка капель дождя падала на дно и исчезала при прохождении через нижнюю часть экрана. Что происходит с текущим кодом, одна строка упадет до дна и исчезнет. Вся сетка не будет проблемой, если я хочу, чтобы они двигались горизонтально. Я не думаю, что проблема кода в rainy_day.py, я думаю, что проблема в raindrop.py. Я разместил оба ниже.
raindrop.py:
import pygame
from pygame.sprite import Sprite
class RainDrop(Sprite):
"""A class to represent a single raindrop in the fleet."""
def __init__(self, rain_game):
"""Initialize the raindrop and set its starting position."""
super().__init__()
self.screen = rain_game.screen
self.raindrop_settings = rain_game.raindrop_settings
self.image = pygame.image.load('images/raindrop.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 check_edges(self):
"""Return True if rain drop reaches the bottom of the screen screen."""
screen_rect = self.screen.get_rect()
if self.rect.top >= screen_rect.bottom:
return True
def update(self):
"""Move the raindrop down."""
self.y += self.raindrop_settings.rain_speed
self.rect.x = self.x
self.rect.y = self.y
rainy_day.py
import sys
import pygame
from raindrop_settings import Settings
from raindrop import RainDrop
class RainyDay:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.raindrop_settings = Settings()
self.screen = pygame.display.set_mode(
(self.raindrop_settings.screen_width, self.raindrop_settings.screen_height))
pygame.display.set_caption("Rainy Day")
self.raindrops = pygame.sprite.Group()
self._create_fleet()
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_raindrops()
self._update_screen()
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _update_raindrops(self):
"""
Check if the fleet is at an edge,
then update the positions of all raindrops in the fleet.
"""
self._check_fleet_edges()
self.raindrops.update()
def _create_fleet(self):
"""Create the fleet of raindrops."""
raindrop = RainDrop(self)
raindrop_width, raindrop_height = raindrop.rect.size
available_space_x = self.raindrop_settings.screen_width - (2 * raindrop_width)
number_raindrops_x = available_space_x // (2 * raindrop_width)
available_space_y = (self.raindrop_settings.screen_height -
(3 * raindrop_height) - raindrop_height)
number_rows = available_space_y // (2 * raindrop_height)
for row_number in range(number_rows):
for raindrop_number in range(number_raindrops_x):
self._create_raindrop(raindrop_number, row_number)
def _create_raindrop(self, raindrop_number, row_number):
"""Create a raindrop and place it in the row."""
raindrop = RainDrop(self)
raindrop_width, raindrop_height = raindrop.rect.size
raindrop.x = raindrop_width + 2 * raindrop_width * raindrop_number
raindrop.rect.x = raindrop.x
raindrop.rect.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number
self.raindrops.add(raindrop)
def _check_fleet_edges(self):
"""Respond appropriately if the raindrops have reached the edge."""
for raindrop in self.raindrops.sprites():
if raindrop.check_edges():
self.raindrops.remove(raindrop)
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.raindrop_settings.bg_color)
self.raindrops.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
rainday = RainyDay()
rainday.run_game()