解决Python编程中pygame包界面渲染出现未响应

在一次使用 pygame 包中的界面渲染中:

size = width,height = 600, 400
background = (255,255,255)
screen = pygame.display.set_mode(size, RESIZABLE)
img = pygame.image.load("./images/image1.png")
position = (50, 50)
while True:
    # 填充背景
    screen.fill(background)
    # 更新图像
    screen.blit(img, position)
    # 更新界面
    pygame.display.flip()

会出现点击窗口界面后无响应,导致渲染失败(如果有动态更新的情况下)。(上述只是简单示例)

后来经过摸索和搜索找到解决办法,即在 while 循环中加入下面代码即可。

for event in pygame.event.get():
    # 如果单击关闭窗口,则退出
    if event.type == pygame.QUIT:
        sys.exit()

全部代码如下

import pygame
import sys
from pygame.locals import *

pygame.init()
size = width,height = 600, 400
background = (255,255,255)
screen = pygame.display.set_mode(size, RESIZABLE)
img = pygame.image.load("./images/image1.png")
position = (50, 50)

while True:
    for event in pygame.event.get():
        # 如果单击关闭窗口,则退出
        if event.type == pygame.QUIT:
            sys.exit()
    # 填充背景
    screen.fill(background)
    # 更新图像
    screen.blit(img, position)
    # 更新界面
    pygame.display.flip()

一次编程实现过程中遇到的实际问题及解决办法。

iAmOnGoing

posted @ 2021-05-12 09:28  AlsoRan  阅读(609)  评论(0编辑  收藏  举报