-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
497 lines (397 loc) · 14.6 KB
/
main.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from kivy.app import App
from kivy.animation import Animation
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, StringProperty, ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.config import Config
from kivy.clock import Clock
from kivy.uix.image import Image
import sys
import random
import time
# Default Size of Window
width, height = Window.size
# Config of Window
Config.set('graphics', 'width', str(width))
Config.set('graphics', 'height', str(height))
Config.set('graphics', 'resizable', False)
Config.write()
# Markup
Builder.load_file('common.kv')
# Widget "Ball"
class Ball(Widget):
ball_status = ObjectProperty()
im = Image()
ball_color = ObjectProperty()
ball_num = ObjectProperty()
ball_side = ObjectProperty()
size_window = Window.size
def __init__(self, color="black", num="1", side="left", **kwargs):
self.ball_status = str("rings/balls/" + color + "/" + side + "/" + num + ".png")
super(Ball, self).__init__(**kwargs)
self.ball_color = color
self.ball_num = num
self.ball_side = side
def update(self, color="black", num="1", side="left"):
self.ball_color = color
self.ball_num = num
self.ball_side = side
self.ball_status = str("rings/balls/" + color + "/" + side + "/" + num + ".png")
# Widget "BackGround"
class BackGround(Widget):
size_window = Window.size
pass
# class Task. Generate color and count of balls
class Task(Widget):
size_window = Window.size
text_task = StringProperty()
color = ObjectProperty('None')
count = ObjectProperty(0)
# Randomize color
def rand_color(self):
color = random.choice(['green', 'red', 'yellow', 'black', 'white'])
return color
# Give new Task
def update(self, balls_color, combo):
self.color = self.rand_color()
self.count = random.choice([i for i in range(1, balls_color[self.color])])
self.text_task = self.color + ' ' + str(self.count)
pair = [self.count, self.color]
while pair in combo:
self.color = self.rand_color()
self.count = random.choice([i for i in range(1, balls_color[self.color])])
self.text_task = self.color + ' ' + str(self.count)
pair = [self.count, self.color]
return self.text_task
# Widget "Timer"
class MyClock(Label):
a = NumericProperty(0)
pos_hint_x = 0
pos_hint_y = 0
font_size = 70
def start(self):
Animation.cancel_all(self)
self.anim = Animation(a=0, duration=self.a)
def finish_callback(animation, clock):
clock.text = "--:--"
self.anim.bind(on_complete=finish_callback)
self.anim.start(self)
def reset(self):
self.a = 40
def pause(self):
self.anim.stop(self)
def t_continue(self):
self.anim.start(self)
def on_a(self, instance, value):
self.text = str(round(value, 1))
# Widget "Timer"
class MyClockCountdown(Label):
a = NumericProperty(0)
pos_hint_x = 0
pos_hint_y = 0
font_size = 70
def start(self):
Animation.cancel_all(self)
self.anim = Animation(a=0, duration=self.a)
def finish_callback(animation, clock):
clock.text = "Start"
time.sleep(1)
TouchTheRingsApp.menu.countdown.remove_widget(self)
TouchTheRingsApp.menu.remove_widget(TouchTheRingsApp.menu.countdown)
self.anim.bind(on_complete=finish_callback)
self.anim.start(self)
def reset(self):
self.a = 40
def pause(self):
self.anim.stop(self)
def t_continue(self):
self.anim.start(self)
def on_a(self, instance, value):
self.text = str(round(value, 1))
# Widget "Task"
class TaskLabel(Label):
pass
# Widget "Score"
class ScoreLabel(Label):
pass
class Pause(Widget):
size_window = Window.size
def Continue(self):
TouchTheRingsApp.menu.game.timer.start()
TouchTheRingsApp.menu.game.remove_widget(self)
return 'Continue'
def MainMenu(self):
TouchTheRingsApp.menu.game.remove_widget(self)
TouchTheRingsApp.menu.remove_widget(TouchTheRingsApp.menu.game)
# Game Engine
class RingsGame(Widget):
# Window settings
size_window = Window.size
background = BackGround()
pause = Pause()
# Objects in GamePlay
score_text = StringProperty()
text_task = ObjectProperty()
score = NumericProperty(0)
label = FloatLayout()
timer = MyClock()
task = Task()
# Balls
balls = dict()
balls_color = {'green': 0, 'red': 0, 'yellow': 0, 'black': 0, 'white': 0}
combo_color = []
# Balls graph
common_ball_1 = ObjectProperty()
common_ball_20 = ObjectProperty()
graph = [[0] * 53 for i in range(0, 53)]
# Balls update after pressing
def balls_update(self, side):
if side == 'left':
# Re-linking of Left Circle ( Switching only colors)
local = self.balls[('left', 27)].ball_color
for i in range(0, 26):
self.balls[('left', 27 - i)].ball_color = self.balls[('left', 27 - i - 1)].ball_color
self.balls[('left', 1)].ball_color = local
for i in range(1, 28):
self.balls[('left', i)].update(self.balls[('left', i)].ball_color, self.balls[('left', i)].ball_num,
self.balls[('left', i)].ball_side)
# self.add_widget(self.balls[('left', i)])
if side == 'right':
# Re-linking of Right Circle
# First an twentieth Balls are common
self.common_ball_1 = self.balls[('left', 1)].ball_color
self.common_ball_20 = self.balls[('left', 20)].ball_color
# Give new colors to common balls, before that backup 1 and 20 balls
self.balls[('left', 1)].ball_color = self.balls[('right', 27)].ball_color
self.balls[('left', 20)].ball_color = self.balls[('right', 19)].ball_color
# Updating balls on the layer
self.balls[('left', 1)].update(self.balls[('left', 1)].ball_color,
self.balls[('left', 1)].ball_num,
self.balls[('left', 1)].ball_side)
self.balls[('left', 20)].update(self.balls[('left', 20)].ball_color,
self.balls[('left', 20)].ball_num,
self.balls[('left', 20)].ball_side)
# Switching Colors in circle
for i in range(0, 6):
self.balls[('right', 27 - i)].ball_color = self.balls[('right', 27 - i - 1)].ball_color
for i in range(8, 25):
self.balls[('right', 27 - i)].ball_color = self.balls[('right', 27 - i - 1)].ball_color
# Give colors of common balls to neighbours
self.balls[('right', 2)].ball_color = self.common_ball_1
self.balls[('right', 21)].ball_color = self.common_ball_20
# Updating balls on the layer
self.balls[('right', 2)].update(self.balls[('right', 2)].ball_color,
self.balls[('right', 2)].ball_num,
self.balls[('right', 2)].ball_side)
self.balls[('right', 21)].update(self.balls[('right', 21)].ball_color,
self.balls[('right', 21)].ball_num,
self.balls[('right', 21)].ball_side)
# Updating balls on the layer
for i in range(1, 28):
if i != 1 and i != 20:
self.balls[('right', i)].update(self.balls[('right', i)].ball_color,
self.balls[('right', i)].ball_num,
self.balls[('right', i)].ball_side)
# Randomizer of color
def rand_color(self):
color = random.choice(['green', 'red', 'yellow', 'black', 'white'])
self.balls_color[color] += 1
return color
# Initialize main Objects
def __init__(self, **kwargs):
super(RingsGame, self).__init__(**kwargs)
# left
for i in range(2, 27):
self.graph[i][i + 1] = 1
self.graph[i + 1][i] = 1
self.graph[i][i - 1] = 1
self.graph[i - 1][i] = 1
self.graph[1][27] = 1
self.graph[27][1] = 1
self.graph[1][2] = 1
self.graph[27][26] = 1
# right
for i in range(29, 52):
self.graph[i][i + 1] = 1
self.graph[i + 1][i] = 1
self.graph[i][i - 1] = 1
self.graph[i - 1][i] = 1
self.graph[28][52] = 1
self.graph[28][29] = 1
self.graph[52][28] = 1
self.graph[52][51] = 1
# common
self.graph[1][28] = 1
self.graph[28][1] = 1
self.graph[1][52] = 1
self.graph[52][1] = 1
self.graph[20][45] = 1
self.graph[45][20] = 1
self.graph[20][46] = 1
self.graph[46][20] = 1
for i in range(1, 28):
self.balls[('left', i)] = Ball(self.rand_color(), str(i), 'left')
self.add_widget(self.balls[('left', i)])
for i in range(1, 28):
if i != 1 and i != 20:
self.balls[('right', i)] = Ball(self.rand_color(), str(i), 'right')
self.add_widget(self.balls[('right', i)])
self.score = 0
self.score_text = str(self.score)
self.label.add_widget(self.timer)
self.add_widget(self.label)
self.analysis()
# Control
def press(self, num_btn):
if num_btn == 1:
self.preparing_analysis()
self.balls_update('left')
self.simplified_analysis()
if num_btn == 2:
self.preparing_analysis()
self.balls_update('right')
self.simplified_analysis()
# Convert to key for dict of colors in circles
def convert_to_key(self, num):
key = ('side', num)
if num < 28:
key = ('left', num)
else:
if num < 27 + 19:
key = ('right', num - 27 + 1)
else:
key = ('right', num - 27 + 2)
return key
# Search all combo's
def analysis(self):
used = set()
for v in range(1, 53):
q = [v]
color = self.balls[self.convert_to_key(v)].ball_color
used.add(v)
count = 1
while len(q) > 0:
now = q.pop(0)
for i in range(1, 53):
if self.graph[now][i] == 1:
if i not in used and color == self.balls[self.convert_to_key(i)].ball_color:
q.append(i)
used.add(i)
count += 1
while count > 0:
self.combo_color.append([count, color])
count -= 1
# Simplified preparing to Analysis of combo's
def preparing_analysis(self):
used = set()
q_g = [1, 20]
for v in q_g:
q = [v]
color = self.balls[self.convert_to_key(v)].ball_color
used.add(v)
count = 1
while len(q) > 0:
now = q.pop(0)
for i in range(1, 53):
if self.graph[now][i] == 1:
if i not in used and color == self.balls[self.convert_to_key(i)].ball_color:
q.append(i)
used.add(i)
count += 1
while count > 0:
self.combo_color.pop(self.combo_color.index([count, color]))
count -= 1
# Simplified Analyze of combo's
def simplified_analysis(self):
used = set()
q_g = [1, 20]
for v in q_g:
q = [v]
color = self.balls[self.convert_to_key(v)].ball_color
used.add(v)
count = 1
while len(q) > 0:
now = q.pop(0)
for i in range(1, 53):
if self.graph[now][i] == 1:
if i not in used and color == self.balls[self.convert_to_key(i)].ball_color:
q.append(i)
used.add(i)
count += 1
while count > 0:
self.combo_color.append([count, color])
count -= 1
# Give a new task
def set_new_task(self):
return self.task.update(self.balls_color, self.combo_color)
# Update main Game engine
def update(self, dt):
if self.timer.a == 0:
self.text_task = self.set_new_task()
self.timer.reset()
self.timer.start()
pair = [self.task.count, self.task.color]
if pair in self.combo_color:
self.text_task = self.set_new_task()
self.timer.reset()
self.timer.start()
self.score += 1
self.score_text = str(self.score)
def press_pause(self):
self.timer.pause()
self.add_widget(self.pause)
def reset(self):
self.timer.start()
self.timer.reset()
self.text_task = self.set_new_task()
class Exit_window(Widget):
size_window = Window.size
def Cancel(self):
return 'Cancel'
def __del__(self):
TouchTheRingsApp.menu.remove_widget(self)
def exit(self):
sys.exit()
class Countdown(Widget):
size_window = Window.size
clock = MyClock()
def start(self):
self.clock.reset()
self.clock.start()
class Rules(Widget):
size_window = Window.size
def return_Menu(self):
TouchTheRingsApp.menu.remove_widget(self)
class Author_widget(Widget):
size_window = Window.size
def return_Menu(self):
TouchTheRingsApp.menu.remove_widget(self)
class Menu(Widget):
size_window = Window.size
exit = Exit_window()
game = RingsGame()
countdown = Countdown()
rules = Rules()
author = Author_widget()
def press_Game(self):
self.game.reset()
# self.countdown.start()
# self.add_widget(self.countdown)
Clock.schedule_interval(self.game.update, 1 / 90)
self.add_widget(self.game)
def press_Author(self):
self.add_widget(self.author)
def press_Rules(self):
self.add_widget(self.rules)
def press_Exit(self):
self.add_widget(self.exit)
# App
class TouchTheRingsApp(App):
menu = Menu()
def build(self):
return self.menu
if __name__ == '__main__':
TouchTheRingsApp().run()