簡體   English   中英

Python/Pygame:循環遍歷 Sprite 列表的副本

[英]Python/Pygame: loop over a copy of Sprite list

我正在嘗試使用下面的代碼創建雨滴流以給人一種下雨的感覺。 雨滴用於對雨滴進行建模。 它有一個 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_add_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類下重新寫了update方法如下:

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運行

raindrop.rect.y -= self.settings.screen_height

改變雨滴的recty屬性。

但是當調用Raindropupdate函數時,該更改會立即被覆蓋:

def update(self):
    """Shift raindrops to one level below"""
    self.y += self.settings.rain_speed
    self.rect.y = self.y

因為它再次更改為self.y

只需刪除_remove_add_raindrops (刪除並立即將Raindrop self.raindropsself.raindrops沒有任何意義)並更改您的Raindrop類如下:

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

    def update(self):
        """Shift raindrops to one level below"""
        self.rect.move_ip(0, self.settings.rain_speed)
        if not pygame.display.get_surface().get_rect().bottom > self.rect.top:
            self.rect.bottom = 0

所以Raindrop類可以檢查自己是否離開屏幕。 如果您想使用浮點數而不是整數來存儲Sprite的位置,我建議使用Vector2類來這樣做,而不是使用單個屬性,因為從長遠來看,這樣可以更容易地對角移動您的精靈。

問題是 self.y 無限增加,因為我沒有在原始代碼或更新后的代碼中的任何地方重新設置它。

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

將上面最后一行從“self.rect.y”更改為“self.y”解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM