Python pygame屏幕无法显示

Python pygame屏幕无法显示,python,pygame,surface,Python,Pygame,Surface,我在Python3(和pygame)中有以下代码,但是白色表面无法显示,我不明白为什么。这和它放在哪里有关吗?我试过缩进,但也没用?代码如下: import pygame from pygame.locals import* pygame.init() screen=pygame.display.set_mode((800,600)) # Variable to keep our main loop running running = True # Our main loop! whil

我在Python3(和pygame)中有以下代码,但是白色表面无法显示,我不明白为什么。这和它放在哪里有关吗?我试过缩进,但也没用?代码如下:

import pygame
from pygame.locals import*
pygame.init()

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


# Variable to keep our main loop running
running = True

# Our main loop!
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
        if event.type == KEYDOWN:
            # If the Esc key has been pressed set running to false to exit the main loop
            if event.key == K_ESCAPE:
                running = False
            # Check for QUIT event; if QUIT, set running to false
            elif event.type == QUIT:
                running = False

            # Create the surface and pass in a tuple with its length and width
            surf = pygame.Surface((50, 50))
            # Give the surface a color to differentiate it from the background
            surf.fill((255, 255, 255))
            rect = surf.get_rect()

            screen.blit(surf, (400, 300))
            pygame.display.flip()

所以看起来你的缩进是错误的

您需要在事件循环之外定义曲面和更新屏幕等

至少您必须将
screen.blit(surf,(400300))
pygame.display.flip()移动到事件循环之外

这是固定的:

# Our main loop!
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
        if event.type == KEYDOWN:
            # If the Esc key has been pressed set running to false to exit the main loop
            if event.key == K_ESCAPE:
                running = False
            # Check for QUIT event; if QUIT, set running to false
            elif event.type == QUIT:
                running = False

    # Create the surface and pass in a tuple with its length and width
    surf = pygame.Surface((50, 50))
    # Give the surface a color to differentiate it from the background
    surf.fill((255, 255, 255))
    rect = surf.get_rect()

    screen.blit(surf, (400, 300))
    pygame.display.flip()

所以看起来你的缩进是错误的

您需要在事件循环之外定义曲面和更新屏幕等

至少您必须将
screen.blit(surf,(400300))
pygame.display.flip()移动到事件循环之外

这是固定的:

# Our main loop!
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
        if event.type == KEYDOWN:
            # If the Esc key has been pressed set running to false to exit the main loop
            if event.key == K_ESCAPE:
                running = False
            # Check for QUIT event; if QUIT, set running to false
            elif event.type == QUIT:
                running = False

    # Create the surface and pass in a tuple with its length and width
    surf = pygame.Surface((50, 50))
    # Give the surface a color to differentiate it from the background
    surf.fill((255, 255, 255))
    rect = surf.get_rect()

    screen.blit(surf, (400, 300))
    pygame.display.flip()

你有错误吗?“我试过缩进,但也没用?”-是的,不要那样做。随机缩进和去缩进你的代码,对你没有帮助。叶子-你的评论也没有太大的帮助。我的意思是对与曲面图相关的代码进行缩进。特里斯坦-不,没有错误。只是一片空白的黑色screen@pythoncarrot但窗口不显示的全部原因是缩进。不正确。这个程序运行得很好。(我可能将其粘贴到stackoverflow错误,因为它弄乱了我的缩进)。只是当我添加曲面位时,它不会绘制。注意上面的编辑-当这样使用时-会显示一个黑屏,但不会显示所需的曲面。您是否收到错误?“我尝试了缩进,但也不起作用?”-是的,不要这样做。随机缩进和去缩进你的代码,对你没有帮助。叶子-你的评论也没有太大的帮助。我的意思是对与曲面图相关的代码进行缩进。特里斯坦-不,没有错误。只是一片空白的黑色screen@pythoncarrot但窗口不显示的全部原因是缩进。不正确。这个程序运行得很好。(我可能将其粘贴到stackoverflow错误,因为它弄乱了我的缩进)。当我添加曲面位时,它不会绘制。注意上面的编辑-当这样使用时-会显示一个黑屏,但不会显示所需的曲面。