-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
144 lines (110 loc) · 3.05 KB
/
Copy pathgame.py
File metadata and controls
144 lines (110 loc) · 3.05 KB
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
# coding: utf-8
import ui
import random
BG_CELL = (0.9, 0.9, 0.9, 1.0)
BG_BOARD = (0.9, 0.85, 0.8, 1.0)
class Board (ui.View):
def __init__(self):
self.width = 1024
self.height = 700
self.background_color = BG_BOARD
def add_cell(self, text, col, row, touch_handler):
x_pos = ((self.width / 5) * col) - (self.width / 10)
y_pos = ((self.height / 5) * row) - (self.height / 10)
cell = Cell(text, x_pos, y_pos, touch_handler)
self.add_subview(cell)
class Cell (ui.View):
def __init__(self, text, x, y, touch_handler):
#self.multitouch_enabled = False
self.is_clicked = False
self.name = text
self.background_color = BG_CELL
self.border_width = 1.0
self.border_color = 0.3
self.width = 200
self.height = 100
#self.flex = 'WH'
self.corner_radius = 5
self.center = (x, y)
label = ui.Label(name=text)
#label.flex = 'LRT'
label.font = ('<system-bold>', 24)
label.text = text
label.alignment = ui.ALIGN_CENTER
label.size_to_fit()
label.center = (self.width / 2, self.height / 2)
#label.line_break_mode = ui.LB_CLIP
#label.number_of_lines = 1
self.add_subview(label)
self.label = label
self.touch_handler = touch_handler
self.last_ended_time = 0
def touch_ended(self, touch):
# touch ended outside view, ignore it
if not 0 < touch.location[0] < self.width:
return
if not 0 < touch.location[1] < self.height:
return
# doubleclick checker
if touch.timestamp - self.last_ended_time <= 0.2:
for subview in self.superview.subviews:
subview.background_color = BG_CELL
subview.label.text_color = 'black'
self.superview.background_color = BG_BOARD
return
self.last_ended_time = touch.timestamp
# register as a proper click
self.touch_handler(self)
def turn_red(self):
self.background_color = (1.0, 0, 0, 1)
self.label.text_color = 'white'
def turn_blue(self):
self.background_color = (0, 0, 1.0, 1)
self.label.text_color = 'white'
def turn_grey(self):
self.background_color = (0.5, 0.5, 0.5, 1)
self.label.text_color = 'white'
def turn_black(self):
self.background_color = (0, 0, 0, 1)
self.label.text_color = 'white'
def touch_handler(cell):
global red_words, blue_words
global red_revealed, blue_revealed
if cell.name in red_words:
cell.turn_red()
red_revealed += 1
elif cell.name in blue_words:
cell.turn_blue()
blue_revealed += 1
elif cell.name in assassin:
cell.turn_black()
else:
cell.turn_grey()
WORDLIST = []
with open("wordlist.txt", 'r') as f:
WORDLIST = f.readlines()
red_index = random.randint(8,9)
blue_index = 17 - red_index
words = random.sample(WORDLIST, 25)
assassin = words[0]
red_words = words[1:red_index+1]
blue_words = words[red_index+1:18]
red_revealed = 0
blue_revealed = 0
v = Board()
random.shuffle(words)
col = 1
row = 1
for word in words:
v.add_cell(word, col, row, touch_handler)
col += 1
if col == 6:
col = 1
row += 1
for cell in v.subviews:
touch_handler(cell)
if len(red_words) > len(blue_words):
v.background_color = '#ffaaaa'
else:
v.background_color = '#aac8ff'
v.present('full_screen')