为什么我得到不同的结果

时间:2016-12-12 16:46:31

标签: python pygame sprite

为什么在绘制到屏幕期间使用组精灵对象的方法更新时会得到不同的结果。当通过调用方法时,我只得到一行精灵,当我运行循环并将每个精灵的y轴移动一个时,我得到整个群(几行)向下移动。 这是代码

drop.py

import pygame
from pygame.sprite import Sprite


class Drop(Sprite):
"""Class that represent single drop in rain"""

    def __init__(self, settings, screen):
        """Initialize drop and sets its starting position"""
        super().__init__()
        self.screen = screen
        self.settings = settings

        #Load drop image and sets its rect attribute
        self.image = pygame.image.load('images/drop.bmp')
        self.rect = self.image.get_rect()

        #Start each drop on top-left on the screen
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        #store drop exact position
        self.y = float(self.rect.y)


    def update(self):
        self.y += 1
        self.rect.y = self.y        

我将所有功能存储在单独的文件中。只是为了让它不那么混乱。函数update_screen()在代码中描述的注释中的问题我不明白为什么通过sprite来循环,这样改变y轴并使整个组移动,但是当我使用sprite对象的方法时.update()他们在小组中我只得到部分小组在屏幕上移动。

func_rain.py

import pygame
import sys
from drop import Drop
from time import sleep

def create_rain(settings, screen, drops):
    """Create a fleet of drops."""
    #Create an drop and find the number of drops in a row and number rows

    drop = Drop(settings, screen)
    number_drops_x = get_number_drops_x(settings, drop.rect.width)
    number_rows = get_number_rows(settings, drop.rect.height)

    for row_number in range(number_rows):
        for drop_number in range (number_drops_x):
            create_drop(settings, screen, drops, drop_number, row_number)      

def get_number_drops_x(settings, drop_width):    
    """Determine the number of drops that fit in a row."""
    available_space_x = settings.screen_width - 2 * drop_width
    number_drops_x = int(available_space_x / (2 * drop_width))
    return number_drops_x


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


def create_drop(settings, screen, drops, drop_number, row_number):
    """Create drop and place it in the row"""

    drop = Drop(settings, screen)
    drop_width = drop.rect.width
    drop.x = drop_width + 2 * drop_width * drop_number
    drop.rect.x = drop.x
    drop.rect.y = drop.rect.height + 2 * drop.rect.height * row_number
    drops.add(drop)


def check_keydown_events(event):
    """Respond to key presses"""                     
    if event.key == pygame.K_q:
        sys.exit()


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

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event)


def update_screen(settings, screen, drops):
    """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)
#This loop
    for drop in drops:
        drop.rect.y += 1
"""Here is the problem!!!!!! if I use for loop just above I get whole group      
   consisting from 5 rows of drops (image has resolution 50*62 pix). But when 
   I use drops.update() commented below (no for loop) I get on screen only
   one row of drops."""
    #drops.update()
    drops.draw(screen)
    pygame.display.flip()

rain_settings.py

 class Settings():
    """A class to store all ran settings"""

    def __init__(self):
        #Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (100, 100, 100)

最后一个主文件。 rain.py

import pygame
from rain_settings import Settings
import func_rain as fr
from pygame.sprite import Group

def run_rain():
    """Initialize pygame and screen object"""
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))

    drops = Group()
    fr.create_rain(settings, screen, drops)

    while True:

        fr.check_events()
        fr.update_screen(settings, screen, drops)    


run_rain()

我真的很感激如果有人能解释我做错了什么。我无法弄清楚。请

1 个答案:

答案 0 :(得分:0)

所以我一直在玩,我通过类方法更新获得sprite的运动。

def update(self):
    self.rect.y += 1

虽然直接改变 self.rect.y 的作品以及对y属性的间接改变,但仍然有一个谜。