练习13-3:雨滴 ——寻找一幅雨滴图像,并创建一系列整齐排列的雨滴。让这些雨滴往下落,直到到达屏幕底端后消失。
1.raining.py
import sys
import pygame
from random import randint
from settings import Settings
from rain import Raindrop
class Rainsky:
""" 管理游戏资源和行为的类 """
def __init__(self):
""" 初始化游戏并创建游戏资源 """
pygame.init()
self.settings = Settings()
# 创建一个显示窗口,并设置窗口尺寸
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Raining Day")
self.rains = pygame.sprite.Group()
self._create_rains()
def run_game(self):
""" 开始游戏的主循环 """
while True:
self._check_events()
self._update_rains()
self._update_screen()
def _update_rains(self):
""" 更新细雨中所有雨滴的位置 """
self.rains.update()
def _check_events(self):
""" 响应按键和鼠标事件 """
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _create_rains(self, number_rains_x=10, number_rows=1):
""" 创建细雨 """
# 创建一滴雨点并计算一行可容纳多少滴雨点
rain = Raindrop(self)
rain_width, rain_height = rain.rect.size
availiable_space_x = self.settings.screen_width - (5 * rain_width)
self.number_rains_x = number_rains_x
self.number_rains_x = availiable_space_x // (5 * rain_width)
# 计算屏幕可容纳多少行雨滴
availiable_space_y = (self.settings.screen_height - (10 * rain_height))
self.number_rows = number_rows
number_rows = availiable_space_y // (5 * rain_height)
# 创建雨滴行列
for row_number in range(number_rows):
for rain_number in range(self.number_rains_x):
self._create_rain(rain_number, row_number)
def _create_rain(self, rain_number, row_number):
# 创建一滴雨点并将其加入到当前行
rain = Raindrop(self)
rain_width, rain_height = rain.rect.size
rain.rect.x = rain_width + 5 * rain_width * rain_number
rain.y = rain.rect.height + 5 * rain.rect.height * row_number
rain.rect.y = rain.y
# 这里很重要!!!
# 如果写作:
# rain.rect.y = rain.rect.height + 5 * rain.rect.height * row_number
# 屏幕只会出现一行雨滴
# 原因是(rain.rect.height + 5 * rain.rect.height * row_number)
# 是计算当前雨滴在当前列的位置,然后使用雨滴的属性y来设置其rect的位置,这是创建雨滴行列的过程,并不能展示雨滴下落过程
# rain是类Raindrop()的实例,在Raindrop中rain.y在一直变化,在函数_update_rains()中更新,以此来展示雨滴下落过程
self.rains.add(rain)
def _update_screen(self):
""" 更新屏幕上的图像,并切换到新屏幕 """
self.screen.fill(self.settings.bg_color)
self.rains.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = Rainsky()
ai.run_game()
2.settings.py
class Settings:
""" 存储游戏中所有设置的类 """
def __init__(self):
""" 初始化游戏的设置 """
# 屏幕设置
self.screen_width = 1500
self.screen_height = 800
self.bg_color = (240, 240, 240)
# 雨滴下落速度设置
self.raindrop_speed = 0.5
3.rain.py
import pygame
from pygame.sprite import Sprite
class Raindrop(Sprite):
""" 表示单滴雨点的类 """
def __init__(self, ai_game):
""" 初始化雨滴并设置其起始位置 """
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
# 加载外星人图像并设置其rect属性
self.image = pygame.image.load('images/raindrops.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.settings.raindrop_speed
self.rect.y = self.y
运行效果如下:
练习13-4:连绵细雨 ——修改为完成练习11-3而编写的代码,使得一行雨滴消失在屏幕底端后,屏幕顶端又出现一行新雨滴并开始往下落。
只需在代码raining.py中加上检查雨滴是否到达屏幕底端,如果是,则再生成新的雨滴出现在屏幕顶端
import sys
import pygame
from random import randint
from settings import Settings
from rain import Raindrop
class Rainsky:
""" 管理游戏资源和行为的类 """
def __init__(self):
""" 初始化游戏并创建游戏资源 """
pygame.init()
self.settings = Settings()
# 创建一个显示窗口,并设置窗口尺寸
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Raining Day")
self.rains = pygame.sprite.Group()
self._create_rains()
def run_game(self):
""" 开始游戏的主循环 """
while True:
self._check_events()
self._update_rains()
self._update_screen()
def _update_rains(self):
""" 更新细雨中所有雨滴的位置 """
self._check_rains_bottom()
self.rains.update()
def _check_events(self):
""" 响应按键和鼠标事件 """
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _create_rains(self, number_rains_x=10, number_rows=1):
""" 创建细雨 """
# 创建一滴雨点并计算一行可容纳多少滴雨点
rain = Raindrop(self)
rain_width, rain_height = rain.rect.size
availiable_space_x = self.settings.screen_width - (5 * rain_width)
self.number_rains_x = number_rains_x
self.number_rains_x = availiable_space_x // (5 * rain_width)
# 计算屏幕可容纳多少行雨滴
availiable_space_y = (self.settings.screen_height - (10 * rain_height))
self.number_rows = number_rows
number_rows = availiable_space_y // (5 * rain_height)
# 创建雨滴行列
for row_number in range(number_rows):
for rain_number in range(self.number_rains_x):
self._create_rain(rain_number, row_number)
def _create_rain(self, rain_number, row_number):
# 创建一滴雨点并将其加入到当前行
rain = Raindrop(self)
rain_width, rain_height = rain.rect.size
rain.rect.x = rain_width + 5 * rain_width * rain_number
rain.y = rain.rect.height + 5 * rain.rect.height * row_number
rain.rect.y = rain.y
# 这里很重要!!!
# 如果写作:
# rain.rect.y = rain.rect.height + 5 * rain.rect.height * row_number
# 屏幕只会出现一行雨滴
# 原因是(rain.rect.height + 5 * rain.rect.height * row_number)
# 是计算当前雨滴在当前列的位置,然后使用雨滴的属性y来设置其rect的位置,这是创建雨滴行列的过程,并不能展示雨滴下落过程
# rain是类Raindrop()的实例,在Raindrop中rain.y在一直变化,在函数_update_rains()中更新,以此来展示雨滴下落过程
self.rains.add(rain)
def _check_rains_bottom(self):
""" 检查是否有雨滴到达了屏幕底端,并做出响应 """
screen_rect = self.screen.get_rect()
for rain in self.rains.copy():
if rain.rect.bottom >= screen_rect.bottom:
self.rains.remove(rain)
random_number_x = randint(0, screen_rect.right)
rain_number = random_number_x / (rain.rect.width * 5)
self._create_rain(rain_number, 0)
def _update_screen(self):
""" 更新屏幕上的图像,并切换到新屏幕 """
self.screen.fill(self.settings.bg_color)
self.rains.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = Rainsky()
ai.run_game()
运行效果如下:
但是这不太自然,接下来再稍微改善一下
要实现雨滴行列之间距离随机化,从而实现雨滴的位置随机;要实现雨滴下落速度的随机化,还不会差太多,限制在(1,2)数倍内;实现雨滴尺寸大小的随机化,限制在(10,20)内,利用函数
pygame.transform.smoothscale()来完成
1.raining.py
import random
import sys
import pygame
from random import randint
from settings import Settings
from rain import Raindrop
class Rainsky:
""" 管理游戏资源和行为的类 """
def __init__(self):
""" 初始化游戏并创建游戏资源 """
pygame.init()
self.settings = Settings()
# 创建一个显示窗口,并设置窗口尺寸
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Raining Day")
self.rains = pygame.sprite.Group()
self._create_rains()
def run_game(self):
""" 开始游戏的主循环 """
while True:
self._check_events()
self._update_rains()
self._update_screen()
def _update_rains(self):
""" 更新细雨中所有雨滴的位置 """
self._check_rains_bottom()
self.rains.update()
def _check_events(self):
""" 响应按键和鼠标事件 """
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _create_rains(self):
""" 创建细雨 """
# 创建一滴雨点并计算一行可容纳多少滴雨点
rain = Raindrop(self)
rain_width, rain_height = rain.rect.size
number_rains_x = self.settings.screen_width // (8 * rain_width)
# 计算屏幕可容纳多少行雨滴
number_rows = self.settings.screen_height // (8 * rain_height)
# 创建雨滴行列
for row_number in range(number_rows):
for rain_number in range(number_rains_x):
self._create_rain()
def _create_rain(self):
# 创建一滴雨点并将其加入到当前行
rain = Raindrop(self)
screen_rect = self.screen.get_rect()
rain.rect.x = random.randint(0, screen_rect.right)
rain.y = random.randint(0, 300)
rain.rect.y = rain.y
self.rains.add(rain)
def _check_rains_bottom(self):
""" 检查是否有雨滴到达了屏幕底端,并做出响应 """
screen_rect = self.screen.get_rect()
for rain in self.rains.copy():
if rain.rect.bottom >= screen_rect.bottom:
self.rains.remove(rain)
self._create_rain()
def _update_screen(self):
""" 更新屏幕上的图像,并切换到新屏幕 """
self.screen.fill(self.settings.bg_color)
self.rains.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = Rainsky()
ai.run_game()
2.rain.py
import pygame
import random
from pygame.sprite import Sprite
class Raindrop(Sprite):
""" 表示单滴雨点的类 """
def __init__(self, ai_game):
""" 初始化雨滴并设置其起始位置 """
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
# 加载外星人图像并设置其rect属性
self.image = pygame.image.load('images/raindrop.bmp')
rect_mul = random.randint(10, 20)
self.image = pygame.transform.smoothscale(self.image, (rect_mul, rect_mul * 1.5))
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):
""" 向下移动雨滴 """
raindrop_speed_mul = random.uniform(1, 2)
self.y += self.settings.raindrop_speed * raindrop_speed_mul
self.rect.y = self.y
3.settings.py
class Settings:
""" 存储游戏中所有设置的类 """
def __init__(self):
""" 初始化游戏的设置 """
# 屏幕设置
self.screen_width = 1500
self.screen_height = 800
self.bg_color = (250, 250, 250)
# 雨滴下落速度设置
self.raindrop_speed = 0.5
运行效果如下: