python - Pygame 窗口没有响应,但程序继续运行
问题描述
我是一个相当新的使用 python 的爱好者。我安装 Pygame 已经一周了,经过多次尝试,当我运行任何程序时,仍然无法启动或打开 Pygame 窗口。我已经尽可能多地观看了教程,并阅读了我能找到的所有关于类似问题的文章。我已经复制了在其他网站上找到的所有解决方案,但问题仍然存在。尽管如此,奇怪的是,我使用的 IDE (Pycharm) 很少输出错误消息,而是继续运行但从不启动 Pygame 窗口。我正在使用安装了 python 3.8.1 和 Pygame 版本 1.9.6 的 Pycharm。我正在使用带有 High Sierra 的 Mac。
我非常感谢任何人可以提供的任何帮助。
下面的代码只输出 pygame 版本和“欢迎”消息,但在没有启动窗口的情况下继续运行。
import pygame
(width, height) = (1000, 700)
screen=pygame.display.set_mode((width, height))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
下一个代码块的输出与上面的完全相同。
# courtesy of Ene Uran www.daniweb.com
import pygame as pg
pg.init()
screen = pg.display.set_mode((400, 300))
pg.display.set_caption('Draw/fill rectangles using pygame')
white = 0xFFFFFF
red = 0xFF0000
green = 0x00FF00
blue = 0x0000FF
yellow = 0xFFFF00
screen.fill(white, (250, 50, 77, 33))
screen.fill(red, (30, 20, 70, 120))
screen.fill(red, (140, 70, 90, 80))
screen.fill(green, (150, 80, 70, 60))
screen.fill(yellow, (200, 170, 150, 60))
screen.fill(blue, (70, 200, 100, 70))
pg.display.update()
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
raise SystemExit
以下代码块也返回相同的输出:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 24)
#clock = pygame.time.Clock()
#font = pygame.font.Font(None, 32)
cycles = 0
while True:
pygame.event.get()
screen.fill(0)
# text = font.render('Cycles : %d' % cycles, True, (255, 255, 255))
# screen.blit(text, (100, 100))
cycles += 1
pygame.display.update()
此代码^^ 来自此问题的原始堆栈溢出论坛:Pygame 窗口在几秒钟后没有响应
解决方案
pygame 1.9.6
不适python 3.8.1
用于mac os。
有关详细信息,请参阅https://github.com/pygame/pygame/issues/555。
尝试安装 pygame 开发版本,例如 pygame 2.0.0.dev6:
pip3 install pygame==2.0.0.dev6
但要注意错误。
推荐阅读
- java - Spring Boot mongoTemplate 计数查询很慢
- html - 使用 vh 避免在 auto-width max-height flex CSS 布局中出现 letterboxing
- flutter - 如何知道 animated_text_kit 包中当前正在播放的文本?
- python - 如何将变量从url传递给方法
- postgresql - 在 Postgres Aurora 上具有索引和哈希分区的 DDL
- python - 如何根据两个或多个字段在 Solr 中使用“fq”过滤文档?
- android - 使用活页夹在 NavigationView 标题中设置文本
- ruby-on-rails - gem install eventmachine -v '1.2.7' 错误
- symfony - Encore Webpack - 使用 copy-webpack-plugin 复制文件夹结构
- c++ - 模板导致类方法的链接器错误