即使在调用pygame.display.update()之后,Pygame显示也不会更新

时间:2020-05-04 03:01:09

标签: python pygame

我正在尝试在pygame中创建一个简单的程序,在该程序中将显示可引至您鼠标的行,但是即使我调用pygame.display.update(),这些行也不会消失。整个屏幕开始充满线条。这是代码:

import pygame
pygame.init()

win = pygame.display.set_mode((720,360))
pygame.display.set_caption("Random Code")

run = True
font = pygame.font.SysFont("javanesetext",30,True,False)#Bold = True, Italic = False
text = font.render("Hi",1,(255,0,0))
clock = pygame.time.Clock()

mousePos = pygame.mouse.get_pos()
def redrawGameWindow():
    win.blit(text,(36,36))
    pygame.draw.line(win,(255,255,255),(mousePos[0],0),(mousePos[0],360))


while run:
    mousePos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redrawGameWindow()
    pygame.display.update()

能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

win.fill((0, 0, 0))添加到您的redrawGameWindow()函数中以在屏幕上重新绘制背景:

def redrawGameWindow():
    win.fill((0, 0, 0))
    win.blit(text,(36,36))
    pygame.draw.line(win,(255,255,255),(mousePos[0],0),(mousePos[0],360))

答案 1 :(得分:0)

问题是您需要重新绘制屏幕背景。 pygame.display.update()仅添加到屏幕,不会清除并绘制。因此,您需要自己使用win.fill((0,0,0))清除它。这会将整个屏幕绘制成黑色,以便您可以再次在其上绘制

def redrawGameWindow():
    win.fill((0,0,0))
    win.blit(text,(36,36))
    pygame.draw.line(win,(255,255,255),(mousePos[0],0),(mousePos[0],360))