0

I am trying to make blocks fall to fall to the ground, but sadly the old sprites won't disappear. I have tried to move sprites with .move_ip, but then I get an error message, saying that my rect doesn't have attribute 'move_ip'. Also a little bit non-related, but how do I make the blocks stack on top of eachother? I suppose it's something to do with worldy?

import pygame
import random

pygame.init()
WHITE = (255, 255, 255)
worldx = 590
worldy = 770
screen = pygame.display.set_mode([worldx, worldy])
screen.fill([0,0,0])
gravity = 1
class Block1(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.image = pygame.image.load("img_12.png")
        self.rect = self.image.get_rect(topleft = (x,y))
        self.x = x
        self.y = y
        self.speed_y = 0
        
    def update(self):   
        self.speed_y += gravity
        self.y += self.speed_y
        self.rect.y = self.y
        if self.rect.y >= worldy:
            self.speed_y = 0

    
        
x = 0
y = 1
dt = 0
timer = 1
all_sprites_list = pygame.sprite.Group()
stopped_blocks = pygame.sprite.Group()
clock = pygame.time.Clock()

score = 0
block = Block1

mäng_töötab = True
while mäng_töötab:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mäng_töötab = False
     
    timer -= dt
    if timer <= 0:
        x = random.randrange(0, 18) * 32
        all_sprites_list.add(block(x,y))
        timer = 1
    all_sprites_list.draw(screen)
    all_sprites_list.update()        
    pygame.display.flip()
    dt = clock.tick(60) / 1000
    
pygame.quit()  
rind
  • 1
  • 1
  • 1
    you need to screen.fill every frame –  Dec 22 '21 at 02:08
  • Not sure, because then my blocks would disappear, but I want that after landing my blocks would stay where they landed. – rind Dec 22 '21 at 02:25
  • Every game loop you should draw the background, then your blocks, then your sprites. You can try an do a fill in the old position of the sprites, but that's too much hassle. – import random Dec 22 '21 at 02:44

1 Answers1

0

You have to clear the display in every frame:

while mäng_töötab:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mäng_töötab = False
     
    timer -= dt
    if timer <= 0:
        x = random.randrange(0, 18) * 32
        all_sprites_list.add(block(x,y))
        timer = 1

    screen.fill(0)                           # <---    

    all_sprites_list.draw(screen)
    all_sprites_list.update()        
    pygame.display.flip()
    dt = clock.tick(60) / 1000

The typical PyGame application loop has to:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174