首页 > 解决方案 > Pygame 窗口打不开

问题描述

我对 Python 完全陌生,我刚刚开始学习 Pygame。我现在正在尝试绘制形状,但我无法打开窗口。我知道问题是什么,这是我的代码:

`#import libraries
import pygame
import math
 #init pygame
 pygame.init()

 #clock
 clock = pygame.time.Clock()

 #window screen

screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))

#RGBA colors
 Black = (0, 0, 0)
 White = (255, 255, 255)
 Blue = (0, 0, 255)
 Red = (255, 0, 0)
 Green = (0, 255, 0)

 #program
 running = True
 while running:
     for event in pygame.event.get():
       running = False

screen.fill(Black)
pygame.display.update()
clock.tick(30)
 #Quit
pygame.quit()
quit()`

标签: pythonpygame

解决方案


您将立即退出主循环,并且您也没有绘制任何东西。试试这个:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(Black)
    pygame.display.update()
    clock.tick(30)

#Quit
pygame.quit()
quit()

免责声明:我目前无法尝试,但它应该可以让您朝着正确的方向开始。


推荐阅读