2

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)

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Levi
  • 51
  • 5
  • It seems weird to me that you're creating new surfaces. A surface is an image in which you can blit at different positions. So create 10 surfaces at the beginning (as that's how many unique surfaces you actually use), and then blit them multiple times instead of creating many duplicate surfaces (as they are quite expensive). – Ted Klein Bergman Aug 17 '21 at 16:54
  • is this solving my problem ? – Levi Aug 17 '21 at 17:07
  • @Levi try and see for yourself, it's not that hard to test – Matiiss Aug 17 '21 at 21:42
  • I've done it and it didn't solved my Problem. It increased the speed of the code. around 20 fps, but my Problem is still there. I notices that the Problem starts as soon as a object gets removed from the list. – Levi Aug 18 '21 at 09:49
  • @Levi Sorry, but I cannot reproduce the problem. (My system seems to be performing too well) – Rabbid76 Aug 18 '21 at 11:15
  • 1
    Alright. I integrated an FPS counter, so it's not the performance. I think the problem is unsolvable or something like that. Thanks anyway! – Levi Aug 18 '21 at 11:33
  • Every problem is solvable because you created it in a part of the code. So you can find another way to do what the code which causes a problem does. – D_00 Sep 03 '21 at 08:21

0 Answers0