1+ from ursina import *
2+ app = Ursina ()
3+
4+ camera .orthographic = True
5+ camera .fov = 4
6+ camera .position = (1 ,1 )
7+
8+ player = Entity (name = 'O' ,color = color .azure )
9+ cursor = Tooltip (color = player .color ,origin = (0 ,0 ),scale = 4 ,enabled = True )
10+ cursor .background .color = color .clear
11+
12+ bg = Entity (parent = scene ,model = 'quad' ,texture = 'shore' ,scale = (16 ,8 ),z = 10 ,color = color .light_gray )
13+ mouse .visible = True
14+
15+ board = [[None for x in range (3 )]for y in range (3 )]
16+ for y in range (3 ):
17+ for x in range (3 ):
18+ b = Button (parent = scene ,position = (x ,y ))
19+ board [x ][y ]= b
20+
21+ def on_click (b = b ):
22+ b .text = player .name
23+ b .color = player .color
24+ b .collision = False
25+ checkforvictory ()
26+
27+ if player .name == 'O' :
28+ player .name = 'X'
29+ player .color = color .orange
30+ else :
31+ player .name = 'O'
32+ player .color = color .azure
33+
34+ cursor .text = ''
35+ cursor .color = player .color
36+ b .on_click = on_click
37+
38+ def checkforvictory ():
39+ name = player .name
40+ won = (
41+ (board [0 ][0 ].text == name and board [1 ][0 ].text == name and board [2 ][0 ].text == name ) or # across the bottom
42+ (board [0 ][1 ].text == name and board [1 ][1 ].text == name and board [2 ][1 ].text == name ) or # across the middle
43+ (board [0 ][2 ].text == name and board [1 ][2 ].text == name and board [2 ][2 ].text == name ) or # across the top
44+ (board [0 ][0 ].text == name and board [0 ][1 ].text == name and board [0 ][2 ].text == name ) or # down the left side
45+ (board [1 ][0 ].text == name and board [1 ][1 ].text == name and board [1 ][2 ].text == name ) or # down the middle
46+ (board [2 ][0 ].text == name and board [2 ][1 ].text == name and board [2 ][2 ].text == name ) or # down the right side
47+ (board [0 ][0 ].text == name and board [1 ][1 ].text == name and board [2 ][2 ].text == name ) or # diagonal /
48+ (board [0 ][2 ].text == name and board [1 ][1 ].text == name and board [2 ][0 ].text == name )) # diagonal \
49+
50+
51+ if won :
52+ print ('Winner is:' ,name )
53+ destroy (cursor )
54+ mouse .visible = True
55+ Panel (z = 1 , scale = 10 , model = 'quad' )
56+ t = Text (f'Player\n { name } \n won!' , scale = 3 , origin = (0 ,0 ), background = True )
57+ t .create_background (padding = (.5 ,.25 ), radius = Text .size / 2 )
58+ t .background .color = player .color .tint (- .2 )
59+ app .run ()
60+
0 commit comments