ICM week 6 project (Canadian Traffic Simulator)
This week, I wrote a sketch in P5 which emulates a busy street with cars and pedestrians. Three manually written functions are contained in the draw loop: crosswalk(), PedestriansThatSaysSorry(), and CarsThatCrash().
crosswalk() is the most simple function in the draw loop, it consist of a for loops which draws a series of white rectangles which form a crosswalk on the canvas.
The other two functions, PedestriansThatSaysSorry() and CarsThatCrash() are functions that contains objects in an array along with instructions for interactions between the objects.
PedestriansThatSaysSorry() contains the pedestrians array, which calls in a group of objects from the “person” class – the class written for the pedestrians on the screen. The “person” class contains information for drawing an ellipse and a rectangle with a random fill and a bounce function, along with a bump() function that measures the distance between pedestrians. Once the array of pedestrians are called in, the bump() function from the “person” class is used to determine is two people are bumping into each other. The final portion of the PedestriansThatSaysSorry() function display a text that reads “sorry” at the position of the object when two objects from the pedestrians array bump into each other.
CarsThatCrash() contains the cars array, which calls in a group of objects from the “car” class – the class written for the cars on the screen. The car class contains instructions for drawing a rectangle with random fill, along with two ellipses to make up the shape of a car. Additionally, a bounce function moves the cars on the x axis and a crash function measures the distance between the car objects’ rectangles to determine if two car objects crashes into each other. After calling in an array of car objects, CarsThatCrash() uses an “if” statement with the crash function from the car object to check if two car objects are bumping into each other, and if they are, the width of the car rectangle divides by 2.
And there we have it, a cartoon simulation of a busy street full of traffic where no one follows any traffic rules. Fortunately the pedestrians are not coded to have any reactions if a car bumps into them, and the cars somehow keeps getting half as long every time they crash, and at least the pedestrians are polite enough to say sorry to one another if they bump into each other.
link to the sketch: