Advertisement
rishabbansal21

tictactoe

May 26th, 2021
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. from ursina import *
  2.  
  3. app = Ursina()
  4.  
  5. player = Entity(name='O', color=color.rgb(178,102,255))
  6.  
  7. cursor = Tooltip(player.name, color=player.color, origin=(0,0), scale=2, enabled=True)
  8. cursor.background.color = color.clear
  9.  
  10. #bg = Entity(parent=scene, model='quad', texture='shore', scale=(16,8), z=10, color=color.light_gray)
  11.  
  12. mouse.visible = False
  13.  
  14. board = [ [None, None, None],
  15.           [None, None, None],
  16.           [None, None, None] ]
  17.  
  18. for x in range(3):
  19.     for y in range(3):
  20.         b = Button(parent=scene, position=(x,y))
  21.         board[x][y] = b
  22.  
  23.         def on_click(b=b):
  24.             b.text = player.name
  25.             b.color = player.color
  26.             b.collision = False
  27.             check_for_victory()
  28.  
  29.             if player.name == 'O':
  30.                 player.name = 'X'
  31.                 player.color = color.rgb(204,255,153)
  32.  
  33.             else:
  34.                 player.name = 'O'
  35.                 player.color = color.rgb(178,102,255)
  36.  
  37.             cursor.text = player.name
  38.             cursor.color = player.color
  39.  
  40.         b.on_click = on_click
  41.  
  42.  
  43. def check_for_victory():
  44.     name = player.name
  45.  
  46.     won = (
  47.     (board[0][0].text == name and board[1][0].text == name and board[2][0].text == name) or # across the bottom
  48.     (board[0][1].text == name and board[1][1].text == name and board[2][1].text == name) or # across the middle
  49.     (board[0][2].text == name and board[1][2].text == name and board[2][2].text == name) or # across the top
  50.     (board[0][0].text == name and board[0][1].text == name and board[0][2].text == name) or # down the left side
  51.     (board[1][0].text == name and board[1][1].text == name and board[1][2].text == name) or # down the middle
  52.     (board[2][0].text == name and board[2][1].text == name and board[2][2].text == name) or # down the right side
  53.     (board[0][0].text == name and board[1][1].text == name and board[2][2].text == name) or # diagonal /
  54.     (board[0][2].text == name and board[1][1].text == name and board[2][0].text == name))   # diagonal \
  55.  
  56.     if won:
  57.         print('winner is:', name)
  58.        
  59. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement