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()