Created
September 4, 2017 05:31
-
-
Save indirectjump/2e4941987764a6e8bc8ba6dc1817f60e to your computer and use it in GitHub Desktop.
Traffic Light Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Yes, good work