为什么这个代码没有生成雨滴网格?

2024-05-19 02:55:09 发布

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

所以,我需要pygameforpython3.6的帮助。这是我上编程课的第一个学期,是的,这是我需要帮助的家庭作业。当我的雨滴是静态的时,我让代码工作,但是一旦我出于某种原因加入了运动,它现在只加载了一行。我需要一个雨滴网格向下移动的屏幕,并加载一个新的行,每次底部的一行从屏幕上消失。是的,也许有比拥有所有这些模块更简单的方法,但这正是本书想要的。到目前为止,我得到的是: 雨滴游戏.py在

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():
    # Initialize game and create a screen object.
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, 
settings.screen_height))
    pygame.display.set_caption("13-3")

    # Make raindrop group.
    raindrops = Group()

    # Create the raindrops.
    gf.create_raindrops(settings, screen, raindrops)

    # Start the main loop for the game.
    while True:

        gf.update_raindrops(raindrops)
        gf.update_screen(settings, screen, raindrops)

        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

run_game()

在设置.py在

^{pr2}$

游戏_函数.py在

import sys
import pygame
from raindrop import Raindrop

def update_screen(settings, screen, raindrops):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(settings.bg_color)
    raindrops.draw(screen)

    # Make the most recently drawn screen visible.
    pygame.display.flip()

def get_number_raindrops_x(settings, raindrop_width):
    """Determine the number of raindrops that fit in a row."""
    available_space_x = settings.screen_width - 2 * raindrop_width
    number_raindrops_x = int(available_space_x / (2 * raindrop_width))
    return number_raindrops_x

def get_number_rows(settings, raindrop_height):
    """Determine the number of rows of raindrops that fit on the screen."""
    available_space_y = settings.screen_height - (2 * raindrop_height)
    number_rows = int(available_space_y / (2 * raindrop_height))
    return number_rows

def create_raindrop(settings, screen, raindrops, raindrop_number, row_number):
    """Create a raindrop and place it in the row."""
    raindrop = Raindrop(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_raindrops(settings, screen, raindrops):
    """Create a full sky of raindrops."""
    # Create a raindrop and find the number of raindrops in a row.
    raindrop = Raindrop(settings, screen)
    number_raindrops_x = get_number_raindrops_x(settings, raindrop.rect.width)
    number_rows = get_number_rows(settings, raindrop.rect.height)

    # Create the full sky of raindrops.
    for row_number in range(number_rows):
        for raindrop_number in range(number_raindrops_x):
            create_raindrop(settings, screen, raindrops, raindrop_number, row_number)

def update_raindrops(raindrops):
    """Update the positions of all raindrops in sky."""
    raindrops.update()

在雨滴.py在

import pygame
from pygame.sprite import Sprite

class Raindrop(Sprite):
    """A class to represent a single raindrop in the sky."""

    def __init__(self, settings, screen):
        """Initialize the raindrop and set its starting position."""
        super(Raindrop, self).__init__()
        self.screen = screen
        self.settings = settings

        # Load the raindrop image and set its rect attribute.
        self.image = pygame.image.load('images/raindrop.png')
        self.rect = self.image.get_rect()

        # Start each new raindrop near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        self.y = float(self.rect.y)

    def update(self):
        """Move raindrop down."""
        self.y += self.settings.raindrop_speed_factor
        self.rect.y = self.y

    def blitme(self):
        """Draw the raindrop at its current location."""
        self.screen.blit(self.image, self.rect)

有人能告诉我我做错了什么吗,也许该如何让它在最下面一行离开时产生一个新的行,因为我不知道。:/


Tags: oftheinrectimportselfnumbersettings
2条回答

create_raindrop()中,您需要更改:

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

收件人:

^{pr2}$

Raindrop.update()中,您必须检查位置并更改它。在

如果它离开屏幕底部(self.rect.y >= setting,screen_height
然后你必须把它移到顶部(self.rect.y = 0)。或上边框上方的事件。在

def update(self):
    """Move raindrop down."""
    self.y += self.settings.raindrop_speed_factor

    # if it leaves bottom border
    if self.y >= self.settings.screen_height:
        # then put above top border
        self.y = -self.rect.height

    self.rect.y = self.y

我会很好地创建两行以上的顶行。开始时,它们将在屏幕外,但当你向下移动时,它们将在你将底行移到顶部之前填充顶行。在

^{pr2}$

相关问题 更多 >

    热门问题