Save the time when mouse is pressed to use it in a variable - Python Mode

edited February 2017 in Python Mode

Hello,

I'm trying to save the time in a variable to use it for a figure to disappear 10 seconds after it appears or disappear randomly. and for every figure that appears on screen.

At the moment It saves the time many times.

"""
Mouse Press.

Move the mouse to position the shape.
Press the mouse button to invert the color.
"""
#tick=millis()


def update_tick():
    #text(12)
    #textAlign(CENTER)
    pd= millis()/1000
    return pd

def setup():
    global times
    size(640, 360)
    noSmooth()
    fill(126)
    background(102)
    times=[]


def draw():
    #times=[]
    the_time=update_tick()
    if mousePressed:

        stroke(255)
        line(mouseX - 66, mouseY, mouseX + 66, mouseY)
        line(mouseX, mouseY - 66, mouseX, mouseY + 66)
        times.append(the_time) 
        print times
    else:
        stroke(0)

Thank you!!

Answers

  • Thank you, I've re formatted my question.

  • You should replace variable mousePressed w/ callback function mousePressed() so it triggers once, b/c draw() is called back at about 60 FPS.

  • And for every figure that appears on screen.

    Then you should consider to create a class to represent 1 Figure:
    http://py.Processing.org/reference/class.html

    Take a look at Objects Tutorial's link below: $-)
    http://py.Processing.org/tutorials/objects/

  • Thank you! Im following your example in, https://forum.processing.org/two/discussion/19200/create-10-instances-of-an-object-python#latest dont understand what is happening:

    Its telling me AtrtibuteError: item instance has no attribute "x" in the function display

    my_items=[]
    i=0
    
    class item():
        def __ini__(self):
            self.i=i
            self.x,self.y =mouseX, mouseY
            self.appear=update_tick()
            self.disappear=self.appear()+2
    
        def display(self):  # Display method
            ellipse(self.x - 66, self.y, 20, 20)
            ellipse(self.x, self.y - 66, 20, 20)
    
    
        def leaves(self):
            if self.disappear==update_tick():
                my_items.remove(self)    
    
        def script(self): 
            self.display()
            self.leaves()                   
    
    def update_tick():
        #text(12)
        #textAlign(CENTER)
        pd= millis()/1000
        text("Time: %.1f" % tick, 3, 3) 
        return pd
    
    def setup():
        #global times
        size(640, 360)
        noSmooth()
        fill(126)
        background(102)
        ellipseMode(CENTER)
        #times=[]
        #times.append(the_time)
    
    def mousePressed():
        global i
        if (mouseButton != CENTER):
            i+=1
            my_items.append(item())
    
    def draw():
        background(255)
        for i in my_items:
            i.display()
    
  • edited February 2017

    A class' constructor is called def __init__(self):, not def __ini__(self):! [-X
    Also class' names should follow the UpperCaseCamel's naming convention. :-B
    So your class should be renamed to: class Item:. B-)

  • Ah yes, I had already noted that, but thanks.:D that's not the main problem, as still doesn't work :(

  • edited February 2017

    You've got an organizational problem about Separation of Concerns (SoC):
    https://en.Wikipedia.org/wiki/Separation_of_concerns

    For example your function update_tick(): It both displays current value of some undeclared global variable called tick & return current millis() divided by 1000. @-)

    You should instead split those 2 different concerns as 2 distinct functions. *-:)

    A slightly more serious concern violation is in your method leaves().
    It accesses its own container and remove() itself from it! @-)

    Although it's completely possible given my_items is a global variable, it's still bad programming practice, b/c an object should be independent from its container. =;

    Rather than remove() itself, it should flag itself for removal.
    Then the top main program can invoke remove() when that condition is True. B-)

    You even assign to property i the current value of global variable i. :-@
    If you feel you need some static counter, you can declare it inside the class itself. :\">

    class Item:
        counter = 0
    
        def __init__(self):
            Item.counter += 1
            self.register = Item.counter
    
  • edited March 2017 Answer ✓

    Thanks, I had already solved :-)

Sign In or Register to comment.