我是 Python 的新手,我正在尝试创建从屏幕底部落下并在它们到达末尾后消失的雨滴网格(这是来自 Python Crash Course book)。
我设法使网格和水滴掉落,但是水滴在屏幕上留下了痕迹,我一直在弄清楚为什么。
我检查了许多我可以找到的类似问题,但仍然找不到解决方案
这是我的游戏和 Randrop 实例的代码:
import pygame
import sys
from settings import Settings
from raindrop import Raindrop
class RaindropsGame:
"""Overall class to manage game"""
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
self.raindrops = pygame.sprite.Group()
self.screen_caption = pygame.display.set_caption("Raindrops")
self.BackGround = pygame.image.load('images/clouds.bmp')
def run_game(self):
while True:
self._update_screen()
self._create_fleet()
self.check_events()
self._update_raindrops()
def _update_screen(self):
self.screen.blit(self.BackGround, (0, 0))
self.raindrops.draw(self.screen)
pygame.display.flip()
def _update_raindrops(self):
self._check_fleet_edges()
self.raindrops.update()
if not self.raindrops:
self._create_fleet()
def check_events(self):
"""Check keyboard key presses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
def _create_raindrop(self, raindrop_number, row_number):
raindrop = Raindrop(self)
raindrop_width, raindrop_height = raindrop.rect.size
raindrop.x = 1 * raindrop_width * raindrop_number
raindrop.rect.x = raindrop.x
raindrop.y = raindrop.rect.height + raindrop.rect.height * row_number
raindrop.rect.y = raindrop.y
self.raindrops.add(raindrop)
def _create_fleet(self):
raindrop = Raindrop(self)
raindrop_width, raindrop_height = raindrop.rect.size
available_space_x = self.settings.screen_width - 2* raindrop_width
number_raindrops_x = available_space_x // (1 * raindrop_width)
available_space_y = (self.settings.screen_height - 5 * 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 _check_fleet_edges(self):
for raindrop in self.raindrops.sprites():
if raindrop.check_edges():
self.raindrops.remove(raindrop)
if __name__ in '__main__':
ai = RaindropsGame()
ai.run_game()
和
雨滴实例:
import pygame
from pygame.sprite import Sprite
from settings import Settings
class Raindrop(Sprite):
"""Class to manage raindrops"""
def __init__(self, ai_game):
super().__init__()
self.settings = ai_game.settings
self.screen = ai_game.screen
self.image = pygame.image.load('images/raindrop.bmp').convert_alpha()
self.rect = self.image.get_rect()
self.rect.y = self.rect.height
self.y = float(self.rect.y)
def check_edges(self):
screen_rect = self.screen.get_rect()
if self.rect.bottom >= screen_rect.bottom or self.rect.top <=0:
return True
def update(self):
"""Move the raindrop down."""
self.y += self.settings.raindrop_speed
self.rect.y = self.y
def blitme(self):
self.screen.blit(self.image, self.rect)
我非常感谢您对此提供的任何帮助,如果需要,这里是我看到的问题的屏幕截图: