gpt4 book ai didi

pygame - 为什么屏幕在 PyGame 中不刷新?

转载 作者:行者123 更新时间:2023-12-05 02:24:00 31 4
gpt4 key购买 nike

我对 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()

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

最佳答案

你需要做的是在循环中 blit 一个背景,因为我们正在做的是将 mousecoords 一个一个地 blit 到彼此之上

做这样的事情:

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()

这样你在每次更新之间用黑色填充屏幕,所以鼠标位置被 blit,然后它被填充清除,然后新的位置被 blit 等等

关于pygame - 为什么屏幕在 PyGame 中不刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393287/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com