矩形故障,其余部分不移动 [英] Rectangle glitching, not moving with the rest

查看:60
本文介绍了矩形故障,其余部分不移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
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):
        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)
        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 _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):
        height_counter = 1
        self.raindrops.update()
        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:
                self.settings.raindrop_direction = -1
                self._bring_down_raindrops()
            elif drop.rect.left <= 0:
                self.settings.raindrop_direction = 1
                self._bring_down_raindrops()
            #if drop.rect.y >= self.screen_rect.height or drop.rect.y <= 0:
            #    drop.rect.x = drop.rect.width
             #   drop.y_cord = drop.rect.height 
              #  drop.rect.y = drop.y_cord
        print(height_counter)
        print(self.raindrops)
    
    def _update_game(self):
        self.screen.fill(self.backgroundcolor)
        self.raindrops.draw(self.screen)
        pygame.display.flip()
    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    sys.exit()
    def run_game(self):
        while True:
            self._check_events()
            self._update_drops()
            self._update_game()

if __name__ == "__main__":
    rd = RainDrop()
    rd.run_game()

每次我运行这个程序时,矩形(水滴)到达屏幕末端时会出现一个更大的间隙我已经看了几个小时的代码,但我看不出问题是什么,我认为它必须这样做条件在底部,但我不确定我是否弄乱了它并更改了一些值,但它仍然做同样的事情.

Everytime I run this program the rectangle(waterdrops) when they reach the end of the screen a bigger gap appears I have looked at the code for several hours and I can not see what the problem is, I think it has to do with the condition in bottom but I am not sure I have messed with it and changed some values but it still does the same thing.

推荐答案

你的问题在于当你意识到你已经碰到屏幕边缘时你做了什么.当您发现屏幕上有任何掉落时,您会执行此操作,然后您反转方向.问题是,您已经将顶行上的一些水滴向一个方向移动,但是在您识别出屏幕边缘后,您继续在同一次传递中向相反方向移动其余水滴.事情就是这样结束的.

Your problem is in what you do when you recognize that you've hit the edge of the screen. You do this when you find any drop that has gone off of the screen, and at that point, you reverse direction. The problem is, you've already moved some of the drops on the top row in one direction, but after you recognize the edge of the screen, you then go on to move the rest of the drops in the opposite direction in that same pass. This is how things get out of wack.

您想要做的是注意您已经击中了屏幕的边缘,但不会立即做任何不同的事情,以便您仍然以相同的方式处理屏幕上的所有掉落.画完所有的水滴后,然后改变方向.

What you want to do is note that you've hit the edge of the screen, but not do anything differently right away, so that you still deal with all of the drops on the screen the same way in that pass. After you've drawn all of the drops, you then change direction.

这里有一个 _update_drops 版本,可以做到这一点:

Here's a version of _update_drops that will do this:

def _update_drops(self):
    height_counter = 1
    self.raindrops.update()
    # assume we haven't hit either edge of the screen
    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:
            # remember that we hit the right edge of the screen
            reverse = -1
        elif drop.rect.left <= 0:
            # remember that we hit the left edge of the screen
            reverse = 1

    # if we hit one of the edges, change directions and drop down
    if reverse != 0:
        self.settings.raindrop_direction = reverse
        self._bring_down_raindrops()

这篇关于矩形故障,其余部分不移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆