Skip to content Skip to sidebar Skip to footer

Pygame - Image Smudging / Leaving A Trail On The Screen

I am brand new in Python and I am trying to create grids of raindrops falling down the bottom of the screen and dissapearing after they reach the end (This is from Python Crash Cou

Solution 1:

I tested it and problem is because you create new Raindrops in every loop because you have self._create_fleet() inside loop. You have to use it before loop to create only first raindrops.

defrun_game(self):
    clock = pygame.time.Clock()

    self._create_fleet()

    whileTrue:
        self._update_screen()
        self.check_events()
        self._update_raindrops()

        clock.tick(25)  # the same speed 25 FPS (Frames Per Seconds) on old and new computers

EDIT:

My version with few other changes

  • I use Clock() to have always 25 FPS
  • I don't check if raindrop leave screen at the top
  • I check if top of raindrop (not bottom of raindrom) leaves screen so it looks better
  • I don't remove raindrops but move to the top

enter image description here

import os
import pygame

APP_HOME = os.path.dirname(os.path.abspath(__file__))


classSettings():
    screen_width = 800
    screen_height = 600
    raindrop_speed = 5classRaindrop(pygame.sprite.Sprite):
    """Class to manage raindrops"""def__init__(self, ai_game):
        super().__init__()
        self.settings = ai_game.settings
        self.screen = ai_game.screen
        self.image = pygame.image.load(os.path.join(APP_HOME, 'images', 'raindrop.bmp')).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.y = self.rect.height
        self.y = float(self.rect.y)


    defcheck_edges(self):
        screen_rect = self.screen.get_rect()
        #if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:#    return True# check only bottom edge# and use `top` instead of `botton` check when full dropdown leaves screenif self.rect.top >= screen_rect.bottom:
            returnTruedefupdate(self):
        """Move the raindrop down."""
        self.y += self.settings.raindrop_speed
        self.rect.y = self.y

    defblitme(self):
        self.screen.blit(self.image, self.rect)


classRaindropsGame:
    """Overall class to manage game"""def__init__(self):
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        self.raindrops = pygame.sprite.Group()
        self.screen_caption = pygame.display.set_caption("Raindrops")
        self.BackGround = pygame.image.load(os.path.join(APP_HOME, 'images', 'clouds.bmp'))

    defrun_game(self):
        clock = pygame.time.Clock()
        self._create_fleet()
        whileTrue:
            self._update_screen()
            self.check_events()
            self._update_raindrops()
            clock.tick(25)  # 25 FPSdef_update_screen(self):
        self.screen.blit(self.BackGround, (0, 0))
        self.raindrops.draw(self.screen)
        pygame.display.flip()

    def_update_raindrops(self):
        self._check_fleet_edges()
        self.raindrops.update()
        ifnot self.raindrops:
            self._create_fleet()


    defcheck_events(self):
        """Check keyboard key presses and mouse events."""for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    exit()

    def_create_raindrop(self, column_number, row_number):
        raindrop = Raindrop(self)
        raindrop_width, raindrop_height = raindrop.rect.size
        
        raindrop.x = (1.1 * raindrop_width) * column_number
        raindrop.rect.x = raindrop.x
        
        raindrop.y = (1.2 * raindrop_height) * row_number
        raindrop.rect.y = raindrop.y
        
        self.raindrops.add(raindrop)

    def_create_fleet(self):
        raindrop = Raindrop(self)
        raindrop_width, raindrop_height = raindrop.rect.size
        
        available_space_x = self.settings.screen_width - (2 * raindrop_width)
        number_raindrops_x = available_space_x // (1 * raindrop_width)
        
        available_space_y = self.settings.screen_height - (5 * raindrop_height)
        number_rows = available_space_y // (1 * raindrop_height)
        
        for row_number inrange(number_rows):
            for raindrop_number inrange(number_raindrops_x):
                self._create_raindrop(raindrop_number, row_number)
                
    def_check_fleet_edges(self):
        for raindrop in self.raindrops.sprites():
            if raindrop.check_edges():
                #self.raindrops.remove(raindrop)# move to top instead of removing
                raindrop.rect.bottom = 0
                raindrop.y = raindrop.rect.y
                
if __name__ in'__main__':
    ai = RaindropsGame()
    ai.run_game()

Images:

clouds.bmp

enter image description here

raindrop.bmp

enter image description here

Post a Comment for "Pygame - Image Smudging / Leaving A Trail On The Screen"