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)
     except pygame.error:
         print 'Driver: {0} failed.'.format(driver)
     found = True
 if not found:
     raise Exception('No suitable video driver found!')
 size = [1920,1080]
 self.screen = pygame.display.set_mode(size,pygame.FULLSCREEN)


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

def test(self):
 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:
     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)
     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])
scope = pyscope()

Thus how can I avoid new text being rendered over older text?


It's drawing the text on top of the other text because that's exactly what you're doing. Pygame works by drawing the stuff on the screen, then actually showing it on the screen when you call flip() or update(). If you don't cover up the old number somehow, you're just drawing the new text right on top of the old text. In your example here, you should fill the entire screen with a solid color or draw a solid rect over where the the old text was before drawing the new text on top of that.

Also, don't call flip() so often, it doesn't make sense. flip() takes everything you've drawn to the screen surface and then actually displays it on the screen. If nothing has changed on the screen, calling flip() is a waste of resources.


Admittedly, I have not used pygame myself but after some searching, font.render appears to draw to a surface and all subsequent calls simply draw on top of the underlying surface.

I would suggest reading this enter link description here. I think the solution posited in the top answer (the one with the code examples) will work, but as the second responder suggests, there's probably a better way to do it such as only clearing a portion of the screen, perhaps using pygame.surface.subsurface. I believe you can define a subsurface to be a section of the parent surface. You can then simply fill the subsurface and the parent surface will be changed as well.

  • I concur. The easiest way to get around it would probably be to draw a rectangle over the old text and then draw the new text on top of that.
    – Fred
    Mar 19 '14 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.