模块' pygame'没有' KEYDOWN'会员

时间:2017-06-06 10:23:39

标签: python pygame

我是PyGame的新手,只是想让它检测到按下的键而不是错误 "模块' pygame'没有' KEYDOWN'构件"出现。 我已经为其他问题尝试了很多其他建议,但没有一个对我有用

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'

1 个答案:

答案 0 :(得分:0)

我发布的代码在我尝试时发布,并按预期报告KEYDOWN事件。

您的Python模块是否为Game

顺便说一下,初始化错误检查中的sys.exit不会按照您的想法执行。它应该是sys.exit()