【问题标题】:Pygame WINDOWRESIZED black screenPygame WINDOWRESIZED黑屏
【发布时间】:2022-11-18 13:40:57
【问题描述】:

我正在尝试调整 pygame 中的窗口大小,但只会出现黑屏。请参阅下面的前后图片。我究竟做错了什么?


import pygame as pg
from pygame.locals import *

pg.init()

yellow = (255, 255, 134)
grey = (142, 142, 142)

square_size = 100
width = 7 * square_size
height = 7 * square_size
radius = int(square_size / 2 - 10)

screen = pg.display.set_mode((width, height), RESIZABLE)

screen.fill(grey)

pg.draw.circle(screen,yellow,(square_size,square_size),radius)

pg.display.flip()

while True:
    for ev in pg.event.get():
        if ev.type == pg.QUIT:
            print("quit game")
            pg.quit()
            sys.exit()
        if ev.type == pg.WINDOWRESIZED:
            width, height = screen.get_width(), screen.get_height()
    pg.display.flip()

【问题讨论】:

    标签: python pygame resize


    【解决方案1】:

    调整窗口大小后需要重新绘制场景。我建议在每一帧中重新绘制场景。典型的 PyGame 应用程序循环必须:

    import sys
    import pygame as pg
    from pygame.locals import *
    
    pg.init()
    
    yellow = (255, 255, 134)
    grey = (142, 142, 142)
    
    square_size = 100
    width = 7 * square_size
    height = 7 * square_size
    radius = int(square_size / 2 - 10)
    
    screen = pg.display.set_mode((width, height), RESIZABLE)
    clock = pg.time.Clock()
    
    run = True
    while run:
    
        # limit the frames per second 
        clock.tick(100)
    
        # handle the events
        for ev in pg.event.get():
            if ev.type == pg.QUIT:
                print("quit game")
                run = False
            if ev.type == pg.WINDOWRESIZED:
                width, height = screen.get_width(), screen.get_height()
        
        # clear display
        screen.fill(grey)
    
        # draw scene
        pg.draw.circle(screen,yellow,(square_size,square_size),radius)
    
        # update the display
        pg.display.flip()
    
    pg.quit()
    sys.exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      相关资源
      最近更新 更多