Python 模块';pygame';没有';按键向下';成员
我是PyGame的新手,我只是想让它检测到按下的键B,但出错了 “模块'pygame'没有'KEYDOWN'成员”出现。 我尝试了很多其他的建议来解决其他的问题,但是没有一个对我有效Python 模块';pygame';没有';按键向下';成员,python,pygame,Python,Pygame,我是PyGame的新手,我只是想让它检测到按下的键B,但出错了 “模块'pygame'没有'KEYDOWN'成员”出现。 我尝试了很多其他的建议来解决其他的问题,但是没有一个对我有效 import pygame as Game import sys, time from random import randrange # Setup Variables Width = 500 Height = 500 Change_To = Direction # Initialize PyGame Che
import pygame as Game
import sys, time
from random import randrange
# Setup Variables
Width = 500
Height = 500
Change_To = Direction
# Initialize PyGame
Check_Errors = Game.init()
# Check for errors in initializing PyGame
if Check_Errors[1] != 0:
print("(!)There were {0} initializing errors,
EXITING...".format(Check_Errors[1]))
sys.exit
else:
print("PyGame initialized successfully!")
# Setup Game Board
Board = Game.display.set_mode([Width, Height])
Game.display.set_caption("Snake Game!")
# Setup Events
while True:
for Event in Game.event.get():
if Event.type == Game.KEYDOWN:
if Event.key == Game.K_UP:
Change_To = 'UP'
if Event.key == Game.K_LEFT:
Change_To = 'LEFT'
if Event.key == Game.K_DOWN:
Change_To = 'DOWN'
if Event.key == Game.K_RIGHT:
Change_To = 'RIGHT'
您发布的代码在我尝试它时可以正常工作,并且它会按预期报告KEYDOWN事件 您的Python模块名为
Game
顺便说一下,初始化错误检查中的sys.exit并不像您认为的那样。它应该是
sys.exit()
如果pygame安装可能已损坏,请尝试重新安装。我不知道为什么会这样。