Prerequisites
You should review the following before moving on:
Setup
VINT Hub

Install Pygame Zero
In order to use Pygame Zero, you first have to install it. You do this in the same way you previously installed the Phidget22 library. Simply navigate to your package manager, search for pgzero and press install!
PyCharm
If you're using PyCharm, select File > Settings > Python Interpreter and use the + symbol to install pgzero.
PyScripter
If you're using PyScripter, select Tools > Tools > Install Packages with pip and enter pgzero.
Create Project Structure
Create the following project structure on your computer in the location of your choice.
Download the following files and place them in the appropriate location.
Finally, create a new python file named phidgets_pygame.py and save it in the same location
Write Code (Python)
Copy the code below into phidgets_pygame.py.
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.Devices.DigitalInput import *
import pgzrun
WIDTH = 500
HEIGHT = 500
alien = Actor('alien')
alien.pos = WIDTH/2, HEIGHT/2
alien.pos = 0,0
def draw():
screen.clear()
alien.draw()
def update():
#Map thumbstick range to window size
xpos = mapThumbstick(horizontal.getVoltageRatio(), WIDTH)
ypos = mapThumbstick(vertical.getVoltageRatio(), HEIGHT)
ypos = HEIGHT - ypos #Invert y axis. Try commenting this out to see the impact
#Map alien to thumbstick
alien.pos = xpos, ypos
def set_alien_hurt():
alien.image = 'alien_hurt'
sounds.eep.play()
def set_alien_normal():
alien.image = 'alien'
#Phidgets Code Start
def mapThumbstick(value, size):
#The thumbstick range is -1 to 1 and the window range is 0 to WIDTH and 0 to HEIGHT
return ((value + 1) * size) / 2
#Button Event
def onStateChange(self, state):
if(state):
set_alien_hurt()
else:
set_alien_normal()
#Create, Address, Subscribe to Events and Open
button = DigitalInput()
vertical = VoltageRatioInput()
horizontal = VoltageRatioInput()
vertical.setChannel(0)
horizontal.setChannel(1)
button.setOnStateChangeHandler(onStateChange)
button.openWaitForAttachment(1000)
vertical.openWaitForAttachment(1000)
horizontal.openWaitForAttachment(1000)
#Set data interval to minimum | This will increase the data rate from the sensor and allow for smoother movement
vertical.setDataInterval(vertical.getMinDataInterval())
horizontal.setDataInterval(horizontal.getMinDataInterval())
#Phidgets Code End
pgzrun.go()
Run Your Program
When you move the thumbstick, the alien will also move. When you press the thumbstick down, you will hear a sound and the alien sprite will change to an unhappy alien.
Code Review
As you may have noticed, the program above is based on the program from the Introduction to Pygame Zero. The main difference is that the code to handle mouse clicks has been removed and the screen size has been increased by adjusting the HEIGHT variable. To replace mouse clicks, the Thumbstick Phidget is used.
When using the Thumbstick Phidget, you have three objects to create and use:
- Horizontal Axis
- Vertical Axis
- Push Button
When using the horizontal and vertical axes, it's important to set the data interval to the minimum as shown in the code above. This will ensure your sensor is as responsive as possible. In this program, you are controlling the position of the alien with the sensor, so you must map the thumbstick range to the window range. You can do this with the following formula:
Y = (X - A) / (B - A) * (D - C) + C
where:
- A and B are the range of the Thumbstick (-1 to 1)
- C and D are the range of values you want to map to (0 to WIDTH/HEIGHT)
- X is the current value that falls between A and B
- Y is the value you are looking for that falls between C and D
When using the push button, it's a good idea to use events. Events will allow you to easily determine when the button has been pushed or released. Polling can also be used, however, extra code will be required
Practice
- Try commenting out the section of your code that sets the data interval. What is the result?