python - pygame事件队列功能在鼠标输入时崩溃

标签 python crash pygame eventqueue event-queue

- -解决了 - -

整个事情是由于事件队列中的拼写错误。我将“==”拼写为“-”

我已经好几天没碰壁了,但是我仍然不知道为什么会崩溃:

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

在此先感谢任何可能了解为什么崩溃的人

最佳答案

问题在这一行:

elif event.type -- pygame.KEYUP:

那可能应该是:
elif event.type == pygame.KEYUP:

您的原始代码行是:
elif event.type - (-pygame.KEYUP):

该减法的结果很可能是非零数字,因此此条件始终为true,即使对于非键事件(例如鼠标事件),也将执行下一行。对于这些事件, undefined event.key,因此是您的错误。

关于python - pygame事件队列功能在鼠标输入时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26535630/

相关文章:

python - tkinter optionmenu 第一个选项消失

visual-studio - 在 Visual Studio 中分析故障转储

python - Pygame 混音器一次只播放一种声音

python - pygame.错误: display Surface quit after filling the surface black

python - Django:有没有办法过滤模型并忽略存储的连字符?

c# - Python 到 c# 减少函数理解

Python:如何做嵌套枚举for循环?

crash - JOptionPane 在随机运行次数后停止(在 MacbookAir 上使用 Eclipse Kepler、java 7、Windows 7 64 位)

android - 为什么我不能使用relativeLayout作为parentView?

python - 一次更新所有显示 PyGame