1

I don't understand why old image doesn't clearing and a new image layering over old image in debug

    import pygame
from setting import WIN_HEIGHT, WIN_WIDTH
from debug import debug
from pygame.locals import (
    K_ESCAPE,
    KEYDOWN
)

pygame.init()

# Rise text rectangular
def text_objects(text, font, color):
    text_surf = font.render(text, True, color)
    return text_surf, text_surf.get_rect()


# Make button
def button(screen, color, x, y):
    pygame.draw.rect(screen, color, (x, y, WIN_WIDTH/11, WIN_HEIGHT/11), 0, 15)

# Pause menu
def paused(main_txt):
    screen = pygame.display.get_surface()
    text = pygame.font.SysFont("comicsansms",115)
    text_surf, text_rect = text_objects(main_txt, text, 'black')
    text_rect.center = ((WIN_WIDTH/2), (WIN_HEIGHT/3))
    screen.fill((120, 120, 120))
    screen.blit(text_surf, text_rect)

    while paused:
        music = pygame.mixer.music
        music.pause()
        resume = button(screen, 'green', WIN_WIDTH/3, WIN_HEIGHT/1.95)
        exit = button(screen, (200, 0, 0), WIN_WIDTH/1.77, WIN_HEIGHT/1.95)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
                pygame.quit()

            elif (event.type == KEYDOWN and event.key == K_ESCAPE):
                music.unpause()
                return

        mouse = pygame.mouse.get_pos()
        debug(mouse)

        if WIN_WIDTH/3 + WIN_WIDTH/11 > mouse[0] > WIN_WIDTH/3 and WIN_HEIGHT/1.95 + WIN_HEIGHT/11 > mouse[1] > WIN_HEIGHT/1.95:
            button(screen, 'black', WIN_WIDTH/3, WIN_HEIGHT/1.95)
        else:
            resume

        pygame.display.update()
        pygame.time.Clock().tick(60)

The debug function is

    import pygame

pygame.init()
font = pygame.font.Font(None, 30)
def debug(info, y = 10, x = 10):
    disp_surf = pygame.display.get_surface()
    debug_surf = font.render(str(info), True, 'Red')
    debug_rect = debug_surf.get_rect(topleft = (x,y))
    disp_surf.blit(debug_surf, debug_rect)

enter image description here

  • If performance is not critical you can just `blit` the whole screen every frame and it should solve your problem. – Caridorc Jan 12 '23 at 23:12
  • 1
    Problem was not in the perfomance. Problem was that ```debug``` data was layering one onto another and data was unreadable. I found the problem. I have to paste ```screen.fill((120, 120, 120)) screen.blit(text_surf, text_rect)``` inside ```while``` loop – Володимир К Jan 12 '23 at 23:18
  • 1
    If you found a solution yourself you can post a self-answer to help future visitors of the site – Caridorc Jan 12 '23 at 23:19
  • Your goal is two move an image, but instead you are creating a new image in a different location, you have to make two `x` and `y` variables and change them –  Jan 14 '23 at 07:55

0 Answers0