pythonpygamecrasheventqueue

pygame event queue function crashes on mouse input


i have been hitting my head on the wall for several days now, but I still cannot figure out why this crashes:

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()

when I try to run the code, it will work perfectly, until i mouse over the window, then gives me the error:

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

thanks in advance to anyone who might understand why this is crashing


Solution

  • The problem is in this line:

    elif event.type -- pygame.KEYUP:
    

    That probably should be:

    elif event.type == pygame.KEYUP:
    

    What your original line does is:

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

    The result of this substraction is most likely a non-zero number, so this condition is always true, and the next line gets executed even for non-key events, such as mouse events. For those events event.key is not defined, hence your error.