-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
570 lines (509 loc) · 22.1 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import os
from functools import partial
from jnius import autoclass
from kivy.animation import Animation
from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from PIL import Image, ImageDraw
import game
from android.runnable import run_on_ui_thread
Color = autoclass("android.graphics.Color")
WindowManager = autoclass('android.view.WindowManager$LayoutParams')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
fonts_path = os.path.dirname(__file__)
font_path = os.path.join(fonts_path, 'Marvel-Bold.ttf')
sound1_path = os.path.join(fonts_path, 'select.wav')
sound2_path = os.path.join(fonts_path, 'app_start.wav')
sound3_path = os.path.join(fonts_path, 'start.wav')
sound4_path = os.path.join(fonts_path, 'win.wav')
sound5_path = os.path.join(fonts_path, 'lose.wav')
def interpolate(f_co, t_co, interval):
det_co = [(t - f) / interval for f, t in zip(f_co, t_co)]
for i in range(interval):
yield [round(f + det * i) for f, det in zip(f_co, det_co)]
class BG(BoxLayout):
def __init__(self, **kwargs):
super(BG, self).__init__(**kwargs)
Clock.schedule_once(self.bg_apply)
def bg_apply(self, dt):
window_sizes = Window.size
self.w = window_sizes[0]
self.h = window_sizes[1]
gradient = Image.new('RGBA', (self.w, self.h), color=0)
draw = ImageDraw.Draw(gradient)
f_co = (23, 147, 156)
t_co = (176, 232, 112)
for i, color in enumerate(interpolate(f_co, t_co, self.h)):
draw.line([(0, i), (self.w, i)], tuple(color), width=1)
with open('assets/images/bg.png', 'wb') as f:
gradient.save(f)
self.ids.bg.source = 'assets/images/bg.png'
self.ids.bg.reload()
class Main_Card(FloatLayout):
def __init__(self, **kwargs):
super(Main_Card, self).__init__(**kwargs)
class Button_Card(FloatLayout):
def __init__(self, **kwargs):
super(Button_Card, self).__init__(**kwargs)
class Main_Screen(Screen):
def __init__(self, **kwargs):
super(Main_Screen, self).__init__(**kwargs)
self.game_start()
def game_start(self):
self.turn = game.whoGoesFirst()
#self.turn = "player"
print(self.turn)
self.gameIsPlaying = True
if self.turn == "computer":
move = game.getComputerMove(main_app.board, "O")
game.makeMove(main_app.board, "O", move)
self.turn = "player"
elif self.turn == "player":
pass
def computer_move(self):
if self.turn == "computer":
move = game.getComputerMove(main_app.board, "O")
game.makeMove(main_app.board, "O", move)
a = {1: self.o_1, 2: self.o_2, 3: self.o_3, 4: self.o_4,
5: self.o_5, 6: self.o_6, 7: self.o_7, 8: self.o_8, 9: self.o_9}
Clock.schedule_once(partial(self.set_opacity, a[move], 1, 0.1), 1)
if game.isWinner(main_app.board, "O"):
print('The computer has beaten you! You lose.')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "lose"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
self.clear_screen()
main_app.lose.play()
self.manager.current = "tie"
main_app.board = [" "]*10
else:
self.turn = "player"
self.turn = "player"
def on_touch_down(self, touch):
super().on_touch_down(touch)
def clear_screen(self):
Clock.schedule_once(partial(self.clear_opacity, self.x_1), 0)
Clock.schedule_once(partial(self.clear_opacity, self.o_1), 0)
Clock.schedule_once(
partial(self.clear_opacity, self.x_2), 0.15)
Clock.schedule_once(
partial(self.clear_opacity, self.o_2), 0.15)
Clock.schedule_once(partial(self.clear_opacity, self.x_3), 0.3)
Clock.schedule_once(partial(self.clear_opacity, self.o_3), 0.3)
Clock.schedule_once(
partial(self.clear_opacity, self.x_4), 0.45)
Clock.schedule_once(
partial(self.clear_opacity, self.o_4), 0.45)
Clock.schedule_once(partial(self.clear_opacity, self.x_5), 0.6)
Clock.schedule_once(partial(self.clear_opacity, self.o_5), 0.6)
Clock.schedule_once(
partial(self.clear_opacity, self.x_6), 0.75)
Clock.schedule_once(
partial(self.clear_opacity, self.o_6), 0.75)
Clock.schedule_once(
partial(self.clear_opacity, self.x_7), 0.90)
Clock.schedule_once(
partial(self.clear_opacity, self.o_7), 0.90)
Clock.schedule_once(
partial(self.clear_opacity, self.x_8), 1.05)
Clock.schedule_once(
partial(self.clear_opacity, self.o_8), 1.05)
Clock.schedule_once(partial(self.clear_opacity, self.x_9), 1.2)
Clock.schedule_once(partial(self.clear_opacity, self.o_9), 1.2)
main_app.board = [" "]*10
def on_touch_up(self, touch):
super().on_touch_up(touch)
a_delay = 0.05
if touch.spos[1] < 0.118: # Bottom Button
if main_app.select:
main_app.select.stop()
main_app.select.play()
if self.collide_point(*touch.pos):
self.clear_screen()
return False
# 1st button
if 0.15 < touch.spos[0] < 0.355 and 0.576 < touch.spos[1] < 0.696:
if main_app.board[1] != "O" and main_app.board[1] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[1] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_1, 1, a_delay), 0)
self.o_1.opacity = 0
game.makeMove(main_app.board, "X", 1)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 2nd button
if 0.380 < touch.spos[0] < 0.611 and 0.576 < touch.spos[1] < 0.696:
if main_app.board[2] != "O" and main_app.board[2] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[2] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_2, 1, a_delay), 0)
self.o_2.opacity = 0
game.makeMove(main_app.board, "X", 2)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 3rd button
if 0.633 < touch.spos[0] < 0.847 and 0.576 < touch.spos[1] < 0.696:
if main_app.board[3] != "O" and main_app.board[3] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[3] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_3, 1, a_delay), 0)
self.o_3.opacity = 0
game.makeMove(main_app.board, "X", 3)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 4th button
if 0.15 < touch.spos[0] < 0.355 and 0.437 < touch.spos[1] < 0.567:
if main_app.board[4] != "O" and main_app.board[4] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[4] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_4, 1, a_delay), 0)
self.o_4.opacity = 0
game.makeMove(main_app.board, "X", 4)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 5th button
if 0.380 < touch.spos[0] < 0.611 and 0.437 < touch.spos[1] < 0.567:
if main_app.board[5] != "O" and main_app.board[5] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[5] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_5, 1, a_delay), 0)
self.o_5.opacity = 0
game.makeMove(main_app.board, "X", 5)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 6th button
if 0.633 < touch.spos[0] < 0.847 and 0.437 < touch.spos[1] < 0.567:
if main_app.board[6] != "O" and main_app.board[6] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[6] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_6, 1, a_delay), 0)
self.o_6.opacity = 0
game.makeMove(main_app.board, "X", 6)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 7th button
if 0.15 < touch.spos[0] < 0.355 and 0.304 < touch.spos[1] < 0.423:
if main_app.board[7] != "O" and main_app.board[7] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[7] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_7, 1, a_delay), 0)
self.o_7.opacity = 0
game.makeMove(main_app.board, "X", 7)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 8th button
if 0.380 < touch.spos[0] < 0.611 and 0.304 < touch.spos[1] < 0.423:
if main_app.board[8] != "O" and main_app.board[8] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[8] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_8, 1, a_delay), 0)
self.o_8.opacity = 0
game.makeMove(main_app.board, "X", 8)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
# 9th button
if 0.633 < touch.spos[0] < 0.847 and 0.304 < touch.spos[1] < 0.423:
if main_app.board[9] != "O" and main_app.board[9] != "X" and self.turn == "player":
if main_app.select:
main_app.select.stop()
main_app.select.play()
main_app.board[9] = "X"
Clock.schedule_once(
partial(self.set_opacity, self.x_9, 1, a_delay), 0)
self.o_9.opacity = 0
game.makeMove(main_app.board, "X", 9)
if game.isWinner(main_app.board, "X"):
print('Hooray! You have won the game!')
self.gameIsPlaying = False
main_app.win.play()
self.manager.current = "win"
self.clear_screen()
main_app.board = [" "]*10
else:
if game.isBoardFull(main_app.board):
print('The game is a tie!')
self.gameIsPlaying = False
main_app.lose.play()
self.manager.current = "tie"
self.clear_screen()
main_app.board = [" "]*10
else:
self.turn = 'computer'
self.computer_move()
return False
def on_touch_move(self, touch):
super().on_touch_move(touch)
def clear_opacity(self, image, dt):
anim = Animation(opacity=0, duration=0.25)
anim.start(image)
def set_opacity(self, image, opacity, duration, dt):
anim = Animation(opacity=opacity, duration=duration)
anim.start(image)
class Win_Screen(Screen):
def __init__(self, **kwargs):
super(Win_Screen, self).__init__(**kwargs)
def on_touch_down(self, touch):
super().on_touch_down(touch)
def on_touch_up(self, touch):
super().on_touch_up(touch)
if touch.spos[1] < 0.118: # Bottom Button
if main_app.select:
main_app.select.stop()
main_app.select.play()
if self.collide_point(*touch.pos):
self.manager.current = "intro"
main_app.board = [' '] * 10
return False
def on_touch_move(self, touch):
super().on_touch_move(touch)
class Lose_Screen(Screen):
def __init__(self, **kwargs):
super(Lose_Screen, self).__init__(**kwargs)
def on_touch_down(self, touch):
super().on_touch_down(touch)
def on_touch_up(self, touch):
super().on_touch_up(touch)
if touch.spos[1] < 0.118: # Bottom Button
if main_app.select:
main_app.select.stop()
main_app.select.play()
if self.collide_point(*touch.pos):
self.manager.current = "intro"
main_app.board = [' '] * 10
return False
def on_touch_move(self, touch):
super().on_touch_move(touch)
class Tie_Screen(Screen):
def __init__(self, **kwargs):
super(Tie_Screen, self).__init__(**kwargs)
def on_touch_down(self, touch):
super().on_touch_down(touch)
def on_touch_up(self, touch):
super().on_touch_up(touch)
if touch.spos[1] < 0.118: # Bottom Button
if main_app.select:
main_app.select.stop()
main_app.select.play()
if self.collide_point(*touch.pos):
self.manager.current = "intro"
main_app.board = [' '] * 10
return False
def on_touch_move(self, touch):
super().on_touch_move(touch)
class Intro_Screen(Screen):
def __init__(self, **kwargs):
super(Intro_Screen, self).__init__(**kwargs)
def on_touch_down(self, touch):
super().on_touch_down(touch)
def on_touch_up(self, touch):
super().on_touch_up(touch)
if touch.spos[1] < 0.118: # Bottom Button
main_app.start.play()
if self.collide_point(*touch.pos):
self.manager.current = "main"
return False
def on_touch_move(self, touch):
super().on_touch_move(touch)
class Main_Manager(ScreenManager):
def __init__(self, **kwargs):
super(Main_Manager, self).__init__(**kwargs)
class MainApp(App):
def __init__(self, **kwargs):
super(MainApp, self).__init__(**kwargs)
self.board = [' '] * 10
def build(self):
app = App.get_running_app()
self.app_start = SoundLoader.load('assets/sounds/app_start.wav')
self.app_start.volume = 0.8
self.app_start.play()
self.select = SoundLoader.load('assets/sounds/select.wav')
self.start = SoundLoader.load('assets/sounds/start.wav')
self.win = SoundLoader.load('assets/sounds/win.wav')
self.lose = SoundLoader.load('assets/sounds/lose.wav')
self.select.volume = 0.3
self.lose.volume = 0.8
self.win.volume = 0.8
app.statusbar("#17939C")
@run_on_ui_thread
def statusbar(self, color):
window = activity.getWindow()
window.clearFlags(WindowManager.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.setStatusBarColor(Color.parseColor(color))
window.setNavigationBarColor(Color.parseColor(color))
if __name__ == "__main__":
main_app = MainApp()
main_app.run()