Why is this Python code not working?

edited March 2016 in Python Mode

This is from the excellent book "Algorithms for Visual Design Using Processing". It's a simple code to draw a cube on the screen and then paint it red when clicking on it. There is no error, but the cube doesn't change color. What am I doing wrong?

add_library('peasycam')

class MyObject(object):
    dim = 40
    x =0
    y=0
    z = 0
    picked = False
    c_face = color(255)


    def pick (self, xp, yp):
        if (dist(xp,yp,screenX(x,y,z), screenY(x,y,z))<dim):
            MyObject.picked = True
        else:
            MyObject.picked = False

    def draw3(self):
        if (MyObject.picked == True):
            fill(255,0,0)
        else:
            fill(MyObject.c_face)
        box(MyObject.dim)

b =  MyObject()

def setup():

    size(200,200,P3D)
    #PeasyCam(this,100)
    camera(-100,100,-100,0,0,0,0,0,1)

def draw():

    b.draw3()

    def mousePressed():
        b.pick(mouseX,mouseY)

Answers

  • Answer ✓

    My python is a little rusty, but I seem to recall that whitespace counts. Did you really mean to define mousePressed() inside draw()?

  • Answer ✓

    holy stupid mofo!!!!! -_-

    Thank you so much!

Sign In or Register to comment.