python - 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()`
解决方案
您将立即退出主循环,并且您也没有绘制任何东西。试试这个:
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()
免责声明:我目前无法尝试,但它应该可以让您朝着正确的方向开始。
推荐阅读
- python - 水平居中单选按钮标签
- data-structures - O(n) 的时间复杂度
- swift - 如何在静态 UITableView 中获取嵌入式动态 UITableView 的高度
- python-3.x - python3 NameError 变量未定义
- arrays - 如何从数组中的对象添加新的键和值?
- javascript - 如何使用 Vue.js 根据当前日期/时间设置时间间隔
- python-3.x - 试图在 df 中添加 '- 字符
- java - 我的程序已停止,我不知道为什么
- android - 如何使用android房间(Kotlin)返回新插入的项目(行)ID?
- assembly - 我对汇编代码“Relative jump out of range”有疑问