首页 > 解决方案 > Python + Pygame;screen.fill() 崩溃窗口

问题描述

每当我尝试在我的 pygame 代码中包含“screen.fill(在此处插入值)”时,窗口都会立即崩溃。我可以把它注释掉,我的代码工作得很好。我真的不知道为什么。

我试过移动线路,部分使用其他人的代码 - 没有运气。

import pygame
pygame.init()

win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")

img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
win.blit(img,(450, 150))

running = True
while running:
    screen.fill(0, 0, 0)
    pygame.display.update()
    pygame.time.delay(100)

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

pygame.quit()

当我运行包含该行的代码时,窗口会打开,然后立即关闭。

标签: pythonbackgroundpygamescreenfill

解决方案


如果您尝试用黑色填充屏幕,则必须将颜色作为元组传递:

win.fill((0, 0, 0))

另外,您从不分配screen,也许您打算使用win

Surface.fill的文档。


推荐阅读