题目:寻找一幅雨滴图像,并创建一系列整齐的雨滴。让这些雨滴往下落,直到到达屏幕底端后消失。
自学Python遇到一个想不通的问题。 为什么如下图将self.y = float(self.rect.y)之后,最终生成的画面是只有一行雨滴,而不是几行雨滴。 如果直接用 self.rect.y += 雨滴的速度 就不会出现这样的问题。 有站友有同样的问题吗?谢谢大家!
raindrop模块
import pygame from pygame.sprite import Sprite class Raindrop(Sprite): def __init__(self, rf_settings, screen): super(Raindrop, self).__init__() self.screen = screen self.rf_settings = rf_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.x = float(self.rect.x) self.y = float(self.rect.y) def update(self): self.y += self.rf_settings.raindrop_speed_factor #self.rect.y += self.rf_settings.raindrop_speed_factor self.rect.y = self.y def blitme(self): self.screen.blit(self.image, self.rect)game_function模块
import sys import pygame from raindrop import Raindrop def check_event(): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() def update_screen(rf_settings, screen, raindrops): screen.fill(rf_settings.bg_color) raindrops.draw(screen) pygame.display.flip() def get_number_raindrops_x(rf_settings, raindrop_width): available_space_x = rf_settings.screen_width - 2 * raindrop_width number_raindrops_x = int(available_space_x / (2 * raindrop_width)) return number_raindrops_x def get_number_raindrops_rows(rf_settings, raindrop_height): available_space_y = (rf_settings.screen_height - (3 * raindrop_height)) number_rows = int(available_space_y / (2 * raindrop_height)) return number_rows def create_raindrop(rf_settings, screen, raindrops, raindrop_number, row_number): raindrop = Raindrop(rf_settings, screen) raindrop_width = raindrop.rect.width 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 raindrops.add(raindrop) def create_rain(rf_settings, screen, raindrops): raindrop = Raindrop(rf_settings, screen) number_raindrops_x = get_number_raindrops_x(rf_settings, raindrop.rect.width) number_rows = get_number_raindrops_rows(rf_settings, raindrop.rect.height) for row_number in range(number_rows): for raindrop_number in range(number_raindrops_x): create_raindrop(rf_settings, screen, raindrops, raindrop_number, row_number) def update_raindrops(raindrops): raindrops.update()settings模块
class Settings(): def __init__(self): self.screen_width = 1000 self.screen_height = 600 self.bg_color = (0, 205, 205) self.raindrop_speed_factor = 1```主模块
import sys import pygame from pygame.sprite import Group from settings import Settings from raindrop import Raindrop import game_functions as gf def run_game(): pygame.init() rf_settings = Settings() screen = pygame.display.set_mode( (rf_settings.screen_width, rf_settings.screen_height)) pygame.display.set_caption('Pouring Rain') raindrops = Group() gf.create_rain(rf_settings, screen, raindrops) while True: gf.check_event() gf.update_raindrops(raindrops) gf.update_screen(rf_settings, screen, raindrops) run_game()输出结果