# Not reuse function!

**URL:** <https://discourse.processing.org/t/not-reuse-function/23353>\
**Category:** Processing.py\
**Created:** [August 19, 2020, 8:19am UTC](https://discourse.processing.org/t/not-reuse-function/23353 "2020-08-19T08:19:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [August 19, 2020, 8:19am UTC](https://discourse.processing.org/t/not-reuse-function/23353/1 "2020-08-19T08:19:50Z")

</div>

```auto
tileCountX=2

tileCountY=10

colorsLeft=[]

colorsRight=[]

interpolateShortest=True

def setup():

    size(800,800)

    colorMode(HSB)

    noStroke()

    shakeColors()

    

def draw():

    tileCountX=int(map(mouseX,0,width,2,100))

    tileCountY=int(map(mouseY,0,height,2,10))

    

    tileWidth=width/tileCountX

    tileHeight=height/tileCountY

    for gridY in range(tileCountY):

        col1=colorsLeft[gridY]

        col2=colorsRight[gridY]

        for gridX in range(tileCountX):

            amount=map(gridX,0,tileCountX-1,0,1)

            if interpolateShortest:

                colorMode(RGB)

                interCol=lerpColor(col1,col2,amount)

                colorMode(HSB)

            else:

                interCol=lerpColor(col1,col2,amount)

            fill(interCol)

            posX=tileWidth*gridX

            posY=tileHeight*gridY

            rect(posX,posY,tileWidth,tileHeight)

def shakeColors():

    for i in range(tileCountY):

        colorsLeft.append(color(random(0, 60), random(0, 100), 100)) 

        colorsRight.append(color(random(160, 190), 100, random(0, 100)))

        

def keyPressed():

    global interpolateShortest

    if key=="1": 

        interpolateShortest=True

    elif key=="2": 

        interpolateShortest=False

```

question :when i pressed mouse i couldn’t make shakeColors() function work. How can i make it work ?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [August 19, 2020, 8:28am UTC](https://discourse.processing.org/t/not-reuse-function/23353/2 "2020-08-19T08:28:43Z")

</div>

Welcome, @Mayarch

You’re listening for **key** , not **mouse** input. In other words, when the user presses the 1 or 2 key on the keyboard.

I assume that you want to detect left and right mouse clicks:

```auto
...

def mousePressed():
    global interpolateShortest
    
    if mouseButton == LEFT:
        interpolateShortest=True

    elif mouseButton == RIGHT: 
        interpolateShortest=False

```

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [August 19, 2020, 8:30am UTC](https://discourse.processing.org/t/not-reuse-function/23353/3 "2020-08-19T08:30:29Z")

</div>

def mousePressed():

```
if mouseButton==LEFT:

    shakeColors()

```

sorry i forgot that definition what i want.but i couldn t rework shakeColor function again when i pressed mouse left click

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [August 21, 2020, 8:27pm UTC](https://discourse.processing.org/t/not-reuse-function/23353/4 "2020-08-21T20:27:28Z")

</div>

```auto
tileCountX=2

tileCountY=10

colorsLeft=[]

colorsRight=[]

interpolateShortest=True

def setup():

    size(800,800)

    colorMode(HSB)

    noStroke()

    makeColors()

    shakeColors()

    

def draw():

    tileCountX=int(map(mouseX,0,width,2,100))

    tileCountY=int(map(mouseY,0,height,2,10))

    

    tileWidth=width/tileCountX

    tileHeight=height/tileCountY

    for gridY in range(tileCountY):

        col1=colorsLeft[gridY]

        col2=colorsRight[gridY]

        for gridX in range(tileCountX):

            amount=map(gridX,0,tileCountX-1,0,1)

            if interpolateShortest:

                colorMode(RGB)

                interCol=lerpColor(col1,col2,amount)

                colorMode(HSB)

            else:

                interCol=lerpColor(col1,col2,amount)

            fill(interCol)

            posX=tileWidth*gridX

            posY=tileHeight*gridY

            rect(posX,posY,tileWidth,tileHeight)

def makeColors():

    for i in range(tileCountY):

        colorsLeft.append(color(random(0, 60), random(0, 100), 100)) 

        colorsRight.append(color(random(160, 190), 100, random(0, 100)))

def shakeColors():

    for i in range(tileCountY):

        colorsLeft[i]=color(random(0, 60), random(0, 100), 100) 

        colorsRight[i]=color(random(160, 190), 100, random(0, 100))

        

def keyPressed():

    global interpolateShortest

    if key=="1": 

        interpolateShortest=True

    elif key=="2": 

        interpolateShortest=False

def mousePressed():

    if mouseButton==LEFT:

        shakeColors()

```

i changed the name shakeColors as makeColors and then i redefined colorsLeft, colorsRight lists indexes again with shakeColors function. then solved
