错误:模块“pygame”没有“init”成员,其他

2024-09-28 13:15:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python速成课程,你们中的一些人可能已经看到了我最后的帖子,我决定重做整个外星入侵项目,以便更好地理解代码和代码的用途

我使用的是VScode,我已经安装了基本的Python扩展,还有Python(PyDev)扩展

If you click here,你可以看到我正在读的那本书,但我确信书中提到的语法是正确的

我在这本书的第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”没有“退出”成员

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

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

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

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说这些是错误

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


Tags: the代码importeventgamefordisplaysys
1条回答
网友
1楼 · 发布于 2024-09-28 13:15:43

解决方案:

这是关于“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的默认设置,您可以参考this页:

" 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"

您将得到“缺少模块docstring”和“缺少函数或方法docstring”警告


说明:

你为什么会得到第一次和第二次警告?这是因为“pylint”不能lint C扩展模块。有关详细信息,请参阅this页。“init()”函数位于pygame包下的“base.cp38-win32.pyd”文件中,“pylint”无法将其lint。“退出”也是同样的问题,它位于“constants.cp38-win32.pyd”文件中。 A C extension for CPython is a shared library (e.g. a .so file on Linux, .pyd on Windows)

相关问题 更多 >

    热门问题