#Help with Conditionals - Light On/Off in Processing Python Mode

8 messages ยท Page 1 of 1 (latest)

craggy shadow
#

My code so far - i'm very new to coding ๐Ÿ˜… . The goal is that the light (circle, very primative) turns on and off at a mouse click. Currently it goes goes from On to Off, but not back On... ideas?

LightON = True
LightOFF = False
diameter = 50
x = 75
y = 75

def setup():
background(100)
size(400,400)

def draw():
background(100)
global LightON, LightOFF

noStroke()
fill(255,255,0)
ellipse(100,50,diameter,diameter)

if LightON: 
    fill(255,255,0)
    ellipse(100,50,diameter,diameter)
if LightOFF:
    fill(0,0,0)
    ellipse(100,50,diameter,diameter)

def mouseClicked():
global LightON, LightOFF, x, y
x = mouseX
y = mouseY

if dist(100,50,mouseX,mouseY) < diameter/2:
    LightOFF = True
    LightON = False
pulsar ocean
#

Hello, @craggy shadow , your logic is wrong, bcz you can take only one boolean variable lightState and first set it to False. In the mouseClicked function, you then change this after your if statement:
lightState = not lightState

#

This code will alternate your lightState : if it's True irt'll set it to False, and vice versa

#

It will turn your light from On to Off, then Off to On, and so on ...

pulsar ocean
#

Here is the complete code:

lightState = False
diameter = 50
x = 75
y = 75

def setup():
    background(100)
    size(400,400)

def draw():
    background(100)
    global lightState

    noStroke()
    fill(255,255,0)
    ellipse(100,50,diameter,diameter)

    if not lightState: 
        fill(255,255,0)
        ellipse(100,50,diameter,diameter)
    if lightState:
        fill(0,0,0)
        ellipse(100,50,diameter,diameter)

def mouseClicked():
    global lightState, x, y
    x = mouseX
    y = mouseY

    if dist(100,50,mouseX,mouseY) < diameter/2:
        lightState = not lightState

final fractal
# craggy shadow My code so far - i'm very new to coding ๐Ÿ˜… . The goal is that the light (circle,...

You probably already know why it doesn't work, (because in the mouseClicked, always lightOff will be set to true and lightOn will be set to false, no matter what the current state is).

As shown above by D Biswas, you only need a single variable which stores the current state (true = on and false = off). And in mouseClicked you can toggle the value of this variable by using the not operator, which changes true to false and false to true.

pulsar ocean
craggy shadow
#

Thank you very much @pulsar ocean and @final fractal !