Hardware Reference
In-Depth Information
Engage thrusters
1.
Let's test the butons available on the PiFace module using a Python script:
import piface.pfio as pfio
2.
The piface.pfio module needs to be imported and iniialized:
pfio.init()
3.
The read_input() method returns an 8-bit number that represents the
input states of all the eight butons:
while True:
print(pfio.read_input())
4. Each bit of the 8-bit number corresponds to a digital input on the PiFace module.
The following table shows the switch posiions that are represented by bit posiions:
S7 S6 S5 S4 S3
S2
S1
S0
Switch positions represented by bit positions
5. When a switch is pressed, a paricular bit is set and this bit can be used to interpret
the column posiion of the Connect Four game.
6. For example, when 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.
7. We ignore scenarios when two switches are pressed at the same ime as the
Connect Four game involves playing only one column at a ime.
8. The Connect Four game was designed to be played using a mouse. Let's perform
some tweaks to play it using butons. Let's get started by commening out the
getHumanMove() funcion.
9. When the game is played using a mouse, the coin is actually dragged and dropped
by the player. This needs to be simulated while using the butons. In order to do so,
let's create a copy of the animateComputerMoving(board,column) method
and rename it as animatePlayerMoving(board,column) .
10. In the game, the computer plays the red colour coins while the human player plays
with the black colour coins. So, let's change the animatePlayerMoving(board,
column) method to simulate the human player's coin movements.
11. The following are the modiicaions required in the animatePlayerMoving method:
x = REDPILERECT.left
y = REDPILERECT.top
drawBoard(board, {'x':x, 'y':y, 'color':RED})
animateDroppingToken(board, column, RED)
 
Search WWH ::




Custom Search