zoukankan      html  css  js  c++  java
  • 解决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

  • 相关阅读:
    hive之窗口函数
    linux环境下Mysql的卸载和重新安装和启动
    2017ACM暑期多校联合训练
    2017ACM暑期多校联合训练
    状态压缩dp
    铺砖问题 (状态压缩dp)
    POj 2104 K-th Number (分桶法+线段树)
    POJ 2991 Crane (线段树)
    4 Values whose Sum is 0 POJ 2785 (折半枚举)
    Billboard HDU 2795 (线段树)
  • 原文地址:https://www.cnblogs.com/ache/p/14758403.html
Copyright © 2011-2022 走看看