我正在学习Python Crash课程,你们中的一些人可能看到了我的最新文章,我决定重做整个外星入侵项目,以便我更好地理解代码和代码的目的。
我正在使用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”没有“ 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为什么会说这些是错误?
感谢您抽出宝贵的时间阅读本文,期待您的答复。我相当活跃,所以如果您需要更多信息,我可以回复。
答案 0 :(得分:1)
解决方案:
这与“ pylint”有关,而不是您的代码。第三个:未使用的变量“屏幕”,它 这是因为您创建了变量“屏幕”,但尚未使用它。第一个和第二个,您可以通过配置来解决:
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"
您将收到“缺少模块文档字符串”和“缺少函数或方法文档字符串”的警告。
说明:
您为什么收到第一次和第二次警告?这是因为“ pylint”不能使C扩展模块掉毛。您可以参考this页以获取更多信息。 “ init()”函数位于pygame软件包下的“ base.cp38-win32.pyd”文件中,而“ pylint”不能掉毛。 “ QUIT”是相同的问题,它在“ constants.cp38-win32.pyd”文件中。 A C extension for CPython is a shared library (e.g. a .so file on Linux, .pyd on Windows)