Program stopping after executing certain lines when handling key presses

I’m having a problem where the program stops executing whenever I run certain lines of code when handling key presses and mouse clicks. I’m trying to accept certain inputs to create an object of certain mass, radius and velocity.

def mouseReleased():
    vel = (PVector(mouseX, mouseY).sub(PVector(lastMouseClick[0], lastMouseClick[1]))).mag()
    print(vel)
    bodies.Append(Body(mass, radius, PVector(mouseX, mouseY), PVector(0, 0)))
    
    
def keyReleased():
    print(key)
    if key == "i":
        incr *= -1
    elif key == "p":
        incr *= 10
    elif key == "l":
        incr *= 0.1
    elif key == "r":
        radius += incr
    elif key == "m":
        mass += incr

In particular, pressing any of the keys in the if statement, or executing the bodies.Append() line, will stop the program. The window will stay open but won’t allow further interaction. vel, bodies, incr, radius and mass are all global variables.

Any help would be greatly appreciated!

My advice when reading variable key is to apply this code:
k = key.upper() if key != CODED else keyCode

Then use local variable k instead w/ capitalized letters like the example from this link:

    k = key.upper() if key != CODED else keyCode

    if k == 'W' or k == UP:
        for f in flakes: f.vel.y -= f.maps[2]

    elif k == 'S' or k == DOWN:
        for f in flakes: f.vel.y += f.maps[2]

    elif k == 'A' or k == LEFT:
        for f in flakes: f.vel.x -= f.maps[2]

    elif k == 'D' or k == RIGHT:
        for f in flakes: f.vel.x += f.maps[2]

Ru sure that method is capitalized? If that’s a list type, it’s lower case instead: bodies.append(

2 Likes