1

したがって、基本的には、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 を抜けて残りのコードを実行することです。

すべて問題ないように見えますが、問題は何も表示されないことです。本来のように黒い全画面ウィンドウが表示されますが、白いテキストは表示されません。私は何を間違っていますか?

4

1 に答える 1

3

pygame.displayメモリ (バッファ) にあるものを作成し、すべてscreenこのサーフェスに描画します。画面/モニターでこのサーフェス/バッファーを使用または送信する必要があります。surfaceblitdisplay.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()
于 2016-12-25T19:27:50.087 に答える