繁体   English   中英

错误:模块 'pygame' 没有 'init' 成员,以及其他

[英]Errors: Module 'pygame' has no 'init' member, and others

我正在关注 Python 速成课程,你们中的一些人可能已经看过我的上一篇文章,我决定重做整个外星入侵项目,以便更好地理解代码和代码的目的。

我正在使用 VScode,并且我已经安装了基本的 Python 扩展,并且我还安装了 Python (PyDev) 扩展。

如果你点击这里,你可以看到我正在关注的书,但我确信书中提到的语法是正确的。

我在这本书的第 241 页。 这是代码:

import sys
import pygame

def run_game():
    # Initialize the game and create a screen object
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")

    # Start the main loop for the game.
    while True:

        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Make the most recently drawn screen visible.
        pygame.display.flip()

run_game()

该代码似乎在终端上运行良好,但它说我的代码有 3 个问题。

第一个是:模块'pygame'没有'init'成员

第二个:模块'pygame'没有'QUIT'成员

第三个:未使用的变量“屏幕”

我知道第三个问题更像是一个警告,所以忽略它(我仍在展示它,因为它可能与问题有关)

如您所见,终端反馈很好:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\holca\Desktop\alien_invasion 2> & C:/Users/holca/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/holca/Desktop/alien_invasion 2/alien_invasion2.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
PS C:\Users\holca\Desktop\alien_invasion 2> 

为什么 IDE 说这些是错误?

感谢您花时间阅读本文,期待回复。 我相当活跃,所以如果您需要更多信息,我可以回复。

解决方案:

这是关于“pylint”的东西,而不是你的代码。 第三个:未使用的变量screen,是因为你创建了变量screen,但没有使用它。 第一个和第二个,可以通过配置解决:

Open settings.json file add these settings:
"python.linting.pylintArgs": [
    "--disable=all",
    "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
    "--unsafe-load-any-extension=y", //or add this one: --extension-pkg-whitelist=pygame
  ],

这些设置是pylint的默认设置,可以参考这个页面:

"--disable=all",
"--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",

如果你只添加

"--unsafe-load-any-extension=y"

或者

"--extension-pkg-whitelist=pygame"

您将收到“缺少模块文档字符串”和“缺少 function 或方法文档字符串”警告。


解释:

你为什么得到第一个和第二个waring? 这是因为“pylint”无法对 C 扩展模块进行 lint。 您可以参考页面了解更多信息。 "init()" function 在 "base.cp38-win32.pyd" 文件中,在 pygame ZEFE90A8E604A7C840E88D03A67F6B7PyD8Z 下,可以。 “QUIT”是同样的问题,它在“constants.cp38-win32.pyd”文件中。 CPython 的 C 扩展是一个共享库(Linux 上的 egaso 文件,Windows 上的 .pyd)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM