为什么 PyGame 中的屏幕不刷新?

Y-M*_*Y-M 3 pygame

我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示一个表示鼠标在屏幕上位置的字符串。

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,400),0,32)
myFont = pygame.font.SysFont('arial', 14)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    x,y = pygame.mouse.get_pos()
    label = myFont.render('mouse coords: ' + str(x) + ', ' + str(y), 1, (0,128,255))

    screen.blit(label, (10,10))
    pygame.display.update()
Run Code Online (Sandbox Code Playgroud)

当我四处移动鼠标时,标签会变得模糊,直到文本无法阅读。我确信我正确调用了 screen.blit() 和 pygame.display.update() ,但标签似乎没有更新!任何帮助都会很棒。

Ser*_*ial 5

你需要做的是在循环中位图背景,因为我们正在做的是将鼠标坐标位图一个在另一个之上

做这样的事情:

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,400),0,32)
myFont = pygame.font.SysFont('arial', 14)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    x,y = pygame.mouse.get_pos()
    label = myFont.render('mouse coords: ' + str(x) + ', ' + str(y), 1, (0,128,255))
    screen.fill((0,0,0))
    screen.blit(label, (10,10))
    pygame.display.update()
Run Code Online (Sandbox Code Playgroud)

这样,您可以在每次更新之间用黑色填充屏幕,因此鼠标 pos 被 blitted,然后被填充清除,然后新的 pos 被 blitted 等等