Pygame Key Constants未定义

时间:2015-05-28 01:36:55

标签: python pygame

每当我尝试在游戏中添加键盘输入时,只要我按下键盘上的内容,就会显示K_SPACEK_<ANYKEY>未定义。

我尝试使用其他密钥,但它并没有解决问题,而是让我K_<OTHERKEY>未定义。我正在开发Raspberry Pi。我该怎么做才能解决这个问题?

这是我的代码:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import pygame
import sys
import random
import time

currentlyRunning = True

pygame.init()
mainScreen = pygame.display.set_mode((1280, 1024), pygame.FULLSCREEN | pygame.HWSURFACE)
pygame.display.set_caption("PiPong")
pixelSizeX = 20
pixelSizeY = 16
pixelSize = [pixelSizeX, pixelSizeY]
BACKGROUNDCOLOR = (104, 255, 142)
WHITE = (255, 255, 255)
pongStart = False
pongPlay = False

def pixelRasterize(pixelMultiplier, xy):
    if xy == True:
        return pixelMultiplier * (1280 / 20)
    else:
        return pixelMultiplier * (1024 / 16)

def characterRender(character, color, carX, carY):
    if character == "A":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, True)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, True)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, True)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "a":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(.5, False), pixelRasterize(1, True), pixelRasterize(6, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1.5, False), pixelRasterize(1, True), pixelRasterize(4, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, True), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(2, True), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "B":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) - pixelRasterize(1.5, False), pixelRasterize(1, True), pixelRasterize(2, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1.5, False), pixelRasterize(1, True), pixelRasterize(2, False)])
    elif character == "b":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(3, False)])
    elif character == "C":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "c":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(5, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "D":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(1, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(1, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(3, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(1, True), pixelRasterize(carY, False) - pixelRasterize(2, False), pixelRasterize(1, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(1, True), pixelRasterize(carY, False) + pixelRasterize(2, False), pixelRasterize(1, True), pixelRasterize(1, False)])
    elif character == "d":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(5, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(3, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "E":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
    elif character == "e":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(3, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(5, False)])
    elif character == "F":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
    elif character == "f":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(5, False)])
    elif character == "G":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(7, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(1, True), pixelRasterize(carY, False), pixelRasterize(3, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(3, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
    elif character == "g":
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) - pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True), pixelRasterize(carY, False) + pixelRasterize(3, False), pixelRasterize(5, True), pixelRasterize(1, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) - pixelRasterize(2, True), pixelRasterize(carY, False), pixelRasterize(1, True), pixelRasterize(3, False)])
        mainScreen.fill(color, rect=[pixelRasterize(carX, True) + pixelRasterize(2, True), pixelRasterize(carY, False) + pixelRasterize(1, False), pixelRasterize(1, True), pixelRasterize(5, False)])

while currentlyRunning:
    pygame.mouse.set_visible(False)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            currentlyRunning = False
        if event.type == pygame.KEYDOWN:
            if event.key == K_RETURN:
                if pongPlay == False:
                    pongStart = True
            if event.key == K_W:
                pMoveOne = 1
            elif event.key == K_S:
                pMoveOne = 2
            else:
                pMoveOne = 3
            if event.key == K_UP:
                pMoveTwo = 1
            elif event.key == K_DOWN:
                pMoveTwo = 2
            else:
                pMoveTwo = 3
    mainScreen.fill(BACKGROUNDCOLOR)
    if pongStart == True:
        ponger = random.randint(1, 64)
        pOne = 32
        pTwo = 32
        pOneP = 0
        pTwoP = 0
        pBall = 32
        pProgress = 0
        pMove = False
        pongStart = False
        pongPlay = True
    if pongPlay == True:
        if pongMoveOne == 1:
            pOne -= 1
        elif pongMoveOne == 2:
            pOne += 1
        if pongMoveTwo == 1:
            pTwo -= 1
        elif pongMoveTwo == 2:
            pTwo += 1
        if pMove == False:
            pProgress += 1
            if pBall != ponger:
                if pBall < ponger:
                    pBall += 1
                else:
                    pBall -= 1
        elif pMove == True:
            pProgress -= 1
            if pBall != ponger:
                if pBall < ponger:
                    pBall += 1
                else:
                    pBall -= 1
        if pProgress == -28 and pBall in range(pOne - 2, pOne + 2):
            ponger = random.randint(1, 64)
            if pMove == False:
                pMove = True
            elif pMove == True:
                pMove == False
        elif pProgress == 28 and pBall in range(pTwo -2, pTwo + 2):
            ponger = random.randint(1, 64)
            if pMove == False:
                pMove = True
            elif pMove == True:
                pMove == False
        if pProgress == -32:
            pProgress == 0
            pTwoP += 1
        elif pProgress ==  32:
            pProgress == 0
            pOneP += 1
        if pOneP == 5:
            characterRender("A", (255,255,255), 16, 32)
            time.sleep(10)
            pongPlay = False
        if pTwoP == 5:
            characterRender("A", (255,255,255), 48, 32)
            time.sleep(10)
            pongPlay = False

        mainScreen.fill(WHITE, rect=[pixelRasterize(3, True), pixelRasterize(pOne, False), pixelRasterize(1, True), pixelRasterize(5, True)])
        mainScreen.fill(WHITE, rect=[pixelRasterize(61, True), pixelRasterize(pTwo, False), pixelRasterize(1, True), pixelRasterize(5, True)])
        mainScreen.fill(WHITE, rect=[pixelRasterize(pProgress + 32, True), pixelRasterize(pBall, False), pixelRasterize(1, True), pixelRasterize(1, False)])
    pygame.display.update()
    time.sleep(1 / 60)
pygame.quit()
sys.exit()

2 个答案:

答案 0 :(得分:3)

关键常量应以pygame为前缀。

e.g。

pygame.K_RETURN

您的导入声明已读

import pygame

所有这一切都是导入pygame作为模块,pygame的所有成员仍然必须由pygame.<member>限定才能访问。

如果你编写了像

这样的导入语句
from pygame import K_RETURN, K_S, K_W, K_UP, K_DOWN

from pygame import *

然后你可以像在问题中那样使用关键常量。当您从pygame导入更多内容时,前者可能会变得单调乏味。后一版本被认为是不好的做法,因为它将未知数量的项目导入您的模块全局范围。

您还可以使用pygame.前缀,从而提高可读性,因为该值的来源是明确的。

答案 1 :(得分:3)

另一个答案是正确的,但我只是想给你一个解释,这样你就可以从这个错误中吸取教训,下次你会知道它是什么了!

import pygame

导入模块名称pygame但不输入pygame中定义的函数/类的名称。您需要使用模块名称,以便可以访问以下内容中的函数:

pygame.K_RETURN

基本上,你是解释器知道检查模块pygame的K_RETURN。