简体   繁体   English

Pygame WINDOWRESIZED黑屏

[英]Pygame WINDOWRESIZED black screen

I'm trying to resize a window in pygame but only get a black screen.我试图在 pygame 中调整 window 的大小,但只得到黑屏。 See the before and after pictures below.请参阅下面的前后图片。 What am I doing wrong?我究竟做错了什么?


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()

前

后

You need to redraw the scene after resizing the window. I recommend redrawing the scene in each frame.调整window后需要重绘场景,我推荐每一帧都重绘场景。 The typical PyGame application loop has to:典型的 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM