• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 14th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from turtle import Turtle, Screen, Shape
2.
3.
4. GAME_STATE = [[0 for x in range(3)] for y in range(3)]
5.
6. GAME_OVER = False
7.
8. def setup_player_markers(screen, color='black'):
9.     """Gets the x and o player markers ready to use.
10.
11.    This function sets up shapes that can be used with the turtle.shape()
12.    function. Recall that you could make your turtle look like an actual
13.    turtle (instead of the default arrow shape) by calling
14.    turtle.shape('turtle'). Once this function has been called, you can
15.    change the shape of your turtle to an x or an o by calling
16.    turtle.shape('player x') or turtle.shape('player o')
17.    """
18.
19.     # We will build the x out of a backslash and forward slash
20.     # These numbers are the vertices of the slashes
21.     backslash = ((-50,-50), (50,50))
22.     forwardslash = ((-50,50), (50,-50))
23.     shape = Shape('compound')
26.     screen.register_shape('player x', shape)
27.
28.     # We approximate the o with a 21-sided polygon
29.     # These numbers are the vertices of the polygon
30.     circle = ((-00.00,-50.00), ( 15.45,-47.55), ( 29.39,-40.45),
31.               ( 40.45,-29.39), ( 47.55,-15.45), ( 50.00,-00.00),
32.               ( 47.55, 15.45), ( 40.45, 29.39), ( 29.39, 40.45),
33.               ( 15.45, 47.55), ( 00.00, 50.00), (-15.45, 47.55),
34.               (-29.39, 40.45), (-40.45, 29.39), (-47.55, 15.45),
35.               (-50.00, 00.00), (-47.55,-15.45), (-40.45,-29.39),
36.               (-29.39,-40.45), (-15.45,-47.55), ( -0.00,-50.00),)
37.     shape = Shape('compound')
39.     screen.register_shape('player o', shape)
40.
41. def line(t, x, y, heading, length):
42.     t.penup()
43.     t.goto(x, y)
45.     t.pendown()
46.     t.forward(length)
47.
48. def draw_board(t, x, y, size):
49.     half = size / 2
50.     sixth = size / 6
51.
52.     # Draw the top horizontal line
53.     line(t, x - half, y + sixth, 0, size)
54.
55.     # Draw the bottom horizontal line
56.     line(t, x - half, y - sixth, 0, size)
57.
58.     # Draw the left vertical line
59.     line(t, x - sixth, y + half, 270, size)
60.
61.     # Draw the right vertical line
62.     line(t, x + sixth, y + half, 270, size)
63.
64. def get_player():
65.     global CURRENT_PLAYER
66.     return CURRENT_PLAYER
67.
68. def switch_player():
69.     global CURRENT_PLAYER
70.     if CURRENT_PLAYER == 'x':
71.         CURRENT_PLAYER = 'o'
72.     else:
73.         CURRENT_PLAYER = 'x'
74.
75. def make_play(x, y):
76.     # Discard all clicks outside of the game board area
77.     if x < -150 or x > 150 or y < -150 or y > 150:
78.         return
79.
80. #------------------------------------------------------------------------------#
81. # YOUR CODE BEGINS HERE
82. #------------------------------------------------------------------------------#
83.     # Check if the mouse was clicked in the upper left square
84.     # if -150 < x < -50 and 50 < y < 150:
85.     #     # We need the coordinates of the center of the upper left square
86.     #     center = (-100, 100)
87.     #     # Check if the current player is x
88.     #     if get_player() == 'x':
89.     #         # Move the x turtle to the center of the upper left square
90.     #         PLAYER_X.goto(center)
91.     #         # Stamp the x turtle into that square
92.     #         PLAYER_X.stamp()
93.     #     # If the current player isn't x, then it must be o
94.     #     else:
95.     #         # Move the x turtle to the center of the upper left square
96.     #         PLAYER_O.goto(center)
97.     #         # Stamp the x turtle into that square
98.     #         PLAYER_O.stamp()
99.
100.     #A1
101.     selectGrid(x, y, -150, -50, 50, 150, -100, 100)
102.     #A2
103.     selectGrid(x, y, 50, 150, 50, 150, 100, 100)
104.     #A3
105.     selectGrid(x, y, -50, 50, 50, 150, 0, 100)
106.     #B1
107.     selectGrid(x, y, -150, -50, -50, 50, -100, 0)
108.     #B2
109.     selectGrid(x, y, -50, 50, -50, 50, 0, 0)
110.     #B3
111.     selectGrid(x, y, 50, 150, -50, 50, 100, 0)
112.     #C1
113.     selectGrid(x, y, -150, -50, -150, -50, -100, -100)
114.     #C2
115.     selectGrid(x, y, -50, 50, -150, -50, 0, -100)
116.     #C3
117.     selectGrid(x, y, 50, 150, -150, -50, 100, -100)
118.
119.     # COPY THE CODE ABOVE AND MAKE IT WORK FOR EACH SQUARE ON THE GAME BOARD
120.     # START HERE
121.
122.     # # Check if the mouse was clicked in the upper middle square
123.     # if -50 < x < 50 and 50 < y < 150:
124.     #     # We need the coordinates of the center of the upper middle square
125.     #     center = (0, 100)
126.     #     # Check if the current player is x
127.     #     if get_player() == 'x':
128.     #         # Move the x turtle to the center of the upper left square
129.     #         PLAYER_X.goto(center)
130.     #         # Stamp the x turtle into that square
131.     #         PLAYER_X.stamp()
132.     #     # If the current player isn't x, then it must be o
133.     #     else:
134.     #         # Move the x turtle to the center of the upper left square
135.     #         PLAYER_O.goto(center)
136.     #         # Stamp the x turtle into that square
137.     #         PLAYER_O.stamp()
138.
139.
140. def selectGrid(x, y, min_x, max_x, min_y, max_y, cen_x, cen_y):
141.     # Check if the mouse was clicked in the upper left square
142.     if min_x < x < max_x and min_y < y < max_y:
143.         # We need the coordinates of the center of the upper left square
144.         center = (cen_x, cen_y)
145.         center_coordinates = list(c // 100 for c in center)
146.         print(center_coordinates)
147.         # Check if the current player is x
148.         if get_player() == 'x':
149.             # Move the x turtle to the center of the upper left square
150.             PLAYER_X.goto(center)
151.             # Stamp the x turtle into that square
152.             PLAYER_X.stamp()
153.             switch_player()
154.         # If the current player isn't x, then it must be o
155.         else:
156.             # Move the x turtle to the center of the upper left square
157.             PLAYER_O.goto(center)
158.             # Stamp the x turtle into that square
159.             PLAYER_O.stamp()
160.             switch_player()
161.
162. #     if 50 < x < 150 and 50 < y < 150:
163. #         # We need the coordinates of the center of the upper right square
164. #         center = (100, 100)
165. #         # Check if the current player is x
166. #         if get_player() == 'x':
167. #             # Move the x turtle to the center of the upper right square
168. #             PLAYER_X.goto(center)
169. #             # Stamp the x turtle into that square
170. #             PLAYER_X.stamp()
171. #         # If the current player isn't x, then it must be o
172. #         else:
173. #             # Move the x turtle to the center of the upper right square
174. #             PLAYER_O.goto(center)
175. #             # Stamp the x turtle into that square
176. #             PLAYER_O.stamp()
177.
178. #     if -50 < x < 50 and -50 < y < 50:
179. #         # We need the coordinates of the center of the center square
180. #         center = (0, 0)
181. #         # Check if the current player is x
182. #         if get_player() == 'x':
183. #             # Move the x turtle to the center of the upper left square
184. #             PLAYER_X.goto(center)
185. #             # Stamp the x turtle into that square
186. #             PLAYER_X.stamp()
187. #         # If the current player isn't x, then it must be o
188. #         else:
189. #             # Move the x turtle to the center of the upper left square
190. #             PLAYER_O.goto(center)
191. #             # Stamp the x turtle into that square
192. #             PLAYER_O.stamp()
193.
194. #------------------------------------------------------------------------------#
195. # YOUR CODE ENDS HERE
196. #------------------------------------------------------------------------------#
197.
198.
199.
200. BOARD_SIZE = 300
201. SCREEN_WIDTH = 450
202. SCREEN_HEIGHT = 450
203.
204. SCREEN = Screen()
205.
206. # Resize the screen to a nice square size slightly larger than our board
207. SCREEN.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
208.
209. setup_player_markers(SCREEN, 'black')
210.
211. # Setup a turtle who will draw our game board
212. DONATELLO = Turtle()
213. DONATELLO.pensize(5)
214. DONATELLO.hideturtle()
215. DONATELLO.speed(0)
216. #DONATELLO.dot(0)
217.
218. # Setup a turtle to be the x player
219. PLAYER_X = Turtle()
220. PLAYER_X.shape('player x')
221. PLAYER_X.turtlesize(0.5, 0.5, 10)
222. PLAYER_X.penup()
223. PLAYER_X.hideturtle()
224.
225. # Setup a turtle to be the o player
226. PLAYER_O = Turtle()
227. PLAYER_O.shape('player o')
228. PLAYER_O.turtlesize(0.5, 0.5, 10)
229. PLAYER_O.penup()
230. PLAYER_O.hideturtle()
231.
232. # Draw the board
233. draw_board(DONATELLO, 0, 0, 300)
234.
235. CURRENT_PLAYER = 'x'
236.
237. SCREEN.onclick(make_play)
238.
239. SCREEN.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top