Skip to content

Controls

Omega Core edited this page May 19, 2024 · 1 revision

code found in 'controls.py'

A neat improvement we made to pygame's already existing key system can be observed in our code. Each button in their library is associated with a specific integer, an index in the keys list.


We used a concept called edge detection. It's a very useful tool for finding change in digital output. Graphing the output gave by a digital device (like a button) in time, gives us something like this:

ifyoufoundthiscongrats-love
Source: sparkfun

Notice the sudden change in inputs? Always analising two consecutive values, we have 4 possible configurations:

  • 0 & 0 ---> the line stays low;
  • 0 & 1 ---> the line 'climbed', meaning the detector is rising ;
  • 1 & 0 ---> the line 'fell' back down, meaning the detector is falling ;
  • 1 & 1 ---> the line stays high;

Implementing an abstract class in the 'edgeDetectorEx.py' file doing just that, we create a list of detectors for all keyboard and controller buttons, and update their values each loop. This allows us to change values only when the button is pressed, not everytime is pressed like we would do using the raw input.

An inner class created for a more abstract joystick use is the last important implementation we did here. It recognizes the type of joystick connected and sets the buttons accordingly.

next page →

← previous page