Advertisement
davidhellam

Python: tic-tac-toe 1

Aug 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. from guizero import App, Box, Text, Picture, info
  2.  
  3. app = App(title="Os and Xs", width = 480, height = 320, bg=(204,204,204), layout ="grid")
  4. blank = "25fb.png"
  5. X = "274c.png"
  6. O = "2b55.png"
  7.  
  8. def move1():
  9.     if img_box1.image == blank:
  10.         img_box1.image= img_turn.image
  11.         if img_turn.image == X:
  12.             img_turn.image=O
  13.         else:
  14.             img_turn.image=X
  15.     else:
  16.         info("illegal move","Sorry, that box is already taken!")
  17.  
  18. def move2():
  19.     if img_box2.image == blank:
  20.         img_box2.image= img_turn.image
  21.         if img_turn.image == X:
  22.             img_turn.image=O
  23.         else:
  24.             img_turn.image=X
  25.     else:
  26.         info("illegal move","Sorry, that box is already taken!")
  27.  
  28. def move3():
  29.     if img_box3.image == blank:
  30.         img_box3.image= img_turn.image
  31.         if img_turn.image == X:
  32.             img_turn.image=O
  33.         else:
  34.             img_turn.image=X
  35.     else:
  36.         info("illegal move","Sorry, that box is already taken!")
  37.  
  38. def move4():
  39.     if img_box4.image == blank:
  40.         img_box4.image= img_turn.image
  41.         if img_turn.image == X:
  42.             img_turn.image=O
  43.         else:
  44.             img_turn.image=X
  45.     else:
  46.         info("illegal move","Sorry, that box is already taken!")
  47.  
  48. def move5():
  49.     if img_box5.image == blank:
  50.         img_box5.image= img_turn.image
  51.         if img_turn.image == X:
  52.             img_turn.image=O
  53.         else:
  54.             img_turn.image=X
  55.     else:
  56.         info("illegal move","Sorry, that box is already taken!")
  57.  
  58. def move6():
  59.     if img_box6.image == blank:
  60.         img_box6.image= img_turn.image
  61.         if img_turn.image == X:
  62.             img_turn.image=O
  63.         else:
  64.             img_turn.image=X
  65.     else:
  66.         info("illegal move","Sorry, that box is already taken!")
  67.  
  68. def move7():
  69.     if img_box7.image == blank:
  70.         img_box7.image= img_turn.image
  71.         if img_turn.image == X:
  72.             img_turn.image=O
  73.         else:
  74.             img_turn.image=X
  75.     else:
  76.         info("illegal move","Sorry, that box is already taken!")
  77.  
  78. def move8():
  79.     if img_box8.image == blank:
  80.         img_box8.image= img_turn.image
  81.         if img_turn.image == X:
  82.             img_turn.image=O
  83.         else:
  84.             img_turn.image=X
  85.     else:
  86.         info("illegal move","Sorry, that box is already taken!")
  87.  
  88. def move9():
  89.     if img_box9.image == blank:
  90.         img_box9.image= img_turn.image
  91.         if img_turn.image == X:
  92.             img_turn.image=O
  93.         else:
  94.             img_turn.image=X
  95.     else:
  96.         info("illegal move","Sorry, that box is already taken!")        
  97.    
  98. header = Box(app, width=480, height=20, grid=[0,0,5,1])
  99. box1 = Box(app,width=72,height=72,border=1, grid=[0,1])
  100. box2 = Box(app,width=72,height=72,border=1, grid=[0,2])
  101. box3 = Box(app,width=72,height=72,border=1, grid=[0,3])
  102. box4 = Box(app,width=72,height=72,border=1, grid=[1,1])
  103. box5 = Box(app,width=72,height=72,border=1, grid=[1,2])
  104. box6 = Box(app,width=72,height=72,border=1, grid=[1,3])
  105. box7 = Box(app,width=72,height=72,border=1, grid=[2,1])
  106. box8 = Box(app,width=72,height=72,border=1, grid=[2,2])
  107. box9 = Box(app,width=72,height=72,border=1, grid=[2,3])
  108. spacer1 = Box(app,width=181,height=219, grid=[3,1,1,3])
  109. spacer2 = Box(app,width=80, height=146, grid=[4,1,1,2])
  110. turn = Box(app,width=80,height=73,grid=[4,3])
  111. header.bg=(0,0,0)
  112. #spacer1.bg=(255,0,0)
  113. #spacer2.bg=(255,0,0)
  114. txt_header=Text(header,text="Tic-Tac-Toe")
  115. txt_header.text_color=(255,255,255)
  116. txt_spacer2=Text(spacer2,align="bottom",text="Turn:")
  117. img_turn=Picture(turn,O)
  118.  
  119.  
  120. img_box1 = Picture(box1,"25fb.png")
  121. img_box2 = Picture(box2,"25fb.png")
  122. img_box3 = Picture(box3,"25fb.png")
  123. img_box4 = Picture(box4,"25fb.png")
  124. img_box5 = Picture(box5,"25fb.png")
  125. img_box6 = Picture(box6,"25fb.png")
  126. img_box7 = Picture(box7,"25fb.png")
  127. img_box8 = Picture(box8,"25fb.png")
  128. img_box9 = Picture(box9,"25fb.png")
  129.  
  130. img_box1.when_clicked = move1
  131. img_box2.when_clicked = move2
  132. img_box3.when_clicked = move3
  133. img_box4.when_clicked = move4
  134. img_box5.when_clicked = move5
  135. img_box6.when_clicked = move6
  136. img_box7.when_clicked = move7
  137. img_box8.when_clicked = move8
  138. img_box9.when_clicked = move9
  139.  
  140. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement