Python/Pygame,标签未显示

标签 python pygame pygame-surface

所以基本上我只是想在 python 中使用 pygame 做一些事情。这是代码的一部分,其余代码不会影响这一点,所以我

from pygame import *
from pygame.locals import *
import pygame
from sys import exit
from random import *
import time

pygame.init()
font.init()

screen = display.set_mode((1920, 1080), FULLSCREEN)
screen.fill((0, 0, 0))

countTime = 1
while countTime < 4:
    default_font = pygame.font.get_default_font()
    font_renderer = pygame.font.Font(default_font, 45)
    label = font_renderer.render(str(countTime).\
            encode('utf-8'), 1, (255, 255, 255))
    screen.blit(label, (1920/2, 1080/2))
    countTime += 1
    time.sleep(1)
正如你所看到的,它的目的是创建一个全屏窗口,其中只有一些字母“3”、“2”、“1”,然后再中断 while 并执行我的其余部分。代码。

一切看起来都很好,但问题是什么也没有显示。我只是得到一个黑色的全屏窗口,就像我想要的那样,但没有显示白色文本。我究竟做错了什么?

最佳答案

pygame.display 创建screen,它是内存(缓冲区)中的surface,并且所有blit都在此绘制表面。您必须使用 display.flip()display.update() 将此表面/缓冲区发送到屏幕/监视器上。

<小时/>

编辑:示例代码

import pygame

# --- constants --- (UPPER_CASE names)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# --- classes --- (CamelCase names)

# empty

# --- functions --- (lower_came names)

# empty

# --- main ---

# - init -

pygame.init()

screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()

# - objects -

default_font = pygame.font.get_default_font()
font_renderer = pygame.font.Font(default_font, 45)

# - mainloop -

count_time = 1
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        # close window with button `X`
        if event.type == pygame.QUIT: 
            running = False

        # close window with key `ESC`
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # --- updates (without draws) ---

    label = font_renderer.render(str(count_time), True, WHITE)
    label_rect = label.get_rect()
    # center on screen
    label_rect.center = screen_rect.center

    count_time += 1
    if count_time >= 4:
        running = False

    # --- draws (without updates) ---

    screen.fill(BLACK)
    screen.blit(label, label_rect)
    pygame.display.flip()

    # --- speed ---

    # 1000ms = 1s
    pygame.time.delay(1000) 

# - end -

pygame.quit()

关于Python/Pygame,标签未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323838/

相关文章:

python - 我不知道如何让我的用户输入他们的分数

python - 使用 Beam IO ReadFromPubSub 模块时,可以在 Python 中拉取带有属性的消息吗?不清楚是否支持

python - 防止 YAML 中的十六进制字符串在 Python 中加载为 float/int

python - 创建很多对象并删除它们

python - 使用 Pygame 在 Python 中制作动画

python - 如何判断 Convert()/convert_alpha() 是否已经在 pygame.Surface 上运行?

python - PyTorch:如何打印网络中每一层的输出 blob 大小?

python - Apache日志文件数据分析与python pandas

python - 使用 Pygame, Python 3 Blitting 非矩形 Sprite

python - 随机砖颜色 -> TypeError : 'pygame.Color' object is not callable