-1

- -解决了 - -

这整个事情是因为事件队列中的一个小错字。我将“==”拼错为“--”

我已经把头撞在墙上好几天了,但我仍然无法弄清楚为什么会崩溃:

import pygame, random

#button state variable, order: axes, A(z) B(x) START(enter) SELECT(shift)
#SELECT and START cannot be held
global buttons
buttons = [[0, 0], 0, 0, 0, 0]
pygame.init()
pygame.font.init()
text = pygame.font.Font('PressStart2p.ttf', 8)

debugmode = 1

#read config file
config = open("config.ini")

cfg_resx = int(config.readline())
cfg_resy = int(config.readline())
cfg_res = [cfg_resx, cfg_resy]

cfg_fscreen = config.readline()

if cfg_fscreen == 1:
    cfg_fscreen = pygame.FULLSCREEN

global screen
screen = pygame.display.set_mode(cfg_res)
config.close()


logo = pygame.image.load('titletext.bmp')

pygame.event.get()
global output
output = pygame.Surface([255, 240])

def update():
    pygame.transform.scale(output, cfg_res, screen)
    pygame.display.flip()
    screen.fill([0, 0, 0])

    for event in pygame.event.get():
        print event
        if event.type == pygame.QUIT:
            state = 0
        elif event.type == pygame.MOUSEMOTION:
            continue
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                buttons[0][1] += 1
            elif event.key == pygame.K_DOWN:
                buttons[0][1] -= 1
            elif event.key == pygame.K_RIGHT:
                buttons[0][0] += 1
            elif event.key == pygame.K_LEFT:
                buttons[0][0] -= 1
            elif event.key == pygame.K_z:
                buttons[1] = 1
            elif event.key == pygame.K_x:
                buttons[2] = 1
            elif event.key == pygame.K_RETURN:
                buttons[3] = 1
            elif event.key == pygame.K_RSHIFT:
                buttons[4] = 1
        elif event.type -- pygame.KEYUP:
            if event.key == pygame.K_UP:
                buttons[0][1] -= 1
            elif event.key == pygame.K_DOWN:
                buttons[0][1] += 1
            elif event.key == pygame.K_RIGHT:
                buttons[0][0] -= 1
            elif event.key == pygame.K_LEFT:
                buttons[0][0] += 1
            elif event.key == pygame.K_z:
                buttons[1] = 0
            elif event.key == pygame.K_x:
                buttons[2] = 0

global state
state = 1
while state != 0:
    if state == 1:
        option = 2
    while state == 1: #main menu
        #display logo
        output.blit(logo, [25, 20])

        #display menu text options in correct color
        if option == 2:
            output.blit(text.render('Start', 0, [250, 250, 250]), [100, 100])
        else:
            output.blit(text.render('Start', 0, [200, 200, 200]), [100, 100])
        if option == 3:
            output.blit(text.render('Highscores', 0, [250, 250, 250]), [100, 115])
        else:
            output.blit(text.render('Highscores', 0, [200, 200, 200]), [100, 115])
        if option == 4:
            output.blit(text.render('Quit', 0, [250, 250, 250]), [100, 130])
        else:
            output.blit(text.render('Quit', 0, [200, 200, 200]), [100, 130])
        if buttons[4] == 1:
            option += 1
            buttons[4] = 0
        if option >= 5:
            option = 2

        #change state when "start" is pressed
        if buttons[3] == 1:
            if option == 4:
                state = 0
            else:
                state = option

        update()
pygame.quit()

当我尝试运行代码时,它会完美运行,直到我将鼠标悬停在窗口上,然后给我错误:

Traceback (most recent call last):
  File "H:\GUNFORCE\main.py", line 113, in <module>
    update()
  File "H:\GUNFORCE\main.py", line 65, in update
    if event.key == pygame.K_UP:
AttributeError: event member not defined

提前感谢任何可能理解为什么会崩溃的人

4

1 回答 1

0

问题出在这一行:

elif event.type -- pygame.KEYUP:

那可能应该是:

elif event.type == pygame.KEYUP:

您的原始行所做的是:

elif event.type - (-pygame.KEYUP):

此减法的结果很可能是一个非零数,因此此条件始终为真,即使对于非键事件(例如鼠标事件)也会执行下一行。对于那些事件event.key没有定义,因此你的错误。

于 2014-10-23T19:25:04.180 回答