【问题标题】:Pygame Window not Loading ImagePygame 窗口不加载图像
【发布时间】:2020-03-23 13:39:15
【问题描述】:

今天是我pygame 的第一天,我不明白为什么这段代码不起作用,pygame 窗口是黑色的没有响应,也没有显示图像

import pygame
pygame.init()


screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])
screen.fill((255,255,255))

Quit=input("Press'Y' is you want to quit")

if Quit == "Y":
    pygame.display.quit()






Board = pygame.image.load("TicTacToeBoard.jpg")

screen.blit(Board,(0,0))

pygame.display.flip()

【问题讨论】:

  • 你缺少一个事件循环;就是这样。

标签: python pygame


【解决方案1】:

所有 PyGame 程序都有一个事件循环。这是一个持续循环,接受来自窗口管理器/操作环境的事件。事件是鼠标移动、按钮点击和按键等事件。如果您的程序不接受事件,最终启动程序会认为它已停止响应并可能提示用户终止它。

您现有的代码从控制台获取输入。如果您使用线程,这可以在 PyGame 中完成,然后将事件发布回主循环。但通常将退出作为事件处理更容易。在下面的代码中,我处理了 QUIT 事件的退出,然后按 Q

import pygame

pygame.init()
screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])

Board = pygame.image.load("TicTacToeBoard.jpg")
clock = pygame.time.Clock()

# Main Event Loop
exiting = False
while not exiting:

    # Handle events
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            exiting = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            mouse_pos = pygame.mouse.get_pos()
            print( "Mouse Click at "+str( mouse_pos ) )
        elif ( event.type == pygame.KEYUP ):
            if ( event.key == pygame.K_q ):
                # Q is quit too
                exiting = True    

    # Paint the screen
    screen.fill((255,255,255))
    screen.blit(Board,(0,0))
    pygame.display.flip()

    # Limit frame-rate to 60 FPS
    clock.tick_busy_loop(60)

此外,此代码还将帧速率限制为 60 FPS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多