In this task, we will review interfacing the arcade style buttons (shown in the following image) to Raspberry Pi to play the game. In this task, we will just discuss the software part of the code.
We will make use of the PiFace interface board available at http://www.piface.org.uk/products/piface_digital/ to interface these buttons to Raspberry Pi. The board is a stackable add-on hardware that can be used to interface eight inputs and eight outputs. The PiFace comes with four tactile switches and we can get started with testing the inputs.
The main reason behind using the PiFace is that we need to monitor the switch states to determine there is a key press. The PiFace comes with an I/O expansion option and can be accessed through the SPI interface. These features enable simple control of the game inputs.
/etc/modprobe.d/raspi-blacklist.conf
file using a text editor such as nano
:sudo nano /etc/modprobe.d/raspi-blacklist.conf
#
before the following line:#blacklist spi-bcm2708
modprobe
command:sudo modprobe spi-bcm2708
wget http://pi.cs.man.ac .uk/download/old_install.txt
The installation packages for the PiFace module are available as Debian packages. Refer to the installation guide from http://www.piface.org.uk/guides/. If you install the Debian packages, the Python module imports and the methods used to read the inputs might also vary accordingly.
mv install.txt install.sh chmod +x install.sh ./install.sh
sudo reboot
Piface/scripts/piface-emulator
import piface.pfio as pfio
piface.pfio
module needs to be imported and initialized:pfio.init()
read_input()
method returns an 8-bit number that represents the input states of all the eight buttons:while True: print(pfio.read_input())
S7 |
S6 |
S5 |
S4 |
S3 |
S2 |
S1 |
S0 |
Switch positions represented by bit positions
S0
is pressed, the bit 0 is set and hence the 8-bit word value is 1. When S1
is pressed, the bit 1 is set and the 8-bit word value is 2 and so on.getHumanMove()
function.animateComputerMoving(board,column)
method and rename it as animatePlayerMoving(board,column)
.animatePlayerMoving(board,column)
method to simulate the human player's coin movements.animatePlayerMoving
method:x = REDPILERECT.left y = REDPILERECT.top drawBoard(board, {'x':x, 'y':y, 'color':RED}) animateDroppingToken(board, column, RED)
if turn == HUMAN: animatePlayerMoving(mainBoard,0) makeMove(mainBoard, RED, 0) moves_sound.play() if isWinner(mainBoard, RED): winnerImg = HUMANWINNERIMG break turn = COMPUTER # switch to other player's turn
#wait for a button press while column == 0: column = pfio.read_input() column_position = 0 if column == 1: column_position = 0 elif column == 2: column_position = 1 elif column == 4: column_position = 2 elif column == 8: column_position = 3 elif column == 16: column_position = 4 elif column == 32: column_position = 5 elif column == 64: column_position = 6
read_input()
function to detect any button press. When a button is pressed, the returned value can be anywhere between 1 and 64. The return value is used to identify the column position and the coin movement is simulated.if e.type == EVENT_CHANGE_STATE: if state == 0: rect_list,state = menu.update(e,state) column = 0 while column == 0: column = pfio.read_input() pygame.display.update() if column == 1: state = 1 elif column == 2: state = 2 if state == 1: menu_sound.stop() runGame(isFirstGame) isFirstGame = False state = 0 prev_state = 1 elif state == 2 : print 'Exit' pygame.quit() sys.exit()
3.23.126.63