import turtle
import random
SQUARE_LENGTH = 60
OUTLINE = 5 # the width of the broad
INTERVAL_LENGTH = 10 # the length between two squares
ALL_COLOR = ['red', 'yellow', 'blue', 'purple', 'green']
g_all_square = [] # store the color of every square
g_all_center = []
g_ifclick = True
g_current_square = [] #append all to this list, final choice is g_current_square[-1]
g_all_frame_pen = [] # in this llist, 0,1,2,3,4 are frame of color set, after this all chosen square in board
def init_screen():
global screen
screen = turtle.Screen()
screen.setup(800, 800, 0, 0)
#screen.tracer(0)
#screen.tracer(0)
# set screen refersh to manual mode
draw_board()
draw_color_set()
def draw_filled_square(p_start_x, p_start_y, p_lenth, p_fill_color):
#turtle.delay(None)
pen = turtle.Turtle()
#pen.speed(0)
#pen.hideturtle()
pen.penup()
pen.setx(p_start_x) # set the start point's x coordinate, start point in the left bottom
pen.sety(p_start_y) # set the start point's x coordinate
pen.pendown()
pen.shape('square')
streth_wid = (p_lenth/2) / 10
pen.shapesize(streth_wid,streth_wid, 0)
#pen.pen(p_fill_color, p_fill_color)
pen.color(p_fill_color)
#pen.pencolor(p_fill_color)
#pen.fillcolor(p_fill_color)
#pen.begin_fill()
#for i in range(4):
# pen.forward(p_lenth)
# pen.left(90)
#pen.end_fill()
def draw_frame(p_start_x, p_start_y, p_lenth):
pen = turtle.Turtle()
#pen.speed(5)
pen.penup()
pen.setx(p_start_x) # set the start point's x coordinate, start point in the left bottom
pen.sety(p_start_y) # set the start point's x coordinate
pen.pendown()
pen.pensize(5)
for i in range(4):
pen.forward(p_lenth + 5)
pen.left(90)
g_all_frame_pen.append(pen)
#pen.down()
def draw_board():
#turtle.tracer(False)
# first start point is (0,0)
x0 = -140
y0 = -140
# use colums and lines to define the start point of each quare
for column in range(5): #5 columns in totle
# adjust the y coordinate of start point
y = y0 + column*(SQUARE_LENGTH + INTERVAL_LENGTH)
line_square_color = [] # to store the color of square in this line
line_center = []
for row in range(5): #5 rows in totle
# adjust the x coordinate of start point
# each time add square_length + interval length
x = x0 + row*(SQUARE_LENGTH + INTERVAL_LENGTH)
color = choose_color() # choose_color() return the name of color(string)
line_square_color.append(color)
line_center.append([x,y])
draw_filled_square(x, y, SQUARE_LENGTH, color)
g_all_square.append(line_square_color)
# randomly choose the color of square in board
def choose_color():
color_index = random.randint(0,4) # list has index 0,1,2,3,4
color = ALL_COLOR[color_index]
return color
def draw_color_set():
# set the initial point(left bottom of the square)
x0 = -170
y0 = -215
# only 1 column with 5 rows
for line in range(5):
# adjust the x coordinate of start point
# each time add square_length + interval length
x = x0 + line*(SQUARE_LENGTH + + INTERVAL_LENGTH)
color = ALL_COLOR[line] # the color index is the same as the line
draw_filled_square(x, y0, SQUARE_LENGTH, color)
draw_frame(x-30-2.5, y0-30-2.5, SQUARE_LENGTH)
# show the starting screen
def start_game():
turtle.tracer(False)
init_screen()
turtle.update()
#turtle.exitonclick()
def get_position(x,y):
click_position = [x,y]
return click_position
#return a list containing the click position
#def select_tile():
#turtle.listen()
#mouse = turtle.Turtle()
#click_position = screen.onclick(get_position)
#return click_position
#check whether the point in the color square is the border
# if it is border, not change
def check_point_color_square(p_clicked_point):
global g_ifclick
# reset the original point to (-170, 170) to make the caculation easier
x = p_clicked_point[0]+ 170
y = p_clicked_point[1] + 170
# view (square_length + interval_length) as a period(T)
# x = T*row + row_remain
# row stands for the number of entire square in the left of x
# then solve the equation, we get next two lines
row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
row_remain = x-row*(SQUARE_LENGTH + INTERVAL_LENGTH)
# the same as x
column = y // (SQUARE_LENGTH + INTERVAL_LENGTH)
column_remain = y-column*(SQUARE_LENGTH + INTERVAL_LENGTH)
#check whether the user click in the square or the border
if row_remain > SQUARE_LENGTH or column_remain > SQUARE_LENGTH: #the user click the white space
g_ifclick = True # need to click again, will not change
else: #click inside the square
g_current_square.append([row,column])
center = g_all_center[column][row]
# undo last black frame
if len(g_all_frame_pen) > 5:
last_pen = g_all_frame_pen[-1]
last_pen.undo()
draw_frame(center[0], center[1], SQUARE_LENGTH)
#pass
# get the color of square in board
def get_color(p_coordinate):
row = p_coordinate[0]
column = p_coordinate[1]
color = g_all_square[column, row]
return color
# if click the color set, change g_ifclick = False, get out of while
# other situation, g_ifclick = True, remain in while
def check_point(p_clicked_point):
global g_ifclick
x = p_clicked_point[0]
y = p_clicked_point[1]
if -245 <= y <= -185 and -200 <= x <= 140: # the area of color_set
if -140 <= x <= -130 or \
-70 <= x <= -60 or \
0 <= x <= 10 or \
70 <= x <= 80:
g_ifclick = True #clicked the broad
else: #click the square in the color_set
if len(g_current_square) != 0:
g_ifclick = False # no more click, begin flipping
else: # havent chosen the square in the board to flipping
g_ifclick = True
elif -170 <= x <= 170 and -170 <= y <= 170: # clicked the color square
check_point_color_square()
else: # in white space
g_ifclick = True
#def click_empty_space():
#click = True
#return click
# check if the color is the same, if same, then change
def if_change_color(p_row, p_column, p_final_color):
if_change = False
inital_color = g_all_square[p_column][p_row]
if inital_color == p_final_color:
if_change = True
else:
if_change = False
return if_change
def x_to_row(p_x):
# reset the original point (0,0) to (-170, 170) to make the caculation easier
x = p_x + 170
# view (square_length + interval_length) as a period(T)
# x = T*row + row_remain
# row stands for the number of entire square in the left of x
# then solve the equation, we get next two lines
row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
return row
def y_to_column(p_y):
# reset the original point (0,0) to (-170, 170) to make the caculation easier
y = p_y + 170
# the same as x
column = y // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
return column
#def row_to_x(p_row):
# can be viewed as arithmetic progression
# x0 = -140; d = square_length + interval_length = 60 + 10 = 70
# x = x0 + d*(n-1); n stands for row
# x = 70n - 210
x = 70*p_row - 210
return x
def get_final_color(p_coordinate):
x = p_coordinate[1] #all y makes no different in square
x = x + 200 # change the original point(0,0) t0 (-200, -245) to make calculation easier
row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
final_color = ALL_COLOR[row]
return final_color
def flipping(p_row, p_column, p_final_color):
#check whether the color is same
#if same, then flipping
if_change = if_change_color(p_row, p_column, p_final_color)
if if_change == True:
center_coordinate = g_all_center[p_column][p_row]
center_x = center_coordinate[0]
center_y = center_coordinate[1]
draw_filled_square(center_x, center_y, SQUARE_LENGTH, p_final_color)
flipping(p_row-1, p_column, p_final_color) # flip the left square
flipping(p_row+1, p_column, p_final_color) # flip the right square
flipping(p_row, p_column-1, p_final_color) # flip the below square
flipping(p_row, p_column+1, p_final_color) # flip the upper square
else: #color is different, do not change
pass
#start_game()
#print(g_all_square)
#clicked_point = screen.onclick(get_position)
screen = turtle.Screen()
screen.setup(800, 800, 0, 0)
turtle.tracer(False)
draw_board()
draw_color_set()
turtle.update()
clicked_point = screen.onclick(get_position)
if clicked_point != None:
check_point(clicked_point)
if g_ifclick == False:
chosen_square = g_current_square[-1] #the last chosen square in board
clicked_row = x_to_row(chosen_square[0])
clicked_column = y_to_column(chosen_square[1])
final_color = get_final_color(clicked_point) # the clicked point must in the color set to make g_ifclick = False
flipping(clicked_row, clicked_column, final_color)
turtle.mainloop()