【问题标题】:Pygame background not blittingPygame背景没有blitting
【发布时间】:2011-12-31 16:49:05
【问题描述】:

我只是在学习使用 python 3.2 和 pygame 1.8 进行 GUI 编程。据我了解,以下代码应显示白色背景。但是,我得到的只是黑屏。另外,如果这很重要,我会在 Windows 7 中使用 IDLE:

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
screen.blit(background, (0, 0))

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您必须有一个更新屏幕的主循环。这段代码应该可以工作:

    import pygame
    
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    
    background = pygame.Surface(screen.get_size())
    background.fill((255, 255, 255))
    
    while True:
        screen.blit(background, (0, 0))
        pygame.display.update()
    

    【讨论】:

    • 我现在得到这个错误:在第 11 行 screen.update() AttributeError: 'pygame.Surface' object has no attribute 'update'
    • @Ahmed - 我假设您的意思是在评论中,“添加评论”按钮下方有一个帮助链接,它解释了comment formatting。对于code formatting,请将代码放在`backticks` 中。
    【解决方案2】:

    你忘记更新屏幕了。

    import pygame
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    background = pygame.Surface(screen.get_size())
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))
    
    pygame.display.update()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    

    【讨论】:

      【解决方案3】:

      您不会对背景进行 blit,而是将填充部分放入循环中。你也忘了pygame.display.update()

      【讨论】:

        猜你喜欢
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多