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.Encoder import *
from Phidget22.Devices.DigitalInput import *
import pgzrun
alien = Actor('alien')
alien.pos = 100, 56
WIDTH = 500
HEIGHT = alien.height + 20
def draw():
screen.clear()
alien.draw()
def update():
if(dial.alienMovement > 0):
alien.left += 5
elif(dial.alienMovement < 0):
alien.left -= 5
def set_alien_hurt():
alien.image = 'alien_hurt'
sounds.eep.play()
def set_alien_normal():
alien.image = 'alien'
#Phidgets Code Start
#Button Event
def onStateChange(self, state):
if(state):
set_alien_hurt()
else:
set_alien_normal()
def onPositionChange(self, positionChange, timeChange, indexTriggered):
#position change is only availble through this event, so store the data for use in the update event
dial.alienMovement = positionChange
#Create, Address, Subscribe to Events and Open
dial = Encoder()
button = DigitalInput()
button.setOnStateChangeHandler(onStateChange)
dial.setOnPositionChangeHandler(onPositionChange)
dial.alienMovement = 0 #create this property on the fly to store movement information
dial.openWaitForAttachment(5000)
button.openWaitForAttachment(5000)
#Set data interval to minimum | This will increase the data rate from the sensor and allow for smoother movement
dial.setDataInterval(dial.getMinDataInterval())
#Phidgets Code End
pgzrun.go()
Run Your Program
When you move the dial, the alien will also move. When you press the dial 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. To replace mouse clicks, the Dial Phidget is used.
When using the Dial Phidget, you have two objects to create and use:
- Encoder
- Push Button
When using the Encoder, 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 alien based on the dial movement (left or right). The easiest way to do this is to subscribe to the position change event and store the position change value as shown in the code above. If the change is positive, move the alien to the right, if negative, move it to the left.
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?
- Try increasing how fast the alien moves when the dial is turned.