Pygame 每次我添加一个矩形时它都会偏移它

问题描述

import pygame
import sys
from pygame.sprite import Sprite
class Settings:
    def __init__(self):
        self.raindrop_speed = 3
        self.raindrop_direction = -1
        self.raindrop_dropseed = 3
        self.backgroundcolor = (30,30,30)
        self.height_counter = 1
class Drop(Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("raindrop.png")
        self.rect = self.image.get_rect()
        self.rect.y = self.rect.height
        self.rect.x = self.rect.width
        self.x_cord = self.rect.x
        self.y_cord = self.rect.y

class RainDrop:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((1200,800))
        self.settings = Settings()
        self.backgroundcolor = self.settings.backgroundcolor
        self.raindrops = pygame.sprite.Group()
        self.screen_rect = self.screen.get_rect()
        self._add_raindrops()
    def _add_raindrops(self,row = ""):
        new_raindrop = Drop()
        drop_height = new_raindrop.rect.height 
        drop_width = new_raindrop.rect.width
        print(drop_width)
        screen_space = self.screen_rect.width
        screen_height_space = self.screen_rect.height
        aviable_row_space = screen_height_space //(drop_height*2)
        avivable_screen_space = screen_space - (drop_width*2)
        amount_of_columns = avivable_screen_space//(drop_width*2)
        if row == "row":
            self._add_rows(amount_of_columns)
        else:
            self._add_columns(amount_of_columns,aviable_row_space)
        
    def _add_columns(self,amount_of_columns,aviable_row_space):
        for height_of_drops in range(aviable_row_space):
            for number_of_drops in range(amount_of_columns):
                drop = Drop()
                drop.x_cord = (drop.rect.width *2)* number_of_drops
                drop.y_cord =(drop.rect.height *2)* height_of_drops
                drop.rect.x = drop.x_cord
                drop.rect.y = drop.y_cord
                self.raindrops.add(drop)
    def _add_rows(self,amount_of_columns):
        for number_of_drops in range(amount_of_columns):
            drop = Drop()
            drop.x_cord = (drop.rect.width *2) * number_of_drops
            drop.y_cord = drop.rect.height *2
            drop.rect.x = drop.x_cord
            drop.rect.y = drop.y_cord
            self.raindrops.add(drop)
    def _bring_down_raindrops(self):
        for drop in self.raindrops:
            drop.y_cord += self.settings.raindrop_dropseed
            drop.rect.y = drop.y_cord
    def _update_drops(self):
        self.raindrops.update()
        add_new_drop = False
        reverse = 0
        for drop in self.raindrops.copy():
            drop.x_cord += self.settings.raindrop_direction * self.settings.raindrop_speed
            drop.rect.x = drop.x_cord
            if drop.rect.right >= self.screen_rect.right:
                reverse = -1
            elif drop.rect.left <= 0:
                reverse = 1
            if drop.rect.y >= self.screen_rect.height:
                add_new_drop = True
                self.raindrops.remove(drop)
        if add_new_drop:
            add_new_drop = False
            new_raindrop = Drop()
            drop_height = new_raindrop.rect.height 
            drop_width = new_raindrop.rect.width
            screen_space = self.screen_rect.width
            avivable_screen_space = screen_space - (drop_width*2)
            amount_of_columns = avivable_screen_space//(drop_width*2)
            for number_of_drops in range(amount_of_columns):
                new_drop = Drop()
                new_drop.x_cord = (new_drop.rect.width *2) * number_of_drops
                new_drop.y_cord = (new_drop.rect.height) *2 
                new_drop.rect.x = new_drop.x_cord
                new_drop.rect.y = new_drop.y_cord
                self.raindrops.add(new_drop) 
            print(add_new_drop)
        

        if reverse != 0:
            self.settings.raindrop_direction = reverse
            self._bring_down_raindrops()

我试图在每次被删除添加一行新的矩形(水滴)但是每次我添加一行如果被偏移时,我已经用很多不同的方式解决了这个问题,但还没有找到解决它的方法。发生的事情是,当矩形(雨滴)下降时,一旦它大于屏幕高度,它就会删除一行而不是应该在其上添加新行,它会这样做,但是它总是偏移一定数量。>

宽度 = 60 高度 = 42

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)