Particles are lagging when they get near to the bottom of the display

Viewed 46

The code creates surfaces when the mouse button is clicked. A rectangle is drawn on every surface. When they appear, the falling process should look like something similar to gravity. They fall down and they're rotating.

The problem

So the problem is, that as soon as the surfaces get near the bottom of the display they start to flicker. I tested it with fewer surfaces, but it does not matter how much there are, they're just starting to flicker. I don't know what's happening.

import pygame, random
import sys

from pygame.locals import *
pygame.init()

clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 18)

screen_size = (1000,1000)
screen = pygame.display.set_mode(screen_size)
display = pygame.Surface((500,500))

rects = []
rect_info = []
mouse_pressed = False
fps_timer = 60

fps = str(int(clock.get_fps()))
fps_text = font.render(fps, 1, pygame.Color("coral"))

while True:
    display.fill((10,10,10))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_pressed = True
        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                mouse_pressed = False
    
    
    if mouse_pressed == True:
        for i in range(20):
            number = 3
            print(number/2)
            while number/2 != round(number/2):
                number = random.randint(10,20)
            print(number)
            mx,my = pygame.mouse.get_pos()
            mx,my = mx/2,my/2
            rects.append(pygame.Surface((number,number)))
            rect_info.append([mx,my,0,random.randint(20,40)*-1/12 -1,random.randint(0,1),number, random.randint(-20,20)/12])

    i = 0
    for rect in rects:
        rect.set_colorkey((0,0,0))
        pygame.draw.rect(rect,(255,255,255),(0,0,rect_info[i][5],rect_info[i][5]),2)
        rect_copy = pygame.transform.rotate(rect,rect_info[i][2])
        display.blit(rect_copy,(rect_info[i][0]- rect_copy.get_width()/2,rect_info[i][1] - rect_copy.get_height()/2))

        rect_info[i][1] += rect_info[i][3]
        rect_info[i][0] += rect_info[i][6]

        if rect_info[i][6] > 0:
            rect_info[i][2] -= 2
        else:
            rect_info[i][2] += 2

        rect_info[i][1] += rect_info[i][3]
        rect_info[i][3] += 0.1

        if rect_info[i][1] > 500:
            rects.remove(rect)
            rect_info.remove(rect_info[i])

        i += 1

    fps_timer -= 1
    if fps_timer <= 0:
        fps_timer = 60
        fps = str(int(clock.get_fps()))
        fps_text = font.render(fps, 1, pygame.Color("coral"))


    display.blit(fps_text,(10,10))
    
    screen.blit(pygame.transform.scale(display,screen_size),(0,0))
    pygame.display.update()
    clock.tick(60)

0 Answers
Related