1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Bliting a pause screen makes the pygame so slow

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I have made a simple Chess Game that can be paused. When you hit ESC it will pause the game and when you hit it again, it will unpause, that simple. And the problem is when i pause the game, the image that i am blitting needs to kind of fade in and out, and it fades in so slow and i am not using any sleep function either, not even in unpause, but it fades out still kind of slow when unpause.

    The image that I'm using

    Since the codes for the chess project was too long and even if i put it in here you would not be able to see what i'm seeing, i made a similar example which has the exact problem, in this one i even have the `time.sleep` commented out but it's still so slow:

    import pygame, sys, time
    pygame.init()
    screen = pygame.display.set_mode((800,800))
    pygame.display.set_caption("Test")
    WHITE, BLACK = (255,255,255), (0,0,0)
    paused, i = False, 0
    pauseImg = pygame.image.load('E:/My Apps/Python/Tuts/Tests/_projects/Chess Game/ChessGame/src/assets/textures/UI/Pause-Menu/pause-menu.png') # Use whatever image you want
    def main_loop():
    running = True
    while running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT: running = False
    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_ESCAPE: toggle_pause()
    if not paused:
    pass
    draw_screen()
    pygame.quit()
    sys.exit()
    def draw_screen():
    screen.fill(WHITE)
    draw_pause(screen)
    pygame.display.flip()
    def draw_pause(surface: pygame.Surface):
    global i
    pauseImg.set_alpha(i)
    # time.sleep(0.001) # Not activated, but still pretty slow
    i+=1 if paused and i >= 0 and i != 255 else -1 if not paused and i <= 255 and i != 0 else 0
    print(i)
    surface.blit(pauseImg, (0,0))
    def toggle_pause():
    if i != 0 and i != 255: return
    global paused
    paused = not paused
    if __name__ == '__main__': main_loop()


    I also tried to not to blit the other things and only the pauseImg but then it looked so horrible as i had to keep blitting the pauseImg with the new alpha value and its previous blit wasn't being disappeared. I even was going to use Cython and Numba but it didn't work as i wanted.

    Continue reading...

Compartilhe esta Página