With diesel engines, the fuel is ignited due to the high heat. Gasoline engines require a spark to ignite the mixture. That sparking occurs when between the electrodes of the spark plug. Only it requires a voltage that is much leaner than the 12 volts from the battery, and then that spark must also appear at exactly the right time. That sounds complicated, but is it?
Where a diesel engine uses the heat from the compression to ignite the diesel, a gasoline engine requires a spark to ignite the mixture. A spark plug is used to generate a spark: if there is only a large enough voltage difference between the electrodes of the spark plug, a spark is created there.
The simplest way to get that high voltage to the spark plugs is via spark plug wires connected to an installation where that high voltage is generated. Naturally, the high voltage must reach the spark plugs in the correct order and at the right time. That distribution of the voltage in the correct order is the job of the distributor cap. In that hood, a rotor rotates (with a four-stroke engine at half the speed of the crankshaft) which alternately connects to one of the spark plug cables.
That high voltage does not arise in the distributor cap, but is generated with a coil, actually not much different than a transformer in which the 12 volts from the battery are transformed upwards. Initially, the up-transformed voltage is not enough to create a spark, but by making clever use of magnetism due to alternating voltage (which is caused by briefly interrupting the current), the voltage can still rise enough. The interruption was initially done with a mechanical switch in the form of contact points. This relatively simple construction was sufficient and within limits also offered some freedom to adapt the ignition timing to the operating conditions (load and speed).
However, the degree of freedom to adjust the ignition timing is limited with the above classic construction, in addition, a system with moving parts is susceptible to wear and failure. This is how electronics make their appearance. The contacts are replaced by a transistor (basically an electrical on / off switch) that makes it possible to electronically control the interruption of the current from the ignition coil (for example, based on the signal from a hall sensor). Later, the distributor cap with its rotating rotor is also replaced and in the last decade of the last century we see the distributor cap make way for a solution in which each spark plug gets its own ignition module (including a ignition coil). All this, of course, centrally controlled (and controlled) by the engine management system.