partially delaying

edited September 2016 in Python Mode

Hello Forum!

First off: i'm new to processing, coding and everything around it, so excuse my ignorance :) I created a WASD moveable rectangle on the screen, which doubles it size when you hit the spacebar.

After that, it transforms to its original state. In between the transformation (when it's doubled), i want a delay (no specific time). I tried using time.sleep and delay, but everything seems to freeze the entire program (including the movement controls. How will I be able to only delay only the one statement?

Thanks in advance!

if space_pressed == True:
    rectSize += 2 
if rectSize == 200:
    space_pressed = False
    delay (5000)    
if rectSize >= 100 and space_pressed == False: 
    rectSize += -2 


  • edited September 2016

    This comes up a lot. You have been misled by the name "delay()". It does not do what you think it does, and is not useful for doing what you want. In short, do not use delay().

    The function you really want is call millis(). It returns a positive number. The number it returns is the number of milliseconds since your sketch started running.

    You can remember values returned by millis(). You can add a number to that remembered value, and then check if additional calls to millis() are returning number larger than that. In this way, you can make things happen after a certain number of milliseconds have passed.

    Thus, the process is:

    1) The keyPressed function detects the space bar press, which sets a boolean that knows the rectangle's size should increase.

    2) When the rectangle's size is large enough, and that boolean is still set, then this is the first time, so we need to set up for our waiting period. We clear the boolean so we don't do this first time setup for our waiting period again, and record the current time (the number returned by millis()). We might also want to set a different boolean that tracks when we are waiting.

    3) If we are waiting, compare the current value returned by millis() to our recorded time. Is the difference between the two more than some value? Were we not already decreasing the size? If yes to both, we are done waiting, so clear that boolean and start to decrease the rectangle's size.

    4) Once the size has returned to normal, clear the decreasing size boolean.

  • Answer ✓
    boolean inc, wait, dec;
    int time, s;
    void setup() {
      size(200, 200);
    void draw() {
      if (inc) {
      if (s > 60 && inc) {
        inc = false;
        wait = true;
        time = millis();
      if (wait && millis() > time + 5000) {
        wait = false;
        dec = true;
      if (dec) {
        s = 20;
        dec = false;
      rect(20, 20, s, s);
    void keyPressed(){
      if(key == ' ' && !inc && !wait && !dec){
        inc = true;
  • Thank you for the clear explanation! I got it to work! :)

  • edited September 2016

    Celebrated too early.. The variable time and millis() are the same, even though time is created after rectSize == 200. so it will never get > time + 5000 :(

        if inc == True:
            rectSize += 2 
        if rectSize == 200:
            inc = False
            wait = True
            time = millis()
            if wait == True and millis() > time + 5000:
                wait = False
                dec = True 
                if rectSize >= 100 and dec == True: 
                    rectSize += -2   
  • Your code looks very Python-y. I would check your indenting. For example, your check that rectSize >= 100 is INSIDE the if block for your rectSize == 200 condition... If you know rectSize is already 200, surely it's greater than 100!

    Look again at my conditionals. They are separate.

  • edited September 2016

    I dedented, and it still didnt work.

    For some reason, changing to this worked: :D

    Thank you very much!

       if rectSize > 200 and inc == True:
            inc = False
            wait = True
            time = millis()
  • deindented what?

    your description of the changes isn't clear. please post the entire code.

  • Here is the pastebin: It is a very messy script, i am aware. keep in mind i'm very new to this ;D

Sign In or Register to comment.