The idea of the workshop was that the starting point would be to create a working traffic light, which change through the colours in sequence e.g. Red, Red/Amber, Green, Amber, Red, but for those who completed the challenge early the stretch would be to expand on this to 2 traffic lights, a road and so on.
I created a series of programs in Python which would simulate a road system, traffic lights, junctions, cars, etc in Minecraft as examples which the attendees could take away.
My finished traffic jam.
I created 5 programs to show how I went from creating 1 traffic light to my traffic jam. Each program led on from the next. All the code is on github, https://github.com/martinohanlon/minecraft-traffic.
1light.py - is a really basic implementation of a traffic light, a single function which sets the blocks in turn.
1light-functions.py - is effectively the same program as 1light.py, but I have split the code into 3 functions:
- createTrafficLight() - creates the pole and 3 'lights' on the top
- go() - sets the traffic light to go
- stop() - sets the traffic light to stop
2lights.py - creates 2 traffic lights, which work in unison, one setting to stop, the other setting to go. To do this I created a class called TrafficLight. The main program then created 2 instances of this class and called stop, go methods of the class to change the lights
road.py - to create a road and a junction I extended the program to include new classes for Road() and Junction(), the road now becomes the controlling class, allowing you to create junctions on the road. In order to get the traffic lights changing in unison and allowing the elements of the road to work independently I also implemented threading.
car.py - is where I introduced the Car() class, again cars are controlled by the Road() class and every instance of the Car() class runs in its own thread. Each car works independently checking before it moves whether it is going to enter a closed junction or go into the back of another car. The main program creates a new car on the road every 3 seconds.