簡體   English   中英

Pygame WINDOWRESIZED黑屏

[英]Pygame WINDOWRESIZED black screen

我試圖在 pygame 中調整 window 的大小,但只得到黑屏。 請參閱下面的前后圖片。 我究竟做錯了什么?


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

前

后

調整window后需要重繪場景,我推薦每一幀都重繪場景。 典型的 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