python - pygame - updating score box -


I am learning to use pygame and I have some problem with providing text.

Simplified code I'm working with, like this:

  import pygame sizex = 200; Sizey = 200 pygame.init () clock = pygame.time.Clock () screen = pygame.display.set_mode ((size, sizey)) myfont = pygame.font.font (none, 32) score = pygame.Rect ( 100,100,100, 50) screen.fill ((255,255,255) pygame.draw.rect (screen, (0,250,0), (10,10,10,10), 2) pygame.display.update () in xrange i (0 , 1000): msElapsed = clock.tick (2) text = "I =% d"% i label = myfont.render (text, 1, (0,0,250)) screen. Blit (label, (100, 100)) pygame.display.update (score)   

I just have to update the part of the screen that contains the textbox but this code is not doing this . The texts themselves overwrite and become unreadable after a while.

What am I doing?

The problem is that when you blow a text, the background of the text is transparent (note That's important for example when you want to keep the background image). You must first "clear" the rectangle, and then the text must be fluffed. In your case, as you are using plain colors in the background, you can do this:

  In the xrange (0,1000): msElapsed = clock.tick (2 ) Text = "I =% D"% i label = myfont.render (text, 1, (0,0,250)) screen. Fill ((255,255,255), rect = label.get_rect (topleft = (100,100))) screen. Bleet (label, (100, 100)) pygame.display.update (score)   

If you use an image instead of a solid color, you will see the correct area of ​​the background Will have to reproach

Edit:

As indicated by Bartlomiej, if the new string is a sorter compared to the previous, it will not be completely erased . You can clean the entire screen every time, or try to make it clear how to clean it, for example, to store the area of ​​the previous blit:

  previous_rect = none I in xrange (0,1000): msElapsed = clock.tick (2) text = "I =% d"% i if prev_rect: screen.fill ((255,255,255), rect = prev_rect) label = myfont.render (text, 1, (0,0,250)) previous_rect = label.get_rect (topleft = (100,100)) screen. Blit (label, (100, 100)) pygame.display.update (score)    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -