Skip to content

Instantly share code, notes, and snippets.

@rumikotakahashi
Forked from indirectjump/traffic_light_oop.pde
Created September 4, 2017 05:33
Show Gist options
  • Select an option

  • Save rumikotakahashi/5d67fce73be6e3869738599278f2a643 to your computer and use it in GitHub Desktop.

Select an option

Save rumikotakahashi/5d67fce73be6e3869738599278f2a643 to your computer and use it in GitHub Desktop.
Traffic Light Demo
class Light {
color col;
int state;
float x;
float y;
Light(color c) {
col = c;
}
void on() {
state = 1;
}
void off() {
state = 0;
}
void display() {
if (state == 1) {
fill(col, 255);
} else {
fill(col, 50);
}
ellipse(x, y, 100, 100);
}
}
class TrafficLight {
Light red;
Light yellow;
Light green;
TrafficLight() {
red = new Light(color(255, 0, 0));
red.x = width/2;
red.y = height/5;
red.off();
yellow = new Light(color(255, 255, 0));
yellow.x = width/2;
yellow.y = 2*height/5;
yellow.off();
green = new Light(color(0, 255, 0));
green.x = width/2;
green.y = 3*height/5;
green.off();
}
void stopTraffic() {
red.on();
yellow.off();
green.off();
}
void getReady() {
red.off();
yellow.on();
green.off();
}
void go() {
red.off();
yellow.off();
green.on();
}
void display() {
red.display();
yellow.display();
green.display();
}
}
TrafficLight t;
void setup() {
fullScreen();
background(0);
t=new TrafficLight();
}
void draw() {
clear();
t.display();
}
void keyPressed() {
if (key=='1') {
t.stopTraffic();
}
if (key=='2') {
t.getReady();
}
if (key=='3') {
t.go();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment