Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import Turtle, Screen, Shape
- GAME_STATE = [[0 for x in range(3)] for y in range(3)]
- GAME_OVER = False
- def setup_player_markers(screen, color='black'):
- """Gets the x and o player markers ready to use.
- This function sets up shapes that can be used with the turtle.shape()
- function. Recall that you could make your turtle look like an actual
- turtle (instead of the default arrow shape) by calling
- turtle.shape('turtle'). Once this function has been called, you can
- change the shape of your turtle to an x or an o by calling
- turtle.shape('player x') or turtle.shape('player o')
- """
- # We will build the x out of a backslash and forward slash
- # These numbers are the vertices of the slashes
- backslash = ((-50,-50), (50,50))
- forwardslash = ((-50,50), (50,-50))
- shape = Shape('compound')
- shape.addcomponent(backslash, '', color)
- shape.addcomponent(forwardslash, '', color)
- screen.register_shape('player x', shape)
- # We approximate the o with a 21-sided polygon
- # These numbers are the vertices of the polygon
- circle = ((-00.00,-50.00), ( 15.45,-47.55), ( 29.39,-40.45),
- ( 40.45,-29.39), ( 47.55,-15.45), ( 50.00,-00.00),
- ( 47.55, 15.45), ( 40.45, 29.39), ( 29.39, 40.45),
- ( 15.45, 47.55), ( 00.00, 50.00), (-15.45, 47.55),
- (-29.39, 40.45), (-40.45, 29.39), (-47.55, 15.45),
- (-50.00, 00.00), (-47.55,-15.45), (-40.45,-29.39),
- (-29.39,-40.45), (-15.45,-47.55), ( -0.00,-50.00),)
- shape = Shape('compound')
- shape.addcomponent(circle, '', color)
- screen.register_shape('player o', shape)
- def line(t, x, y, heading, length):
- t.penup()
- t.goto(x, y)
- t.setheading(heading)
- t.pendown()
- t.forward(length)
- def draw_board(t, x, y, size):
- half = size / 2
- sixth = size / 6
- # Draw the top horizontal line
- line(t, x - half, y + sixth, 0, size)
- # Draw the bottom horizontal line
- line(t, x - half, y - sixth, 0, size)
- # Draw the left vertical line
- line(t, x - sixth, y + half, 270, size)
- # Draw the right vertical line
- line(t, x + sixth, y + half, 270, size)
- def get_player():
- global CURRENT_PLAYER
- return CURRENT_PLAYER
- def switch_player():
- global CURRENT_PLAYER
- if CURRENT_PLAYER == 'x':
- CURRENT_PLAYER = 'o'
- else:
- CURRENT_PLAYER = 'x'
- def make_play(x, y):
- # Discard all clicks outside of the game board area
- if x < -150 or x > 150 or y < -150 or y > 150:
- return
- #------------------------------------------------------------------------------#
- # YOUR CODE BEGINS HERE
- #------------------------------------------------------------------------------#
- # Check if the mouse was clicked in the upper left square
- # if -150 < x < -50 and 50 < y < 150:
- # # We need the coordinates of the center of the upper left square
- # center = (-100, 100)
- # # Check if the current player is x
- # if get_player() == 'x':
- # # Move the x turtle to the center of the upper left square
- # PLAYER_X.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_X.stamp()
- # # If the current player isn't x, then it must be o
- # else:
- # # Move the x turtle to the center of the upper left square
- # PLAYER_O.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_O.stamp()
- #A1
- selectGrid(x, y, -150, -50, 50, 150, -100, 100)
- #A2
- selectGrid(x, y, 50, 150, 50, 150, 100, 100)
- #A3
- selectGrid(x, y, -50, 50, 50, 150, 0, 100)
- #B1
- selectGrid(x, y, -150, -50, -50, 50, -100, 0)
- #B2
- selectGrid(x, y, -50, 50, -50, 50, 0, 0)
- #B3
- selectGrid(x, y, 50, 150, -50, 50, 100, 0)
- #C1
- selectGrid(x, y, -150, -50, -150, -50, -100, -100)
- #C2
- selectGrid(x, y, -50, 50, -150, -50, 0, -100)
- #C3
- selectGrid(x, y, 50, 150, -150, -50, 100, -100)
- # COPY THE CODE ABOVE AND MAKE IT WORK FOR EACH SQUARE ON THE GAME BOARD
- # START HERE
- # # Check if the mouse was clicked in the upper middle square
- # if -50 < x < 50 and 50 < y < 150:
- # # We need the coordinates of the center of the upper middle square
- # center = (0, 100)
- # # Check if the current player is x
- # if get_player() == 'x':
- # # Move the x turtle to the center of the upper left square
- # PLAYER_X.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_X.stamp()
- # # If the current player isn't x, then it must be o
- # else:
- # # Move the x turtle to the center of the upper left square
- # PLAYER_O.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_O.stamp()
- def selectGrid(x, y, min_x, max_x, min_y, max_y, cen_x, cen_y):
- # Check if the mouse was clicked in the upper left square
- if min_x < x < max_x and min_y < y < max_y:
- # We need the coordinates of the center of the upper left square
- center = (cen_x, cen_y)
- center_coordinates = list(c // 100 for c in center)
- print(center_coordinates)
- # Check if the current player is x
- if get_player() == 'x':
- # Move the x turtle to the center of the upper left square
- PLAYER_X.goto(center)
- # Stamp the x turtle into that square
- PLAYER_X.stamp()
- switch_player()
- # If the current player isn't x, then it must be o
- else:
- # Move the x turtle to the center of the upper left square
- PLAYER_O.goto(center)
- # Stamp the x turtle into that square
- PLAYER_O.stamp()
- switch_player()
- # if 50 < x < 150 and 50 < y < 150:
- # # We need the coordinates of the center of the upper right square
- # center = (100, 100)
- # # Check if the current player is x
- # if get_player() == 'x':
- # # Move the x turtle to the center of the upper right square
- # PLAYER_X.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_X.stamp()
- # # If the current player isn't x, then it must be o
- # else:
- # # Move the x turtle to the center of the upper right square
- # PLAYER_O.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_O.stamp()
- # if -50 < x < 50 and -50 < y < 50:
- # # We need the coordinates of the center of the center square
- # center = (0, 0)
- # # Check if the current player is x
- # if get_player() == 'x':
- # # Move the x turtle to the center of the upper left square
- # PLAYER_X.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_X.stamp()
- # # If the current player isn't x, then it must be o
- # else:
- # # Move the x turtle to the center of the upper left square
- # PLAYER_O.goto(center)
- # # Stamp the x turtle into that square
- # PLAYER_O.stamp()
- #------------------------------------------------------------------------------#
- # YOUR CODE ENDS HERE
- #------------------------------------------------------------------------------#
- BOARD_SIZE = 300
- SCREEN_WIDTH = 450
- SCREEN_HEIGHT = 450
- SCREEN = Screen()
- # Resize the screen to a nice square size slightly larger than our board
- SCREEN.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
- setup_player_markers(SCREEN, 'black')
- # Setup a turtle who will draw our game board
- DONATELLO = Turtle()
- DONATELLO.pensize(5)
- DONATELLO.hideturtle()
- DONATELLO.speed(0)
- #DONATELLO.dot(0)
- # Setup a turtle to be the x player
- PLAYER_X = Turtle()
- PLAYER_X.shape('player x')
- PLAYER_X.turtlesize(0.5, 0.5, 10)
- PLAYER_X.penup()
- PLAYER_X.hideturtle()
- # Setup a turtle to be the o player
- PLAYER_O = Turtle()
- PLAYER_O.shape('player o')
- PLAYER_O.turtlesize(0.5, 0.5, 10)
- PLAYER_O.penup()
- PLAYER_O.hideturtle()
- # Draw the board
- draw_board(DONATELLO, 0, 0, 300)
- CURRENT_PLAYER = 'x'
- SCREEN.onclick(make_play)
- SCREEN.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement