6.3.5 Cmu Cs | Academy

Before data can be plotted, it often needs to be organized. Exercise 6.3.5 often requires:

Cause: You put the movement code inside onKeyPress instead of onStep. Fix: onKeyPress should only toggle a variable (app.moving = True). The actual coordinate change (shape.centerX += 5) must go inside onStep.

Since event handlers are separate functions, they cannot see variables inside app.start or other functions unless those variables are declared as global. 6.3.5 Cmu Cs Academy

Wrong way:

def onKeyPress(key):
    circle.centerX += 15  # Error: circle is not defined

Right way:

circle = None

def onAppStart(app): global circle circle = Circle(200, 200, 20, fill='blue')

This is where the actual movement happens.

def onStep(app):
    # If the flag is true, move the shape by a small amount
    if app.movingRight == True:
        app.player.centerX += 5
if app.movingLeft == True:
    app.player.centerX -= 5