new text rendered over older text in pygame

2019-07-27 08:05发布

问题:

I wrote an application in pygame to display some text. The text consist of a counter which is updated every second or so. I am using raspberry pi for this application. So when I use xserver then everything is displayed correctly but if I use sdl_videodriver fbcon for display then static text is displayed correctly but the counter(text) whose value changes is not displayed correctly. The new value of counter is displayed over the older value and thus after few seconds it becomes unreadable. Following is my code

class pyscope :

 def __init__(self):
    disp_no = os.getenv("DISPLAY")
    if disp_no:
        print "I'm running under X display = {0}".format(disp_no)
    drivers = ['fbcon', 'directfb', 'svgalib']
    found = False
    for driver in drivers:
        if not os.getenv('SDL_VIDEODRIVER'):
            os.putenv('SDL_VIDEODRIVER', driver)
        try:
            pygame.display.init()
        except pygame.error:
            print 'Driver: {0} failed.'.format(driver)
            continue
        found = True
        break
    if not found:
        raise Exception('No suitable video driver found!')
    size = [1920,1080]
    self.screen = pygame.display.set_mode(size,pygame.FULLSCREEN)

    self.screen.fill((0,0,0))
    pygame.font.init()
    pygame.display.update()

def __del__(self):
    "Destructor to make sure pygame shuts down, etc."

def test(self):
    pygame.display.set_caption("Test")
    done=False
    clock=pygame.time.Clock()
    font = pygame.font.SysFont("consolas", 34, True)
    frame_rate = 20
    count = 0
    while done==False:
        for event in  pygame.event.get():
            if event.type == pygame.QUIT:
                done=True
        high_score = 2270
        plan = 2100
        count = count + 1
        font = pygame.font.SysFont("consolas", 200, True)
        if count >100:
            count = 12
        output_string = "ACTUAL          %s" %count
        text = font.render(output_string,True,red)
        pygame.display.flip()
        self.screen.blit(text, [250,420])
        output1 = "random          %.2f" %(float(count)/100*100)
        text = font.render(output1,True,red)
        self.screen.blit(text, [250,540])
        pygame.display.flip()
        clock.tick(20)
        pygame.display.flip()
scope = pyscope()
scope.test()
time.sleep(10)

Thus my question how can I avoid new text being rendered over older text while using sdl_videodriver?

回答1:

It is not enough to update() the screen, you should also "clear" it with a color. Before any blitting/drawing, do:

self.screen.fill((0,0,0))

You currently only do this once, when initializing the application. This should be done on each frame, if you want to keep a fresh, new screen on each frame.



回答2:

It sounds like pygame is not clearing the area of the text counter. Since I don't have access to an raspberry pi I would suggest you to make sure you clear/update the area where the counter is rendered.



回答3:

Before blitting out the text, you could clear the screen area with pygame.draw.rect

pygame.draw.rect(self.screen,(0,0,0),text.get_rect())

Should work OK, as long as your background is a solid color.