为什么未显示“游戏结束”文本?

时间:2019-06-03 16:47:42

标签: python pygame

我有此代码,当我单击 ESC 时,看不到“游戏结束”。该程序等待两秒钟,然后关闭而不显示文本。 Pygame 1.9.6

我在做什么错了?

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -block_size
                lead_y_change = 0
            elif event.key == pygame.K_RIGHT:
                lead_x_change = block_size
                lead_y_change = 0
            elif event.key == pygame.K_UP:
                lead_x_change = 0
                lead_y_change = -block_size
            elif event.key == pygame.K_DOWN:
                lead_x_change = 0
                lead_y_change = block_size
            elif event.key == pygame.K_ESCAPE:
                run = False

    game_display.fill(white)
    lead_x += lead_x_change
    lead_y += lead_y_change
    pygame.draw.rect(game_display, black, [lead_x, lead_y, width, height])
    pygame.display.update()

    clock.tick()

draw_text_middle("Game Over", 40, (0, 0, 0, 255), game_display)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()

1 个答案:

答案 0 :(得分:2)

您必须从事件循环中删除const o = { a: 'a', b: 'b', obj: { key: 'key', }, } const o2 = o o2.a = 'new value' // o and o2 reference the same object console.log(o.a) // this shallow-copies o into o3 const o3 = Object.assign({}, o) // deep copy function deepCopy(obj) { // check if vals are objects // if so, copy that object (deep copy) // else return the value const keys = Object.keys(obj) const newObject = {} for (let i = 0; i < keys.length; i++) { const key = keys[i] if (typeof obj[key] === 'object') { newObject[key] = deepCopy(obj[key]) } else { newObject[key] = obj[key] } } return newObject } const o4 = deepCopy(o) o.obj.key = 'new key!' console.log(o4.obj.key) 调用。 pygame.quit()取消初始化所有pygame模块。调用此函数后,无法绘制任何内容,对任何pygame指令的进一步调用都会导致异常。
我建议在pygame.event.pump()应用程序之前通过.delay处理pygame事件。 pygame可以处理内部动作:

pygame.quit()