-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamescreen.py
438 lines (365 loc) · 12.4 KB
/
gamescreen.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""
Created on Saturday, May 7th, 2018
@author: Sagar Kishore
The game screen which implements the flash card algorithm and provides the
feature to continue where you left off.
"""
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.animation import Animation
from kivy.properties import NumericProperty
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.lang import Builder
from random import randrange
from helperfuncs import word_description, play
class GameScreen(Screen):
Builder.load_string("""
<GameScreen>:
name: 'game screen'
id: RootFL
Button:
text: 'Go back to Stack List'
pos_hint: {'x': 0.025, 'y': 0.85}
size_hint: 0.15, 0.1
on_release: root.save_changes()
BoxLayout:
orientation: 'vertical'
pos_hint: {'center_x': 0.5, 'y': 0.025}
size_hint: 0.5, 0.3
padding: 10, 10
spacing: 5
canvas.before:
Color:
rgba: 1, 1, 1, 0.1
Rectangle:
pos: self.pos
size: self.size
GamePFL:
id: NewPFL
canvas.before:
Color:
rgba: 1, 1, 1, 0.1
Rectangle:
pos: self.pos
size: self.size
GamePFL:
id: MasteredPFL
canvas.before:
Color:
rgba: 0, 1, 0, 0.1
Rectangle:
pos: self.pos
size: self.size
GamePFL:
id: ReviewingPFL
canvas.before:
Color:
rgba: 1, 1, 0, 0.1
Rectangle:
pos: self.pos
size: self.size
GamePFL:
id: LearningPFL
canvas.before:
Color:
rgba: 1, 0, 0, 0.1
Rectangle:
pos: self.pos
size: self.size
<GamePFL@ProgressFloatLayout>:
ProgressBar:
id: PB
pos_hint: {'center_x': 0.5, 'y': 0.3}
size_hint: 0.8, 0.05
# max: root.max_value
Label:
id: Lbl
pos_hint: {'center_x': 0.5, 'y': 0.15}
font_size: 24
""")
game_stack = ObjectProperty(None)
def on_enter(self, *args, **kwargs):
super(GameScreen, self).__init__(*args, **kwargs)
self.rank_dict = self.game_stack.rank_dict
self.max_value = self.game_stack.size
self.ids.NewPFL.ids.PB.max = self.max_value
self.ids.MasteredPFL.ids.PB.max = self.max_value
self.ids.ReviewingPFL.ids.PB.max = self.max_value
self.ids.LearningPFL.ids.PB.max = self.max_value
self.AL = AnimatedLayout()
self.next_word()
self.add_widget(self.AL)
self.update_progressbars()
def on_leave(self):
self.clear_widgets()
def next_word(self, word_rank=None):
old_word = None
if(word_rank is not None):
old_word, new_rank = word_rank
print(f'{old_word.name} will be added to rank {new_rank}')
self.rank_dict[new_rank].add(old_word)
self.update_progressbars()
if(len(self.rank_dict['-1']) == 0):
bottom1, bottom2 = self.updatebottom()
if(len(self.rank_dict['0']) > 10):
new_flag = False
if(len(self.rank_dict['0']) < 16):
new_flag = True
bottom1, bottom2 = self.updatebottom(new_flag)
if(len(self.rank_dict['0']) < 11 and len(self.rank_dict['-1']) > 0):
bottom1 = '-1'
bottom2 = None
# print('bottom1 is -->', bottom1)
# print('bottom2 is -->', bottom2)
word, rank = self.choosefrombottom2(bottom1, bottom2)
while(old_word == word):
try:
new_word, new_rank = self.choosefrombottom2(bottom1, bottom2)
self.rank_dict[rank].add(word)
word, rank = new_word, new_rank
except KeyError:
pass
# print(f"sending word {word.name} to AL of rank {rank}")
self.AL.rank, self.AL.word = (str(rank), word)
print(f'selected word {word.name} from rank {rank}')
print('-------------------------------------------------------')
print('-------------------------------------------------------')
print('-------------------------------------------------------')
print('-------------------------------------------------------')
def choosefrombottom2(self, bottom1, bottom2):
p = randrange(1, 11)
print('probability is -->', p)
print('bottom1 is -->', bottom1)
print('bottom2 is -->', bottom2)
if(p < 8 or bottom2 is None):
return (self.rank_dict[str(bottom1)].pop(), str(bottom1))
else:
return (self.rank_dict[str(bottom2)].pop(), str(bottom2))
def updatebottom(self, new_flag=False):
rd = self.rank_dict
if(new_flag):
lenlist = [len(rd[x]) for x in rd]
else:
lenlist = [0] + [len(rd[x]) for x in rd if x != '-1']
print('lenlist is -->', lenlist)
non_zero_list = []
for i, x in enumerate(lenlist):
if(x > 0):
non_zero_list.append(str(i - 1))
try:
bottom1, bottom2 = non_zero_list[:2]
except ValueError:
bottom1 = non_zero_list[0]
bottom2 = None
print(f'returning --> {bottom1}, {bottom2}')
return (bottom1, bottom2)
def update_progressbars(self):
reviewing_set = self.rank_dict['1'].union(self.rank_dict['2'],
self.rank_dict['3'],
self.rank_dict['4'],
self.rank_dict['5'])
self.ids.NewPFL.ids.PB.value = len(self.rank_dict['-1'])
self.ids.MasteredPFL.ids.PB.value = len(self.rank_dict['6'])
self.ids.ReviewingPFL.ids.PB.value = len(reviewing_set)
self.ids.LearningPFL.ids.PB.value = len(self.rank_dict['0'])
self.ids.NewPFL.ids.Lbl.text = \
f"New words left {len(self.rank_dict['-1'])} / {self.max_value}"
self.ids.MasteredPFL.ids.Lbl.text = \
f"Mastered {len(self.rank_dict['6'])} / {self.max_value} words"
self.ids.ReviewingPFL.ids.Lbl.text = \
f"Reviewing {len(reviewing_set)} / {self.max_value} words"
self.ids.LearningPFL.ids.Lbl.text = \
f"Learning {len(self.rank_dict['0'])} / {self.max_value} words"
def save_changes(self):
self.rank_dict[self.AL.rank].add(self.AL.word)
self.game_stack.rank_dict = self.rank_dict
self.manager.current = 'stack screen'
class ProgressFloatLayout(FloatLayout):
max_value = NumericProperty(-1)
class AnimatedLayout(BoxLayout):
Builder.load_string("""
<AnimatedLayout>:
id: AL
cols: 1
rows: 1
canvas.before:
PushMatrix
Rectangle:
pos: self.pos
size: self.size
Rotate:
angle: self.angle
axis: 0, 1, 0
origin: self.center
canvas.after:
PopMatrix
pos_hint: {'center_x': 0.5, 'y': 0.7}
size_hint_x: 0.5
size_hint_y: 0.3
<FrontFace>:
cols: 1
rows: 1
size_hint: 1, 1
canvas.before:
Rectangle:
pos: root.pos
size: root.size
FloatLayout:
pos: root.pos
Button:
id: word_btn
text: ''
size_hint: 1, 1
pos: root.pos
font_size: 45
Label:
id: rank_label
pos_hint: {'x': 0.35, 'y': 0.2}
font_size: 28
<BackFace>:
rows: 3
cols: 1
orientation: 'vertical'
canvas.before:
Color:
rgba: 0.3, 0.3, 0.3, 1
Rectangle:
pos: root.pos
size: root.size
RelativeLayout:
canvas.before:
Color:
rgba: 0, 0.51, 0.8, 0.4
Rectangle:
pos: FirstBox.pos
size: FirstBox.size
orientation: 'horizontal'
id: FirstBox
size_hint: 1, 0.2
Label:
id: WordNameLabel
font_size: 48
Label:
id: rank_label
pos_hint: {'x': 0.35, 'y': 0.2}
font_size: 28
Button:
text: 'Audio'
size_hint: 0.1, 0.2
pos_hint: {'x': 0.6, 'y': 0.6}
on_release: root.parent.pronunciation(self)
BoxLayout:
canvas.before:
Color:
rgba: 0.3, 0.1, 0.8, 0.4
Rectangle:
pos: SecondBox.pos
size: SecondBox.size
orientation: 'horizontal'
id: SecondBox
size_hint: 1, 0.7
ScrollView:
id: SV
height: root.height * 0.9
Label:
id: WordDescriptionLabel
text: 'word desc goes here'
height: self.texture_size[1]
size_hint_y: None
size: SecondBox.size[0] * 0.85, SecondBox.size[1] * 0.85
text_size: self.width, None
halign: 'left'
valign: 'middle'
markup: True
padding: 100, 100
BoxLayout:
orientation: 'horizontal'
id: ThirdBox
size_hint: 1, 0.1
Button:
id: YesButton
text:'Yes'
on_release: root.parent.yes()
Button:
id: NoButton
text:'No'
on_release: root.parent.no()
""")
angle = NumericProperty(0)
word = ObjectProperty(None)
rank = StringProperty()
def on_word(self, animated_layout, new_word):
self.draw_front_face()
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
def flip_animation(self):
anim = Animation(angle=360, duration=0.4, s=1 / 60, t='in_expo')
anim.start(self)
Clock.schedule_once(self.draw_back_face, anim.duration) # + 0.1)
def slide_animation(self):
pass
def draw_front_face(self):
self.pos_hint = {'center_x': 0.5, 'y': 0.75}
self.size_hint_y = 0.2
self.clear_widgets()
self.front_face = FrontFace()
self.front_face.ids.word_btn.text = self.word.name.title()
self.front_face.ids.word_btn.on_release = self.flip_animation
label_text, label_color = rank_text(self.rank)
self.front_face.ids.rank_label.text = label_text
self.front_face.ids.rank_label.color = label_color
self.add_widget(self.front_face, index=0)
def draw_back_face(self, dt):
self.size_hint_y = 0.6
self.pos_hint = {'center_x': 0.5, 'center_y': 0.65}
self.clear_widgets()
back_face = BackFace()
back_face.ids.WordNameLabel.text = self.word.name.title()
back_face.ids.WordDescriptionLabel.text = word_description(self.word)
label_text, label_color = rank_text(self.rank)
back_face.ids.rank_label.text = label_text
back_face.ids.rank_label.color = label_color
self.add_widget(back_face)
def yes(self):
if(self.rank < '0' or self.rank == '6'):
self.parent.next_word((self.word, '6'))
else:
self.parent.next_word((self.word, str(int(self.rank) + 1)))
self.slide_animation()
print(self.parent.rank_dict)
def no(self):
self.parent.next_word((self.word, '0'))
self.slide_animation()
print(self.parent.rank_dict)
def pronunciation(self, instance):
ret = play(self.word)
if(ret < 0):
instance.disabled = True
class FrontFace(BoxLayout):
pass
class BackFace(BoxLayout):
pass
class ProgressLabel(Label):
occupancy = NumericProperty(0)
class GameScreenApp(App):
def build(self):
return GameScreen()
def rank_text(rank):
if(rank < '0'):
return ('New Word', [1, 1, 1, 1])
if(rank == '0'):
return ('Learning', [1, 0, 0, 1])
if('0' < rank < '6'):
return ('Reviewing', [1, 1, 0, 1])
if(rank == '6'):
return ('Mastered', [0, 1, 0, 1])
def main():
GameScreenApp().run()
if __name__ == '__main__':
main()