首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让pygame在OS X Big Sur安装上访问它的模块(例如,字体、图形)?

问如何让pygame在OS X Big Sur安装上访问它的模块(例如,字体、图形)?
EN

Stack Overflow用户
提问于 2021-06-28 03:37:44
回答 1查看 108关注 0票数 0

我设法在Big Sur上安装了Pygame。但是当我在其中使用模块时,它就失败了。我有一台新的运行Big Sur (11.4)的M1笔记本电脑,我安装了homebrew,python3,pip3,然后是Pygame。

如果我运行这个框架:

代码语言:javascript
复制
import sys

import pygame
from pygame.locals import *

pygame.init()

fps = 60
fpsClock = pygame.time.Clock()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

is_blue = True

# Game loop.
while True:
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_blue = not is_blue

    # Update.

    # Draw.
    if is_blue: color = (0, 128, 255)
    else: color = (255, 100, 0)
    pygame.draw.rect(screen, color, pygame.Rect(30, 30, 60, 60))

    pygame.display.flip()
    fpsClock.tick(fps)

它工作得很好。但是如果我在init后面添加下面这一行:

代码语言:javascript
复制
myfont1 = pygame.font.SysFont("Comic Sans MS", 20)

我得到以下错误消息:

代码语言:javascript
复制
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
/Users/lmiratrix/PycharmProjects/pythonProject1/main_v2.py:8: RuntimeWarning: use font: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py)
(ImportError: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py))
  myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
Traceback (most recent call last):
  File "/Users/lmiratrix/PycharmProjects/pythonProject1/main_v2.py", line 8, in <module>
    myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
  File "/opt/homebrew/lib/python3.9/site-packages/pygame/__init__.py", line 59, in __getattr__
    raise NotImplementedError(missing_msg)
NotImplementedError: font module not available (ImportError: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py))

我不确定如何继续(我希望这不是宇宙告诉我漫画是不可能的!:-)

背景:我真的很难让PyGame一开始就能正常工作,结果在PyCharm中出现了奇怪的错误消息。在发现homebrew会在/opt/homebrew/而不是/usr/local/bin或类似目录中安装所有内容(包括新的python3 )后,我转到了终端,但PyCharm只在/usr/bin中找到了一小段时间的python安装。我尝试卸载Pygame,然后使用spotlight从计算机中删除所有不同的Pygame文件,重新安装所有文件,然后部分成功地登录到这里。

EN

回答 1

Stack Overflow用户

发布于 2021-06-28 07:08:58

根据这两个github问题(#2150,#2500),这个问题可能有几个原因。

首先,我发现您正在运行Python 3.9.5,其中一个问题指出在某些机器上运行较新版本的python和pygame可能会导致问题,因此请尝试使用较旧版本的python (如3.7或3.8)。

其次,你可能会因为pygame的不兼容问题而得到这个错误。上面提到的pygame与M1 Mac不兼容的问题之一,我预计在Big Sur安装上也会出现类似的问题。在上面的github问题中,这个问题已经通过使用PyGame2.0.0版本得到了解决,你可以使用pip install pygame==2.0.0.dev6 --no-cache-dir获得。

您的错误消息还提到循环导入,因此请确保不会有多个文件相互导入。如果您仍然有错误,请查看链接的github问题以找到更多解决方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68154676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档