Typical Pong game with special controls: the players wear some cardboard detected by a camera that defines the position of their controls.
Single and multi-player game based on typical pong adding a special control: the players wear some kind of identity shape (specifically: a filled circle) in their hand that is recognised and processed by the system via a camera and defines the X-Y position of their control.
- Each frame captured by the camera is analysed in order to search for a circle in the left half of the camera frame (for player 1) and a circle in the right half of the camera frame (for player 2).
- The position of each player's circle is represented in the screen board by a square (red for player 1 and green for player 2).
- The player's bat will vertically follow the square representing the position of the circle.
- As "controller" for cam, a paper or cardboard with a filled circle of 5 cm in diameter painted may be used. The essential requirement is that the circle contrasts the background. With respect to the distance, 30 cm is fine for 640x480 camera. However, all this parameters depend on the user's camera and might be modified by changing the code parameters.
Controller may be chosen (gestures or keyboard). Below, two screenshots show the game when using camera controller for one player (first picture) and two players (second picture). As explained above, when camera control is used, the player's circle position is represented with squares.
Platform: Windows / Linux (actually: any system running python virtual machine + dependencies) Tested:
- Windows 10 Home, Python 3.9.0
- Raspberry Pi 3B+, Raspbian, Camera CSI-2, not overclocked: It worked fine when using a keyboard as controller; however, the camera controller gets very slowdown because of low performance. Screen Keyboard and / or camera
Note: steps 2 and onwards must be executed from the terminal, possibly requiring root privileges
- Install Python3 (tested on 3.9.0)
https://www.python.org/downloads/ - Upgrade pip:
>> pip3 install --upgrade pip - Install pygame:
>> pip3 install pygame - Install opencv-python:
>> pip3 install opencv-python - Download the game, go to its directory and run CamPong.py:
>> python3 Campong.Py nb_players control_type
Example for 1 player (nb_players = 1) and keyboard control (control_type = 0):
>> python3 Campong.py 1 0
Help display (Campong.py --help):
positional arguments:
nb_players Number of human players (0, 1 or 2); the other(s) player(s) will be IA(s)
player_control Type of control: 0 keyboard, 1 camera
optional arguments:
-h, --help show this help message and exit
- Player 1 (left): w (go up) and s (go down)
- Player 2 (right): up arrow (go up) and down arrow (go down)
See Introduction to know more about how cam control works
- Constants.py - FPS: may be adjusted to improve the performance.
- Constants.py - SHOW_REAL_FPS: if set to True, the camera task fps and gui fps are printed via serial output.
- Constants.py - IA_OFFSET: the smaller this value, the more accurate the IA (therefore, harder to defeat)
- Constants.py: colours may be also modified in this file.
- GUI_Ball.py - RADIUS_DEFAULT and SPEED_MOD_DEFAULT
- GUI_Bat.py - SPEED_MOD_DEFAULT_BAT