Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. from turtle import *
  2. #draw board
  3. pieces = ["", "", "", "", "", "", "", "", ""]
  4. nextTurn = "X"
  5. bgcolor("black")
  6. pencolor("white")
  7. setup(600, 600)
  8. hideturtle()
  9. speed(10)
  10. up()
  11. pensize(10)
  12.  
  13. # Horizontal bars
  14. goto(-300, 100)
  15. down()
  16. forward(600)
  17. up()
  18. goto(-300, -100)
  19. down()
  20. forward(600)
  21. up()
  22.  
  23. # Vertical bars
  24. goto(-100, 300)
  25. setheading(-90)
  26. down()
  27. forward(600)
  28. up()
  29. goto(100, 300)
  30. down()
  31. forward(600)
  32. up()
  33. pencolor("#00ff00")
  34.  
  35.  
  36. # Draw noughts and crosses
  37. def cross(x, y):
  38. up()
  39. goto(x + 20, y - 20)
  40. setheading(-45)
  41. down()
  42. forward(226)
  43. up()
  44. goto(x + 180, y - 20)
  45. setheading(-135)
  46. down()
  47. forward(226)
  48. up()
  49.  
  50.  
  51. def nought(x, y):
  52. up()
  53. goto(x + 100, y - 180)
  54. setheading(0)
  55. down()
  56. circle(80)
  57. up()
  58.  
  59.  
  60. def drawPieces(peices):
  61. x = -300
  62. y = 300
  63. for piece in pieces:
  64. if piece == "X":
  65. cross(x, y)
  66. elif piece == "O":
  67. nought(x, y)
  68. x = x + 200
  69. if x > 100:
  70. x = -300
  71. y = y - 200
  72.  
  73.  
  74. def clicked(x, y):
  75. global nextTurn, pieces
  76. column = (x + 300) // 200
  77. row = (y - 300) // -200
  78. square = row * 3 + column
  79. square = int(square)
  80. print("You clicked ", x, ",", y, " which is square ", square)
  81. if pieces[square] == "":
  82. pieces[square] = nextTurn
  83. if nextTurn == "X":
  84. nextTurn = "O"
  85. else:
  86. nextTurn = "X"
  87. drawPieces(pieces)
  88. else:
  89. print("That square is already taken")
  90. # Start the game
  91. onscreenclick(clicked)
  92. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement