我正在尝试使用下面的代码创建一个雨滴流来给人一种下雨的感觉。 雨滴用于对雨滴进行建模。它有一个update method()来增加y坐标,这样当我调用此方法时,它看起来就像它正在屏幕上下降一样
from pygame.sprite import Sprite
class Raindrop(Sprite):
"""A class to represent a single raindrop."""
def __init__(self, rain_game):
"""Initialize the raindrop and set its starting position."""
super().__init__()
self.screen = rain_game.screen
self.settings = rain_game.settings
# Load the raindrop image and set its rect attribute
self.image = pygame.image.load('raindrop.bmp')
self.rect = self.image.get_rect()
#Start each raindrop near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
#Store the raindrop`s exact horizontal and vertical positions.
self.x = self.rect.x
self.y = self.rect.y
def update(self):
"""Shift raindrops to one level below"""
self.y += self.settings.rain_speed
self.rect.y = self.y
class Settings:
"""A Class to store all settings for a Sky Full of Stars."""
def __init__(self):
"""Initialize the game`s settings."""
# Screen setiings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (255, 255, 255)
self.rain_speed = 1.0
我对下面代码中的remove\u add\u raindrops方法有问题。 它的工作原理与预期一样,但我的笔记本电脑中只有4排雨滴在4行之后,此代码停止打印任何雨滴到屏幕。因此,我只看到8行雨滴从屏幕的顶部滴到底部
import sys
import pygame
from settings import Settings
from raindrop import Raindrop
class Raining():
"""Overall class to display a sky with raindrops."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("It is raining today")
self.raindrops = pygame.sprite.Group()
self._create_raindrops()
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.y = raindrop_height + 2 * raindrop_height * row_number
raindrop.rect.y = raindrop.y
self.raindrops.add(raindrop)
def _create_raindrops(self):
"""Create a full screen fo raindrops."""
# Create a raindrop and find the number of raindrops in a row.
# Spacing between each raindrop is equal to one raindrop width.
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 // (2 * raindrop_width)
# Determine the number of rows of raindrops that fit on the screen.
available_space_y = (self.settings.screen_height - 2 * raindrop_height)
number_rows = available_space_y // (2 * raindrop_height)
# Create a full screen of raindrops.
for row_number in range(number_rows):
for raindrop_number in range (number_raindrops_x):
self._create_raindrop(raindrop_number, row_number)
def _remove_add_raindrops(self):
"""
Get rid of old raindrops.
Then update their y coordinate and append to the raindrops sprite group
"""
for raindrop in self.raindrops.copy():
if raindrop.rect.y >= self.settings.screen_height:
self.raindrops.remove(raindrop)
raindrop.rect.y -= self.settings.screen_height
self.raindrops.add(raindrop)
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_raindrops()
self._remove_add_raindrops()
self._update_screen()
print(f"Nr of remaining raindrops: {len(self.raindrops)}")
#Make the most recently drawn screen visible.
pygame.display.flip()
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys-exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
def _check_keydown_events(self, event):
"""Respond to keypresses."""
if event.key == pygame.K_q:
sys.exit()
def _update_raindrops(self):
"""Update the positions of all raindrops."""
self.raindrops.update()
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.raindrops.draw(self.screen)
if __name__ == '__main__':
# Make a game instance, and run the game.
rain_game = Raining()
rain_game.run_game()
我还打印了列表中雨滴的数量,它总是固定在40。我想这意味着我在删除掉屏幕下方的雨滴后,会用新的y坐标继续添加新的雨滴
有什么提示吗
更新:
我删除了_remove_add_raindrops方法,并在raindrop类下重新编写了更新方法,如下所示:
def update(self):
"""Shift raindrops to one level below"""
self.y += self.settings.rain_speed
self.rect.y = self.y
if self.rect.top >= self.screen.get_rect().bottom:
self.rect.y -= self.settings.screen_height
这仍然是同样的行为
_remove_add_raindrops
运行更改雨滴的
y
属性但是当调用
Raindrop
的update
函数时,该更改立即被覆盖:因为它再次被更改为
self.y
只需删除
_remove_add_raindrops
(删除并立即将Raindrop
读入self.raindrops
没有任何意义),并将Raindrop
类更改如下:因此
Raindrop
类可以检查自己是否脱离屏幕。如果您想使用浮点而不是整数来存储Sprite
的位置,我建议使用Vector2
类来存储,而不是使用单个属性,因为从长远来看,这样可以更容易地沿对角线移动精灵问题是self.y无限增长,因为我没有在原始代码或更新代码中的任何地方重新设置它
将上面最后一行从“self.rect.y”更改为“self.y”,解决了这个问题
相关问题 更多 >
编程相关推荐