-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
187 lines (143 loc) · 4.55 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
License
-------
GNU GENERAL PUBLIC LICENSE v3
2018
Author
------
Sifan Ye
"""
import sys
import os
import platform
import numpy
import time
if sys.version_info[0] < 3:
from bitalino import BITalino
else:
print("Python Version 3: Using bitalino3X.py")
from bitalino3X import BITalino
def main():
# OS Specific Initializations
clearCmd = "cls||clear"
if platform.system() == 'Windows':
clearCmd = "cls"
print("Using Windows default console size 80x24")
columns = 80
rows = 24
else:
clearCmd = "clear"
rows, columns = os.popen('stty size', 'r').read().split()
print("Connecting to BITalino...")
# Set MAC Address with argument
defaultMACAddress = "20:16:12:21:98:56"
if len(sys.argv) == 2:
macAddress = sys.argv[1]
print("Using address: " + macAddress)
elif len(sys.argv) > 1:
print("Please input only 1 argument, which is the address of the BITalino device.")
print("Running without argument will use default MAC Address = " + defaultMACAddress)
print("Exiting...")
exit()
else:
macAddress = defaultMACAddress
print("Using default MAC address: " + macAddress)
# Setting other attributes
batteryThreshold = 30
acqChannels = [0,1]
samplingRate = 100
nSamples = 20
digitalOutput = [1,1]
# Connect to BITalino
device = BITalino(macAddress)
# Set battery threshold
device.battery(batteryThreshold)
# Read BITalino version
os.system(clearCmd)
print("Device Version:" + device.version())
# Start Acquisition
device.start(samplingRate, acqChannels)
# Take baseline measurement
p1Base = []
p2Base = []
start = time.time()
end = time.time()
samplingTime = 15
print("Sampling for baseline...")
while (end - start) < samplingTime:
# Sampling for baseline
baseSample = device.read(nSamples)
p1Base.append(numpy.mean(baseSample[:,5]))
p2Base.append(numpy.mean(baseSample[:,6]))
end = time.time()
p1B = numpy.mean(p1Base)
p2B = numpy.mean(p2Base)
print("\n")
p1P = "Player 1 Baseline: " + str(p1B)
print(p1P)
p2P = "Player 2 Baseline: " + str(p2B)
print(p2P)
print("\n\n\n\n\n\n")
print("Are you ready for the game? Type 'No' to exit".center(int(columns)," "))
response = sys.stdin.readline().rstrip()
if response == "No":
sys.exit()
print("\n")
print("Starting Game...".center(int(columns), " "))
time.sleep(5)
# Start Game
os.system(clearCmd)
gameRunning = True
player1Progress = 28
while gameRunning:
# While not reaching runningTime, read samples
rawData = device.read(nSamples)
portA1 = rawData[:,5]
#print "Port A1: ", portA1
valueA1 = numpy.mean(portA1 - p1B)
#print "Value A1: ", valueA1
#print ""
portA2 = rawData[:,6]
#print "Port A2: ", portA2
valueA2 = numpy.mean(portA2 - p2B)
#print "Value A2: ", valueA2
#print "\n"
if (valueA2 - valueA1) > 10:
player1Progress-=1
elif (valueA2 - valueA1) > 20:
plater1Progress-=2
elif (valueA1 - valueA2) > 10:
player1Progress+=1
elif (valueA1 - valueA2) > 20:
player1Progress+=2
print("\n\n")
print("Player 1 Reading:".center(int(columns)," "))
print("\n")
print(str(valueA1).center(int(columns)," "))
print("\n\n\n")
print("*****************************I*****************************".center(int(columns)," "))
progress = "P1 *" + ' '*player1Progress + 'O' + ' '*(56-player1Progress) + '* P2'
print(progress.center(int(columns)," "))
print("*****************************I*****************************".center(int(columns)," "))
print("\n\n\n")
print("Player 2 Reading:".center(int(columns)," "))
print("\n")
print(str(valueA2).center(int(columns)," "))
time.sleep(0.2)
os.system(clearCmd)
if player1Progress == 0:
print("\n\n\n\n\n")
print("Player 1 has won".center(int(columns)," "))
gameRunning = False
elif player1Progress == 56:
print("\n\n\n\n\n")
print("Player 2 has won".center(int(columns)," "))
gameRunning = False
# Turn BITalino LED on
device.trigger(digitalOutput)
# Stop acquisition
device.stop()
# Close connection
device.close()
if __name__ == "__main__":
main()