-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic_Tac_Sense_Hat_Joystick.py
136 lines (118 loc) · 4.37 KB
/
Tic_Tac_Sense_Hat_Joystick.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
"""
**Name:** While loop calculator
**Author:** Christoffer Thorske Johnsen
**Created:** 04.01.2019
"""
import time
from sense_hat import SenseHat
sense = SenseHat()
def tic_tac():
X = [255, 0, 0] # Red
O = [0, 0, 255] # Blue
B = [0, 0, 0] #Black
W = [255, 255, 255] #White
brett = [
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W,
B, B, B, B, B, B, B, B,
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W,
B, B, B, B, B, B, B, B,
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W
]
opp_del = ([6, 7, 14, 15], [3, 4, 11, 12], [0, 1, 8, 9],
[30, 31, 38, 39], [27, 28, 35, 36], [24, 25, 32, 33],
[54, 55, 62, 63], [51, 52, 59, 60], [48, 49, 56, 57])
win_pos = ((0, 3, 6), (24, 27, 30), (48, 51, 54), (0, 3, 6), (3, 27, 51),
(6, 30, 54), (0, 27, 54), (6, 27, 48))
end = False
def draw_brett():
sense.set_pixels(brett)
def pl_1():
sense.set_rotation(180)
sense.show_letter("1")
time.sleep(0.6)
draw_brett()
n = choose_numb()
i = opp_del[n][0]
if brett[i] == [255, 0, 0] or brett[i] == [0, 0, 255]:
print('The place is not free!')
pl_1()
else:
for j in opp_del[n]:
brett[j] = [255, 0, 0]
def pl_2():
sense.set_rotation(180)
sense.show_letter("2")
time.sleep(0.6)
draw_brett()
n = choose_numb()
i = opp_del[n][0]
if brett[i] == [255, 0, 0] or brett[i] == [0, 0, 255]:
print('The place is not free!')
pl_2()
else:
for j in opp_del[n]:
brett[j] = [0, 0, 255]
def choose_numb():
while True:
for event in sense.stick.get_events():
if event.action == 'pressed' and event.direction == 'middle':
return 4
elif event.action == 'released' and event.direction == 'down':
while True:
for event in sense.stick.get_events():
if event.action == 'pressed' and event.direction == 'right':
return 2
if event.action == 'pressed' and event.direction == 'left':
return 0
if event.action == 'pressed' and event.direction == 'middle':
return 1
elif event.action == 'released' and event.direction == 'up':
while True:
for event in sense.stick.get_events():
if event.action == 'pressed' and event.direction == 'right':
return 8
if event.action == 'pressed' and event.direction == 'left':
return 6
if event.action == 'pressed' and event.direction == 'middle':
return 7
elif event.action == 'pressed' and event.direction == 'right':
return 5
elif event.action == 'pressed' and event.direction == 'left':
return 3
def win():
for a, b, c in win_pos:
if brett[a] == [0, 0, 255] and brett[b] == [0, 0, 255] and brett[c] == [0, 0, 255] or brett[a] == [255, 0, 0] and brett[b] == [255, 0, 0] and brett[c] == [255, 0, 0]:
print('Congratulations!\n')
return True
if 36 == sum((pos == [0, 0, 255] or pos == [255, 0, 0]) for pos in brett): #Need to change
print("The game ends in a tie\n")
sense.set_rotation(180)
sense.show_message("Tie")
return True
while not end:
draw_brett()
end = win()
if end == True:
print('Player 2 won!!')
sense.set_rotation(180)
sense.show_letter("2")
time.sleep(1)
sense.clear()
break
print("Player 1 choose where to place red marke")
pl_1()
draw_brett()
end = win()
if end == True:
print('Player 1 won!!')
sense.set_rotation(180)
sense.show_letter("1")
time.sleep(3)
sense.clear()
break
print("Player 2 choose where to place blue marke")
pl_2()
tic_tac()