在 pygame 中呈现在旧文本上的新文本 [英] new text rendered over older text in pygame

查看:51
本文介绍了在 pygame 中呈现在旧文本上的新文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 pygame 中编写了一个应用程序来显示一些文本.文本由一个计数器组成,该计数器每隔一秒左右更新一次.我正在为这个应用程序使用树莓派.因此,当我使用 xserver 时,一切都会正确显示,但是如果我使用 sdl_videodriver fbcon 进行显示,则静态文本会正确显示,但其值更改的计数器(文本)无法正确显示.counter 的新值显示在旧值之上,因此几秒钟后它变得不可读.以下是我的代码

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)

因此我的问题是如何避免在使用 sdl_videodriver 时在旧文本上呈现新文本?

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

推荐答案

update() 屏幕是不够的,你还应该用颜色清除"它.在进行任何 blitting/drawing 之前,请执行以下操作:

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.

这篇关于在 pygame 中呈现在旧文本上的新文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆