使整个对象网格落向屏幕底部

2024-05-19 00:00:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让一排雨滴落在屏幕底部,一旦它通过屏幕底部就消失了。当前代码发生的情况是,一行将落到底部并消失。如果我想让它们水平移动,整个网格将不会出现问题。我认为代码问题不在rainydop.py中,我相信问题在raindrop.py中。我在下面都贴了

raindrop.py:

import pygame 
from pygame.sprite import Sprite

class RainDrop(Sprite):
    """A class to represent a single raindrop in the fleet."""

    def __init__(self, rain_game):
        """Initialize the raindrop and set its starting position."""
        super().__init__()
        self.screen = rain_game.screen
        self.raindrop_settings = rain_game.raindrop_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.y = float(self.rect.y)

    def check_edges(self):
        """Return True if rain drop reaches the bottom of the screen screen."""
        screen_rect = self.screen.get_rect()
        if self.rect.top >= screen_rect.bottom:
            return True

    def update(self):
        """Move the raindrop down."""
        self.y += self.raindrop_settings.rain_speed
        self.rect.x = self.x
        self.rect.y = self.y

雨天

import sys
import pygame
from raindrop_settings import Settings
from raindrop import RainDrop

class RainyDay:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.raindrop_settings = Settings()

        self.screen = pygame.display.set_mode(
                (self.raindrop_settings.screen_width, self.raindrop_settings.screen_height))
        pygame.display.set_caption("Rainy Day")

        self.raindrops = pygame.sprite.Group()
        self._create_fleet()

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            self._update_raindrops()            
            self._update_screen()

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    def _update_raindrops(self):
        """
        Check if the fleet is at an edge,
          then update the positions of all raindrops in the fleet.
        """
        self._check_fleet_edges()
        self.raindrops.update()

    def _create_fleet(self):
        """Create the fleet of raindrops."""
        raindrop = RainDrop(self)
        raindrop_width, raindrop_height = raindrop.rect.size
        available_space_x = self.raindrop_settings.screen_width - (2 * raindrop_width)
        number_raindrops_x = available_space_x // (2 * raindrop_width)

        available_space_y = (self.raindrop_settings.screen_height -
                             (3 * raindrop_height) - 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 _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.rect.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number
        self.raindrops.add(raindrop)

    def _check_fleet_edges(self):
        """Respond appropriately if the raindrops have reached the edge."""
        for raindrop in self.raindrops.sprites():
            if raindrop.check_edges():
                self.raindrops.remove(raindrop)

    def _update_screen(self):
        """Update images on the screen, and flip to the new screen."""
        self.screen.fill(self.raindrop_settings.bg_color)
        self.raindrops.draw(self.screen)     
        pygame.display.flip()    

if __name__ == '__main__':
    rainday = RainyDay() 
    rainday.run_game()

Tags: theinrectselfgamenumbersettingsdef
1条回答
网友
1楼 · 发布于 2024-05-19 00:00:11

您必须将位置分配给raindrop.y,而不是raindrop.rect.y(或两者都分配)

raindrop.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number
raindrop.rect.y = raindrop.y

因为稍后当您移动raindrop时,您使用self.y来更改self.rect.y


self.rect.y指定不同的值,但所有对象都具有相同的self.y,因此稍后所有行都移动到同一行,并且只绘制一行

相关问题 更多 >

    热门问题